-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added source code action to trigger souffle-lint Added code action to extract types
- Loading branch information
Showing
19 changed files
with
297 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import org.eclipse.lsp4j.*; | ||
import org.eclipse.lsp4j.jsonrpc.messages.Either; | ||
import parsing.symbols.SouffleContext; | ||
import parsing.symbols.SouffleProjectContext; | ||
import parsing.symbols.SouffleSymbol; | ||
import parsing.symbols.SouffleSymbolType; | ||
|
||
import java.net.URI; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class CodeActionProvider { | ||
public CodeActionProvider() { | ||
} | ||
|
||
public List<Either<Command, CodeAction>> getCodeAction(CodeActionParams params) { | ||
List<Either<Command, CodeAction>> actions = new ArrayList<Either<Command, CodeAction>>(); | ||
|
||
lintCodeAction(params, actions); | ||
|
||
Range cursor = params.getRange(); | ||
SouffleContext context = SouffleProjectContext.getInstance().getContext(params.getTextDocument().getUri(), cursor); | ||
if (context != null) { | ||
SouffleSymbol currentSymbol = context.getSymbol(cursor); | ||
if (currentSymbol != null) { | ||
switch (currentSymbol.getKind()) { | ||
case RELATION_USE: | ||
case RELATION_DECL: | ||
generateIOCodeAction(params, actions, context, currentSymbol); | ||
break; | ||
case TYPE_USE: | ||
case TYPE_DECL: | ||
extractTypeCodeAction(params, actions, currentSymbol); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return actions; | ||
} | ||
|
||
private void extractTypeCodeAction(CodeActionParams params, List<Either<Command, CodeAction>> actions, SouffleSymbol currentSymbol) { | ||
CodeAction extractSubtypeAction = new CodeAction("Extract as Supertype"); | ||
extractSubtypeAction.setKind(CodeActionKind.RefactorExtract); | ||
actions.add(Either.forRight(extractSubtypeAction)); | ||
WorkspaceEdit extractEdit = new WorkspaceEdit(); | ||
TextEdit newTypeText = new TextEdit(); | ||
newTypeText.setNewText(".type " + "subType <: " + currentSymbol + "\n\n"); | ||
|
||
int line = currentSymbol.getRange().getStart().getLine() + 1; | ||
|
||
List<TextEdit> subtypeEdits = new ArrayList<TextEdit>(); | ||
if (currentSymbol.getKind() != SouffleSymbolType.TYPE_DECL) { | ||
TextEdit replaceTypeEdit = new TextEdit(); | ||
replaceTypeEdit.setNewText("subType"); | ||
replaceTypeEdit.setRange(currentSymbol.getRange()); | ||
subtypeEdits.add(replaceTypeEdit); | ||
line = currentSymbol.getRange().getStart().getLine() - 1; | ||
} | ||
extractSubtypeAction.setData("subType"); | ||
Position start = new Position(line, 0); | ||
|
||
Range newTypeRange = new Range(start, start); | ||
newTypeText.setRange(newTypeRange); | ||
|
||
subtypeEdits.add(newTypeText); | ||
extractEdit.setChanges(Map.of(params.getTextDocument().getUri(), subtypeEdits)); | ||
extractSubtypeAction.setEdit(extractEdit); | ||
|
||
CodeAction extractTypeAction = new CodeAction("Extract Type"); | ||
extractTypeAction.setKind(CodeActionKind.RefactorExtract); | ||
actions.add(Either.forRight(extractTypeAction)); | ||
WorkspaceEdit extractTypeEdit = new WorkspaceEdit(); | ||
TextEdit newTypeText1 = new TextEdit(); | ||
newTypeText1.setNewText(".type " + "extractedType = " + currentSymbol + "\n\n"); | ||
|
||
newTypeText1.setRange(newTypeRange); | ||
List<TextEdit> extractEdits = new ArrayList<TextEdit>(); | ||
extractEdits.add(newTypeText1); | ||
if (currentSymbol.getKind() != SouffleSymbolType.TYPE_DECL) { | ||
TextEdit replaceTypeEdit1 = new TextEdit(); | ||
replaceTypeEdit1.setNewText("extractedType"); | ||
replaceTypeEdit1.setRange(currentSymbol.getRange()); | ||
extractEdits.add(replaceTypeEdit1); | ||
} | ||
extractTypeEdit.setChanges(Map.of(params.getTextDocument().getUri(), extractEdits)); | ||
extractTypeAction.setEdit(extractTypeEdit); | ||
} | ||
|
||
private void generateIOCodeAction(CodeActionParams params, List<Either<Command, CodeAction>> actions, SouffleContext context, SouffleSymbol currentSymbol) { | ||
CodeAction inputAction = new CodeAction("Generate .input for relation " + currentSymbol.getName()); | ||
inputAction.setKind(CodeActionKind.Refactor); | ||
actions.add(Either.forRight(inputAction)); | ||
WorkspaceEdit edit = new WorkspaceEdit(); | ||
TextEdit textEdit = new TextEdit(); | ||
textEdit.setNewText("\t.input " + currentSymbol.getName() + "()\n"); | ||
|
||
Position end; | ||
if (currentSymbol.getKind() == SouffleSymbolType.RELATION_DECL) { | ||
end = currentSymbol.getRange().getEnd(); | ||
} else { | ||
end = context.getRange().getEnd(); | ||
} | ||
Position position = new Position(end.getLine() + 2, 0); | ||
Range newRange = new Range(position, position); | ||
textEdit.setRange(newRange); | ||
edit.setChanges(Map.of(params.getTextDocument().getUri(), List.of(textEdit))); | ||
inputAction.setEdit(edit); | ||
|
||
CodeAction outputAction = new CodeAction("Generate .output for relation " + currentSymbol.getName()); | ||
outputAction.setKind(CodeActionKind.Refactor); | ||
actions.add(Either.forRight(outputAction)); | ||
WorkspaceEdit edit1 = new WorkspaceEdit(); | ||
TextEdit textEdit1 = new TextEdit(); | ||
textEdit1.setNewText("\t.output " + currentSymbol.getName() + "()\n"); | ||
|
||
textEdit1.setRange(newRange); | ||
edit1.setChanges(Map.of(params.getTextDocument().getUri(), List.of(textEdit1))); | ||
outputAction.setEdit(edit1); | ||
|
||
if ((currentSymbol.getKind() == SouffleSymbolType.RELATION_DECL || | ||
currentSymbol.getKind() == SouffleSymbolType.COMPONENT_DECL) && | ||
currentSymbol.getPotentialDocumentation().getKey() != null) { | ||
CodeAction formatComments = new CodeAction("Format documentation with /* */"); | ||
formatComments.setKind(CodeActionKind.RefactorRewrite); | ||
WorkspaceEdit commentEdit = new WorkspaceEdit(); | ||
TextEdit commentTextEdit = new TextEdit(); | ||
commentTextEdit.setRange(currentSymbol.getPotentialDocumentation().getValue()); | ||
commentTextEdit.setNewText(currentSymbol.getPotentialDocumentation().getKey()); | ||
commentEdit.setChanges(Map.of(params.getTextDocument().getUri(), List.of(commentTextEdit))); | ||
formatComments.setEdit(commentEdit); | ||
actions.add(Either.forRight(formatComments)); | ||
} | ||
} | ||
|
||
private void lintCodeAction(CodeActionParams params, List<Either<Command, CodeAction>> actions) { | ||
CodeAction codeAction = new CodeAction("Lint with souffle-lint"); | ||
Command command = new Command(); | ||
command.setCommand("souffle-lint"); | ||
Path path = Path.of(URI.create(params.getTextDocument().getUri())); | ||
command.setArguments(List.of(path.toString())); | ||
codeAction.setCommand(command); | ||
codeAction.setKind(CodeActionKind.Source + ".lint"); | ||
actions.add(Either.forRight(codeAction)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import java.util.List; | ||
|
||
public class SouffleLint { | ||
List<SouffleLintContext> fragments; | ||
SouffleLintRule rule; | ||
String source; | ||
String source_file; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
public class SouffleLintContext { | ||
String node_text; | ||
SouffleLintContext context; | ||
SouffleLintPoint start; | ||
SouffleLintPoint end; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
public class SouffleLintExample { | ||
String before; | ||
String after; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
public class SouffleLintPoint { | ||
int row; | ||
int column; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import com.google.gson.annotations.SerializedName; | ||
|
||
import java.util.List; | ||
|
||
public class SouffleLintRule { | ||
String name; | ||
@SerializedName("short") | ||
String shortDescription; | ||
@SerializedName("long") | ||
String longDescription; | ||
boolean captures; | ||
boolean slow; | ||
List<String> queries; | ||
List<SouffleLintExample> examples; | ||
|
||
|
||
public String getExamples(){ | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
for (SouffleLintExample example: examples){ | ||
stringBuilder.append(example.before); | ||
stringBuilder.append(example.after.trim()); | ||
stringBuilder.append(",\n"); | ||
} | ||
stringBuilder.deleteCharAt(stringBuilder.length() - 2); | ||
return stringBuilder.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import java.util.List; | ||
|
||
public class SouffleLints { | ||
List<SouffleLint> lints; | ||
} |
Oops, something went wrong.