Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions sqlmesh/lsp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,10 +496,10 @@ def goto_definition(
target_range = reference.target_range
target_selection_range = reference.target_range

if reference.uri is not None:
if reference.path is not None:
location_links.append(
types.LocationLink(
target_uri=reference.uri,
target_uri=URI.from_path(reference.path).value,
target_selection_range=target_selection_range,
target_range=target_range,
origin_selection_range=reference.range,
Expand All @@ -523,9 +523,9 @@ def find_references(

# Convert references to Location objects
locations = [
types.Location(uri=ref.uri, range=ref.range)
types.Location(uri=URI.from_path(ref.path).value, range=ref.range)
for ref in all_references
if ref.uri is not None
if ref.path is not None
]

return locations if locations else None
Expand Down
64 changes: 31 additions & 33 deletions sqlmesh/lsp/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class LSPModelReference(PydanticModel):
"""A LSP reference to a model, excluding external models."""

type: t.Literal["model"] = "model"
uri: str
path: Path
range: Range
markdown_description: t.Optional[str] = None

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

markdown_description: t.Optional[str] = None

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

type: t.Literal["cte"] = "cte"
uri: str
path: Path
range: Range
target_range: Range

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

type: t.Literal["macro"] = "macro"
uri: str
path: Path
range: Range
target_range: Range
markdown_description: t.Optional[str] = None
Expand Down Expand Up @@ -209,7 +209,7 @@ def get_model_definitions_for_a_path(

references.append(
LSPCteReference(
uri=document_uri.value, # Same file
path=document_uri.to_path(), # Same file
range=table_range,
target_range=target_range,
)
Expand All @@ -219,7 +219,7 @@ def get_model_definitions_for_a_path(
scope=scope,
reference_name=table.name,
read_file=read_file,
referenced_model_uri=document_uri,
referenced_model_path=document_uri.to_path(),
description="",
reference_type="cte",
cte_target_range=target_range,
Expand Down Expand Up @@ -270,7 +270,6 @@ def get_model_definitions_for_a_path(
# Check whether the path exists
if not referenced_model_path.is_file():
continue
referenced_model_uri = URI.from_path(referenced_model_path)

# Extract metadata for positioning
table_meta = TokenPositionDetails.from_meta(table.this.meta)
Expand Down Expand Up @@ -299,7 +298,7 @@ def get_model_definitions_for_a_path(
)
references.append(
LSPExternalModelReference(
uri=referenced_model_uri.value,
path=referenced_model_path,
range=Range(
start=to_lsp_position(start_pos_sqlmesh),
end=to_lsp_position(end_pos_sqlmesh),
Expand All @@ -313,7 +312,7 @@ def get_model_definitions_for_a_path(
scope=scope,
reference_name=normalized_reference_name,
read_file=read_file,
referenced_model_uri=referenced_model_uri,
referenced_model_path=referenced_model_path,
description=description,
yaml_target_range=yaml_target_range,
reference_type="external_model",
Expand All @@ -324,7 +323,7 @@ def get_model_definitions_for_a_path(
else:
references.append(
LSPModelReference(
uri=referenced_model_uri.value,
path=referenced_model_path,
range=Range(
start=to_lsp_position(start_pos_sqlmesh),
end=to_lsp_position(end_pos_sqlmesh),
Expand All @@ -337,7 +336,7 @@ def get_model_definitions_for_a_path(
scope=scope,
reference_name=normalized_reference_name,
read_file=read_file,
referenced_model_uri=referenced_model_uri,
referenced_model_path=referenced_model_path,
description=description,
reference_type="model",
default_catalog=lint_context.context.default_catalog,
Expand Down Expand Up @@ -481,10 +480,9 @@ def get_macro_reference(
return None

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

return LSPMacroReference(
uri=macro_uri.value,
path=path,
range=to_lsp_range(macro_range),
target_range=Range(
start=Position(line=start_line - 1, character=0),
Expand Down Expand Up @@ -517,7 +515,7 @@ def get_built_in_macro_reference(macro_name: str, macro_range: Range) -> t.Optio
end_line_number = line_number + len(source_lines) - 1

return LSPMacroReference(
uri=URI.from_path(Path(filename)).value,
path=Path(filename),
range=macro_range,
target_range=Range(
start=Position(line=line_number - 1, character=0),
Expand Down Expand Up @@ -559,12 +557,12 @@ def get_model_find_all_references(

assert isinstance(model_at_position, LSPModelReference) # for mypy

target_model_uri = model_at_position.uri
target_model_path = model_at_position.path

# Start with the model definition
all_references: t.List[LSPModelReference] = [
LSPModelReference(
uri=model_at_position.uri,
path=model_at_position.path,
range=Range(
start=Position(line=0, character=0),
end=Position(line=0, character=0),
Expand All @@ -575,7 +573,7 @@ def get_model_find_all_references(

# Then add references from the current file
current_file_refs = filter(
lambda ref: isinstance(ref, LSPModelReference) and ref.uri == target_model_uri,
lambda ref: isinstance(ref, LSPModelReference) and ref.path == target_model_path,
get_model_definitions_for_a_path(lint_context, document_uri),
)

Expand All @@ -584,7 +582,7 @@ def get_model_find_all_references(

all_references.append(
LSPModelReference(
uri=document_uri.value,
path=document_uri.to_path(),
range=ref.range,
markdown_description=ref.markdown_description,
)
Expand All @@ -600,7 +598,7 @@ def get_model_find_all_references(

# Get model references that point to the target model
matching_refs = filter(
lambda ref: isinstance(ref, LSPModelReference) and ref.uri == target_model_uri,
lambda ref: isinstance(ref, LSPModelReference) and ref.path == target_model_path,
get_model_definitions_for_a_path(lint_context, file_uri),
)

Expand All @@ -609,7 +607,7 @@ def get_model_find_all_references(

all_references.append(
LSPModelReference(
uri=file_uri.value,
path=path,
range=ref.range,
markdown_description=ref.markdown_description,
)
Expand Down Expand Up @@ -662,7 +660,7 @@ def get_cte_references(
# Add the CTE definition
matching_references = [
LSPCteReference(
uri=document_uri.value,
path=document_uri.to_path(),
range=target_cte_definition_range,
target_range=target_cte_definition_range,
)
Expand All @@ -673,7 +671,7 @@ def get_cte_references(
if ref.target_range == target_cte_definition_range:
matching_references.append(
LSPCteReference(
uri=document_uri.value,
path=document_uri.to_path(),
range=ref.range,
target_range=ref.target_range,
)
Expand Down Expand Up @@ -713,13 +711,13 @@ def get_macro_find_all_references(

assert isinstance(macro_at_position, LSPMacroReference) # for mypy

target_macro_uri = macro_at_position.uri
target_macro_path = macro_at_position.path
target_macro_target_range = macro_at_position.target_range

# Start with the macro definition
all_references: t.List[LSPMacroReference] = [
LSPMacroReference(
uri=target_macro_uri,
path=target_macro_path,
range=target_macro_target_range,
target_range=target_macro_target_range,
markdown_description=None,
Expand All @@ -733,7 +731,7 @@ def get_macro_find_all_references(
# Get macro references that point to the same macro definition
matching_refs = filter(
lambda ref: isinstance(ref, LSPMacroReference)
and ref.uri == target_macro_uri
and ref.path == target_macro_path
and ref.target_range == target_macro_target_range,
get_macro_definitions_for_a_path(lsp_context, file_uri),
)
Expand All @@ -742,7 +740,7 @@ def get_macro_find_all_references(
assert isinstance(ref, LSPMacroReference) # for mypy
all_references.append(
LSPMacroReference(
uri=file_uri.value,
path=path,
range=ref.range,
target_range=ref.target_range,
markdown_description=ref.markdown_description,
Expand Down Expand Up @@ -822,7 +820,7 @@ def _process_column_references(
scope: t.Any,
reference_name: str,
read_file: t.List[str],
referenced_model_uri: URI,
referenced_model_path: Path,
description: t.Optional[str] = None,
yaml_target_range: t.Optional[Range] = None,
reference_type: t.Literal["model", "external_model", "cte"] = "model",
Expand All @@ -837,7 +835,7 @@ def _process_column_references(
scope: The SQL scope to search for columns
reference_name: The full reference name (may include database/catalog)
read_file: The file content as list of lines
referenced_model_uri: URI of the referenced model
referenced_model_path: Path of the referenced model
description: Markdown description for the reference
yaml_target_range: Target range for external models (YAML files)
reference_type: Type of reference - "model", "external_model", or "cte"
Expand All @@ -857,7 +855,7 @@ def _process_column_references(
table_range = _get_column_table_range(column, read_file)
references.append(
LSPCteReference(
uri=referenced_model_uri.value,
path=referenced_model_path,
range=table_range,
target_range=cte_target_range,
)
Expand All @@ -875,7 +873,7 @@ def _process_column_references(
if reference_type == "external_model":
references.append(
LSPExternalModelReference(
uri=referenced_model_uri.value,
path=referenced_model_path,
range=table_range,
markdown_description=description,
target_range=yaml_target_range,
Expand All @@ -884,7 +882,7 @@ def _process_column_references(
else:
references.append(
LSPModelReference(
uri=referenced_model_uri.value,
path=referenced_model_path,
range=table_range,
markdown_description=description,
)
Expand Down
2 changes: 1 addition & 1 deletion sqlmesh/lsp/rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def _rename_cte(cte_references: t.List[LSPCteReference], new_name: str) -> Works
changes: t.Dict[str, t.List[TextEdit]] = {}

for ref in cte_references:
uri = ref.uri
uri = URI.from_path(ref.path).value
if uri not in changes:
changes[uri] = []

Expand Down
16 changes: 8 additions & 8 deletions tests/lsp/test_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def test_reference() -> None:
references = get_model_definitions_for_a_path(lsp_context, active_customers_uri)

assert len(references) == 1
uri = references[0].uri
assert uri is not None
assert URI(uri) == URI.from_path(sushi_customers_path)
path = references[0].path
assert path is not None
assert path == sushi_customers_path

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

assert references[0].uri.endswith("orders.py")
assert str(references[0].path).endswith("orders.py")
assert get_string_from_range(read_file, references[0].range) == "sushi.orders"
assert (
references[0].markdown_description
Expand All @@ -76,9 +76,9 @@ def test_reference_with_alias() -> None:
| end_ts | INT | |
| event_date | DATE | |"""
)
assert references[1].uri.endswith("order_items.py")
assert str(references[1].path).endswith("order_items.py")
assert get_string_from_range(read_file, references[1].range) == "sushi.order_items"
assert references[2].uri.endswith("items.py")
assert str(references[2].path).endswith("items.py")
assert get_string_from_range(read_file, references[2].range) == "sushi.items"


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

assert len(references) == 1
assert references[0].uri == URI.from_path(items_path).value
assert references[0].path == items_path

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

# For testing outside position, use a position before the current reference
Expand Down
4 changes: 2 additions & 2 deletions tests/lsp/test_reference_cte.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_cte_parsing():
position = Position(line=ranges[1].start.line, character=ranges[1].start.character + 4)
references = get_references(lsp_context, URI.from_path(sushi_customers_path), position)
assert len(references) == 1
assert references[0].uri == URI.from_path(sushi_customers_path).value
assert references[0].path == sushi_customers_path
assert isinstance(references[0], LSPCteReference)
assert (
references[0].range.start.line == ranges[1].start.line
Expand All @@ -42,7 +42,7 @@ def test_cte_parsing():
position = Position(line=ranges[1].start.line, character=ranges[1].start.character + 4)
references = get_references(lsp_context, URI.from_path(sushi_customers_path), position)
assert len(references) == 1
assert references[0].uri == URI.from_path(sushi_customers_path).value
assert references[0].path == sushi_customers_path
assert isinstance(references[0], LSPCteReference)
assert (
references[0].range.start.line == ranges[1].start.line
Expand Down
Loading