Skip to content

Commit 8791727

Browse files
committed
chore(lsp): move autocomplete to the lsp
1 parent 28567cd commit 8791727

4 files changed

Lines changed: 64 additions & 8 deletions

File tree

sqlmesh/lsp/main.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,50 @@ def workspace_diagnostic(
434434
)
435435
return types.WorkspaceDiagnosticReport(items=[])
436436

437+
@self.server.feature(types.TEXT_DOCUMENT_COMPLETION)
438+
def completion(
439+
ls: LanguageServer, params: types.CompletionParams
440+
) -> t.Optional[types.CompletionList]:
441+
"""Handle completion requests from the client."""
442+
try:
443+
uri = URI(params.text_document.uri)
444+
context = self._context_get_or_load(uri)
445+
446+
# Get completions using the existing completions module
447+
completion_response = get_sql_completions(context, uri)
448+
449+
# Convert to LSP completion items
450+
completion_items = []
451+
452+
# Add model completions
453+
for model in completion_response.models:
454+
completion_items.append(
455+
types.CompletionItem(
456+
label=model,
457+
kind=types.CompletionItemKind.Reference,
458+
detail="SQLMesh Model",
459+
)
460+
)
461+
462+
# Add keyword completions
463+
for keyword in completion_response.keywords:
464+
completion_items.append(
465+
types.CompletionItem(
466+
label=keyword,
467+
kind=types.CompletionItemKind.Keyword,
468+
detail="SQL Keyword",
469+
)
470+
)
471+
472+
return types.CompletionList(
473+
is_incomplete=False,
474+
items=completion_items,
475+
)
476+
477+
except Exception as e:
478+
ls.show_message(f"Error getting completions: {e}", types.MessageType.Error)
479+
return None
480+
437481
def _get_diagnostics_for_uri(self, uri: URI) -> t.Tuple[t.List[types.Diagnostic], int]:
438482
"""Get diagnostics for a specific URI, returning (diagnostics, result_id).
439483

vscode/extension/src/extension.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,6 @@ export async function activate(context: vscode.ExtensionContext) {
8383
),
8484
)
8585

86-
context.subscriptions.push(
87-
vscode.languages.registerCompletionItemProvider(
88-
selector,
89-
completionProvider(lspClient),
90-
),
91-
)
92-
9386
// Register the webview
9487
const lineagePanel = new LineagePanel(context.extensionUri, lspClient)
9588
context.subscriptions.push(
@@ -165,6 +158,15 @@ export async function activate(context: vscode.ExtensionContext) {
165158
context.subscriptions.push(lspClient)
166159
}
167160

161+
if (lspClient && !lspClient.hasCompletionCapability()) {
162+
context.subscriptions.push(
163+
vscode.languages.registerCompletionItemProvider(
164+
selector,
165+
completionProvider(lspClient),
166+
),
167+
)
168+
}
169+
168170
traceInfo('Extension activated')
169171
}
170172

vscode/extension/src/lsp/lsp.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ export class LSPClient implements Disposable {
2121
this.client = undefined
2222
}
2323

24+
public hasCompletionCapability(): boolean {
25+
if (!this.client) {
26+
traceError('LSP client is not initialized')
27+
return false
28+
}
29+
const capabilities = this.client.initializeResult?.capabilities
30+
const completion = capabilities?.completionProvider
31+
return completion !== undefined
32+
}
33+
2434
public async start(): Promise<Result<undefined, ErrorType>> {
2535
if (!outputChannel) {
2636
outputChannel = window.createOutputChannel('sqlmesh-lsp')

vscode/extension/tests/completions.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ test('Autocomplete for model names', async () => {
4444
await window.waitForTimeout(500)
4545

4646
// Check if the autocomplete suggestion for sushi.customers is visible
47-
await expect(window.locator('text=sushi.customers')).toBeVisible()
47+
expect(await window.locator('text=sushi.customers').count()).toBe(1)
4848

4949
await close()
5050
} finally {

0 commit comments

Comments
 (0)