Skip to content

Commit

Permalink
Fix semantic tokens offset due to document updates (eclipse-jdtls#1552)
Browse files Browse the repository at this point in the history
Signed-off-by: 0dinD <zerodind@gmail.com>
  • Loading branch information
0dinD authored Sep 27, 2020
1 parent 8d5ee5d commit 8b7e12d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.manipulation.CoreASTProvider;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.JobHelpers;
import org.eclipse.jdt.ls.core.internal.handlers.DocumentLifeCycleHandler;
import org.eclipse.jdt.ls.core.internal.handlers.JsonRpcHelpers;
import org.eclipse.jdt.ls.core.internal.semantictokens.SemanticTokenManager;
import org.eclipse.jdt.ls.core.internal.semantictokens.SemanticTokens;
import org.eclipse.jdt.ls.core.internal.semantictokens.SemanticTokensLegend;
import org.eclipse.jdt.ls.core.internal.semantictokens.SemanticTokensVisitor;
import org.eclipse.jface.text.IDocument;

public class SemanticTokensCommand {
public static SemanticTokens provide(String uri) {
Expand All @@ -38,22 +34,17 @@ public static SemanticTokens provide(String uri) {
}

private static SemanticTokens doProvide(String uri) {
IDocument document = null;

ITypeRoot typeRoot = JDTUtils.resolveTypeRoot(uri);
if (typeRoot != null) {
try {
document = JsonRpcHelpers.toDocument(typeRoot.getBuffer());
} catch (JavaModelException e) {
JavaLanguageServerPlugin.logException("Failed to provide semantic tokens for " + uri, e);
}
}
if (document == null) {
if (typeRoot == null) {
return new SemanticTokens(Collections.emptyList());
}

SemanticTokensVisitor collector = new SemanticTokensVisitor(document, SemanticTokenManager.getInstance());
CompilationUnit root = CoreASTProvider.getInstance().getAST(typeRoot, CoreASTProvider.WAIT_YES, new NullProgressMonitor());
if (root == null) {
return new SemanticTokens(Collections.emptyList());
}

SemanticTokensVisitor collector = new SemanticTokensVisitor(root, SemanticTokenManager.getInstance());
root.accept(collector);
return collector.getSemanticTokens();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.IPackageBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
Expand All @@ -31,17 +32,15 @@
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.ls.core.internal.handlers.JsonRpcHelpers;
import org.eclipse.jface.text.IDocument;

public class SemanticTokensVisitor extends ASTVisitor {
private IDocument document;
private CompilationUnit cu;
private SemanticTokenManager manager;
private List<SemanticToken> tokens;

public SemanticTokensVisitor(IDocument document, SemanticTokenManager manager) {
public SemanticTokensVisitor(CompilationUnit cu, SemanticTokenManager manager) {
this.manager = manager;
this.document = document;
this.cu = cu;
this.tokens = new ArrayList<>();
}

Expand Down Expand Up @@ -85,9 +84,8 @@ private List<Integer> encodedTokens() {
int currentLine = 0;
int currentColumn = 0;
for (SemanticToken token : this.tokens) {
int[] lineAndColumn = JsonRpcHelpers.toLine(this.document, token.getOffset());
int line = lineAndColumn[0];
int column = lineAndColumn[1];
int line = cu.getLineNumber(token.getOffset()) - 1;
int column = cu.getColumnNumber(token.getOffset());
int deltaLine = line - currentLine;
if (deltaLine != 0) {
currentLine = line;
Expand Down

0 comments on commit 8b7e12d

Please sign in to comment.