-
Notifications
You must be signed in to change notification settings - Fork 122
Semantic Tokens ext point #1683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jukzi
merged 15 commits into
eclipse-jdt:master
from
BoykoAlex:sync-jdt-syntax-highlighting
Dec 9, 2024
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e63d613
Syntax Highlighting from ext point - Initial work
BoykoAlex 50c583c
Async contributed semantic tokens
BoykoAlex 236f199
Synchronously computed contributed semantic tokens
BoykoAlex b172fae
Cleanup
BoykoAlex 6ec091c
Update license headers dates
BoykoAlex c5de564
Extension point docs and minor adjustments
BoykoAlex 43d6d0f
Polish and debug failing test
BoykoAlex a0a4c19
Revert test debugging
BoykoAlex b1644b4
Debug unit test
BoykoAlex 0e308f0
Correction
BoykoAlex a5ee3b4
Example in tests and a unit test
BoykoAlex 389cb9c
Attempt to fix plugin loading test
BoykoAlex acad0c6
Review comments
BoykoAlex ac3967c
Bump minor version of the bundle
BoykoAlex c544c78
Review comments
BoykoAlex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
137 changes: 137 additions & 0 deletions
137
....tests/src/org/eclipse/jdt/text/tests/semantictokens/SampleSqlSemanticTokensProvider.java
This file contains hidden or 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,137 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2024 Broadcom Inc. and others. | ||
| * This program and the accompanying materials are made | ||
| * available under the terms of the Eclipse Public License 2.0 | ||
| * which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| * | ||
| * Contributors: | ||
| * Alex Boyko (Broadcom Inc.) - Initial implementation | ||
| *******************************************************************************/ | ||
| package org.eclipse.jdt.text.tests.semantictokens; | ||
|
|
||
| import java.text.NumberFormat; | ||
| import java.text.ParseException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import org.eclipse.jdt.core.dom.ASTVisitor; | ||
| import org.eclipse.jdt.core.dom.CompilationUnit; | ||
| import org.eclipse.jdt.core.dom.Expression; | ||
| import org.eclipse.jdt.core.dom.StringLiteral; | ||
| import org.eclipse.jdt.core.dom.TextBlock; | ||
|
|
||
| import org.eclipse.jdt.ui.text.java.ISemanticTokensProvider; | ||
|
|
||
| /** | ||
| * Semantic tokens are computed for string literals or text blocks starting with "SQL:" prefix. | ||
| * <ul> | ||
| * <li>SELECT, WHERE, IN, FROM are KEYWORD</li> | ||
| * <li>*, <, >, ==, != etc are OPEARATOR</li> | ||
| * <li>Words starting from capital are CLASS</li> | ||
| * <li>Numbers are NUMBER</li> | ||
| * <li>all other lower case starting words are LOCAL_VARIABLE</li> | ||
| * </ul> | ||
| */ | ||
| public class SampleSqlSemanticTokensProvider implements ISemanticTokensProvider { | ||
|
|
||
| private static final String SQL_PREFIX = "SQL:"; | ||
|
|
||
| @Override | ||
| public Collection<SemanticToken> computeSemanticTokens(CompilationUnit ast) { | ||
| List<SemanticToken> tokens = new ArrayList<>(); | ||
| ast.accept(new ASTVisitor() { | ||
|
|
||
| @Override | ||
| public boolean visit(StringLiteral node) { | ||
| tokens.addAll(reconileEmbeddedExpression(node)); | ||
| return super.visit(node); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean visit(TextBlock node) { | ||
| tokens.addAll(reconileEmbeddedExpression(node)); | ||
| return super.visit(node); | ||
| } | ||
|
|
||
| }); | ||
| return tokens; | ||
| } | ||
|
|
||
| private List<SemanticToken> reconileEmbeddedExpression(Expression valueExp) { | ||
| String text = null; | ||
| int offset = 0; | ||
| if (valueExp instanceof StringLiteral sl && sl.getLiteralValue().startsWith(SQL_PREFIX)) { | ||
| text = sl.getEscapedValue(); | ||
| int skip = 1 + SQL_PREFIX.length(); | ||
| text = text.substring(skip, text.length() - 1); | ||
| offset = sl.getStartPosition() + skip; // +1 to skip over opening " and over "SQL:" | ||
| } else if (valueExp instanceof TextBlock tb && tb.getLiteralValue().startsWith(SQL_PREFIX)) { | ||
| text = tb.getEscapedValue(); | ||
| int skip = 3 + SQL_PREFIX.length(); | ||
| text = text.substring(skip, text.length() - 3); | ||
| offset = tb.getStartPosition() + skip; // +3 to skip over opening """ and over "SQL:" | ||
| } | ||
| return compute(text, offset); | ||
| } | ||
|
|
||
| private List<SemanticToken> compute(String text, int offset) { | ||
| if (text == null) { | ||
| return Collections.emptyList(); | ||
| } | ||
| List<SemanticToken> tokens = new ArrayList<>(); | ||
| Matcher matcher= Pattern.compile("[\\w*=><!]+").matcher(text); | ||
| while (matcher.find()) { | ||
| String token = matcher.group(); | ||
| if (!token.isBlank()) { | ||
| int start = matcher.start(); | ||
| int end = matcher.end(); | ||
| tokens.add(new SemanticToken(start + offset, end - start, getTokenType(token))); | ||
| } | ||
| } | ||
| return tokens; | ||
| } | ||
|
|
||
| private TokenType getTokenType(String token) { | ||
| try { | ||
| NumberFormat.getInstance().parse(token); | ||
| return TokenType.NUMBER; | ||
| } catch (ParseException e) { | ||
| switch (token) { | ||
| case "SELECT": | ||
| case "WHERE": | ||
| case "WHEN": | ||
| case "ALL": | ||
| case "BY": | ||
| case "ORDER": | ||
| case "LIKE": | ||
| case "IN": | ||
| case "FROM": | ||
| case "NOT": | ||
| return TokenType.KEYWORD; | ||
| case "*": | ||
| case "(": | ||
| case "-": | ||
| case ">": | ||
| case "<": | ||
| case ">=": | ||
| case "<=": | ||
| case "==": | ||
| case "!=": | ||
| return TokenType.OPERATOR; | ||
| default: | ||
| if (token.length() > 0 && Character.isUpperCase(token.charAt(0))) { | ||
| return TokenType.CLASS; | ||
| } | ||
| return TokenType.LOCAL_VARIABLE; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } |
50 changes: 50 additions & 0 deletions
50
....text.tests/src/org/eclipse/jdt/text/tests/semantictokens/SemanticTokensProviderTest.java
This file contains hidden or 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,50 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2024 Broadcom Inc. and others. | ||
| * This program and the accompanying materials are made | ||
| * available under the terms of the Eclipse Public License 2.0 | ||
| * which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| * | ||
| * Contributors: | ||
| * Alex Boyko (Broadcom Inc.) - Initial implementation | ||
| *******************************************************************************/ | ||
| package org.eclipse.jdt.text.tests.semantictokens; | ||
|
|
||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
|
|
||
| import org.eclipse.jdt.text.tests.AbstractSemanticHighlightingTest; | ||
|
|
||
| import org.eclipse.jface.text.Position; | ||
|
|
||
| import org.eclipse.jdt.internal.ui.javaeditor.SemanticHighlightings; | ||
|
|
||
| public class SemanticTokensProviderTest extends AbstractSemanticHighlightingTest { | ||
|
|
||
| @Rule | ||
| public SemanticHighlightingTestSetup shts= new SemanticHighlightingTestSetup( "/SHTest/src/STTest.java"); | ||
|
|
||
| @Test | ||
| public void contributedHighlighting() throws Exception { | ||
| setUpSemanticHighlighting(SemanticHighlightings.CLASS); | ||
| setUpSemanticHighlighting(SemanticHighlightings.NUMBER); | ||
| setUpSemanticHighlighting(SemanticHighlightings.LOCAL_VARIABLE); | ||
| Position[] actual= getSemanticHighlightingPositions(); | ||
| Position[] expected= new Position[] { | ||
| createPosition(0, 6, 1), | ||
| createPosition(1, 1, 6), | ||
| createPosition(1, 20, 6), | ||
| createPosition(1, 27, 1), | ||
| createPosition(1, 29, 4), | ||
| createPosition(1, 34, 1), | ||
| createPosition(1, 36, 5), | ||
| createPosition(1, 42, 1), | ||
| createPosition(1, 44, 2), | ||
| createPosition(1, 47, 3), | ||
| }; | ||
| assertEqualPositions(expected, actual); | ||
| } | ||
|
|
||
|
|
||
| } | ||
3 changes: 3 additions & 0 deletions
3
org.eclipse.jdt.text.tests/testResources/semanticHighlightingTest1/STTest.java
This file contains hidden or 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,3 @@ | ||
| class A { | ||
| String sql = "SQL: SELECT * FROM T WHERE a == 567"; | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.