Skip to content

Commit 8f6b9f2

Browse files
committed
Include the property in the table type definition when finding all references on a property usage
Closes #1166
1 parent f98390d commit 8f6b9f2

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
4141
local y = x.Property
4242
```
4343
- Rename symbols now works on properties of table types (including across files)
44+
- Similarly, find all references on a property will now include the original table type definition if it exists. Rename
45+
will also modify this table type property name. ([#1166](https://github.com/JohnnyMorganz/luau-lsp/issues/1166))
4446

4547
### Changed
4648

src/operations/References.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,31 @@ std::vector<Reference> WorkspaceFolder::findAllTableReferences(
157157
references.push_back(Reference{moduleName, expr->location});
158158
}
159159
}
160+
161+
for (const auto [type, referencedTy] : module->astResolvedTypes)
162+
{
163+
if (isSameTable(ty, Luau::follow(referencedTy)))
164+
{
165+
if (property)
166+
{
167+
if (auto typeTable = type->as<Luau::AstTypeTable>())
168+
{
169+
for (const auto& prop : typeTable->props)
170+
{
171+
if (prop.name.value == *property)
172+
{
173+
references.push_back(Reference{moduleName, prop.location});
174+
break;
175+
}
176+
}
177+
}
178+
}
179+
else
180+
{
181+
references.push_back(Reference{moduleName, type->location});
182+
}
183+
}
184+
}
160185
}
161186

162187
// If its a property, include its original declaration location if not yet found
@@ -671,7 +696,6 @@ lsp::ReferenceResult WorkspaceFolder::references(const lsp::ReferenceParams& par
671696
if (prop.location.containsClosed(position))
672697
{
673698
auto references = findAllTableReferences(Luau::follow(*possibleTableTy), cancellationToken, prop.name.value);
674-
references.push_back(Reference{moduleName, prop.location});
675699
return processReferences(fileResolver, references);
676700
}
677701
}

tests/References.test.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,34 @@ TEST_CASE_FIXTURE(Fixture, "find_references_from_an_inline_table_property_in_a_t
147147
CHECK_EQ(lsp::Range{{6, 20}, {6, 24}}, result->at(1).range);
148148
}
149149

150+
TEST_CASE_FIXTURE(Fixture, "find_references_of_a_property_includes_the_original_table_type")
151+
{
152+
// Finding reference of "name" in "T.name"
153+
auto source = R"(
154+
type Tbl = {
155+
name: string
156+
}
157+
158+
local T: Tbl
159+
local x = T.name
160+
)";
161+
162+
auto uri = newDocument("foo.luau", source);
163+
164+
lsp::ReferenceParams params;
165+
params.textDocument = lsp::TextDocumentIdentifier{uri};
166+
params.position = lsp::Position{6, 22};
167+
168+
auto result = workspace.references(params, nullptr);
169+
REQUIRE(result);
170+
REQUIRE_EQ(2, result->size());
171+
172+
sortResults(result);
173+
174+
CHECK_EQ(lsp::Range{{2, 12}, {2, 16}}, result->at(0).range);
175+
CHECK_EQ(lsp::Range{{6, 20}, {6, 24}}, result->at(1).range);
176+
}
177+
150178
TEST_CASE_FIXTURE(Fixture, "find_references_of_a_global_function")
151179
{
152180
auto source = R"(
@@ -413,6 +441,41 @@ TEST_CASE_FIXTURE(Fixture, "cross_module_find_references_of_an_exported_table_ty
413441
CHECK_EQ(result->at(1).range, lsp::Range{{4, 20}, {4, 28}});
414442
}
415443

444+
TEST_CASE_FIXTURE(Fixture, "cross_module_find_references_of_an_exported_table_type_property_when_selecting_a_property_usage")
445+
{
446+
auto uri = newDocument("tbl.luau", R"(
447+
export type Table = {
448+
Property: string
449+
}
450+
return {}
451+
)");
452+
453+
auto user = newDocument("user.luau", R"(
454+
local tbl = require("tbl.luau")
455+
456+
local t: tbl.Table
457+
local v = t.Property
458+
)");
459+
460+
// Index reverse deps
461+
workspace.frontend.parse(workspace.fileResolver.getModuleName(user));
462+
463+
lsp::ReferenceParams params;
464+
params.textDocument = lsp::TextDocumentIdentifier{user};
465+
params.position = lsp::Position{4, 23}; // 'Property' usage when indexing 't'
466+
467+
auto result = workspace.references(params, nullptr);
468+
REQUIRE(result);
469+
REQUIRE_EQ(2, result->size());
470+
471+
sortResults(result);
472+
473+
CHECK_EQ(result->at(0).uri, uri);
474+
CHECK_EQ(result->at(0).range, lsp::Range{{2, 12}, {2, 20}});
475+
CHECK_EQ(result->at(1).uri, user);
476+
CHECK_EQ(result->at(1).range, lsp::Range{{4, 20}, {4, 28}});
477+
}
478+
416479
TEST_CASE_FIXTURE(Fixture, "references_respect_cancellation")
417480
{
418481
auto cancellationToken = std::make_shared<Luau::FrontendCancellationToken>();

0 commit comments

Comments
 (0)