Skip to content

Commit fc2af07

Browse files
committed
running style
1 parent 695d1ee commit fc2af07

2 files changed

Lines changed: 33 additions & 106 deletions

File tree

sqlmesh/lsp/completions.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ def get_sql_completions(
2626
# Get keywords from file content if provided
2727
file_keywords = set()
2828
if content:
29-
file_keywords = extract_keywords_from_content(
30-
content, get_dialect(context, file_uri)
31-
)
29+
file_keywords = extract_keywords_from_content(content, get_dialect(context, file_uri))
3230

3331
# Combine keywords - SQL keywords first, then file keywords
3432
all_keywords = list(sql_keywords) + list(file_keywords - sql_keywords)
@@ -90,9 +88,7 @@ def get_macros(
9088
return [MacroCompletion(name=name, description=doc) for name, doc in macros.items()]
9189

9290

93-
def get_keywords(
94-
context: t.Optional[LSPContext], file_uri: t.Optional[URI]
95-
) -> t.Set[str]:
91+
def get_keywords(context: t.Optional[LSPContext], file_uri: t.Optional[URI]) -> t.Set[str]:
9692
"""
9793
Return a list of sql keywords for a given file.
9894
If no context is provided, return ANSI SQL keywords.
@@ -103,11 +99,7 @@ def get_keywords(
10399
If both a context and a file_uri are provided, returns the keywords
104100
for the dialect of the model that the file belongs to.
105101
"""
106-
if (
107-
file_uri is not None
108-
and context is not None
109-
and file_uri.to_path() in context.map
110-
):
102+
if file_uri is not None and context is not None and file_uri.to_path() in context.map:
111103
file_info = context.map[file_uri.to_path()]
112104

113105
# Handle ModelInfo objects
@@ -150,17 +142,11 @@ def get_keywords_from_tokenizer(dialect: t.Optional[str] = None) -> t.Set[str]:
150142
return expanded_keywords
151143

152144

153-
def get_dialect(
154-
context: t.Optional[LSPContext], file_uri: t.Optional[URI]
155-
) -> t.Optional[str]:
145+
def get_dialect(context: t.Optional[LSPContext], file_uri: t.Optional[URI]) -> t.Optional[str]:
156146
"""
157147
Get the dialect for a given file.
158148
"""
159-
if (
160-
file_uri is not None
161-
and context is not None
162-
and file_uri.to_path() in context.map
163-
):
149+
if file_uri is not None and context is not None and file_uri.to_path() in context.map:
164150
file_info = context.map[file_uri.to_path()]
165151

166152
# Handle ModelInfo objects
@@ -181,9 +167,7 @@ def get_dialect(
181167
return None
182168

183169

184-
def extract_keywords_from_content(
185-
content: str, dialect: t.Optional[str] = None
186-
) -> t.Set[str]:
170+
def extract_keywords_from_content(content: str, dialect: t.Optional[str] = None) -> t.Set[str]:
187171
"""
188172
Extract identifiers from SQL content using the tokenizer.
189173

sqlmesh/lsp/main.py

Lines changed: 27 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,7 @@ def _create_lsp_context(self, paths: t.List[Path]) -> t.Optional[LSPContext]:
115115
return None
116116

117117
# All the custom LSP methods are registered here and prefixed with _custom
118-
def _custom_all_models(
119-
self, ls: LanguageServer, params: AllModelsRequest
120-
) -> AllModelsResponse:
118+
def _custom_all_models(self, ls: LanguageServer, params: AllModelsRequest) -> AllModelsResponse:
121119
uri = URI(params.textDocument.uri)
122120
# Get the document content
123121
content = None
@@ -149,9 +147,7 @@ def _custom_all_models_for_render(
149147
self._ensure_context_in_folder(current_path)
150148
if self.lsp_context is None:
151149
raise RuntimeError("No context found")
152-
return AllModelsForRenderResponse(
153-
models=self.lsp_context.list_of_models_for_rendering()
154-
)
150+
return AllModelsForRenderResponse(models=self.lsp_context.list_of_models_for_rendering())
155151

156152
def _custom_format_project(
157153
self, ls: LanguageServer, params: FormatProjectRequest
@@ -173,9 +169,7 @@ def _custom_format_project(
173169

174170
def _custom_api(
175171
self, ls: LanguageServer, request: ApiRequest
176-
) -> t.Union[
177-
ApiResponseGetModels, ApiResponseGetColumnLineage, ApiResponseGetLineage
178-
]:
172+
) -> t.Union[ApiResponseGetModels, ApiResponseGetColumnLineage, ApiResponseGetLineage]:
179173
ls.log_trace(f"API request: {request}")
180174
if self.lsp_context is None:
181175
current_path = Path.cwd()
@@ -197,9 +191,7 @@ def _custom_api(
197191
# /api/lineage/{model}
198192
model_name = urllib.parse.unquote(path_parts[2])
199193
lineage = model_lineage(model_name, self.lsp_context.context)
200-
non_set_lineage = {
201-
k: v for k, v in lineage.items() if v is not None
202-
}
194+
non_set_lineage = {k: v for k, v in lineage.items() if v is not None}
203195
return ApiResponseGetLineage(data=non_set_lineage)
204196

205197
if len(path_parts) == 4:
@@ -208,9 +200,7 @@ def _custom_api(
208200
column = urllib.parse.unquote(path_parts[3])
209201
models_only = False
210202
if hasattr(request, "params"):
211-
models_only = bool(
212-
getattr(request.params, "models_only", False)
213-
)
203+
models_only = bool(getattr(request.params, "models_only", False))
214204
column_lineage_response = column_lineage(
215205
model_name, column, models_only, self.lsp_context.context
216206
)
@@ -236,9 +226,7 @@ def _register_features(self) -> None:
236226
for name, method in self._supported_custom_methods.items():
237227

238228
def create_function_call(method_func: t.Callable) -> t.Callable:
239-
def function_call(
240-
ls: LanguageServer, params: t.Any
241-
) -> t.Dict[str, t.Any]:
229+
def function_call(ls: LanguageServer, params: t.Any) -> t.Dict[str, t.Any]:
242230
try:
243231
response = method_func(ls, params)
244232
except Exception as e:
@@ -255,9 +243,7 @@ def initialize(ls: LanguageServer, params: types.InitializeParams) -> None:
255243
try:
256244
# Check if the client supports pull diagnostics
257245
if params.capabilities and params.capabilities.text_document:
258-
diagnostics = getattr(
259-
params.capabilities.text_document, "diagnostic", None
260-
)
246+
diagnostics = getattr(params.capabilities.text_document, "diagnostic", None)
261247
if diagnostics:
262248
self.client_supports_pull_diagnostics = True
263249
ls.log_trace("Client supports pull diagnostics")
@@ -270,8 +256,7 @@ def initialize(ls: LanguageServer, params: types.InitializeParams) -> None:
270256
if params.workspace_folders:
271257
# Store all workspace folders for later use
272258
self.workspace_folders = [
273-
Path(self._uri_to_path(folder.uri))
274-
for folder in params.workspace_folders
259+
Path(self._uri_to_path(folder.uri)) for folder in params.workspace_folders
275260
]
276261

277262
# Try to find a SQLMesh config file in any workspace folder (only at the root level)
@@ -289,9 +274,7 @@ def initialize(ls: LanguageServer, params: types.InitializeParams) -> None:
289274
)
290275

291276
@self.server.feature(types.TEXT_DOCUMENT_DID_OPEN)
292-
def did_open(
293-
ls: LanguageServer, params: types.DidOpenTextDocumentParams
294-
) -> None:
277+
def did_open(ls: LanguageServer, params: types.DidOpenTextDocumentParams) -> None:
295278
uri = URI(params.text_document.uri)
296279
context = self._context_get_or_load(uri)
297280
models = context.map[uri.to_path()]
@@ -309,9 +292,7 @@ def did_open(
309292
)
310293

311294
@self.server.feature(types.TEXT_DOCUMENT_DID_CHANGE)
312-
def did_change(
313-
ls: LanguageServer, params: types.DidChangeTextDocumentParams
314-
) -> None:
295+
def did_change(ls: LanguageServer, params: types.DidChangeTextDocumentParams) -> None:
315296
uri = URI(params.text_document.uri)
316297
context = self._context_get_or_load(uri)
317298
models = context.map[uri.to_path()]
@@ -329,9 +310,7 @@ def did_change(
329310
)
330311

331312
@self.server.feature(types.TEXT_DOCUMENT_DID_SAVE)
332-
def did_save(
333-
ls: LanguageServer, params: types.DidSaveTextDocumentParams
334-
) -> None:
313+
def did_save(ls: LanguageServer, params: types.DidSaveTextDocumentParams) -> None:
335314
uri = URI(params.text_document.uri)
336315

337316
# Reload the entire context and create a new LSPContext
@@ -365,9 +344,7 @@ def formatting(
365344
document = ls.workspace.get_text_document(params.text_document.uri)
366345
before = document.source
367346
if self.lsp_context is None:
368-
raise RuntimeError(
369-
f"No context found for document: {document.path}"
370-
)
347+
raise RuntimeError(f"No context found for document: {document.path}")
371348

372349
target = next(
373350
(
@@ -394,9 +371,7 @@ def formatting(
394371
start=types.Position(line=0, character=0),
395372
end=types.Position(
396373
line=len(document.lines),
397-
character=len(document.lines[-1])
398-
if document.lines
399-
else 0,
374+
character=len(document.lines[-1]) if document.lines else 0,
400375
),
401376
),
402377
new_text=after,
@@ -407,27 +382,20 @@ def formatting(
407382
return []
408383

409384
@self.server.feature(types.TEXT_DOCUMENT_HOVER)
410-
def hover(
411-
ls: LanguageServer, params: types.HoverParams
412-
) -> t.Optional[types.Hover]:
385+
def hover(ls: LanguageServer, params: types.HoverParams) -> t.Optional[types.Hover]:
413386
"""Provide hover information for an object."""
414387
try:
415388
uri = URI(params.text_document.uri)
416389
self._ensure_context_for_document(uri)
417390
document = ls.workspace.get_text_document(params.text_document.uri)
418391
if self.lsp_context is None:
419-
raise RuntimeError(
420-
f"No context found for document: {document.path}"
421-
)
392+
raise RuntimeError(f"No context found for document: {document.path}")
422393

423394
references = get_references(self.lsp_context, uri, params.position)
424395
if not references:
425396
return None
426397
reference = references[0]
427-
if (
428-
isinstance(reference, LSPCteReference)
429-
or not reference.markdown_description
430-
):
398+
if isinstance(reference, LSPCteReference) or not reference.markdown_description:
431399
return None
432400
return types.Hover(
433401
contents=types.MarkupContent(
@@ -472,9 +440,7 @@ def goto_definition(
472440
self._ensure_context_for_document(uri)
473441
document = ls.workspace.get_text_document(params.text_document.uri)
474442
if self.lsp_context is None:
475-
raise RuntimeError(
476-
f"No context found for document: {document.path}"
477-
)
443+
raise RuntimeError(f"No context found for document: {document.path}")
478444

479445
references = get_references(self.lsp_context, uri, params.position)
480446
location_links = []
@@ -518,9 +484,7 @@ def goto_definition(
518484
)
519485
return location_links
520486
except Exception as e:
521-
ls.show_message(
522-
f"Error getting references: {e}", types.MessageType.Error
523-
)
487+
ls.show_message(f"Error getting references: {e}", types.MessageType.Error)
524488
return []
525489

526490
@self.server.feature(types.TEXT_DOCUMENT_REFERENCES)
@@ -533,25 +497,16 @@ def find_references(
533497
self._ensure_context_for_document(uri)
534498
document = ls.workspace.get_text_document(params.text_document.uri)
535499
if self.lsp_context is None:
536-
raise RuntimeError(
537-
f"No context found for document: {document.path}"
538-
)
500+
raise RuntimeError(f"No context found for document: {document.path}")
539501

540-
all_references = get_all_references(
541-
self.lsp_context, uri, params.position
542-
)
502+
all_references = get_all_references(self.lsp_context, uri, params.position)
543503

544504
# Convert references to Location objects
545-
locations = [
546-
types.Location(uri=ref.uri, range=ref.range)
547-
for ref in all_references
548-
]
505+
locations = [types.Location(uri=ref.uri, range=ref.range) for ref in all_references]
549506

550507
return locations if locations else None
551508
except Exception as e:
552-
ls.show_message(
553-
f"Error getting locations: {e}", types.MessageType.Error
554-
)
509+
ls.show_message(f"Error getting locations: {e}", types.MessageType.Error)
555510
return None
556511

557512
@self.server.feature(types.TEXT_DOCUMENT_DIAGNOSTIC)
@@ -564,10 +519,7 @@ def diagnostic(
564519
diagnostics, result_id = self._get_diagnostics_for_uri(uri)
565520

566521
# Check if client provided a previous result ID
567-
if (
568-
hasattr(params, "previous_result_id")
569-
and params.previous_result_id == result_id
570-
):
522+
if hasattr(params, "previous_result_id") and params.previous_result_id == result_id:
571523
# Return unchanged report if diagnostics haven't changed
572524
return types.RelatedUnchangedDocumentDiagnosticReport(
573525
kind=types.DocumentDiagnosticReportKind.Unchanged,
@@ -616,10 +568,7 @@ def workspace_diagnostic(
616568

617569
# Check if we have a previous result ID for this file
618570
previous_result_id = None
619-
if (
620-
hasattr(params, "previous_result_ids")
621-
and params.previous_result_ids
622-
):
571+
if hasattr(params, "previous_result_ids") and params.previous_result_ids:
623572
for prev in params.previous_result_ids:
624573
if prev.uri == uri.value:
625574
previous_result_id = prev.value
@@ -655,9 +604,7 @@ def workspace_diagnostic(
655604

656605
@self.server.feature(
657606
types.TEXT_DOCUMENT_COMPLETION,
658-
types.CompletionOptions(
659-
trigger_characters=["@"]
660-
), # advertise "@" for macros
607+
types.CompletionOptions(trigger_characters=["@"]), # advertise "@" for macros
661608
)
662609
def completion(
663610
ls: LanguageServer, params: types.CompletionParams
@@ -729,9 +676,7 @@ def completion(
729676
get_sql_completions(None, URI(params.text_document.uri))
730677
return None
731678

732-
def _get_diagnostics_for_uri(
733-
self, uri: URI
734-
) -> t.Tuple[t.List[types.Diagnostic], int]:
679+
def _get_diagnostics_for_uri(self, uri: URI) -> t.Tuple[t.List[types.Diagnostic], int]:
735680
"""Get diagnostics for a specific URI, returning (diagnostics, result_id).
736681
737682
Since we no longer track version numbers, we always return 0 as the result_id.
@@ -866,9 +811,7 @@ def _diagnostics_to_lsp_diagnostics(
866811
"""
867812
lsp_diagnostics = {}
868813
for diagnostic in diagnostics:
869-
lsp_diagnostic = SQLMeshLanguageServer._diagnostic_to_lsp_diagnostic(
870-
diagnostic
871-
)
814+
lsp_diagnostic = SQLMeshLanguageServer._diagnostic_to_lsp_diagnostic(diagnostic)
872815
if lsp_diagnostic is not None:
873816
# Create a unique key combining message and range
874817
diagnostic_key = (

0 commit comments

Comments
 (0)