Skip to content

Commit

Permalink
Distinguish role of id at cursor.
Browse files Browse the repository at this point in the history
  • Loading branch information
toinehartman committed Jul 8, 2024
1 parent 4aa54fb commit 6b1b44e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module lang::rascal::lsp::refactor::Exception

data Cursor;

alias Capture = tuple[loc def, loc use];

data IllegalRenameReason
Expand All @@ -10,7 +12,7 @@ data IllegalRenameReason
;

data RenameException
= illegalRename(loc location, set[IllegalRenameReason] reason)
= illegalRename(Cursor cursor, set[IllegalRenameReason] reason)
| unsupportedRename(rel[loc location, str message] issues)
| unexpectedFailure(str message)
;
18 changes: 14 additions & 4 deletions rascal-lsp/src/main/rascal/lang/rascal/lsp/refactor/Rename.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,20 @@ private bool rascalMayOverloadSameName(set[loc] defs, map[loc, Define] definitio
return rascalMayOverload(defs, potentialOverloadDefinitions);
}
private list[DocumentEdit] computeDocumentEdits(WorkspaceInfo ws, loc cursor, str name) {
loc useDefAtCursor = findSmallestContaining(ws.defines.defined + ws.useDef<0>, cursor);
private list[DocumentEdit] computeDocumentEdits(WorkspaceInfo ws, Tree cursorT, str name) {
loc cursorLoc = cursorT.src;
str cursorName = "<cursorT>";
set[Define] defs = getOverloadedDefines(ws, useDefAtCursor, rascalMayOverloadSameName);
cursorNamedDefs = (ws.defines<id, defined>)[cursorName];
smallestUse = findSmallestContaining(ws.useDef<0>, cursorLoc);
smallestDef = findSmallestContaining(cursorNamedDefs, cursorLoc);
Cursor cursor = smallestUse < smallestDef ? use(smallestUse) : def(smallestDef);
if (field(l) := cursor) throw unsupportedRename({<l, "Field names">});
if (cursor.l.scheme == "unknown") throw unexpectedFailure("Could not find cursor location.");
set[Define] defs = getOverloadedDefines(ws, cursor, rascalMayOverloadSameName);
set[loc] uses = ({} | it + getUses(ws, def) | def <- defs.defined);
rel[loc file, Define defines] defsPerFile = {<d.defined.top, d> | d <- defs};
Expand All @@ -224,7 +234,7 @@ private list[DocumentEdit] computeDocumentEdits(WorkspaceInfo ws, loc cursor, st
list[DocumentEdit] renameRascalSymbol(Tree cursor, set[loc] workspaceFolders, PathConfig pcfg, str newName) {
WorkspaceInfo ws = gatherWorkspaceInfo(workspaceFolders, pcfg);
return computeDocumentEdits(ws, cursor.src, newName);
return computeDocumentEdits(ws, cursor, newName);
}
//// WORKAROUNDS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import lang::rascalcore::check::Checker;
import util::FileSystem;
import util::Reflective;

data Cursor = use(loc l)
| def(loc l)
| field(loc l)
;

alias MayOverloadFun = bool(set[loc] defs, map[loc, Define] defines);

data WorkspaceInfo (
Expand Down Expand Up @@ -63,5 +68,8 @@ set[Define] getOverloadedDefines(WorkspaceInfo ws, set[loc] defs, MayOverloadFun
return overloadedDefs;
}

set[Define] getOverloadedDefines(WorkspaceInfo ws, loc useDef, MayOverloadFun mayOverloadF) =
getOverloadedDefines(ws, getDefs(ws, useDef) != {} ? getDefs(ws, useDef) : {useDef}, mayOverloadF);
set[Define] getOverloadedDefines(WorkspaceInfo ws, use(cursor), MayOverloadFun mayOverloadF) =
getOverloadedDefines(ws, getDefs(ws, cursor), mayOverloadF);

set[Define] getOverloadedDefines(WorkspaceInfo ws, def(cursor), MayOverloadFun mayOverloadF) =
getOverloadedDefines(ws, {cursor}, mayOverloadF);

0 comments on commit 6b1b44e

Please sign in to comment.