-
Notifications
You must be signed in to change notification settings - Fork 54
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
1 parent
60ef8b8
commit ffd155b
Showing
7 changed files
with
306 additions
and
60 deletions.
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
118 changes: 118 additions & 0 deletions
118
...test/src/org/eclipse/lsp4e/test/semanticTokens/SemanticTokensDataStreamProcessorTest.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,118 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Avaloq Group AG. | ||
* 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 | ||
*******************************************************************************/ | ||
package org.eclipse.lsp4e.test.semanticTokens; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.function.Function; | ||
|
||
import org.eclipse.jdt.annotation.NonNull; | ||
import org.eclipse.jface.text.BadLocationException; | ||
import org.eclipse.jface.text.Document; | ||
import org.eclipse.jface.text.IDocument; | ||
import org.eclipse.jface.text.TextAttribute; | ||
import org.eclipse.jface.text.rules.IToken; | ||
import org.eclipse.lsp4e.LSPEclipseUtils; | ||
import org.eclipse.lsp4e.operations.semanticTokens.SemanticTokensDataStreamProcessor; | ||
import org.eclipse.lsp4e.test.AllCleanRule; | ||
import org.eclipse.lsp4j.Position; | ||
import org.eclipse.lsp4j.SemanticTokensLegend; | ||
import org.eclipse.swt.custom.StyleRange; | ||
import org.eclipse.swt.graphics.Color; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
public class SemanticTokensDataStreamProcessorTest { | ||
@Rule | ||
public AllCleanRule clear = new AllCleanRule(); | ||
|
||
private static final Color RED = new Color(255, 0, 0); | ||
|
||
private @NonNull Function<Position, Integer> offsetMapper(IDocument document) { | ||
return (p) -> { | ||
try { | ||
return LSPEclipseUtils.toOffset(p, document); | ||
} catch (BadLocationException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}; | ||
} | ||
|
||
private static final IToken RED_TOKEN = new IToken() { | ||
@Override | ||
public boolean isWhitespace() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isUndefined() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isOther() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isEOF() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public Object getData() { | ||
return new TextAttribute(RED); | ||
} | ||
}; | ||
|
||
private static @NonNull Function<String, IToken> KEYWORD_TOKEN_TYPE_MAPPER = t -> { | ||
if ("keyword".equals(t)) { | ||
return RED_TOKEN; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
|
||
@Test | ||
public void testKeyword() throws InterruptedException, ExecutionException { | ||
String text = | ||
"type foo {\n" + | ||
" \n" + | ||
"}\n" + | ||
"type bar extends foo {\n" + | ||
" \n" + | ||
"}\n"; | ||
Document document = new Document(text); | ||
|
||
List<List<Integer>> expectedTokens = new ArrayList<>(); | ||
expectedTokens.add(Arrays.asList(0,0,4,1,0)); | ||
expectedTokens.add(Arrays.asList(3,0,4,1,0)); | ||
expectedTokens.add(Arrays.asList(0,9,7,1,0)); | ||
|
||
List<Integer> expectedStream =expectedTokens.stream().flatMap(List::stream).toList(); | ||
|
||
SemanticTokensDataStreamProcessor processor = new SemanticTokensDataStreamProcessor(KEYWORD_TOKEN_TYPE_MAPPER, offsetMapper(document)); | ||
|
||
List<StyleRange> styleRanges = processor.getStyleRanges(expectedStream, getSemanticTokensLegend()); | ||
|
||
List<StyleRange> expectedStyleRanges = Arrays.asList(new StyleRange(0, 4, RED, null), new StyleRange(15, 4, RED, null), new StyleRange(24, 7, RED, null)); | ||
assertEquals(expectedStyleRanges, styleRanges); | ||
} | ||
|
||
private SemanticTokensLegend getSemanticTokensLegend() { | ||
SemanticTokensLegend semanticTokensLegend = new SemanticTokensLegend(); | ||
semanticTokensLegend.setTokenTypes(Arrays.asList("keyword","other")); | ||
semanticTokensLegend.setTokenModifiers(Arrays.asList("obsolete")); | ||
return semanticTokensLegend; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...sp4e.test/src/org/eclipse/lsp4e/test/semanticTokens/SemanticTokensLegendProviderTest.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,69 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Avaloq Group AG. | ||
* 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 | ||
*******************************************************************************/ | ||
package org.eclipse.lsp4e.test.semanticTokens; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.eclipse.core.resources.IFile; | ||
import org.eclipse.core.resources.IProject; | ||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.jface.text.BadLocationException; | ||
import org.eclipse.jface.text.IDocument; | ||
import org.eclipse.lsp4e.LanguageServiceAccessor; | ||
import org.eclipse.lsp4e.operations.semanticTokens.SemanticTokensLegendProvider; | ||
import org.eclipse.lsp4e.test.AllCleanRule; | ||
import org.eclipse.lsp4e.test.TestUtils; | ||
import org.eclipse.lsp4e.tests.mock.MockLanguageServer; | ||
import org.eclipse.lsp4j.SemanticTokensLegend; | ||
import org.eclipse.lsp4j.SemanticTokensWithRegistrationOptions; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
public class SemanticTokensLegendProviderTest { | ||
|
||
@Rule | ||
public AllCleanRule clear = new AllCleanRule(); | ||
|
||
private IProject project; | ||
|
||
@Before | ||
public void setUp() throws CoreException { | ||
project = TestUtils.createProject(getClass().getName() + System.currentTimeMillis()); | ||
} | ||
|
||
@Test | ||
public void testSemanticTokensLegendProvider() throws BadLocationException, CoreException { | ||
// Setup Server Capabilities | ||
List<String> tokenTypes = Arrays.asList("keyword","other"); | ||
List<String> tokenModifiers = Arrays.asList("obsolete"); | ||
SemanticTokensLegend legend = new SemanticTokensLegend(tokenTypes, tokenModifiers); | ||
SemanticTokensWithRegistrationOptions semanticTokensWithRegistrationOptions = new SemanticTokensWithRegistrationOptions(legend); | ||
semanticTokensWithRegistrationOptions.setFull(true); | ||
semanticTokensWithRegistrationOptions.setRange(false); | ||
|
||
MockLanguageServer.INSTANCE.getInitializeResult().getCapabilities().setSemanticTokensProvider(semanticTokensWithRegistrationOptions); | ||
|
||
// Setup test data | ||
IFile file = TestUtils.createUniqueTestFile(project, "lspt", "test content"); | ||
IDocument document = TestUtils.openTextViewer(file).getDocument(); | ||
// start the LS | ||
LanguageServiceAccessor.getLanguageServers(document, c -> true); | ||
|
||
SemanticTokensLegendProvider semanticTokensLegendProvider = new SemanticTokensLegendProvider(); | ||
SemanticTokensLegend semanticTokensLegend = semanticTokensLegendProvider.getSemanticTokensLegend(MockLanguageServer.INSTANCE); | ||
assertNotNull(semanticTokensLegend); | ||
assertEquals(tokenTypes, semanticTokensLegend.getTokenTypes()); | ||
assertEquals(tokenModifiers, semanticTokensLegend.getTokenModifiers()); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/semanticTokens/StyleRangeHolderTest.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,77 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Avaloq Group AG. | ||
* 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 | ||
*******************************************************************************/ | ||
package org.eclipse.lsp4e.test.semanticTokens; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotEquals; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.eclipse.jface.text.DocumentEvent; | ||
import org.eclipse.jface.text.Region; | ||
import org.eclipse.jface.text.TextEvent; | ||
import org.eclipse.lsp4e.operations.semanticTokens.StyleRangeHolder; | ||
import org.eclipse.lsp4e.test.AllCleanRule; | ||
import org.eclipse.swt.custom.StyleRange; | ||
import org.eclipse.swt.graphics.Color; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
public class StyleRangeHolderTest { | ||
@Rule | ||
public AllCleanRule clear = new AllCleanRule(); | ||
|
||
private static final Color RED = new Color(255, 0, 0); | ||
private List<StyleRange> originalStyleRanges = Arrays.asList(new StyleRange(0, 4, RED, null), new StyleRange(15, 4, RED, null), new StyleRange(24, 7, RED, null)); | ||
|
||
@Test | ||
public void testAllDocumentRanges() { | ||
StyleRangeHolder holder = new StyleRangeHolder(); | ||
holder.saveStyles(originalStyleRanges); | ||
|
||
StyleRange[] allDocumentRanges = holder.overlappingRanges(new Region(0, 50)); | ||
|
||
assertNotEquals(originalStyleRanges, allDocumentRanges); // styles must be copied | ||
assertEquals(originalStyleRanges.size(), allDocumentRanges.length); | ||
} | ||
|
||
@Test | ||
public void testPartialDocumentRanges() { | ||
StyleRangeHolder holder = new StyleRangeHolder(); | ||
holder.saveStyles(originalStyleRanges); | ||
|
||
StyleRange[] allDocumentRanges = holder.overlappingRanges(new Region(0, 20)); // only two ranges overlap this region | ||
|
||
assertEquals(2, allDocumentRanges.length); | ||
} | ||
|
||
@Test | ||
public void testDocumentChange() { | ||
StyleRangeHolder holder = new StyleRangeHolder(); | ||
holder.saveStyles(originalStyleRanges); | ||
|
||
TextEvent textEvent = new TextEvent(0, 1, " ", null, new DocumentEvent(), false) {}; | ||
|
||
// this will remove the first style and shift the last two | ||
holder.textChanged(textEvent); | ||
|
||
StyleRange[] noOverlappingRanges = holder.overlappingRanges(new Region(0, 10)); // only one range overlap this region | ||
|
||
assertEquals(0, noOverlappingRanges.length); | ||
|
||
StyleRange[] twoShiftedOverlappingRanges = holder.overlappingRanges(new Region(10, 50)); // only one range overlap this region | ||
|
||
assertEquals(2, twoShiftedOverlappingRanges.length); | ||
assertEquals(16, twoShiftedOverlappingRanges[0].start); | ||
assertEquals(4, twoShiftedOverlappingRanges[0].length); | ||
assertEquals(25, twoShiftedOverlappingRanges[1].start); | ||
assertEquals(7, twoShiftedOverlappingRanges[1].length); | ||
} | ||
} |
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
Oops, something went wrong.