Skip to content

Commit

Permalink
added \'add missing license\' action for Rascal
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenvinju committed Oct 23, 2024
1 parent 6be8c16 commit 0ab364f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand Down
42 changes: 42 additions & 0 deletions rascal-lsp/src/main/rascal/lang/rascal/lsp/Actions.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import lang::rascal::vis::ImportGraph;
import util::Reflective;
import util::IDEServices;
import List;
import IO;

@synopsis{Here we list Rascal-specific code commands}
@description{
Expand All @@ -51,6 +52,10 @@ data Command
list[CodeAction] rascalCodeActions(Focus focus, PathConfig pcfg=pathConfig()) {
result = [];

if ([*_, start[Module] top] := focus) {
result += addLicenseAction(top, pcfg);
}

if ([*_, Toplevel t, *_] := focus) {
result += toplevelCodeActions(t);
}
Expand All @@ -64,6 +69,43 @@ list[CodeAction] rascalCodeActions(Focus focus, PathConfig pcfg=pathConfig()) {
return result;
}

@synopsis{Add a license header if there isn't one.}
list[CodeAction] addLicenseAction(start[Module] \module, PathConfig pcfg) {
Tags tags = \module.top.header.tags;

if ((Tags) `<Tag*_ > @license<TagString _> <Tag* _>` !:= tags) {
license = findLicense(pcfg);
if (license != "") {
license = "@license{
'<license>
'}\n";
return [action(edits=[makeLicenseEdit(\module@\loc, license)], title="Add missing license header")];
}
}

return [];
}

str findLicense(PathConfig pcfg) {
for (loc src <- pcfg.srcs) {
while (!exists(src + "pom.xml") && src.path != "" && src.path != "/") {
src = src.parent;
}

if (exists(src + "LICENSE")) {
return trim(readFile(src + "LICENSE"));
}
else if (exists(src + "LICENSE.md")) {
return trim(readFile(src + "LICENSE.md"));
}
}

return "";
}

DocumentEdit makeLicenseEdit(loc \module, str license)
= changed(\module.top, [replace(\module.top(0, 0), license)]);

@synopsis{Rewrite immediate return to expression.}
list[CodeAction] toplevelCodeActions(Toplevel t:
(Toplevel) `<Tags tags>
Expand Down

0 comments on commit 0ab364f

Please sign in to comment.