Skip to content

Commit

Permalink
Added code action for reformatting documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdaridis committed Nov 10, 2022
1 parent 57a405c commit 74740f4
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [0.1.6]

Added code actions for reformatting documentation comments.

## [0.1.5]

Added code actions for generating .input/.output for relations.
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ Provide documentation about relations taken from comments (for multiline comment

- Generate .input/.output for relation

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

- Reformat documentation comments

![Reformat comments example](images/format_comment.png)

- Document symbols

Expand All @@ -70,6 +74,10 @@ For the VS Code plugin run `npm run package` and the resulting .js file will be
In libraries with heavy use of the C preprocessor macros, sometimes parsing fails giving a false syntax error.

## Release Notes

### 0.1.6

Added code actions for reformatting documentation comments.
### 0.1.5

Added code actions for generating .input/.output for relations.
Expand Down
Binary file modified build/libs/Souffle_Ide_Plugin-1.0-SNAPSHOT.jar
Binary file not shown.
Binary file added images/format_comment.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.1.2",
"version": "0.1.6",
"engines": {
"vscode": "^1.65.0"
},
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/SouffleTextDocumentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,18 @@ public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(CodeActio
edit1.setChanges(Map.of(params.getTextDocument().getUri(), List.of(textEdit1)));
outputAction.setEdit(edit1);

if(currentSymbol.getKind() == SouffleSymbolType.RELATION_DECL && currentSymbol.getPotentialDocumentation().getKey() != null){
CodeAction formatComments = new CodeAction("Format documentation with /* */");
formatComments.setKind(CodeActionKind.Refactor);
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));
}

}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/parsing/SouffleDeclarationVisitor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parsing;

import org.eclipse.lsp4j.Range;
import org.eclipse.xtext.xbase.lib.Pair;
import parsing.souffle.SouffleBaseVisitor;
import parsing.souffle.SouffleParser;
import parsing.symbols.*;
Expand Down Expand Up @@ -81,6 +82,7 @@ public SouffleSymbol visitRelation_decl(SouffleParser.Relation_declContext ctx)
SouffleContext documentContext = currentContext.peek();
documentContext.addToSubContext(declarationContext);
String documentation = Utils.getDocumentation(ctx.getStart(), parser);
Pair<String, Range> potentialDoc = Utils.getPotentialDocumentation(ctx.getStart(), parser);

currentScope.push(new ArrayDeque<>());
ctx.relation_names().accept(this);
Expand All @@ -94,6 +96,7 @@ public SouffleSymbol visitRelation_decl(SouffleParser.Relation_declContext ctx)
for(SouffleSymbol relationName: relationNames){
SouffleRelation relation = new SouffleRelation(relationName.getName(), relationName.getRange(), true);
relation.setDocumentation(documentation);
relation.setPotentialDocumentation(potentialDoc);
for(SouffleSymbol attribute: attributes){
SouffleAttribute arg = (SouffleAttribute) attribute;
relation.addArg(arg);
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/parsing/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.antlr.v4.runtime.tree.TerminalNode;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.xtext.xbase.lib.Pair;
import parsing.souffle.SouffleLexer;
import parsing.souffle.SouffleParser;

Expand Down Expand Up @@ -43,4 +44,30 @@ public static String getDocumentation(Token ctx, SouffleParser souffleParser) {
}
return documentation;
}

public static Pair<String, Range> getPotentialDocumentation(Token ctx, SouffleParser souffleParser) {
String documentation = null;
Range range = null;
int i = ctx.getTokenIndex();
BufferedTokenStream tokens = (BufferedTokenStream) souffleParser.getTokenStream();
List<Token> cmtChannel =
tokens.getHiddenTokensToLeft(i, SouffleLexer.HIDDEN);
StringBuilder sb = new StringBuilder("/**\n");
if ( cmtChannel!=null ) {
for(Token token: cmtChannel){
if(token != null && token.getText().contains("//")){
sb.append("\t\t");
sb.append(token.getText().replaceAll("\\*", "").replaceAll("/", "").trim());
sb.append("\n");
} else return Pair.of(null,null);
}
sb.append("*/");
documentation = sb.toString();
Position p1 = new Position(cmtChannel.get(0).getLine() - 1, cmtChannel.get(0).getCharPositionInLine());
Position p2 = new Position(cmtChannel.get(cmtChannel.size() - 1).getLine() - 1, cmtChannel.get(cmtChannel.size() - 1).getStopIndex());
range = new Range(p1, p2);
}

return Pair.of(documentation, range);
}
}
12 changes: 12 additions & 0 deletions src/main/java/parsing/symbols/SouffleSymbol.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parsing.symbols;

import org.eclipse.lsp4j.Range;
import org.eclipse.xtext.xbase.lib.Pair;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -16,6 +17,10 @@ public class SouffleSymbol {
protected List<SouffleSymbol> declarations;
protected String documentation;



protected Pair<String, Range> potentialDocumentation;

public SouffleSymbol(String name, SouffleSymbolType kind, Range range) {
this.name = name;
this.kind = kind;
Expand Down Expand Up @@ -43,6 +48,13 @@ public void setDocumentation(String documentation) {
this.documentation = documentation;
}

public Pair<String, Range> getPotentialDocumentation() {
return potentialDocumentation;
}

public void setPotentialDocumentation(Pair<String, Range> potentialDocumentation) {
this.potentialDocumentation = potentialDocumentation;
}
public SouffleSymbol getDeclaration() {
if(declarations.isEmpty()){
return null;
Expand Down

0 comments on commit 74740f4

Please sign in to comment.