From 109e21459493f229d01543d7ff54b951a67d690e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 8 Jul 2024 22:48:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=81=A2=E5=A4=8D=E5=B7=A5=E4=BD=9C=E5=8C=BA?= =?UTF-8?q?=E7=AC=A6=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../languageFeature/workspaceSymbol.ts | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/editorTable/languageFeature/workspaceSymbol.ts b/src/editorTable/languageFeature/workspaceSymbol.ts index 535437a..be194d9 100644 --- a/src/editorTable/languageFeature/workspaceSymbol.ts +++ b/src/editorTable/languageFeature/workspaceSymbol.ts @@ -1,5 +1,7 @@ import * as vscode from 'vscode'; import * as y3 from 'y3-helper'; +import { FieldInfo } from '../editorTable'; +import * as jsonc from 'jsonc-parser'; interface ObjectResult { object: y3.table.EditorObject; @@ -9,6 +11,7 @@ interface ObjectResult { interface FieldResult { objectResult: ObjectResult; + fieldInfo: FieldInfo; query: string; other: string; } @@ -39,6 +42,38 @@ class FieldInfomation extends vscode.SymbolInformation { ), ); } + + private computePosition(text: string, offset: number) { + let line = 0; + let character = 0; + for (let i = 0; i < offset; i++) { + if (text[i] === '\n') { + line++; + character = 0; + } else { + character++; + } + } + return new vscode.Position(line, character); + } + + async updateInformation() { + const object = this.fieldResult.objectResult.object; + const root = object.tree; + if (!root) { + return; + } + for (const property of root.children!) { + const key = property.children![0]; + if (key.value === this.fieldResult.fieldInfo.field) { + this.location.range = new vscode.Range( + this.computePosition(object.json!, property.offset), + this.computePosition(object.json!, property.offset + property.length), + ); + return; + } + } + } } class Provider implements vscode.WorkspaceSymbolProvider { @@ -50,7 +85,7 @@ class Provider implements vscode.WorkspaceSymbolProvider { if (token.isCancellationRequested) { return; } - let matchResult = query.match(/^([^\.\/]*)([\.\/]?)([^\.\/]*)?$/); + let matchResult = query.match(/^([^\.\/、。]*)([\.\/、。]?)([^\.\/、。]*)?$/); if (!matchResult) { return; } @@ -68,6 +103,13 @@ class Provider implements vscode.WorkspaceSymbolProvider { return results; } + async resolveWorkspaceSymbol(symbol: ObjectInfomation | FieldInfomation, token: vscode.CancellationToken) { + if (symbol instanceof FieldInfomation) { + await symbol.updateInformation(); + } + return symbol; + } + private compileString(str: string) { let result: { [key: number]: number } = {}; for (let i = 0; i < str.length; i++) { @@ -120,12 +162,14 @@ class Provider implements vscode.WorkspaceSymbolProvider { if (fieldInfo.desc && this.matchString(queryChars, fieldInfo.desc)) { result.push({ objectResult, + fieldInfo, query: fieldInfo.desc, other: field, }); } else if (this.matchString(queryChars, field)) { result.push({ objectResult, + fieldInfo, query: field, other: fieldInfo.desc ?? '', });