Skip to content

Commit 78257c9

Browse files
committed
support show reference count
1 parent 2e9f044 commit 78257c9

4 files changed

Lines changed: 63 additions & 3 deletions

File tree

out/extension.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

out/extension.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
],
5959
"configuration": "./language-configuration.json",
6060
"capabilities" : {
61-
"renameProvider" : "true"
61+
"renameProvider" : "true",
62+
"resolveProvider": "true"
6263
}
6364
}
6465
],

src/extension.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ export function activate(context: ExtensionContext) {
3232
// 获取初始配置
3333
updateFeatureStatus();
3434

35+
context.subscriptions.push(vscode.languages.registerCodeLensProvider(
36+
[{ pattern: '**/*.sql' }, { pattern: '**/*.fql' }],
37+
new FlinkSQLCodeLensProvider()
38+
));
39+
40+
context.subscriptions.push(vscode.commands.registerCommand('extension.showReferences', (uri: vscode.Uri, position: vscode.Position, locations: vscode.Location[]) => {
41+
vscode.commands.executeCommand('editor.action.showReferences', uri, position, locations);
42+
}));
43+
3544
// 监听配置更改事件
3645
context.subscriptions.push(
3746
vscode.workspace.onDidChangeConfiguration((e) => {
@@ -122,4 +131,54 @@ class MyRenameProvider implements vscode.RenameProvider {
122131

123132
return edit;
124133
}
134+
}
135+
136+
137+
138+
class FlinkSQLCodeLensProvider implements vscode.CodeLensProvider {
139+
140+
141+
provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): vscode.ProviderResult<vscode.CodeLens[]> {
142+
const codeLenses = [];
143+
for (let line = 0; line < document.lineCount; line++) {
144+
const lineOfCode = document.lineAt(line);
145+
// 添加你的SQL语法检测逻辑
146+
if (/CREATE TEMPORARY|CREATE TABLE/i.test(lineOfCode.text)) {
147+
codeLenses.push(new vscode.CodeLens(lineOfCode.range));
148+
}
149+
}
150+
return codeLenses;
151+
}
152+
resolveCodeLens?(codeLens: vscode.CodeLens, token: vscode.CancellationToken): vscode.ProviderResult<vscode.CodeLens> {
153+
const editor = vscode.window.activeTextEditor;
154+
if (!editor) {
155+
return;
156+
}
157+
158+
const document = editor.document;
159+
const tableName = document.getText(codeLens.range);
160+
const references = this.findReferences(document, tableName);
161+
162+
if (references.length > 0) {
163+
codeLens.command = {
164+
title: `${references.length} reference(s) to ${tableName}`,
165+
command: "extension.showReferences",
166+
arguments: [document.uri, codeLens.range.start, references]
167+
};
168+
}
169+
return codeLens;
170+
}
171+
172+
findReferences(document: vscode.TextDocument | undefined, tableName: string | undefined): vscode.Range[] {
173+
const references = [];
174+
if (document && tableName) {
175+
for (let line = 0; line < document.lineCount; line++) {
176+
const lineOfCode = document.lineAt(line);
177+
if (lineOfCode.text.includes(tableName)) {
178+
references.push(lineOfCode.range);
179+
}
180+
}
181+
}
182+
return references;
183+
}
125184
}

0 commit comments

Comments
 (0)