Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
Added tree view for current active editor
Browse files Browse the repository at this point in the history
  • Loading branch information
haiyangToAI authored and danwos committed Jun 21, 2023
1 parent cc2b23e commit d513628
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ hover.
**Sphinx-Needs Views:**

![image](https://raw.githubusercontent.com/useblocks/sphinx-needs-vscode/main/docs/_images/sn-views.gif)

**Sphinx-Needs Current Explorer View**

![image](https://raw.githubusercontent.com/useblocks/sphinx-needs-vscode/main/docs/_images/sn-current-view.gif)
8 changes: 7 additions & 1 deletion client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function activate(context: ExtensionContext) {
console.info('SNV: Activated Sphinx-Needs-VsCode Extension.');

// TreeView of Sphinx-Needs Objects
const needsExplorerProvider = new NeedsExplorerProvider();
const needsExplorerProvider = new NeedsExplorerProvider(false);
window.createTreeView('sphinxNeedsExplorer', {
treeDataProvider: needsExplorerProvider
});
Expand Down Expand Up @@ -49,6 +49,12 @@ export function activate(context: ExtensionContext) {
env.openExternal(Uri.parse(item));
});

// Treeview of Sphinx-Needs objects in current active editor
const needsCurrentProvider = new NeedsExplorerProvider(true);
window.createTreeView('sphinxNeedsCurrent', {
treeDataProvider: needsCurrentProvider
});

// The server is implemented in node
const serverModule = context.asAbsolutePath(path.join('server', 'out', 'server.js'));
// The debug options for the server
Expand Down
73 changes: 60 additions & 13 deletions client/src/needsExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@ interface DocConf {
srcDir: string;
}

interface NeedsPerDoc {
[doc_path: string]: Needs;
}

interface NeedsInfo {
needs: Needs;
allFiles: string[];
src_dir: string;
needs_json: string;
needs_per_doc: NeedsPerDoc;
}

interface NeedsInfos {
[path: string]: NeedsInfo | undefined;
[path: string]: NeedsInfo;
}

let tslogger: TimeStampedLogger;
Expand All @@ -57,13 +62,15 @@ export class NeedsExplorerProvider implements vscode.TreeDataProvider<vscode.Tre
needs: {},
allFiles: [],
src_dir: '',
needs_json: ''
needs_json: '',
needs_per_doc: {}
};
snvConfigs: SNVConfig;
needsInfos: NeedsInfos = {};
isMultiDocs = false;
currEditorView = false;

constructor() {
constructor(currEditorView: boolean) {
// Get workspace configurations and init snvConfigs
this.snvConfigs = this.getSNVConfigurations();

Expand All @@ -76,7 +83,10 @@ export class NeedsExplorerProvider implements vscode.TreeDataProvider<vscode.Tre
// Load all needsJsons from workspace configurations
this.needsInfos = this.loadAllNeedsJsonsToInfos();

// Only watch active editor change to update tree view when is multi docs
// Check if current treeview for active editor
this.currEditorView = currEditorView;

// Watch active editor change to update tree view
vscode.window.onDidChangeActiveTextEditor(() => this.onActiveEditorChanged());

// Create file watcher for needs.json
Expand Down Expand Up @@ -144,14 +154,33 @@ export class NeedsExplorerProvider implements vscode.TreeDataProvider<vscode.Tre
}

private onActiveEditorChanged(): void {
if (this.isMultiDocs && vscode.window.activeTextEditor) {
if (vscode.window.activeTextEditor) {
const curr_doc = vscode.window.activeTextEditor.document.uri.fsPath;
Object.values(this.needsInfos).forEach((need_info) => {
// Check if current active editor relevant
let found_need_info = false;
let found_need_info_key = '';
for (const [idx, need_info] of Object.entries(this.needsInfos)) {
if (need_info?.allFiles && need_info?.allFiles.indexOf(curr_doc) >= 0) {
this.needsInfo = need_info;
this._onDidChangeTreeData.fire(undefined);
found_need_info = true;
found_need_info_key = idx;
}
});
}

if (found_need_info) {
this.needsInfo = this.needsInfos[found_need_info_key];
if (this.currEditorView) {
this.needsInfo.needs = this.needsInfo.needs_per_doc[curr_doc];
}
} else {
this.needsInfo = {
needs: {},
allFiles: [],
src_dir: '',
needs_json: '',
needs_per_doc: {}
};
}
this._onDidChangeTreeData.fire(undefined);
}
}

Expand Down Expand Up @@ -219,7 +248,8 @@ export class NeedsExplorerProvider implements vscode.TreeDataProvider<vscode.Tre
needs: {},
allFiles: [],
src_dir: '',
needs_json: ''
needs_json: '',
needs_per_doc: {}
};
}
}
Expand Down Expand Up @@ -367,7 +397,7 @@ export class NeedsExplorerProvider implements vscode.TreeDataProvider<vscode.Tre
return all_needs_infos;
}

