Skip to content

Commit f3b4082

Browse files
committed
multiple updates
1. bug fix when a .out file is open in activeEditor, the outline will be displayed automatically. 2. added settings to enable toggle outline to be collapsed or expanded by default. 3. enhanced feature. the status of the toggle list will be kept when navigating to different tabs
1 parent 6441e95 commit f3b4082

File tree

2 files changed

+156
-89
lines changed

2 files changed

+156
-89
lines changed

extension.js

Lines changed: 122 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -16,58 +16,74 @@ class OrcaOutlineProvider {
1616
this._matches = matches;
1717
// Initialize the EventEmitter
1818
this._onDidChangeTreeData = new vscode.EventEmitter();
19-
this._filePath = ""; // Initialize the file path
19+
this._filePath = ""; // Initialize the file path
20+
this._expandedState = {}; // Initialize the expanded state
2021
}
2122

22-
getTreeItem(element) {
23-
const treeItem = new vscode.TreeItem(element.label || element.title);
24-
if (element.children && element.children.length > 0) {
25-
treeItem.collapsibleState = vscode.TreeItemCollapsibleState.Expanded;
26-
} else {
27-
treeItem.collapsibleState = vscode.TreeItemCollapsibleState.None;
28-
}
29-
treeItem.command = element.command;
30-
treeItem.tooltip = element.tooltip;
31-
return treeItem;
32-
}
33-
34-
getChildren(element) {
35-
if (element) {
36-
// If we have an element, return its children
37-
return element.children.map(child => {
38-
return {
39-
label: `${child.title} (Line ${child.line + 1})`,
40-
children: child.children, // This will hold any further nested children
41-
command: {
42-
command: 'vscode.open',
43-
arguments: [vscode.Uri.file(this._filePath), {
44-
selection: new vscode.Range(child.line, 0, child.line, 0)
45-
}],
46-
title: 'Open File'
47-
},
48-
tooltip: `${child.title} (Line ${child.line + 1})`
49-
};
50-
});
51-
} else {
52-
// If there's no element, return the top-level matches
23+
getTreeItem(element) {
24+
const treeItem = new vscode.TreeItem(element.label || element.title);
25+
26+
if (element.children && element.children.length > 0) {
27+
const defaultCollapsed = vscode.workspace.getConfiguration().get('orcatoc.defaultCollapsed', true);
28+
29+
// Check if an expanded state has been set, otherwise use the default
30+
const isExpanded = this._expandedState[element.label || element.title];
31+
if (isExpanded !== undefined) {
32+
treeItem.collapsibleState = isExpanded ? vscode.TreeItemCollapsibleState.Expanded : vscode.TreeItemCollapsibleState.Collapsed;
33+
} else {
34+
treeItem.collapsibleState = defaultCollapsed ? vscode.TreeItemCollapsibleState.Collapsed : vscode.TreeItemCollapsibleState.Expanded;
35+
}
36+
} else {
37+
treeItem.collapsibleState = vscode.TreeItemCollapsibleState.None;
38+
}
39+
40+
treeItem.command = element.command;
41+
treeItem.tooltip = element.tooltip;
42+
return treeItem;
43+
}
44+
45+
46+
getChildren(element) {
47+
if (element) {
48+
// If we have an element, return its children
49+
return element.children.map(child => {
50+
return {
51+
label: `${child.title} (Line ${child.line + 1})`,
52+
children: child.children, // This will hold any further nested children
53+
command: {
54+
command: 'vscode.open',
55+
arguments: [vscode.Uri.file(this._filePath), {
56+
selection: new vscode.Range(child.line, 0, child.line, 0)
57+
}],
58+
title: 'Open File'
59+
},
60+
tooltip: `${child.title} (Line ${child.line + 1})`
61+
};
62+
});
63+
} else {
64+
// If there's no element, return the top-level matches
5365
//console.log(typeof this._matches, this._matches);
54-
return this._matches.map(match => {
55-
return {
56-
label: `${match.title} (Line ${match.line + 1})`,
57-
children: match.children, // This will hold the first level of children
58-
command: {
59-
command: 'vscode.open',
60-
arguments: [vscode.Uri.file(this._filePath), {
61-
selection: new vscode.Range(match.line, 0, match.line, 0)
62-
}],
63-
title: 'Open File'
64-
},
65-
tooltip: `${match.title} (Line ${match.line + 1})`
66-
};
67-
});
68-
}
69-
}
70-
66+
return this._matches.map(match => {
67+
return {
68+
label: `${match.title} (Line ${match.line + 1})`,
69+
children: match.children, // This will hold the first level of children
70+
command: {
71+
command: 'vscode.open',
72+
arguments: [vscode.Uri.file(this._filePath), {
73+
selection: new vscode.Range(match.line, 0, match.line, 0)
74+
}],
75+
title: 'Open File'
76+
},
77+
tooltip: `${match.title} (Line ${match.line + 1})`
78+
};
79+
});
80+
}
81+
82+
}
83+
84+
setExpandedState(element, isExpanded) {
85+
this._expandedState[element.label || element.title] = isExpanded;
86+
}
7187

