Skip to content

Commit fbd2632

Browse files
authored
refactor(lsp): references now use paths (#5036)
1 parent 9b04f45 commit fbd2632

11 files changed

+78
-78
lines changed

sqlmesh/lsp/main.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,10 +496,10 @@ def goto_definition(
496496
target_range = reference.target_range
497497
target_selection_range = reference.target_range
498498

499-
if reference.uri is not None:
499+
if reference.path is not None:
500500
location_links.append(
501501
types.LocationLink(
502-
target_uri=reference.uri,
502+
target_uri=URI.from_path(reference.path).value,
503503
target_selection_range=target_selection_range,
504504
target_range=target_range,
505505
origin_selection_range=reference.range,
@@ -523,9 +523,9 @@ def find_references(
523523

524524
# Convert references to Location objects
525525
locations = [
526-
types.Location(uri=ref.uri, range=ref.range)
526+
types.Location(uri=URI.from_path(ref.path).value, range=ref.range)
527527
for ref in all_references
528-
if ref.uri is not None
528+
if ref.path is not None
529529
]
530530

531531
return locations if locations else None

sqlmesh/lsp/reference.py

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class LSPModelReference(PydanticModel):
2929
"""A LSP reference to a model, excluding external models."""
3030

3131
type: t.Literal["model"] = "model"
32-
uri: str
32+
path: Path
3333
range: Range
3434
markdown_description: t.Optional[str] = None
3535

@@ -40,9 +40,9 @@ class LSPExternalModelReference(PydanticModel):
4040
type: t.Literal["external_model"] = "external_model"
4141
range: Range
4242
target_range: t.Optional[Range] = None
43-
uri: t.Optional[str] = None
44-
"""The URI of the external model, typically a YAML file, it is optional because
45-
external models can be unregistered and so they URI is not available."""
43+
path: t.Optional[Path] = None
44+
"""The path of the external model, typically a YAML file, it is optional because
45+
external models can be unregistered and so the path is not available."""
4646

4747
markdown_description: t.Optional[str] = None
4848

@@ -51,7 +51,7 @@ class LSPCteReference(PydanticModel):
5151
"""A LSP reference to a CTE."""
5252

5353
type: t.Literal["cte"] = "cte"
54-
uri: str
54+
path: Path
5555
range: Range
5656
target_range: Range
5757

@@ -60,7 +60,7 @@ class LSPMacroReference(PydanticModel):
6060
"""A LSP reference to a macro."""
6161

6262
type: t.Literal["macro"] = "macro"
63-
uri: str
63+
path: Path
6464
range: Range
6565
target_range: Range
6666
markdown_description: t.Optional[str] = None
@@ -209,7 +209,7 @@ def get_model_definitions_for_a_path(
209209

210210
references.append(
211211
LSPCteReference(
212-
uri=document_uri.value, # Same file
212+
path=document_uri.to_path(), # Same file
213213
range=table_range,
214214
target_range=target_range,
215215
)
@@ -219,7 +219,7 @@ def get_model_definitions_for_a_path(
219219
scope=scope,
220220
reference_name=table.name,
221221
read_file=read_file,
222-
referenced_model_uri=document_uri,
222+
referenced_model_path=document_uri.to_path(),
223223
description="",
224224
reference_type="cte",
225225
cte_target_range=target_range,
@@ -270,7 +270,6 @@ def get_model_definitions_for_a_path(
270270
# Check whether the path exists
271271
if not referenced_model_path.is_file():
272272
continue
273-
referenced_model_uri = URI.from_path(referenced_model_path)
274273

275274
# Extract metadata for positioning
276275
table_meta = TokenPositionDetails.from_meta(table.this.meta)
@@ -299,7 +298,7 @@ def get_model_definitions_for_a_path(
299298
)
300299
references.append(
301300
LSPExternalModelReference(
302-
uri=referenced_model_uri.value,
301+
path=referenced_model_path,
303302
range=Range(
304303
start=to_lsp_position(start_pos_sqlmesh),
305304
end=to_lsp_position(end_pos_sqlmesh),
@@ -313,7 +312,7 @@ def get_model_definitions_for_a_path(
313312
scope=scope,
314313
reference_name=normalized_reference_name,
315314
read_file=read_file,
316-
referenced_model_uri=referenced_model_uri,
315+
referenced_model_path=referenced_model_path,
317316
description=description,
318317
yaml_target_range=yaml_target_range,
319318
reference_type="external_model",
@@ -324,7 +323,7 @@ def get_model_definitions_for_a_path(
324323
else:
325324
references.append(
326325
LSPModelReference(
327-
uri=referenced_model_uri.value,
326+
path=referenced_model_path,
328327
range=Range(
329328
start=to_lsp_position(start_pos_sqlmesh),
330329
end=to_lsp_position(end_pos_sqlmesh),
@@ -337,7 +336,7 @@ def get_model_definitions_for_a_path(
337336
scope=scope,
338337
reference_name=normalized_reference_name,
339338
read_file=read_file,
340-
referenced_model_uri=referenced_model_uri,
339+
referenced_model_path=referenced_model_path,
341340
description=description,
342341
reference_type="model",
343342
default_catalog=lint_context.context.default_catalog,
@@ -481,10 +480,9 @@ def get_macro_reference(
481480
return None
482481

483482
# Create a reference to the macro definition
484-
macro_uri = URI.from_path(path)
485483

486484
return LSPMacroReference(
487-
uri=macro_uri.value,
485+
path=path,
488486
range=to_lsp_range(macro_range),
489487
target_range=Range(
490488
start=Position(line=start_line - 1, character=0),
@@ -517,7 +515,7 @@ def get_built_in_macro_reference(macro_name: str, macro_range: Range) -> t.Optio
517515
end_line_number = line_number + len(source_lines) - 1
518516

519517
return LSPMacroReference(
520-
uri=URI.from_path(Path(filename)).value,
518+
path=Path(filename),
521519
range=macro_range,
522520
target_range=Range(
523521
start=Position(line=line_number - 1, character=0),
@@ -559,12 +557,12 @@ def get_model_find_all_references(
559557

560558
assert isinstance(model_at_position, LSPModelReference) # for mypy
561559

562-
target_model_uri = model_at_position.uri
560+
target_model_path = model_at_position.path
563561

564562
# Start with the model definition
565563
all_references: t.List[LSPModelReference] = [
566564
LSPModelReference(
567-
uri=model_at_position.uri,
565+
path=model_at_position.path,
568566
range=Range(
569567
start=Position(line=0, character=0),
570568
end=Position(line=0, character=0),
@@ -575,7 +573,7 @@ def get_model_find_all_references(
575573

576574
# Then add references from the current file
577575
current_file_refs = filter(
578-
lambda ref: isinstance(ref, LSPModelReference) and ref.uri == target_model_uri,
576+
lambda ref: isinstance(ref, LSPModelReference) and ref.path == target_model_path,
579577
get_model_definitions_for_a_path(lint_context, document_uri),
580578
)
581579

@@ -584,7 +582,7 @@ def get_model_find_all_references(
584582

585583
all_references.append(
586584
LSPModelReference(
587-
uri=document_uri.value,
585+
path=document_uri.to_path(),
588586
range=ref.range,
589587
markdown_description=ref.markdown_description,
590588
)
@@ -600,7 +598,7 @@ def get_model_find_all_references(
600598

601599
# Get model references that point to the target model
602600
matching_refs = filter(
603-
lambda ref: isinstance(ref, LSPModelReference) and ref.uri == target_model_uri,
601+
lambda ref: isinstance(ref, LSPModelReference) and ref.path == target_model_path,
604602
get_model_definitions_for_a_path(lint_context, file_uri),
605603
)
606604

@@ -609,7 +607,7 @@ def get_model_find_all_references(
609607

610608
all_references.append(
611609
LSPModelReference(
612-
uri=file_uri.value,
610+
path=path,
613611
range=ref.range,
614612
markdown_description=ref.markdown_description,
615613
)
@@ -662,7 +660,7 @@ def get_cte_references(
662660
# Add the CTE definition
663661
matching_references = [
664662
LSPCteReference(
665-
uri=document_uri.value,
663+
path=document_uri.to_path(),
666664
range=target_cte_definition_range,
667665
target_range=target_cte_definition_range,
668666
)
@@ -673,7 +671,7 @@ def get_cte_references(
673671
if ref.target_range == target_cte_definition_range:
674672
matching_references.append(
675673
LSPCteReference(
676-
uri=document_uri.value,
674+
path=document_uri.to_path(),
677675
range=ref.range,
678676
target_range=ref.target_range,
679677
)
@@ -713,13 +711,13 @@ def get_macro_find_all_references(
713711

714712
assert isinstance(macro_at_position, LSPMacroReference) # for mypy
715713

716-
target_macro_uri = macro_at_position.uri
714+
target_macro_path = macro_at_position.path
717715
target_macro_target_range = macro_at_position.target_range
718716

719717
# Start with the macro definition
720718
all_references: t.List[LSPMacroReference] = [
721719
LSPMacroReference(
722-
uri=target_macro_uri,
720+
path=target_macro_path,
723721
range=target_macro_target_range,
724722
target_range=target_macro_target_range,
725723
markdown_description=None,
@@ -733,7 +731,7 @@ def get_macro_find_all_references(
733731
# Get macro references that point to the same macro definition
734732
matching_refs = filter(
735733
lambda ref: isinstance(ref, LSPMacroReference)
736-
and ref.uri == target_macro_uri
734+
and ref.path == target_macro_path
737735
and ref.target_range == target_macro_target_range,
738736
get_macro_definitions_for_a_path(lsp_context, file_uri),
739737
)
@@ -742,7 +740,7 @@ def get_macro_find_all_references(
742740
assert isinstance(ref, LSPMacroReference) # for mypy
743741
all_references.append(
744742
LSPMacroReference(
745-
uri=file_uri.value,
743+
path=path,
746744
range=ref.range,
747745
target_range=ref.target_range,
748746
markdown_description=ref.markdown_description,
@@ -822,7 +820,7 @@ def _process_column_references(
822820
scope: t.Any,
823821
reference_name: str,
824822
read_file: t.List[str],
825-
referenced_model_uri: URI,
823+
referenced_model_path: Path,
826824
description: t.Optional[str] = None,
827825
yaml_target_range: t.Optional[Range] = None,
828826
reference_type: t.Literal["model", "external_model", "cte"] = "model",
@@ -837,7 +835,7 @@ def _process_column_references(
837835
scope: The SQL scope to search for columns
838836
reference_name: The full reference name (may include database/catalog)
839837
read_file: The file content as list of lines
840-
referenced_model_uri: URI of the referenced model
838+
referenced_model_path: Path of the referenced model
841839
description: Markdown description for the reference
842840
yaml_target_range: Target range for external models (YAML files)
843841
reference_type: Type of reference - "model", "external_model", or "cte"
@@ -857,7 +855,7 @@ def _process_column_references(
857855
table_range = _get_column_table_range(column, read_file)
858856
references.append(
859857
LSPCteReference(
860-
uri=referenced_model_uri.value,
858+
path=referenced_model_path,
861859
range=table_range,
862860
target_range=cte_target_range,
863861
)
@@ -875,7 +873,7 @@ def _process_column_references(
875873
if reference_type == "external_model":
876874
references.append(
877875
LSPExternalModelReference(
878-
uri=referenced_model_uri.value,
876+
path=referenced_model_path,
879877
range=table_range,
880878
markdown_description=description,
881879
target_range=yaml_target_range,
@@ -884,7 +882,7 @@ def _process_column_references(
884882
else:
885883
references.append(
886884
LSPModelReference(
887-
uri=referenced_model_uri.value,
885+
path=referenced_model_path,
888886
range=table_range,
889887
markdown_description=description,
890888
)

sqlmesh/lsp/rename.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def _rename_cte(cte_references: t.List[LSPCteReference], new_name: str) -> Works
9090
changes: t.Dict[str, t.List[TextEdit]] = {}
9191

9292
for ref in cte_references:
93-
uri = ref.uri
93+
uri = URI.from_path(ref.path).value
9494
if uri not in changes:
9595
changes[uri] = []
9696

tests/lsp/test_reference.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ def test_reference() -> None:
2525
references = get_model_definitions_for_a_path(lsp_context, active_customers_uri)
2626

2727
assert len(references) == 1
28-
uri = references[0].uri
29-
assert uri is not None
30-
assert URI(uri) == URI.from_path(sushi_customers_path)
28+
path = references[0].path
29+
assert path is not None
30+
assert path == sushi_customers_path
3131

3232
# Check that the reference in the correct range is sushi.customers
3333
path = active_customers_uri.to_path()
@@ -61,7 +61,7 @@ def test_reference_with_alias() -> None:
6161
with open(waiter_revenue_by_day_path, "r") as file:
6262
read_file = file.readlines()
6363

64-
assert references[0].uri.endswith("orders.py")
64+
assert str(references[0].path).endswith("orders.py")
6565
assert get_string_from_range(read_file, references[0].range) == "sushi.orders"
6666
assert (
6767
references[0].markdown_description
@@ -76,9 +76,9 @@ def test_reference_with_alias() -> None:
7676
| end_ts | INT | |
7777
| event_date | DATE | |"""
7878
)
79-
assert references[1].uri.endswith("order_items.py")
79+
assert str(references[1].path).endswith("order_items.py")
8080
assert get_string_from_range(read_file, references[1].range) == "sushi.order_items"
81-
assert references[2].uri.endswith("items.py")
81+
assert str(references[2].path).endswith("items.py")
8282
assert get_string_from_range(read_file, references[2].range) == "sushi.items"
8383

8484

@@ -102,7 +102,7 @@ def test_standalone_audit_reference() -> None:
102102
references = get_model_definitions_for_a_path(lsp_context, URI.from_path(audit_path))
103103

104104
assert len(references) == 1
105-
assert references[0].uri == URI.from_path(items_path).value
105+
assert references[0].path == items_path
106106

107107
# Check that the reference in the correct range is sushi.items
108108
with open(audit_path, "r") as file:
@@ -161,7 +161,7 @@ def test_filter_references_by_position() -> None:
161161
position_inside = Position(line=middle_line, character=middle_char)
162162
filtered = list(filter(by_position(position_inside), all_references))
163163
assert len(filtered) == 1
164-
assert filtered[0].uri == reference.uri
164+
assert filtered[0].path == reference.path
165165
assert filtered[0].range == reference.range
166166

167167
# For testing outside position, use a position before the current reference

tests/lsp/test_reference_cte.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def test_cte_parsing():
2727
position = Position(line=ranges[1].start.line, character=ranges[1].start.character + 4)
2828
references = get_references(lsp_context, URI.from_path(sushi_customers_path), position)
2929
assert len(references) == 1
30-
assert references[0].uri == URI.from_path(sushi_customers_path).value
30+
assert references[0].path == sushi_customers_path
3131
assert isinstance(references[0], LSPCteReference)
3232
assert (
3333
references[0].range.start.line == ranges[1].start.line
@@ -42,7 +42,7 @@ def test_cte_parsing():
4242
position = Position(line=ranges[1].start.line, character=ranges[1].start.character + 4)
4343
references = get_references(lsp_context, URI.from_path(sushi_customers_path), position)
4444
assert len(references) == 1
45-
assert references[0].uri == URI.from_path(sushi_customers_path).value
45+
assert references[0].path == sushi_customers_path
4646
assert isinstance(references[0], LSPCteReference)
4747
assert (
4848
references[0].range.start.line == ranges[1].start.line

0 commit comments

Comments
 (0)