private loadNeedsJsonToInfo(needsJsonFilePath: string | undefined): NeedsInfo | undefined {
private loadNeedsJsonToInfo(needsJsonFilePath: string | undefined): NeedsInfo {
// Check needs.json path and get needs object from needs.json if exists
if (needsJsonFilePath && this.pathExists(needsJsonFilePath)) {
tslogger.debug(`SNV Explorer -> Loaded nedds json: ${needsJsonFilePath}`);
Expand Down Expand Up @@ -412,9 +442,10 @@ export class NeedsExplorerProvider implements vscode.TreeDataProvider<vscode.Tre
});
}

// Calculate all files paths in current srcDir
// Calculate all files paths in current srcDir and needs_per_doc
const all_files_path: string[] = [];
let need_doc_path: string;
const needs_per_doc: NeedsPerDoc = {};
Object.values(needs_objects).forEach((nd) => {
if (curr_src_dir.endsWith('/')) {
need_doc_path = curr_src_dir + nd.docname + nd.doctype;
Expand All @@ -425,16 +456,32 @@ export class NeedsExplorerProvider implements vscode.TreeDataProvider<vscode.Tre
if (all_files_path.indexOf(need_doc_path) === -1) {
all_files_path.push(need_doc_path);
}

// Calculate needs objects per doc
if (!(need_doc_path in needs_per_doc)) {
needs_per_doc[need_doc_path] = {};
}
if (!(nd.id in needs_per_doc[need_doc_path])) {
needs_per_doc[need_doc_path][nd.id] = nd;
}
});

const needs_info: NeedsInfo = {
needs: needs_objects,
allFiles: all_files_path,
src_dir: curr_src_dir,
needs_json: needsJsonFilePath
needs_json: needsJsonFilePath,
needs_per_doc: needs_per_doc
};
return needs_info;
}
return {
needs: {},
allFiles: [],
src_dir: '',
needs_json: '',
needs_per_doc: {}
};
}

private getNeedFilePath(need: Need): vscode.Uri {
Expand Down
Binary file added docs/_images/sn-current-view.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Changelog
Under development

* Added setting :ref:`multiDocs` to support multiple docs inside one workspace, see :ref:`advanced`.
* Added explorer :ref:`currView` for sphinx-needs objects of current active editor.

0.2.1
-----
Expand Down
14 changes: 14 additions & 0 deletions docs/ui.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,22 @@ Help View

This view displays useful links about Sphinx-Needs and current extension Sphinx-Needs-VsCode.

.. _currView:

Current View
^^^^^^^^^^^^

This current view displays all the Sphinx-Needs objects in current active opened file.

Example
^^^^^^^

**Sphinx-Needs View**

.. image:: /_images/sn-views.gif
:align: center

**Current View**

.. image:: /_images/sn-current-view.gif
:align: center
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
}
],
"views": {
"explorer": [
{
"id": "sphinxNeedsCurrent",
"name": "Sphinx-Needs Current"
}
],
"sphinx-needs": [
{
"id": "sphinxNeedsExplorer",
Expand Down

0 comments on commit d513628

Please sign in to comment.