Skip to content
14 changes: 12 additions & 2 deletions rascal-lsp/src/main/rascal/lsp/lang/rascal/lsp/refactor/Rename.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,17 @@ public Edits rascalRenameSymbol(loc cursorLoc, list[Tree] cursor, str newName, s
ms = rascalTModelForNames([mname], ccfg, dummy_compile1);

<found, tm, ms> = getTModelForModule(mname, ms);
if (!found) throw "No TModel for module \'<mname>\'";
if (!found) {
if (ms.status[mname]? && MStatus::ignored() in ms.status[mname]) {
// If a module is annotated with `@ignoreCompiler`, silently skip it
// We just need a way to map the TModel back to the module location, so let's artificially add that
return tmodel()
[modelName = mname]
[moduleLocs = (mname: l)]
;
}
throw "No TModel for module \'<mname>\'";
}
return tm;
}

Expand Down Expand Up @@ -395,7 +405,7 @@ private loc nameSuffix(loc l, set[Define] defs, Renamer r) {

void renameUses(set[Define] defs, str newName, TModel tm, Renamer r) {
escName = normalizeEscaping(newName);
tm = getConditionallyAugmentedTModel(getModuleScopes(tm)[tm.modelName].top, defs, {augmentUses()}, r);
tm = getConditionallyAugmentedTModel(getModuleFile(tm), defs, {augmentUses()}, r);

definitions = {<d.defined, d> | d <- defs};
useDefs = toMap(tm.useDef o definitions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ bool isContainedInScope(loc l, loc scope, TModel tm) {
return any(loc fromScope <- reachableFrom, isContainedIn(l, fromScope));
}

loc getModuleFile(TModel tm) = getModuleScopes(tm)[tm.modelName].top;
loc getModuleFile(TModel tm) = tm.moduleLocs[tm.modelName].top when tm.moduleLocs[tm.modelName]?;

private set[str] reservedNames = getRascalReservedIdentifiers();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ set[Define] findAdditionalConstructorDefinitions(set[Define] cursorDefs, Tree tr
return {};
}

loc localScope = getModuleScopes(tm)[tm.modelName];
scopes = getModuleScopes(tm);
if (!scopes[tm.modelName]?) {
return {};
}
loc localScope = scopes[tm.modelName];
if (!any(Define d <- cursorDefs, isContainedInScope(d.defined, localScope, tm) || isContainedInScope(localScope, d.scope, tm))) {
return {};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ bool isUnsupportedCursor(list[Tree] _: [*_, Name n1, *_, (Expression) `<Expressi
void renameAdditionalUses(set[Define] fieldDefs, str newName, TModel tm, Renamer r) {
if (any(role <- fieldDefs.idRole, !isFieldRole(role)) || {} := fieldDefs) fail renameAdditionalUses;

loc mloc = getModuleScopes(tm)[tm.modelName].top;
Tree tr = r.getConfig().parseLoc(mloc);
Tree tr = r.getConfig().parseLoc(getModuleFile(tm));
visit (tr) {
case (Expression) `<Expression e> has <Name n>`: {
eFieldDefs = getFieldDefinitions(e, "<n>", tm, r.getConfig().tmodelForLoc);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
@license{
Copyright (c) 2018-2025, NWO-I CWI and Swat.engineering
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
}
module lang::rascal::tests::rename::ModuleAnnotations

import lang::rascal::tests::rename::TestUtils;

test bool useInIgnoredModule() = testRenameOccurrences({
byText("Ignored", "
'import Main;
'int quz() = foo();", {}, annotations = "@ignoreCompiler{For test purposes.}"),
byText("Main", "int foo() = 8;", {0})
});

test bool defInIgnoredModule() = testRenameOccurrences({
byText("Main", "
'import Ignored;
'int quz() = foo();", {}),
byText("Main", "int foo() = 8;", {}, annotations = "@ignoreCompiler{For test purposes.}")
});
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import util::Util;


//// Fixtures and utility functions
data TestModule = byText(str name, str body, set[int] nameOccs, str newName = name, set[int] skipCursors = {})
data TestModule = byText(str name, str body, set[int] nameOccs, str newName = name, set[int] skipCursors = {}, str annotations = "")
| byLoc(str name, loc file, set[int] nameOccs, str newName = name, set[int] skipCursors = {});

data RenameException
Expand Down Expand Up @@ -131,7 +131,7 @@ bool testProject(set[TestModule] modules, str testName, bool(set[TestModule] mod
}

pcfg = getTestPathConfig(testDir);
modulesByLocation = {mByLoc | m <- modules, mByLoc := (m is byLoc ? m : byLoc(m.name, storeTestModule(testDir, m.name, m.body), m.nameOccs, newName = m.newName, skipCursors = m.skipCursors))};
modulesByLocation = {mByLoc | m <- modules, mByLoc := (m is byLoc ? m : byLoc(m.name, storeTestModule(testDir, m.name, m.body, annotations = m.annotations), m.nameOccs, newName = m.newName, skipCursors = m.skipCursors))};

for (m <- modulesByLocation) {
try {
Expand Down Expand Up @@ -378,9 +378,9 @@ private tuple[loc, list[Tree]] findCursor(loc f, str id, int occ) {
return <cl, computeFocusList(m, cl.begin.line, cl.begin.column + 1)>;
}

private loc storeTestModule(loc dir, str name, str body) {
private loc storeTestModule(loc dir, str name, str body, str annotations = "") {
str moduleStr = "
'module <name>
'<annotations> module <name>
'<body>
";

Expand Down
Loading