Skip to content

Commit 194d96c

Browse files
author
lhy
committed
feat: 添加使用 PawSQL 优化 SQL 的上下文菜单功能
在编辑器中添加了一个新的上下文菜单项,允许用户使用 PawSQL 优化选中的 SQL 语句。该功能通过解析当前 SQL 文件并找到最接近光标的查询来实现优化。
1 parent 36bb5fa commit 194d96c

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@
113113
"command": "pawsql.setDefaultWorkspace",
114114
"title": "%commands.setDefaultWorkspace%",
115115
"icon": "$(star)"
116+
},
117+
{
118+
"command": "pawsql.contextMenu.optimizeWithSelectedWorkspace",
119+
"title": "%commands.optimizeSQL%",
120+
"icon": "$(sparkle)"
116121
}
117122
],
118123
"configuration": [
@@ -226,6 +231,13 @@
226231
"when": "view == pawsqlSidebar && viewItem == workspaceItem",
227232
"group": "inline"
228233
}
234+
],
235+
"editor/context": [
236+
{
237+
"command": "pawsql.contextMenu.optimizeWithSelectedWorkspace",
238+
"when": "editorLangId =~ /sql/",
239+
"group": "navigation"
240+
}
229241
]
230242
},
231243
"icons": {

package.nls.en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"commands.refreshTree": "Refresh",
66
"commands.createWorkspace": "Create a Workspace",
77
"commands.setDefaultWorkspace": "Set as Default Workspace",
8-
8+
"commands.optimizeSQL": "Optimize SQL with PawSQL",
99
"config.initialization.title": "PawSQL Setup",
1010
"config.initialization.version.title": "PawSQL Edition",
1111
"config.initialization.backendUrl.title": "PawSQL Server",

package.nls.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"commands.refreshTree": "刷新",
66
"commands.createWorkspace": "创建工作空间",
77
"commands.setDefaultWorkspace": "设置为默认工作空间",
8+
"commands.optimizeSQL": "使用 PawSQL 优化此 SQL",
89
"config.initialization.title": "PawSQL Client",
910
"config.initialization.version.title": "PawSQL 版本",
1011
"config.initialization.backendUrl.title": "PawSQL 服务器",

src/CommandManager.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { SqlCodeLensProvider } from "./SqlCodeLensProvider";
88
import { ConfigurationService } from "./configurationService";
99
import { LanguageService } from "./LanguageService";
1010
import path from "path";
11+
import parse from "./utils/parse";
1112

1213
export class CommandManager {
1314
constructor(
@@ -19,6 +20,7 @@ export class CommandManager {
1920
public async initializeCommands(): Promise<void> {
2021
try {
2122
this.registerApiKeyCommands();
23+
this.registerContextMenuCommands();
2224
} catch (error) {
2325
ErrorHandler.handle("initialize.commands.failed", error);
2426
}
@@ -32,6 +34,55 @@ export class CommandManager {
3234
this.context.subscriptions.push(configFileDefaultDisposable);
3335
}
3436

37+
private registerContextMenuCommands(): void {
38+
const contextMenuOptimizeDisposable = vscode.commands.registerCommand(
39+
"pawsql.contextMenu.optimizeWithSelectedWorkspace",
40+
() => this.handleContextMenuOptimize()
41+
);
42+
this.context.subscriptions.push(contextMenuOptimizeDisposable);
43+
}
44+
45+
public async handleContextMenuOptimize(): Promise<void> {
46+
const editor = vscode.window.activeTextEditor;
47+
if (!editor || editor.document.languageId !== "sql") {
48+
return;
49+
}
50+
51+
const cursorPosition = editor.selection.active;
52+
const document = editor.document;
53+
const text = document.getText();
54+
const queries = await parse(text);
55+
56+
let closestQuery: { range: vscode.Range; text: string } | null = null;
57+
let minDistance = Number.MAX_VALUE;
58+
59+
for (const query of queries) {
60+
const queryIndex = text.indexOf(query);
61+
if (queryIndex === -1) continue;
62+
63+
const startPos = document.positionAt(queryIndex);
64+
const endPos = document.positionAt(queryIndex + query.length);
65+
const range = new vscode.Range(startPos, endPos);
66+
67+
const distance = Math.min(
68+
Math.abs(cursorPosition.line - startPos.line),
69+
Math.abs(cursorPosition.line - endPos.line)
70+
);
71+
72+
if (distance < minDistance) {
73+
minDistance = distance;
74+
closestQuery = { range, text: query };
75+
}
76+
}
77+
78+
if (closestQuery) {
79+
await this.extension.handleWorkspaceSelectionWithRangeQuery(
80+
closestQuery.text,
81+
closestQuery.range
82+
);
83+
}
84+
}
85+
3586
public async handleFileDefaultWorkspaceSelection(): Promise<void> {
3687
const apiKey = await ConfigurationService.getApiKey();
3788

0 commit comments

Comments
 (0)