Skip to content

Commit 695d1ee

Browse files
committed
feat(lsp): include model completion metadata
1 parent 2adf9e6 commit 695d1ee

6 files changed

Lines changed: 32 additions & 31 deletions

File tree

sqlmesh/lsp/completions.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
from functools import lru_cache
22
from sqlglot import Dialect, Tokenizer
3-
from sqlmesh.lsp.custom import AllModelsResponse, MacroCompletion
3+
from sqlmesh.lsp.custom import (
4+
AllModelsResponse,
5+
MacroCompletion,
6+
ModelCompletion,
7+
)
48
from sqlmesh import macro
59
import typing as t
610
from sqlmesh.lsp.context import AuditTarget, LSPContext, ModelTarget
7-
from sqlmesh.lsp.custom import ModelCompletion
811
from sqlmesh.lsp.description import generate_markdown_description
912
from sqlmesh.lsp.uri import URI
1013

@@ -30,8 +33,10 @@ def get_sql_completions(
3033
# Combine keywords - SQL keywords first, then file keywords
3134
all_keywords = list(sql_keywords) + list(file_keywords - sql_keywords)
3235

36+
models = list(get_models(context, file_uri))
3337
return AllModelsResponse(
34-
models=list(get_models(context, file_uri)),
38+
models=[m.name for m in models],
39+
model_completions=models,
3540
keywords=all_keywords,
3641
macros=list(get_macros(context, file_uri)),
3742
)

sqlmesh/lsp/custom.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ class ModelCompletion(PydanticModel):
3838

3939

4040
class AllModelsResponse(CustomMethodResponseBaseClass):
41-
"""
42-
Response to get all the models that are in the current project.
43-
"""
41+
"""Response to get all models that are in the current project."""
4442

45-
models: t.List[ModelCompletion]
43+
#: Deprecated: use ``model_completions`` instead
44+
models: t.List[str]
45+
model_completions: t.List[ModelCompletion]
4646
keywords: t.List[str]
4747
macros: t.List[MacroCompletion]
4848

sqlmesh/lsp/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ def completion(
680680

681681
completion_items = []
682682
# Add model completions
683-
for model in completion_response.models:
683+
for model in completion_response.model_completions:
684684
completion_items.append(
685685
types.CompletionItem(
686686
label=model.name,

tests/lsp/test_completions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ def test_get_macros():
4141
assert add_one_macro.description
4242

4343

44+
def test_model_completions_include_descriptions():
45+
context = Context(paths=["examples/sushi"])
46+
lsp_context = LSPContext(context)
47+
48+
completions = LSPContext.get_completions(lsp_context, None)
49+
50+
model_entry = next(
51+
(m for m in completions.model_completions if m.name == "sushi.customers"),
52+
None,
53+
)
54+
assert model_entry is not None
55+
assert model_entry.description
56+
57+
4458
def test_get_sql_completions_with_context_no_file_uri():
4559
context = Context(paths=["examples/sushi"])
4660
lsp_context = LSPContext(context)

vscode/extension/src/completion/completion.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,10 @@ export const completionProvider = (
1919
if (isErr(result)) {
2020
return []
2121
}
22-
const modelCompletions = result.value.models.map(model => {
23-
const item = new vscode.CompletionItem(
24-
model.name,
25-
vscode.CompletionItemKind.Reference,
26-
)
27-
item.detail = 'SQLMesh Model'
28-
if (model.description) {
29-
item.documentation = new vscode.MarkdownString(model.description)
30-
}
31-
return item
32-
})
22+
const modelCompletions = result.value.models.map(
23+
model =>
24+
new vscode.CompletionItem(model, vscode.CompletionItemKind.Reference),
25+
)
3326
const keywordCompletions = result.value.keywords.map(
3427
keyword =>
3528
new vscode.CompletionItem(keyword, vscode.CompletionItemKind.Keyword),

vscode/extension/src/lsp/custom.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,6 @@ export interface RenderModelEntry {
2525
rendered_query: string
2626
}
2727

28-
export interface ModelCompletion {
29-
name: string
30-
description: string | null | undefined
31-
}
32-
33-
export interface MacroCompletion {
34-
name: string
35-
description: string | null | undefined
36-
}
37-
3828
// @eslint-disable-next-line @typescript-eslint/consistent-type-definition
3929
export type CustomLSPMethods =
4030
| AllModelsMethod
@@ -51,9 +41,8 @@ interface AllModelsRequest {
5141
}
5242

5343
interface AllModelsResponse {
54-
models: ModelCompletion[]
44+
models: string[]
5545
keywords: string[]
56-
macros: MacroCompletion[]
5746
}
5847

5948
export interface AbstractAPICallRequest {

0 commit comments

Comments
 (0)