-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
104 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import * as y3 from 'y3-helper'; | ||
|
||
export async function test() { | ||
let table = y3.table.open('单位'); | ||
let table = y3.table.openTable('单位'); | ||
let obj = await table.get(134218426); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,30 @@ | ||
import * as vscode from 'vscode'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { isPathValid, getFileNameByVscodeUri } from '../utility'; | ||
import * as y3 from 'y3-helper'; | ||
import { Table } from '../constants'; | ||
import { EditorObject } from './editorTable'; | ||
|
||
/** | ||
* 提供物编数据的Json文件内的中英文字段搜索的DocumentSymbolProvider | ||
*/ | ||
export class GoEditorTableDocumentSymbolProvider implements vscode.DocumentSymbolProvider { | ||
private englishKeyToChineseKey: any; | ||
constructor(private zhlanguageJson: any = undefined ) { | ||
let englishKeyToChineseKeyJsonPath = path.join(__dirname, "../../config/englishKeyToChineseKey.json"); | ||
if (isPathValid(englishKeyToChineseKeyJsonPath)) { | ||
try { | ||
this.englishKeyToChineseKey = JSON.parse(fs.readFileSync(englishKeyToChineseKeyJsonPath, 'utf8')); | ||
} | ||
catch (error) { | ||
vscode.window.showErrorMessage("读取和解析" + englishKeyToChineseKeyJsonPath + "时失败,错误为:" + error); | ||
} | ||
} | ||
else { | ||
vscode.window.showErrorMessage("在以下路径找不到englishKeyToChineseKey.json:\n"+englishKeyToChineseKeyJsonPath); | ||
} | ||
} | ||
public provideDocumentSymbols( | ||
document: vscode.TextDocument, token: vscode.CancellationToken): | ||
Thenable<vscode.SymbolInformation[]> { | ||
let res: vscode.SymbolInformation[] = []; | ||
if (token.isCancellationRequested) { | ||
return Promise.resolve(res); | ||
} | ||
res=this.getEditorTableJsonDocumentSymbols(document); | ||
let objectMap: { [key: string]: EditorObject | null } = {}; | ||
|
||
return Promise.resolve(res); | ||
} | ||
private getEditorTableJsonDocumentSymbols(document: vscode.TextDocument): vscode.SymbolInformation[] { | ||
let res: vscode.SymbolInformation[] = []; | ||
const keyToLine: { [key: string]: number } = {}; | ||
let editorTableJsonData:any = JSON.parse(document.getText()); | ||
for (let i = 0; i < document.lineCount; i++){ | ||
let line = document.lineAt(i).text; | ||
const matches = line.match(/"\s*([^"]+)"\s*(?=:)/g);// 正则表达式匹配双引号内,且后缀为':'的字符串,视为Json的键 | ||
if (matches) { | ||
matches.forEach(match => { | ||
match = match.substring(1, match.length - 1); | ||
keyToLine[match] = i; | ||
}); | ||
}; | ||
} | ||
let fileName: string = getFileNameByVscodeUri(vscode.Uri.file(document.fileName)); | ||
let chineseName = this.zhlanguageJson[editorTableJsonData['name']]; | ||
let finalFileName = fileName; | ||
if (chineseName !== undefined && typeof chineseName === 'string') { | ||
finalFileName = chineseName + "(" + fileName.substring(0, fileName.length - 5) + ")";//这是一个单位(134219828)"的格式 | ||
} | ||
for (let key in keyToLine) { | ||
let name = key; | ||
let kind: vscode.SymbolKind; | ||
|
||
if (typeof editorTableJsonData[key] === typeof []) { | ||
kind = vscode.SymbolKind.Array; | ||
export class Provider implements vscode.DocumentSymbolProvider { | ||
async provideDocumentSymbols(document: vscode.TextDocument) { | ||
const uri = document.uri; | ||
if (objectMap[uri.path] === undefined) { | ||
objectMap[uri.path] = await y3.table.getObject(uri) ?? null; | ||
} | ||
else if (typeof editorTableJsonData[key]===typeof {} ) { | ||
kind = vscode.SymbolKind.Module; | ||
let object = objectMap[uri.path]; | ||
if (object === null) { | ||
return; | ||
} | ||
else if (typeof editorTableJsonData[key] === typeof true) { | ||
kind = vscode.SymbolKind.Boolean; | ||
} | ||
else if (!isNaN(editorTableJsonData[key])) { | ||
kind = vscode.SymbolKind.Number; | ||
} | ||
else if (typeof editorTableJsonData[key] === typeof "") { | ||
kind = vscode.SymbolKind.String; | ||
} | ||
else { | ||
kind = vscode.SymbolKind.Module; | ||
} | ||
|
||
let uri: vscode.Uri = document.uri; | ||
let location: vscode.Location = new vscode.Location(document.uri, new vscode.Position(keyToLine[key], 0)); | ||
let containerName = finalFileName; | ||
if (key in this.englishKeyToChineseKey) { | ||
// todo:获得字段对应的中文名 | ||
name = this.englishKeyToChineseKey[key] + '(' + key + ')'; | ||
let symbolInformation: vscode.SymbolInformation = new vscode.SymbolInformation( | ||
name, | ||
kind, | ||
containerName, | ||
location | ||
); | ||
res.push(symbolInformation); | ||
} | ||
|
||
} | ||
|
||
return res; | ||
return undefined; | ||
} | ||
} | ||
|
||
export function init() { | ||
|
||
vscode.languages.registerDocumentSymbolProvider({ | ||
scheme: 'file', | ||
language: 'json', | ||
}, new GoEditorTableDocumentSymbolProvider()); | ||
|
||
}, new Provider()); | ||
vscode.workspace.onDidChangeTextDocument((e) => { | ||
delete objectMap[e.document.uri.path]; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters