Skip to content

Commit 2ac0226

Browse files
committed
feat(vscode): ability to render model
[ci skip]
1 parent 190f4a6 commit 2ac0226

5 files changed

Lines changed: 91 additions & 9 deletions

File tree

sqlmesh/lsp/context.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
from sqlmesh.core.context import Context
44
import typing as t
55

6+
from sqlmesh.core.model.definition import SqlModel
7+
from sqlmesh.lsp.custom import RenderModelEntry
8+
from sqlmesh.lsp.uri import URI
9+
610

711
@dataclass
812
class ModelTarget:
@@ -49,3 +53,18 @@ def __init__(self, context: Context) -> None:
4953
**model_map,
5054
**audit_map,
5155
}
56+
57+
58+
def render_model(context: LSPContext, uri: URI) -> t.Iterator[RenderModelEntry]:
59+
model_target = context.map[uri.to_path()]
60+
if isinstance(model_target, ModelTarget):
61+
for name in model_target.names:
62+
model = context.context.get_model(name)
63+
if isinstance(model, SqlModel):
64+
rendered_query = context.context.render(model).sql(dialect=model.dialect)
65+
yield RenderModelEntry(
66+
name=model.name,
67+
fqn=model.fqn,
68+
description=model.description,
69+
rendered_query=rendered_query,
70+
)

sqlmesh/lsp/custom.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,29 @@ class AllModelsResponse(PydanticModel):
2020

2121
models: t.List[str]
2222
keywords: t.List[str]
23+
24+
25+
RENDER_MODEL_FEATURE = "sqlmesh/render_model"
26+
27+
28+
class RenderModelRequest(PydanticModel):
29+
textDocumentUri: str
30+
31+
32+
class RenderModelEntry(PydanticModel):
33+
"""
34+
An entry in the rendered model.
35+
"""
36+
37+
name: str
38+
fqn: str
39+
description: str
40+
rendered_query: str
41+
42+
43+
class RenderModelResponse(PydanticModel):
44+
"""
45+
Response to render a model.
46+
"""
47+
48+
models: t.List[RenderModelEntry]

sqlmesh/lsp/main.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,15 @@
2020
ApiResponseGetModels,
2121
)
2222
from sqlmesh.lsp.completions import get_sql_completions
23-
from sqlmesh.lsp.context import LSPContext, ModelTarget
24-
from sqlmesh.lsp.custom import ALL_MODELS_FEATURE, AllModelsRequest, AllModelsResponse
23+
from sqlmesh.lsp.context import LSPContext, ModelTarget, render_model as render_model_context
24+
from sqlmesh.lsp.custom import (
25+
ALL_MODELS_FEATURE,
26+
RENDER_MODEL_FEATURE,
27+
AllModelsRequest,
28+
AllModelsResponse,
29+
RenderModelRequest,
30+
RenderModelResponse,
31+
)
2532
from sqlmesh.lsp.reference import (
2633
get_references,
2734
)
@@ -88,6 +95,12 @@ def all_models(ls: LanguageServer, params: AllModelsRequest) -> AllModelsRespons
8895
except Exception as e:
8996
return get_sql_completions(None, uri)
9097

98+
@self.server.feature(RENDER_MODEL_FEATURE)
99+
def render_model(ls: LanguageServer, params: RenderModelRequest) -> RenderModelResponse:
100+
uri = URI(params.textDocumentUri)
101+
context = self._context_get_or_load(uri)
102+
return RenderModelResponse(models=list(render_model_context(context, uri)))
103+
91104
@self.server.feature(API_FEATURE)
92105
def api(ls: LanguageServer, request: ApiRequest) -> t.Dict[str, t.Any]:
93106
ls.log_trace(f"API request: {request}")

vscode/extension/src/commands/openTestQuery.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ export function openTestQuery() {
44
return async () => {
55
// Get the current active editor
66
const activeEditor = vscode.window.activeTextEditor
7-
7+
88
// Create a new untitled document with SQL language
99
const document = await vscode.workspace.openTextDocument({
1010
language: 'sql',
11-
content: 'SELECT * FROM test'
11+
content: 'SELECT * FROM test',
1212
})
1313

1414
// Determine the view column for side-by-side display
1515
let viewColumn: vscode.ViewColumn
1616
if (activeEditor) {
1717
// Open beside the current editor
18-
viewColumn = activeEditor.viewColumn
19-
? activeEditor.viewColumn + 1
18+
viewColumn = activeEditor.viewColumn
19+
? activeEditor.viewColumn + 1
2020
: vscode.ViewColumn.Two
2121
} else {
2222
// If no active editor, open in column two
@@ -27,7 +27,7 @@ export function openTestQuery() {
2727
await vscode.window.showTextDocument(document, {
2828
viewColumn: viewColumn,
2929
preview: true,
30-
preserveFocus: false
30+
preserveFocus: false,
3131
})
3232
}
33-
}
33+
}

vscode/extension/src/lsp/custom.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,32 @@ export interface AllModelsMethod {
44
response: AllModelsResponse
55
}
66

7+
export interface RenderModelMethod {
8+
method: 'sqlmesh/render_model'
9+
request: RenderModelRequest
10+
response: RenderModelResponse
11+
}
12+
13+
interface RenderModelRequest {
14+
textDocumentUri: string
15+
}
16+
17+
interface RenderModelResponse {
18+
models: RenderModelEntry[]
19+
}
20+
21+
interface RenderModelEntry {
22+
name: string
23+
fqn: string
24+
description: string
25+
rendered_query: string
26+
}
27+
728
// @eslint-disable-next-line @typescript-eslint/consistent-type-definition
8-
export type CustomLSPMethods = AllModelsMethod | AbstractAPICall
29+
export type CustomLSPMethods =
30+
| AllModelsMethod
31+
| AbstractAPICall
32+
| RenderModelMethod
933

1034
interface AllModelsRequest {
1135
textDocument: {

0 commit comments

Comments
 (0)