|
| 1 | +from lsprotocol.types import Position, DocumentHighlightKind |
| 2 | + |
| 3 | +from sqlmesh.core.context import Context |
| 4 | +from sqlmesh.lsp.context import LSPContext, ModelTarget |
| 5 | +from sqlmesh.lsp.rename import get_document_highlights |
| 6 | +from sqlmesh.lsp.uri import URI |
| 7 | +from tests.lsp.test_reference_cte import find_ranges_from_regex |
| 8 | + |
| 9 | + |
| 10 | +def test_get_document_highlights_cte(): |
| 11 | + context = Context(paths=["examples/sushi"]) |
| 12 | + lsp_context = LSPContext(context) |
| 13 | + |
| 14 | + # Use the existing customers.sql model which has CTEs |
| 15 | + sushi_customers_path = next( |
| 16 | + path |
| 17 | + for path, info in lsp_context.map.items() |
| 18 | + if isinstance(info, ModelTarget) and "sushi.customers" in info.names |
| 19 | + ) |
| 20 | + |
| 21 | + with open(sushi_customers_path, "r", encoding="utf-8") as file: |
| 22 | + read_file = file.readlines() |
| 23 | + |
| 24 | + test_uri = URI.from_path(sushi_customers_path) |
| 25 | + |
| 26 | + # Find the ranges for "current_marketing" CTE (not outer one) |
| 27 | + ranges = find_ranges_from_regex(read_file, r"current_marketing(?!_outer)") |
| 28 | + assert len(ranges) >= 2 # Should have definition + usage |
| 29 | + |
| 30 | + # Test highlighting CTE definition - position on "current_marketing" definition |
| 31 | + position = Position(line=ranges[0].start.line, character=ranges[0].start.character + 4) |
| 32 | + highlights = get_document_highlights(lsp_context, test_uri, position) |
| 33 | + |
| 34 | + assert highlights is not None |
| 35 | + assert len(highlights) >= 2 # Definition + at least 1 usage |
| 36 | + |
| 37 | + # Check that we have both definition (Write) and usage (Read) highlights |
| 38 | + highlight_kinds = [h.kind for h in highlights] |
| 39 | + assert DocumentHighlightKind.Write in highlight_kinds # CTE definition |
| 40 | + assert DocumentHighlightKind.Read in highlight_kinds # CTE usage |
| 41 | + |
| 42 | + # Test highlighting CTE usage - position on "current_marketing" usage |
| 43 | + position = Position(line=ranges[1].start.line, character=ranges[1].start.character + 4) |
| 44 | + highlights = get_document_highlights(lsp_context, test_uri, position) |
| 45 | + |
| 46 | + assert highlights is not None |
| 47 | + assert len(highlights) >= 2 # Should find the same references |
| 48 | + |
| 49 | + |
| 50 | +def test_get_document_highlights_no_symbol(): |
| 51 | + context = Context(paths=["examples/sushi"]) |
| 52 | + lsp_context = LSPContext(context) |
| 53 | + |
| 54 | + # Use the existing customers.sql model |
| 55 | + sushi_customers_path = next( |
| 56 | + path |
| 57 | + for path, info in lsp_context.map.items() |
| 58 | + if isinstance(info, ModelTarget) and "sushi.customers" in info.names |
| 59 | + ) |
| 60 | + |
| 61 | + test_uri = URI.from_path(sushi_customers_path) |
| 62 | + |
| 63 | + # Test position not on any CTE symbol - just on a random keyword |
| 64 | + position = Position(line=5, character=5) |
| 65 | + highlights = get_document_highlights(lsp_context, test_uri, position) |
| 66 | + |
| 67 | + assert highlights is None |
| 68 | + |
| 69 | + |
| 70 | +def test_get_document_highlights_multiple_ctes(): |
| 71 | + context = Context(paths=["examples/sushi"]) |
| 72 | + lsp_context = LSPContext(context) |
| 73 | + |
| 74 | + # Use the existing customers.sql model which has both outer and inner CTEs |
| 75 | + sushi_customers_path = next( |
| 76 | + path |
| 77 | + for path, info in lsp_context.map.items() |
| 78 | + if isinstance(info, ModelTarget) and "sushi.customers" in info.names |
| 79 | + ) |
| 80 | + |
| 81 | + with open(sushi_customers_path, "r", encoding="utf-8") as file: |
| 82 | + read_file = file.readlines() |
| 83 | + |
| 84 | + test_uri = URI.from_path(sushi_customers_path) |
| 85 | + |
| 86 | + # Test the outer CTE - "current_marketing_outer" |
| 87 | + outer_ranges = find_ranges_from_regex(read_file, r"current_marketing_outer") |
| 88 | + assert len(outer_ranges) >= 2 # Should have definition + usage |
| 89 | + |
| 90 | + # Test highlighting outer CTE - should only highlight that CTE |
| 91 | + position = Position( |
| 92 | + line=outer_ranges[0].start.line, character=outer_ranges[0].start.character + 4 |
| 93 | + ) |
| 94 | + highlights = get_document_highlights(lsp_context, test_uri, position) |
| 95 | + |
| 96 | + assert highlights is not None |
| 97 | + assert len(highlights) == len(outer_ranges) # Should match all occurrences of outer CTE |
| 98 | + |
| 99 | + # Test the inner CTE - "current_marketing" (not outer) |
| 100 | + inner_ranges = find_ranges_from_regex(read_file, r"current_marketing(?!_outer)") |
| 101 | + assert len(inner_ranges) >= 2 # Should have definition + usage |
| 102 | + |
| 103 | + # Test highlighting inner CTE - should only highlight that CTE, not the outer one |
| 104 | + position = Position( |
| 105 | + line=inner_ranges[0].start.line, character=inner_ranges[0].start.character + 4 |
| 106 | + ) |
| 107 | + highlights = get_document_highlights(lsp_context, test_uri, position) |
| 108 | + |
| 109 | + # This should return the column usages as well |
| 110 | + assert highlights is not None |
| 111 | + assert len(highlights) == 4 |
0 commit comments