-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
320 additions
and
1 deletion.
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
100 changes: 100 additions & 0 deletions
100
gradle-language-server/src/main/java/com/microsoft/gradle/compile/GradleASTVisitor.java
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,100 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Microsoft Corporation and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Microsoft Corporation - initial API and implementation | ||
*******************************************************************************/ | ||
package com.microsoft.gradle.compile; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.microsoft.gradle.semantictokens.SemanticToken; | ||
import com.microsoft.gradle.semantictokens.TokenModifier; | ||
import com.microsoft.gradle.semantictokens.TokenType; | ||
|
||
import org.codehaus.groovy.ast.ASTNode; | ||
import org.codehaus.groovy.ast.ClassCodeVisitorSupport; | ||
import org.codehaus.groovy.ast.ModuleNode; | ||
import org.codehaus.groovy.ast.expr.MapEntryExpression; | ||
import org.codehaus.groovy.ast.expr.MethodCallExpression; | ||
import org.codehaus.groovy.ast.expr.PropertyExpression; | ||
import org.codehaus.groovy.ast.expr.VariableExpression; | ||
import org.codehaus.groovy.control.SourceUnit; | ||
|
||
public class GradleASTVisitor extends ClassCodeVisitorSupport { | ||
|
||
public GradleASTVisitor(GradleCompilationUnit unit) { | ||
this.compilationUnit = unit; | ||
} | ||
|
||
private GradleCompilationUnit compilationUnit; | ||
private List<SemanticToken> tokens = new ArrayList<>(); | ||
|
||
public List<SemanticToken> getSemanticTokens() { | ||
return this.tokens; | ||
} | ||
|
||
private void addToken(int line, int column, int length, TokenType tokenType, int modifiers) { | ||
if (length > 0) { | ||
tokens.add(new SemanticToken(line, column, length, tokenType, modifiers)); | ||
} | ||
} | ||
|
||
private void addToken(ASTNode node, TokenType tokenType, int modifiers) { | ||
addToken(node.getLineNumber(), node.getColumnNumber(), node.getLength(), tokenType, modifiers); | ||
} | ||
|
||
private void addToken(ASTNode node, TokenType tokenType) { | ||
addToken(node.getLineNumber(), node.getColumnNumber(), node.getLength(), tokenType, 0); | ||
} | ||
|
||
public void visitCompilationUnit() { | ||
this.compilationUnit.iterator().forEachRemaining(unit -> visitSourceUnit(unit)); | ||
} | ||
|
||
public void visitSourceUnit(SourceUnit unit) { | ||
ModuleNode moduleNode = unit.getAST(); | ||
if (moduleNode != null) { | ||
visitModule(moduleNode); | ||
} | ||
} | ||
|
||
public void visitModule(ModuleNode node) { | ||
node.getClasses().forEach(classNode -> { | ||
super.visitClass(classNode); | ||
}); | ||
} | ||
|
||
@Override | ||
public void visitMethodCallExpression(MethodCallExpression node) { | ||
if (TokenModifier.isDefaultLibrary(node.getMethod().getText())) { | ||
addToken(node.getMethod(), TokenType.FUNCTION, TokenModifier.DEFAULT_LIBRARY.bitmask); | ||
} else { | ||
addToken(node.getMethod(), TokenType.FUNCTION); | ||
} | ||
super.visitMethodCallExpression(node); | ||
} | ||
|
||
@Override | ||
public void visitMapEntryExpression(MapEntryExpression node) { | ||
addToken(node.getKeyExpression(), TokenType.PARAMETER); | ||
super.visitMapEntryExpression(node); | ||
} | ||
|
||
@Override | ||
public void visitVariableExpression(VariableExpression node) { | ||
addToken(node, TokenType.VARIABLE); | ||
super.visitVariableExpression(node); | ||
} | ||
|
||
@Override | ||
public void visitPropertyExpression(PropertyExpression node) { | ||
addToken(node.getProperty(), TokenType.PROPERTY); | ||
super.visitPropertyExpression(node); | ||
} | ||
} |
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
94 changes: 94 additions & 0 deletions
94
gradle-language-server/src/main/java/com/microsoft/gradle/semantictokens/SemanticToken.java
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,94 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Microsoft Corporation and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Microsoft Corporation - initial API and implementation | ||
*******************************************************************************/ | ||
package com.microsoft.gradle.semantictokens; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
public class SemanticToken { | ||
private final TokenType tokenType; | ||
private final int tokenModifiers; | ||
private final int line; | ||
private final int column; | ||
private final int length; | ||
|
||
public SemanticToken(int line, int column, int length, TokenType tokenType, int tokenModifiers) { | ||
this.line = line; | ||
this.column = column; | ||
this.length = length; | ||
this.tokenType = tokenType; | ||
this.tokenModifiers = tokenModifiers; | ||
} | ||
|
||
public TokenType getTokenType() { | ||
return tokenType; | ||
} | ||
|
||
public int getTokenModifiers() { | ||
return tokenModifiers; | ||
} | ||
|
||
public int getLine() { | ||
return line; | ||
} | ||
|
||
public int getColumn() { | ||
return column; | ||
} | ||
|
||
public int getLength() { | ||
return length; | ||
} | ||
|
||
public static List<Integer> encodedTokens(List<SemanticToken> tokens) { | ||
tokens.sort(new Comparator<SemanticToken>() { | ||
@Override | ||
public int compare(final SemanticToken a, final SemanticToken b) { | ||
int lineResult = Integer.valueOf(a.getLine()).compareTo(Integer.valueOf(b.getLine())); | ||
if (lineResult == 0) { | ||
return Integer.valueOf(a.getColumn()).compareTo(Integer.valueOf(b.getColumn())); | ||
} | ||
return lineResult; | ||
} | ||
}); | ||
int numTokens = tokens.size(); | ||
List<Integer> data = new ArrayList<>(numTokens * 5); | ||
int currentLine = 0; | ||
int currentColumn = 0; | ||
for (int i = 0; i < numTokens; i++) { | ||
SemanticToken token = tokens.get(i); | ||
int line = token.getLine() - 1; | ||
int column = token.getColumn() - 1; | ||
if (line < 0 || column < 0) { | ||
continue; | ||
} | ||
int deltaLine = line - currentLine; | ||
if (deltaLine != 0) { | ||
currentLine = line; | ||
currentColumn = 0; | ||
} | ||
int deltaColumn = column - currentColumn; | ||
currentColumn = column; | ||
// Disallow duplicate/conflict token (if exists) | ||
if (deltaLine != 0 || deltaColumn != 0 || i == 0) { | ||
int tokenTypeIndex = token.getTokenType().ordinal(); | ||
int tokenModifiers = token.getTokenModifiers(); | ||
data.add(deltaLine); | ||
data.add(deltaColumn); | ||
data.add(token.getLength()); | ||
data.add(tokenTypeIndex); | ||
data.add(tokenModifiers); | ||
} | ||
} | ||
return data; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
gradle-language-server/src/main/java/com/microsoft/gradle/semantictokens/TokenModifier.java
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,46 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Microsoft Corporation and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Microsoft Corporation - initial API and implementation | ||
*******************************************************************************/ | ||
package com.microsoft.gradle.semantictokens; | ||
|
||
import java.util.List; | ||
|
||
import org.eclipse.lsp4j.SemanticTokenModifiers; | ||
|
||
public enum TokenModifier { | ||
|
||
DEFAULT_LIBRARY(SemanticTokenModifiers.DefaultLibrary); | ||
|
||
private String genericName; | ||
// See https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html | ||
private static List<String> defaultLibrary = List.of("afterEvaluate", "allprojects", "ant", "apply", "artifacts", | ||
"beforeEvaluate", "buildscript", "configurations", "configure", "copy", "copySpec", "dependencies", "javaexec", | ||
"repositories", "subprojects", "task"); | ||
|
||
public final int bitmask = 1 << ordinal(); | ||
|
||
TokenModifier(String genericName) { | ||
this.genericName = genericName; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return genericName; | ||
} | ||
|
||
public static boolean isDefaultLibrary(String method) { | ||
if (TokenModifier.defaultLibrary.contains(method)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
gradle-language-server/src/main/java/com/microsoft/gradle/semantictokens/TokenType.java
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,31 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Microsoft Corporation and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Microsoft Corporation - initial API and implementation | ||
*******************************************************************************/ | ||
package com.microsoft.gradle.semantictokens; | ||
|
||
import org.eclipse.lsp4j.SemanticTokenTypes; | ||
|
||
public enum TokenType { | ||
FUNCTION(SemanticTokenTypes.Function), PROPERTY(SemanticTokenTypes.Property), VARIABLE(SemanticTokenTypes.Variable), | ||
PARAMETER(SemanticTokenTypes.Parameter); | ||
|
||
private String genericName; | ||
|
||
TokenType(String genericName) { | ||
this.genericName = genericName; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return genericName; | ||
} | ||
} |