Skip to content

Commit

Permalink
Improved descriptions in refactor preview.
Browse files Browse the repository at this point in the history
  • Loading branch information
toinehartman committed Nov 6, 2024
1 parent 68df61c commit ebdf1f7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
16 changes: 14 additions & 2 deletions rascal-lsp/src/main/rascal/lang/rascal/lsp/refactor/Rename.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -595,10 +595,22 @@ Edits rascalRenameSymbol(Tree cursorT, set[loc] workspaceFolders, str newName, P
return id;
};
set[RenameLocation] registerChangeAnnotations(set[RenameLocation] locs, str label, str description = "These changes are required for a correct renaming. They can be previewed here, but it is not advised to disable them.", bool needsConfirmation = false) {
set[RenameLocation] modifiedLocs = {};
for (rl <- locs) {
if (nothing() := rl.annotation) {
modifiedLocs += rl[annotation = just(registerChangeAnnotation(label, description, needsConfirmation))];
} else {
modifiedLocs += rl;
}
}
return modifiedLocs;
}
<defs, uses, getRenames> = rascalGetDefsUses(ws, cur, rascalMayOverloadSameName, registerChangeAnnotation);
rel[loc file, RenameLocation defines] defsPerFile = {<d.l.top, d> | d <- defs};
rel[loc file, RenameLocation uses] usesPerFile = {<u.l.top, u> | u <- uses};
rel[loc file, RenameLocation defines] defsPerFile = {<d.l.top, d> | d <- registerChangeAnnotations(defs, "Definitions")};
rel[loc file, RenameLocation uses] usesPerFile = {<u.l.top, u> | u <- registerChangeAnnotations(uses, "References")};
set[loc] \files = defsPerFile.file + usesPerFile.file;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import Relation;

import analysis::typepal::TModel;

import lang::rascalcore::check::ATypeUtils;
import lang::rascalcore::check::Checker;

import lang::rascal::\syntax::Rascal;
Expand Down Expand Up @@ -541,12 +542,15 @@ DefsUsesRenames rascalGetDefsUses(WorkspaceInfo ws, cursor(typeParam(), cursorLo
return <fromLocs(defs), fromLocs(useDefs - defs), NO_RENAMES>;
}
private str describeFact(just(AType tp)) = "type \'<prettyAType(tp)>\'";
private str describeFact(nothing()) = "unknown type";
set[RenameLocation] rascalGetHasUses(WorkspaceInfo ws, set[loc] defs, str cursorName, ChangeAnnotationRegister registerChangeAnnotation) {
return {
rl(name.src, annotation = just(registerChangeAnnotation("dynamic field name", "", true)))
rl(name.src, annotation = just(registerChangeAnnotation("Use of `has <cursorName>` on value of <describeFact(getFact(ws, e.src))>", "Due to the dynamic nature of these names, it cannot be ensured that they need to be renamed. Please review these suggested changes.", true)))
| loc l <- rascalReachableModules(ws, defs)
, start[Module] m := parseModuleWithSpacesCached(l)
, /(Expression) `<Expression _> has <Name name>` := m
, /(Expression) `<Expression e> has <Name name>` := m
, "<name>" == cursorName
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ private DocumentEdit sortChanges(changed(loc l, list[TextEdit] edits)) = changed
}));
private default DocumentEdit sortChanges(DocumentEdit e) = e;

private list[DocumentEdit] stripAnnotations(list[DocumentEdit] edits) =
visit (edits) { case replace(range, replacement, _) => replace(range, replacement) };

private void verifyTypeCorrectRenaming(loc root, Edits edits, PathConfig pcfg) {
executeDocumentEdits(sortEdits(edits<0>));
executeDocumentEdits(sortEdits(stripAnnotations(edits<0>)));
remove(pcfg.resources);
RascalCompilerConfig ccfg = rascalCompilerConfig(pcfg)[forceCompilationTopModule = true][verbose = false][logPathConfig = false];
throwAnyErrors(checkAll(root, ccfg));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ import analysis::diff::edits::TextEdits;

test bool renameToReservedName() {
edits = getEdits("int foo = 8;", 0, "foo", "int", "", "");
newNames = {name | e <- edits<0>, changed(_, replaces) := e
, r <- replaces, replace(_, name) := r};
newNames = {name | e <- edits<0>
, r <- e.edits
, name := r.replacement};

return newNames == {"\\int"};
}
Expand Down

0 comments on commit ebdf1f7

Please sign in to comment.