Skip to content

Commit

Permalink
Merge pull request #559 from usethesource/fix/rename-refactoring/type…
Browse files Browse the repository at this point in the history
…-errors

Fix rename refactoring type errors
  • Loading branch information
toinehartman authored Jan 28, 2025
2 parents f415889 + 3d1323e commit de49e49
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
13 changes: 7 additions & 6 deletions rascal-lsp/src/main/rascal/lang/rascal/lsp/refactor/Rename.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ void throwAnyErrors(program(_, msgs)) {
throwAnyErrors(msgs);
}

private set[IllegalRenameReason] rascalCheckLegalName(str name, set[IdRole] roles) {
private set[IllegalRenameReason] rascalCheckLegalNameByRoles(str name, set[IdRole] roles) {
escName = rascalEscapeName(name);
tuple[type[&T <: Tree] as, str desc] asType = <#Name, "identifier">;
tuple[type[Tree] as, str desc] asType = <#Name, "identifier">;
if ({moduleId(), *_} := roles) asType = <#QualifiedName, "module name">;
if ({constructorId(), *_} := roles) asType = <#NonterminalLabel, "constructor name">;
if ({fieldId(), *_} := roles) asType = <#NonterminalLabel, "constructor field name">;
Expand All @@ -90,10 +90,11 @@ private set[IllegalRenameReason] rascalCheckLegalName(str name, set[IdRole] role
return {};
}

private void rascalCheckLegalName(str name, Symbol sym) {
private void rascalCheckLegalNameByType(str name, Symbol sym) {
escName = rascalEscapeName(name);
g = grammar(#start[Module]);
if (tryParseAs(type(sym, g.rules), escName) is nothing) {
if (type[Tree] t := type(sym, g.rules)
, tryParseAs(t, escName) is nothing) {
throw illegalRename("\'<escName>\' is not a valid name at this position", {invalidName(escName, "<sym>")});
}
}
Expand Down Expand Up @@ -177,7 +178,7 @@ private set[IllegalRenameReason] rascalCollectIllegalRenames(TModel ws, rel[loc
set[loc] editFiles = defsPerFile.file + usesPerFile.file;
set[IllegalRenameReason] reasons = {};
reasons += rascalCheckLegalName(newName, definitionsRel(ws)[defsPerFile.rename.l].idRole);
reasons += rascalCheckLegalNameByRoles(newName, definitionsRel(ws)[defsPerFile.rename.l].idRole);
reasons += rascalCheckDefinitionsOutsideWorkspace(ws, defsPerFile.rename.l);
reasons += rascalCheckCausesDoubleDeclarations(ws, defsPerFile.rename.l, newNameDefs, newName);
for (file <- editFiles) {
Expand Down Expand Up @@ -601,7 +602,7 @@ Edits rascalRenameSymbol(Tree cursorT, set[loc] workspaceFolders, str newName, P
loc cursorLoc = cursorT.src;
str cursorName = "<cursorT>";
rascalCheckLegalName(newName, typeOf(cursorT));
rascalCheckLegalNameByType(newName, typeOf(cursorT));
step("preloading minimal workspace information", 1);
set[TModel] localTmodelsForFiles(ProjectFiles projectFiles) = tmodelsForProjectFiles(projectFiles, rascalTModels, getPathConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ POSSIBILITY OF SUCH DAMAGE.
module lang::rascal::tests::rename::Grammars

import lang::rascal::tests::rename::TestUtils;
import lang::rascal::lsp::refactor::Exception;

test bool productionType() = testRenameOccurrences({0, 1, 2, 3}, "
'Foo func(Foo f) = f.child;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ POSSIBILITY OF SUCH DAMAGE.
module lang::rascal::tests::rename::ProjectOnDisk

import lang::rascal::lsp::refactor::Rename;
import lang::rascal::lsp::refactor::Util;
import lang::rascal::tests::rename::TestUtils;
import util::Reflective;
import lang::rascalcore::check::Checker;

Edits testProjectOnDisk(loc projectDir, str file, str oldName, int occurrence = 0, str newName = "<oldName>_new") {
PathConfig pcfg;
Expand Down

0 comments on commit de49e49

Please sign in to comment.