Skip to content

Commit

Permalink
Added deprecated code warnings and quickfix code actions
Browse files Browse the repository at this point in the history
  • Loading branch information
jdaridis committed Nov 20, 2022
1 parent 5ec5392 commit 5801299
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log

## [0.3.0]

- Added primitive type list autocomplete
- Added relation data structure list
- Better type documentation (include super types)
- Added deprecated code warnings

## [0.2.5]

- Added support for snippets
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ This is a plugin adding basic smart features to the Soufflé language, using the

![Genarate I/O example](images/generate_io.png)

- Extract type

![Extract example](images/extract_type.png)

- Snippets for fact / rule templates
- Snippets for decl / comp generation
- Deprecated code warning and quickfixes

- Reformat documentation comments

Expand Down Expand Up @@ -82,6 +87,13 @@ In libraries with heavy use of the C preprocessor macros, sometimes parsing fail

## Release Notes

### 0.3.0

- Added primitive type list autocomplete
- Added relation data structure list
- Better type documentation (include super types)
- Added deprecated code warnings

### 0.2.5

- Added support for snippets
Expand Down
Binary file modified build/libs/Souffle_Ide_Plugin-1.0-SNAPSHOT.jar
Binary file not shown.
Binary file added images/extract_type.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "souffle-lang-server",
"displayName": "Soufflé Datalog Language Server",
"description": "Soufflé Datalog Language Server. Add smart features to the Soufflé Datalog Language with the help of LSP in a VS code plugin",
"version": "0.2.5",
"version": "0.3.0",
"engines": {
"vscode": "^1.65.0"
},
Expand Down
56 changes: 52 additions & 4 deletions src/main/java/CodeActionProvider.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import com.google.gson.JsonArray;
import org.eclipse.lsp4j.*;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import parsing.symbols.SouffleContext;
Expand All @@ -17,7 +18,6 @@ public CodeActionProvider() {

public List<Either<Command, CodeAction>> getCodeAction(CodeActionParams params) {
List<Either<Command, CodeAction>> actions = new ArrayList<Either<Command, CodeAction>>();

lintCodeAction(params, actions);
lintSLowCodeAction(params, actions);
// lintAllCodeAction(params, actions);
Expand All @@ -40,16 +40,64 @@ public List<Either<Command, CodeAction>> getCodeAction(CodeActionParams params)
}
}

diagnosticQuickfixCodeAction(params, actions);

return actions;
}

private void diagnosticQuickfixCodeAction(CodeActionParams params, List<Either<Command, CodeAction>> actions) {
if(!params.getContext().getDiagnostics().isEmpty()){
for(Diagnostic diagnostic: params.getContext().getDiagnostics()){
if(diagnostic.getTags().contains(DiagnosticTag.Deprecated)){
CodeAction deprecatedQuickfix = new CodeAction();
deprecatedQuickfix.setKind(CodeActionKind.QuickFix);
deprecatedQuickfix.setTitle("Refactor deprecated code");
WorkspaceEdit deprecatedWorkspace = new WorkspaceEdit();
TextEdit deprecatedEdit = new TextEdit();
deprecatedEdit.setNewText("");
TextEdit deprecatedNew = new TextEdit();
if(diagnostic.getData() != null){
JsonArray jsonArray = (JsonArray) diagnostic.getData();
String problem = jsonArray.get(0).toString().replaceAll("\"", "").trim();
String argument = jsonArray.get(1).toString().replaceAll("\"", "").trim();

Position start = new Position(diagnostic.getRange().getStart().getLine() + 1, 0);
deprecatedEdit.setRange(diagnostic.getRange());
deprecatedNew.setRange(new Range(start, start));
switch (problem){
case ".number_type":
deprecatedNew.setNewText(".type "+ argument + " <: number\n");
break;
case ".symbol_type":
deprecatedNew.setNewText(".type "+ argument + " <: symbol\n");
break;
case "input":
deprecatedNew.setNewText(".input "+ argument + "\n");
break;
case "output":
deprecatedNew.setNewText(".output "+ argument + "\n");
break;
case "printsize":
deprecatedNew.setNewText(".printsize "+ argument + "\n");
break;
}
deprecatedWorkspace.setChanges(Map.of(params.getTextDocument().getUri(), List.of(deprecatedNew, deprecatedEdit)));
deprecatedQuickfix.setEdit(deprecatedWorkspace);
deprecatedQuickfix.setDiagnostics(List.of(diagnostic));
}
actions.add(Either.forRight(deprecatedQuickfix));
}
}
}
}

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");
newTypeText.setNewText(".type " + "subType <: " + currentSymbol.getName() + "\n\n");

