Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix common keyword fields across module boundaries #552

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
21 changes: 11 additions & 10 deletions rascal-lsp/src/main/rascal/lang/rascal/lsp/refactor/Rename.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,11 @@ private bool rascalIsFunctionLocal(TModel ws, cursor(use(), cursorLoc, _)) =
private bool rascalIsFunctionLocal(TModel _, cursor(typeParam(), _, _)) = true;
private default bool rascalIsFunctionLocal(_, _) = false;

Maybe[AType] rascalAdtCommonKeywordFieldType(TModel ws, str fieldName, Define _:<_, _, _, _, _, DefInfo defInfo>) {
Maybe[AType] rascalAdtCommonKeywordFieldType(TModel ws, str fieldName, Define _:<_, _, _, _, _, DefInfo defInfo>, PathConfig(loc) getPathConfig) {
if (defInfo.commonKeywordFields?
&& kwf:(KeywordFormal) `<Type _> <Name kwName> = <Expression _>` <- defInfo.commonKeywordFields
&& "<kwName>" == fieldName) {
ws = (ws | appendTModel(it, tm) | tm <- rascalTModels({kwf.src.top}, getPathConfig(kwf.src.top)));
if (ft:just(_) := getFact(ws, kwf.src)) return ft;
throw "Unknown field type for <kwf.src>";
}
Expand All @@ -286,10 +287,10 @@ Maybe[AType] rascalConsFieldType(str fieldName, Define _:<_, _, _, constructorId
return nothing();
}

private CursorKind rascalGetDataFieldCursorKind(TModel ws, loc container, loc cursorLoc, str cursorName) {
private CursorKind rascalGetDataFieldCursorKind(TModel ws, loc container, loc cursorLoc, str cursorName, PathConfig(loc) getPathConfig) {
for (Define dt <- rascalGetADTDefinitions(ws, container)
&& AType adtType := dt.defInfo.atype) {
if (just(fieldType) := rascalAdtCommonKeywordFieldType(ws, cursorName, dt)) {
if (just(fieldType) := rascalAdtCommonKeywordFieldType(ws, cursorName, dt, getPathConfig)) {
// Case 4 or 5 (or 0): common keyword field
return dataCommonKeywordField(dt.defined, fieldType);
}
Expand All @@ -313,15 +314,15 @@ private CursorKind rascalGetDataFieldCursorKind(TModel ws, loc container, loc cu
throw illegalRename("Cannot rename \'<cursorName>\'; it is not defined in this workspace", {definitionsOutsideWorkspace(fromDefs)});
}

private CursorKind rascalGetCursorKind(TModel ws, loc cursorLoc, str cursorName, rel[loc l, CursorKind kind] locsContainingCursor, rel[loc field, loc container] fields, rel[loc kw, loc container] keywords) {
private CursorKind rascalGetCursorKind(TModel ws, loc cursorLoc, str cursorName, rel[loc l, CursorKind kind] locsContainingCursor, rel[loc field, loc container] fields, rel[loc kw, loc container] keywords, PathConfig(loc) getPathConfig) {
loc c = min(locsContainingCursor.l);
switch (locsContainingCursor[c]) {
case {moduleName(), *_}: {
return moduleName();
}
case {keywordParam(), dataKeywordField(_, _), *_}: {
if ({loc container} := keywords[c]) {
return rascalGetDataFieldCursorKind(ws, container, cursorLoc, cursorName);
return rascalGetDataFieldCursorKind(ws, container, cursorLoc, cursorName, getPathConfig);
}
}
case {collectionField(), dataField(_, _), dataKeywordField(_, _), dataCommonKeywordField(_, _), *_}: {
Expand All @@ -340,7 +341,7 @@ private CursorKind rascalGetCursorKind(TModel ws, loc cursorLoc, str cursorName,
// Case 1 (or 0): collection field
return collectionField();
}
return rascalGetDataFieldCursorKind(ws, container, cursorLoc, cursorName);
return rascalGetDataFieldCursorKind(ws, container, cursorLoc, cursorName, getPathConfig);
}
}
case {def(), *_}: {
Expand All @@ -349,7 +350,7 @@ private CursorKind rascalGetCursorKind(TModel ws, loc cursorLoc, str cursorName,
if (d.idRole is fieldId
&& Define adt: <_, _, _, dataId(), _, _> <- ws.defines
&& isStrictlyContainedIn(c, adt.defined)) {
return rascalGetDataFieldCursorKind(ws, adt.defined, cursorLoc, cursorName);
return rascalGetDataFieldCursorKind(ws, adt.defined, cursorLoc, cursorName, getPathConfig);
}
return def();
}
Expand Down Expand Up @@ -384,7 +385,7 @@ private CursorKind rascalGetCursorKind(TModel ws, loc cursorLoc, str cursorName,
throw unsupportedRename("Could not retrieve information for \'<cursorName>\' at <cursorLoc>.");
}

private Cursor rascalGetCursor(TModel ws, Tree cursorT) {
private Cursor rascalGetCursor(TModel ws, Tree cursorT, PathConfig(loc) getPathConfig) {
loc cursorLoc = cursorT.src;
str cursorName = "<cursorT>";

Expand Down Expand Up @@ -432,7 +433,7 @@ private Cursor rascalGetCursor(TModel ws, Tree cursorT) {
throw unsupportedRename("Renaming \'<cursorName>\' at <cursorLoc> is not supported.");
}

CursorKind kind = rascalGetCursorKind(ws, cursorLoc, cursorName, locsContainingCursor, fields, keywords);
CursorKind kind = rascalGetCursorKind(ws, cursorLoc, cursorName, locsContainingCursor, fields, keywords, getPathConfig);
return cursor(kind, min(locsContainingCursor.l), cursorName);
}

Expand Down Expand Up @@ -575,7 +576,7 @@ Edits rascalRenameSymbol(Tree cursorT, set[loc] workspaceFolders, str newName, P
TModel ws = loadLocs(tmodel(), preloadFiles(workspaceFolders, cursorLoc), localTmodelsForFiles);

step("analyzing name at cursor", 1);
cur = rascalGetCursor(ws, cursorT);
cur = rascalGetCursor(ws, cursorT, getPathConfig);

step("loading required type information", 1);
if (!rascalIsFunctionLocal(ws, cur)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ test bool commonKeywordField() = testRenameOccurrences({0, 1, 2, 3}, "
", decls = "data D(int foo = 0, int baz = 0) = d();"
);

test bool commonKeywordFieldFromOtherModule() = testRenameOccurrences({
byText("Foo", "data D(int foo = 0, int baz = 0) = d();", {0})
, byText("Bar",
"import Foo;
'D oneTwo = d(foo=1, baz=2);
", {0})
});

test bool multipleConstructorField() = testRenameOccurrences({0, 1, 2, 3}, "
'x = d(1, 2);
'y = x.foo;
Expand Down
Loading