Skip to content

Commit

Permalink
Check for specific unsupported (for now) cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
toinehartman committed Jun 12, 2024
1 parent 47a9698 commit 093bbf0
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 9 deletions.
39 changes: 30 additions & 9 deletions rascal-lsp/src/main/rascal/lang/rascal/lsp/Rename.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,32 @@ bool isLegalRename(TModel tm, start[Module] m, set[loc] defLocs, set[loc] useLoc
return false;
}
checkUnsupported(m.src, tm, defLocs);
return true;
}
void checkUnsupported(loc moduleLoc, TModel tm, set[loc] defLocs) {
Maybe[str] isUnsupportedDefinition(Define _: <scope, id, _, functionId(), _, _>) = just("Global function definitions might span multiple modules; unsupported for now.")
when moduleLoc == scope; // function is defined in module scope
Maybe[str] isUnsupportedDefinition(Define _) = nothing();
println("Module scope: <moduleLoc>");
if (loc def <- defLocs, just(msg) := isUnsupportedDefinition(tm.definitions[def])) {
throw UnsupportedRename(def, msg);
}
}
str escapeName(str name) = name in getRascalReservedIdentifiers() ? "\\<name>" : name;
list[DocumentEdit] renameRascalSymbol(start[Module] m, Tree cursor, set[loc] workspaceFolders, TModel tm, str newName) {
loc cursorLoc = cursor.src;
set[loc] defs = getDefinitions(tm, cursorLoc);
if (defs == {}) {
// Cursor points at a definition
defs += cursorLoc;
// Cursor points at *name* within definition
defs += findDeclarationAroundName(m, cursorLoc);
}
set[loc] uses = ({} | it + getUses(tm, def) | loc def <- defs);
Expand Down Expand Up @@ -211,6 +225,19 @@ list[DocumentEdit] renameRascalSymbol(start[Module] m, Tree cursor, set[loc] wor
// Workaround to be able to pattern match on the emulated `src` field
data Tree (loc src = |unknown:///|(0,0,<0,0>,<0,0>));
loc findDeclarationAroundName(start[Module]m, loc nameLoc) {
// we want to find the *largest* tree of defined non-terminal type of which the declared name is at nameLoc
top-down visit (m.top) {
case t: appl(prod(_, _, _), _): {
if (just(nameLoc) := locationOfName(t)) {
return t.src;
}
}
}
throw UnexpectedFailure("No declaration found with name at <nameLoc> in module <m>");
}
loc findNameInDeclaration(start[Module] m, loc declLoc) {
// we want to find the smallest tree of defined non-terminal type with source location `declLoc`
visit(m.top) {
Expand All @@ -228,10 +255,4 @@ loc findNameInDeclaration(start[Module] m, loc declLoc) {
Maybe[loc] locationOfName(Name n) = just(n.src);
Maybe[loc] locationOfName(QualifiedName qn) = just((qn.names[-1]).src);
Maybe[loc] locationOfName(FunctionDeclaration f) = just(f.signature.name.src);
default Maybe[loc] locationOfName(Tree t) {
println("Unsupported: cannot find name in <t.src>");
print(prettyTree(t));
rprintln(t);
return nothing();
}
default Maybe[loc] locationOfName(Tree t) = nothing();
42 changes: 42 additions & 0 deletions rascal-lsp/src/main/rascal/lang/rascal/tests/Rename.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,48 @@ test bool nestedFunctionParameter() = {0, 1} == testRenameOccurrences("
'}
");

test bool nestedPublicFunction() = {0, 1} == testRenameOccurrences("
'public int foo(int f) {
' return f;
'}
'foo(1);
");

test bool nestedDefaultFunction() = {0, 1} == testRenameOccurrences("
'int foo(int f) {
' return f;
'}
'foo(1);
");

test bool nestedPrivateFunction() = {0, 1} == testRenameOccurrences("
'private int foo(int f) {
' return f;
'}
'foo(1);
");

@expected{UnsupportedRename}
test bool publicFunction() = testRename("foo(1);", decls = "
'public int foo(int f) {
' return f;
'}
");

@expected{UnsupportedRename}
test bool defaultFunction() = testRename("", decls = "
'int foo(int f) {
' return f;
'}
");

@expected{UnsupportedRename}
test bool privateFunction() = testRename("", decls = "
'private int foo(int f) {
' return f;
'}
");

test bool publicFunctionParameter() = {0, 1} == testRenameOccurrences("", decls = "
'public int f(int foo) {
' return foo;
Expand Down

0 comments on commit 093bbf0

Please sign in to comment.