-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreeProvider.js
More file actions
140 lines (140 loc) · 4.99 KB
/
Copy pathtreeProvider.js
File metadata and controls
140 lines (140 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"use strict";
// ## Outline Tree Provider
// Sidebar TreeView showing H1/H2/H3 as a collapsible Table of Contents.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommentOutlineProvider = exports.OutlineNode = void 0;
const vscode = __importStar(require("vscode"));
// --- Tree Node ---
class OutlineNode extends vscode.TreeItem {
constructor(label, level, lineNumber, filePath) {
super(label, vscode.TreeItemCollapsibleState.Collapsed);
this.label = label;
this.level = level;
this.lineNumber = lineNumber;
this.filePath = filePath;
this.children = [];
this.description = `line ${lineNumber + 1}`;
this.tooltip = label;
this.iconPath = this.iconForLevel(level);
this.command = {
command: 'commentstyle.jumpToLine',
title: 'Jump to line',
arguments: [filePath, lineNumber],
};
}
iconForLevel(level) {
if (level === 'h1')
return new vscode.ThemeIcon('symbol-namespace');
if (level === 'h2')
return new vscode.ThemeIcon('symbol-class');
return new vscode.ThemeIcon('symbol-method');
}
makeLeaf() {
this.collapsibleState = vscode.TreeItemCollapsibleState.None;
return this;
}
}
exports.OutlineNode = OutlineNode;
// --- Tree Data Provider ---
class CommentOutlineProvider {
constructor() {
this._onDidChangeTreeData = new vscode.EventEmitter();
this.onDidChangeTreeData = this._onDidChangeTreeData.event;
this.roots = [];
}
// @param parsedLines ParsedLine[] - from active document
// @param filePath string - active file URI path
refresh(parsedLines, filePath) {
this.roots = this.buildTree(parsedLines, filePath);
this._onDidChangeTreeData.fire();
}
getTreeItem(node) {
return node;
}
getChildren(node) {
if (!node)
return this.roots;
return node.children;
}
// @param lines ParsedLine[] - heading lines only
// @param filePath string
// @returns OutlineNode[] - hierarchy
buildTree(lines, filePath) {
const roots = [];
let currentH1 = null;
let currentH2 = null;
const headings = lines.filter(l => l.type === 'h1' || l.type === 'h2' || l.type === 'h3');
for (const h of headings) {
const node = new OutlineNode(h.content, h.type, h.line, filePath);
if (h.type === 'h1') {
roots.push(node);
currentH1 = node;
currentH2 = null;
}
else if (h.type === 'h2') {
if (currentH1) {
currentH1.children.push(node);
currentH1.collapsibleState = vscode.TreeItemCollapsibleState.Expanded;
}
else {
roots.push(node);
}
currentH2 = node;
}
else {
// h3
if (currentH2) {
currentH2.children.push(node.makeLeaf());
currentH2.collapsibleState = vscode.TreeItemCollapsibleState.Expanded;
}
else if (currentH1) {
currentH1.children.push(node.makeLeaf());
}
else {
roots.push(node.makeLeaf());
}
}
}
// Collapse leaf H1s that have no children
for (const r of roots) {
if (r.children.length === 0)
r.makeLeaf();
}
return roots;
}
}
exports.CommentOutlineProvider = CommentOutlineProvider;
//# sourceMappingURL=treeProvider.js.map