7288
// Only a getter for onDidChangeTreeData
7389
get onDidChangeTreeData() {
@@ -77,7 +93,17 @@ class OrcaOutlineProvider {
7793
update(matches, filePath) {
7894
this._matches = matches;
7995
this._filePath = filePath; // Store the file path here
80-
this._onDidChangeTreeData.fire();
96+
// this._onDidChangeTreeData.fire(); // Trigger the event emitter
97+
this.refresh(); // Refresh the tree view
98+
}
99+
// Method to refresh the tree view
100+
refresh() {
101+
this._onDidChangeTreeData.fire(undefined);
102+
}
103+
104+
// share the matches
105+
getParsedMatches() {
106+
return this._matches;
81107
}
82108
}
83109

@@ -131,8 +157,8 @@ class OrcaFileSystemProvider {
131157
const entries = fs.readdirSync(uri.fsPath, { withFileTypes: true });
132158
return entries.map(entry => {
133159
const type = entry.isFile() ? vscode.FileType.File :
134-
entry.isDirectory() ? vscode.FileType.Directory :
135-
vscode.FileType.Unknown;
160+
entry.isDirectory() ? vscode.FileType.Directory :
161+
vscode.FileType.Unknown;
136162
return [entry.name, type];
137163
});
138164
} catch (error) {
@@ -159,7 +185,7 @@ let orcaOutlineProvider = new OrcaOutlineProvider([]);
159185

160186
async function showOrcaOutline() { // Make the function asynchronous
161187
const activeEditor = vscode.window.activeTextEditor;
162-
188+
163189
if (!activeEditor) {
164190
// Check if there's a file with .out extension in the workspace
165191
if (vscode.workspace.textDocuments.some(doc => doc.uri.scheme === 'file' && doc.fileName.endsWith('.out'))) {
@@ -199,6 +225,7 @@ async function showOrcaOutline() { // Make the function asynchronous
199225
} catch (error) {
200226
vscode.window.showErrorMessage(`Error processing the file: ${error.message}`);
201227
}
228+
//orcaOutlineProvider.refresh(); // Refresh the tree view
202229
}
203230

204231
function parseOrcaFile(document, filePath) {
@@ -223,7 +250,7 @@ function parseOrcaFile(document, filePath) {
223250
title = toTitleCase(title.trim());
224251

225252
allMatches.push({
226-
line: line+1,
253+
line: line + 1,
227254
title: title,
228255
level: pattern.level,
229256
children: [],
@@ -239,7 +266,7 @@ function parseOrcaFile(document, filePath) {
239266
}
240267

241268
progress.report({ message: `Processing Pattern ${index + 1} of ${totalPatterns}`, increment: (100 / totalPatterns) });
242-
269+
243270
if (token.isCancellationRequested) {
244271
reject("Operation was cancelled by the user.");
245272
}
@@ -266,9 +293,9 @@ function insertDummyHeadings(allMatches) {
266293
let correctedMatches = [];
267294
let lastMatch = null;
268295

269-
for(let i = 0; i < allMatches.length; i++) {
296+
for (let i = 0; i < allMatches.length; i++) {
270297
let currentLevel = allMatches[i].level;
271-
298+
272299
if (lastMatch && currentLevel - lastMatch.level > 1) {
273300
for (let j = 1; j < currentLevel - lastMatch.level; j++) {
274301
correctedMatches.push({
@@ -281,11 +308,11 @@ function insertDummyHeadings(allMatches) {
281308
});
282309
}
283310
}
284-
311+
285312
correctedMatches.push(allMatches[i]);
286313
lastMatch = allMatches[i];
287314
}
288-
315+
289316
return correctedMatches;
290317
}
291318

@@ -303,12 +330,12 @@ async function replaceKeywords(matches) {
303330
}
304331

305332
function toTitleCase(str) {
306-
return str.replace(/\w\S*/g, function(txt) {
333+
return str.replace(/\w\S*/g, function (txt) {
307334
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
308335
});
309336
}
310337

311-
function deactivate() {}
338+
function deactivate() { }
312339

313340
function activate(context) {
314341
const orcaProvider = new OrcaFileSystemProvider();
@@ -317,12 +344,40 @@ function activate(context) {
317344
context.subscriptions.push(vscode.commands.registerCommand('extension.showOrcaOutline', showOrcaOutline));
318345
// Register the global instance
319346
vscode.window.registerTreeDataProvider('orcaFileOutline', orcaOutlineProvider);
320-
// Listen to changes in the active editor
321-
context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(editor => {
322-
if (editor && editor.document.languageId === 'orcaOut' && editor.document.fileName.endsWith('.out')) {
323-
showOrcaOutline();
324-
}
325-
}));
347+
// Listen to changes in the active editor
348+
context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(editor => {
349+
if (editor && editor.document.languageId === 'orcaOut' && editor.document.fileName.endsWith('.out')) {
350+
showOrcaOutline();
351+
}
352+
}));
353+
// Automatically show outline if a .out file is already open
354+
const activeEditor = vscode.window.activeTextEditor;
355+
if (activeEditor && activeEditor.document.languageId === 'orcaOut' && activeEditor.document.fileName.endsWith('.out')) {
356+
showOrcaOutline();
357+
}
358+
359+
const treeView = vscode.window.createTreeView('orcaFileOutline', { treeDataProvider: orcaOutlineProvider });
360+
361+
treeView.onDidExpandElement(event => {
362+
orcaOutlineProvider.setExpandedState(event.element, true);
363+
});
364+
365+
treeView.onDidCollapseElement(event => {
366+
orcaOutlineProvider.setExpandedState(event.element, false);
367+
});
368+
369+
370+
vscode.workspace.onDidOpenTextDocument(document => {
371+
if (document.languageId === 'orcaOut' && document.fileName.endsWith('.out') && !vscode.window.activeTextEditor) {
372+
showOrcaOutline();
373+
}
374+
});
375+
// listen to changes in the configuration
376+
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(e => {
377+
if (e.affectsConfiguration('orcatoc.defaultCollapsed')) {
378+
orcaOutlineProvider.refresh();
379+
}
380+
}));
326381
}
327382

328383
async function showOrcaOutlineExternal(context, uri) {
@@ -348,12 +403,8 @@ async function showOrcaOutlineExternal(context, uri) {
348403
vscode.window.showErrorMessage(`Failed to open ORCA file: ${error.message}`);
349404
}
350405
}
351-
352-
353406
}
354407

355-
356-
357408
module.exports = {
358409
activate,
359410
deactivate

package.json

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,47 @@
1717
"contributes": {
1818
"commands": [
1919
{
20-
"command": "extension.showOrcaOutline",
21-
"title": "Show ORCA Outline"
20+
"command": "extension.showOrcaOutline",
21+
"title": "Show ORCA Outline"
2222
},
2323
{
2424
"command": "extension.showOrcaOutlineExternal",
2525
"title": "Show ORCA Outline External"
2626
}
2727
],
2828
"views": {
29-
"explorer": [
30-
{
31-
"id": "orcaFileOutline",
32-
"name": "ORCA FILE OUTLINE",
33-
"when": "resourceExtname =~ /.out$/"
34-
}
35-
]
29+
"explorer": [
30+
{
31+
"id": "orcaFileOutline",
32+
"name": "ORCA FILE OUTLINE",
33+
"when": "resourceExtname =~ /.out$/"
34+
}
35+
]
3636
},
37-
"languages": [{
38-
"id": "orcaOut",
39-
"extensions": [".out"],
40-
"aliases": ["ORCA-output", "orca-output"],
41-
"configuration": "./language-configuration.json"
42-
}]
37+
"languages": [
38+
{
39+
"id": "orcaOut",
40+
"extensions": [
41+
".out"
42+
],
43+
"aliases": [
44+
"ORCA-output",
45+
"orca-output"
46+
],
47+
"configuration": "./language-configuration.json"
48+
}
49+
],
50+
"configuration": {
51+
"title": "Your Extension Configuration",
52+
"properties": {
53+
"orcatoc.defaultCollapsed": {
54+
"type": "boolean",
55+
"default": true,
56+
"description": "Control whether the outline is collapsed by default."
57+
}
58+
}
59+
}
4360
},
44-
4561
"scripts": {
4662
"lint": "eslint .",
4763
"pretest": "npm run lint",
@@ -58,8 +74,8 @@
5874
"@vscode/test-electron": "^2.3.4"
5975
},
6076
"repository": {
61-
"type": "git",
62-
"url": "https://github.com/liqunkang/orca_TOC.git"
77+
"type": "git",
78+
"url": "https://github.com/liqunkang/orca_TOC.git"
6379
},
6480
"license": "GPL-3.0",
6581
"publisher": "liqunKang",

0 commit comments

Comments
 (0)