Skip to content

Commit

Permalink
Merge pull request apache#6352 from dbalek/dbalek/lsp-mn-sym-pos-fix
Browse files Browse the repository at this point in the history
LSP: Micronaut symbols position fixed.
  • Loading branch information
dbalek authored Aug 17, 2023
2 parents 2ed7f55 + e68a690 commit 35a02e1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public List<StructureElement> getStructure(Document doc) {
.file(cc.getFileObject())
.expandedStartOffset(symbolLocation.getStart())
.expandedEndOffset(symbolLocation.getEnd())
.selectionStartOffset(symbolLocation.getStart())
.selectionEndOffset(symbolLocation.getEnd())
.selectionStartOffset(symbolLocation.getSelectionStart())
.selectionEndOffset(symbolLocation.getSelectionEnd())
.build());
}
}, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import com.sun.source.tree.ClassTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.util.SourcePositions;
import com.sun.source.util.TreePath;
import com.sun.source.util.TreePathScanner;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
Expand Down Expand Up @@ -122,10 +124,12 @@ private synchronized boolean initialize(CompilationController cc) {

public static List<SymbolLocation> scan(CompilationController cc) {
final List<SymbolLocation> ret = new ArrayList<>();
SourcePositions sp = cc.getTrees().getSourcePositions();
TreePathScanner<Void, String> scanner = new TreePathScanner<Void, String>() {
@Override
public Void visitClass(ClassTree node, String path) {
Element cls = cc.getTrees().getElement(this.getCurrentPath());
TreePath treePath = this.getCurrentPath();
Element cls = cc.getTrees().getElement(treePath);
if (cls != null) {
Pair<AnnotationMirror, AnnotationMirror> metaAnnotated = isMetaAnnotated(cls);
if (metaAnnotated != null) {
Expand All @@ -141,15 +145,16 @@ public Void visitClass(ClassTree node, String path) {
+ (metaAnnotated.second() != null ? " <: @" + metaAnnotated.second().getAnnotationType().asElement().getSimpleName() : "")
+ ") " + node.getSimpleName();
int[] span = cc.getTreeUtilities().findNameSpan(node);
ret.add(new SymbolLocation(name, span[0], span[1]));
ret.add(new SymbolLocation(name, (int) sp.getStartPosition(treePath.getCompilationUnit(), node), (int) sp.getEndPosition(treePath.getCompilationUnit(), node), span[0], span[1]));
}
}
return super.visitClass(node, path);
}

@Override
public Void visitMethod(MethodTree node, String path) {
MthIterator it = new MthIterator(cc.getTrees().getElement(this.getCurrentPath()), cc.getElements(), cc.getTypes());
TreePath treePath = this.getCurrentPath();
MthIterator it = new MthIterator(cc.getTrees().getElement(treePath), cc.getElements(), cc.getTypes());
while (it.hasNext()) {
for (AnnotationMirror ann : it.next().getAnnotationMirrors()) {
String method = getEndpointMethod((TypeElement) ann.getAnnotationType().asElement());
Expand All @@ -158,7 +163,7 @@ public Void visitMethod(MethodTree node, String path) {
if ("value".contentEquals(entry.getKey().getSimpleName()) || "uri".contentEquals(entry.getKey().getSimpleName())) {
String name = '@' + (path != null ? path : "") + entry.getValue().getValue() + " -- " + method;
int[] span = cc.getTreeUtilities().findNameSpan(node);
ret.add(new SymbolLocation(name, span[0], span[1]));
ret.add(new SymbolLocation(name, (int) sp.getStartPosition(treePath.getCompilationUnit(), node), (int) sp.getEndPosition(treePath.getCompilationUnit(), node), span[0], span[1]));
return null;
}
}
Expand Down Expand Up @@ -280,14 +285,18 @@ public int getIndexVersion() {
}

public static class SymbolLocation {
private String name;
private int start;
private int end;
private final String name;
private final int start;
private final int end;
private final int selectionStart;
private final int selectionEnd;

private SymbolLocation(String name, int start, int end) {
private SymbolLocation(String name, int start, int end, int selectionStart, int selectionEnd) {
this.name = name;
this.start = start;
this.end = end;
this.selectionStart = selectionStart;
this.selectionEnd = selectionEnd;
}

public String getName() {
Expand All @@ -301,6 +310,14 @@ public int getStart() {
public int getEnd() {
return end;
}

public int getSelectionStart() {
return selectionStart;
}

public int getSelectionEnd() {
return selectionEnd;
}
}

private static class MthIterator implements Iterator<ExecutableElement> {
Expand Down

0 comments on commit 35a02e1

Please sign in to comment.