Skip to content

Commit

Permalink
syntax view 适应 ls-rust
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Apr 15, 2023
1 parent ff291fb commit 22ee425
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions src/tree_view/treeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ class SyntaxNodeOrToken extends vscode.TreeItem {
constructor(
public readonly label: string,
public readonly collapsibleState: vscode.TreeItemCollapsibleState,
public readonly index: number,
private range: string
) {
super(label, collapsibleState);
this.tooltip = this.index.toString();
// this.tooltip = this.index.toString();
this.description = this.range;
}

Expand All @@ -22,36 +21,48 @@ class SyntaxNodeOrToken extends vscode.TreeItem {
// };
}

export class EmmyLuaSyntaxTreeProvider implements vscode.TreeDataProvider<SyntaxNodeOrToken> {
interface PsiViewAttr {
simple_view: string
}

interface PsiViewNode {
name: string;
attr?: PsiViewAttr;
children?: PsiViewNode[];
}

export class EmmyLuaSyntaxTreeProvider implements vscode.TreeDataProvider<PsiViewNode> {
constructor(private luaContext: LuaContext) { }

getTreeItem(element: SyntaxNodeOrToken): vscode.TreeItem {
return element;
getTreeItem(element: PsiViewNode): vscode.TreeItem {
return new SyntaxNodeOrToken(
element.name,
element.children ? vscode.TreeItemCollapsibleState.Collapsed : vscode.TreeItemCollapsibleState.None,
element.attr?.simple_view ?? ""
);
}

getChildren(element?: SyntaxNodeOrToken): Thenable<SyntaxNodeOrToken[]> {
getChildren(element?: PsiViewNode): Thenable<PsiViewNode[]> {
return new Promise((resolve, reject) => {
const activeEditor = vscode.window.activeTextEditor;
const params = { uri: activeEditor?.document.uri.toString(), index: element ? element.index : 1 }
this.luaContext.client?.sendRequest<any[]>("$/syntaxTree/nodes", params).then(data => {
resolve(data.map(it => {
return new SyntaxNodeOrToken(
it.label,
it.isNode ? vscode.TreeItemCollapsibleState.Collapsed : vscode.TreeItemCollapsibleState.None,
it.index,
it.range
);
}))
})
if (!element) {
const params = { uri: activeEditor?.document.uri.toString() }
this.luaContext.client?.sendRequest<{ data: PsiViewNode }>("$/syntaxTree/nodes", params).then(psi => {
resolve([psi.data])
})
}
else {
resolve(element.children ?? [])
}
})
}

refresh(): void {
this._onDidChangeTreeData.fire();
}

private _onDidChangeTreeData: vscode.EventEmitter<SyntaxNodeOrToken | undefined | null | void> = new vscode.EventEmitter<SyntaxNodeOrToken | undefined | null | void>();
readonly onDidChangeTreeData: vscode.Event<SyntaxNodeOrToken | undefined | null | void> = this._onDidChangeTreeData.event;
private _onDidChangeTreeData: vscode.EventEmitter<PsiViewNode | undefined | null | void> = new vscode.EventEmitter<PsiViewNode | undefined | null | void>();
readonly onDidChangeTreeData: vscode.Event<PsiViewNode | undefined | null | void> = this._onDidChangeTreeData.event;
}

function onDidChangeTextDocument(event: vscode.TextDocumentChangeEvent) {
Expand All @@ -78,7 +89,7 @@ export function active(luaContext: LuaContext) {
vscode.window.createTreeView('emmylua.syntax.tree', {
treeDataProvider: provider!
});

context.subscriptions.push(vscode.workspace.onDidChangeTextDocument(onDidChangeTextDocument, null, context.subscriptions));
context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(onDidChangeActiveTextEditor, null, context.subscriptions));
})
Expand Down

0 comments on commit 22ee425

Please sign in to comment.