int line = currentSymbol.getRange().getStart().getLine() + 1;

Expand All @@ -71,12 +119,12 @@ private void extractTypeCodeAction(CodeActionParams params, List<Either<Command,
extractEdit.setChanges(Map.of(params.getTextDocument().getUri(), subtypeEdits));
extractSubtypeAction.setEdit(extractEdit);

CodeAction extractTypeAction = new CodeAction("Extract Type");
CodeAction extractTypeAction = new CodeAction("Extract equivalence 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.setNewText(".type " + "extractedType = " + currentSymbol.getName() + "\n\n");

newTypeText1.setRange(newTypeRange);
List<TextEdit> extractEdits = new ArrayList<TextEdit>();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/logging/LSClientLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void reportDeprecated(Range range, String uri, String message, Object dat
return;
}
Diagnostic diagnostic = new Diagnostic(range, message);
diagnostic.setSeverity(DiagnosticSeverity.Warning);
diagnostic.setSeverity(DiagnosticSeverity.Hint);
diagnostic.setTags(List.of(DiagnosticTag.Deprecated));
diagnostic.setData(data);
diagnostics.get(uri).add(diagnostic);
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/parsing/SouffleDeclarationVisitor.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package parsing;

import logging.LSClientLogger;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.eclipse.lsp4j.Range;
import org.eclipse.xtext.xbase.lib.Pair;
import parsing.souffle.SouffleBaseVisitor;
import parsing.souffle.SouffleParser;
import parsing.symbols.*;

import java.util.ArrayDeque;
import java.util.List;

public class SouffleDeclarationVisitor extends SouffleBaseVisitor<SouffleSymbol> {

Expand Down Expand Up @@ -123,6 +127,20 @@ public SouffleSymbol visitRelation_decl(SouffleParser.Relation_declContext ctx)
}

// System.err.println(declarationContext);
TerminalNode deprecatedToken = null;
if(ctx.relation_tags() != null){
if(ctx.relation_tags().INPUT_QUALIFIER() != null){
deprecatedToken = ctx.relation_tags().INPUT_QUALIFIER();
} else if(ctx.relation_tags().OUTPUT_QUALIFIER() != null){
deprecatedToken = ctx.relation_tags().OUTPUT_QUALIFIER();
} else if( ctx.relation_tags().PRINTSIZE_QUALIFIER() != null){
deprecatedToken = ctx.relation_tags().PRINTSIZE_QUALIFIER();
}
if(deprecatedToken != null){
String text = deprecatedToken.getText();
LSClientLogger.getInstance().reportDeprecated(Utils.toRange(ctx.relation_tags()), documentUri, "Use of tag "+ text +" is deprecated", List.of(text, relationNames.getFirst().getName()));
}
}
return null;
}

Expand Down Expand Up @@ -160,6 +178,21 @@ public SouffleSymbol visitType_decl(SouffleParser.Type_declContext ctx) {
declarationContext.addToContextScope(type);
typeName.addType(type);
}
} else if(ctx.qualified_name() != null){
SouffleSymbol superTypeSymbol = ctx.qualified_name().accept(this);
SouffleType superType = new SouffleType(superTypeSymbol.getName(), superTypeSymbol.getRange());
typeName.setType(superType);
}

TerminalNode deprecatedToken = null;
if(ctx.NUMBER_TYPE() != null){
deprecatedToken = ctx.NUMBER_TYPE();
} else if(ctx.SYMBOL_TYPE() != null){
deprecatedToken = ctx.SYMBOL_TYPE();
}
if(deprecatedToken != null){
String text = deprecatedToken.getText();
LSClientLogger.getInstance().reportDeprecated(Utils.toRange(ctx), documentUri, "Use of "+text+"is deprecated", List.of(text, typeName.getName()));
}
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/parsing/symbols/SouffleAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Object getValue() {
public String toString() {
String s = name;
if(type != null){
s += ": " + type;
s += ": " + type.getName();
}
return s;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/parsing/symbols/SouffleType.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public String toString() {
sb.append(type1.getName());
sb.append(" | ");
}
sb.append(union.get(i));
sb.append(union.get(i).getName());
} else if(type != null){
sb.append(" <: ");
sb.append(type.getName());
Expand Down

0 comments on commit 5801299

Please sign in to comment.