generated from redhat-developer/new-project-template
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Support semantic token MultilineTokenSupport
Fixes #401 Signed-off-by: azerr <[email protected]>
- Loading branch information
1 parent
5d5674f
commit 58147b4
Showing
5 changed files
with
221 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
119 changes: 119 additions & 0 deletions
119
src/test/java/com/redhat/devtools/lsp4ij/features/semanticTokens/LuaSemanticTokensTest.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,119 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Red Hat, Inc. | ||
* Distributed under license by Red Hat, Inc. All rights reserved. | ||
* This program is made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, | ||
* and is available at http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
******************************************************************************/ | ||
package com.redhat.devtools.lsp4ij.features.semanticTokens; | ||
|
||
import com.redhat.devtools.lsp4ij.fixtures.LSPSemanticTokensFixtureTestCase; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* Completion tests by emulating LSP 'textDocument/semanticTokens' responses | ||
* from the Lua language server. | ||
*/ | ||
public class LuaSemanticTokensTest extends LSPSemanticTokensFixtureTestCase { | ||
|
||
public LuaSemanticTokensTest() { | ||
// Use *.luax instead of *.lua to avoid consuming the lua textmate | ||
super("*.luax"); | ||
} | ||
|
||
|
||
public void testSemanticTokens() { | ||
// 1. Test completion items result | ||
assertSemanticTokens("test.luax", | ||
""" | ||
--[[ | ||
** {================================================================== | ||
** Testing memory limits | ||
** =================================================================== | ||
--]] | ||
print("memory-allocation errors") | ||
""", | ||
""" | ||
{ | ||
"data": [ | ||
0, | ||
2, | ||
4, | ||
17, | ||
0, | ||
6, | ||
0, | ||
5, | ||
12, | ||
512 | ||
] | ||
} | ||
""", | ||
""" | ||
--<LSP_COMMENT>[[ | ||
</LSP_COMMENT> ** {================================================================== | ||
** Testing memory limits | ||
** =================================================================== | ||
--]] | ||
<LSP_FUNCTION>print</LSP_FUNCTION>("memory-allocation errors") | ||
""" | ||
); | ||
} | ||
|
||
private void assertSemanticTokens(@NotNull String fileName, | ||
@NotNull String editorContentText, | ||
@NotNull String jsonSemanticTokens, | ||
@NotNull String expected) { | ||
String semanticProvider = """ | ||
{ | ||
"legend": { | ||
"tokenTypes": [ | ||
"namespace", | ||
"type", | ||
"class", | ||
"enum", | ||
"interface", | ||
"struct", | ||
"typeParameter", | ||
"parameter", | ||
"variable", | ||
"property", | ||
"enumMember", | ||
"event", | ||
"function", | ||
"method", | ||
"macro", | ||
"keyword", | ||
"modifier", | ||
"comment", | ||
"string", | ||
"number", | ||
"regexp", | ||
"operator", | ||
"decorator" | ||
], | ||
"tokenModifiers": [ | ||
"declaration", | ||
"definition", | ||
"readonly", | ||
"static", | ||
"deprecated", | ||
"abstract", | ||
"async", | ||
"modification", | ||
"documentation", | ||
"defaultLibrary", | ||
"global" | ||
] | ||
}, | ||
"range": true, | ||
"full": true | ||
}"""; | ||
assertSemanticTokens(fileName, editorContentText, semanticProvider, jsonSemanticTokens, expected); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/test/java/com/redhat/devtools/lsp4ij/fixtures/LSPSemanticTokensFixtureTestCase.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) 2024 Red Hat, Inc. | ||
* Distributed under license by Red Hat, Inc. All rights reserved. | ||
* This program is made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, | ||
* and is available at http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
******************************************************************************/ | ||
package com.redhat.devtools.lsp4ij.fixtures; | ||
|
||
import com.redhat.devtools.lsp4ij.JSONUtils; | ||
import com.redhat.devtools.lsp4ij.features.semanticTokens.inspector.SemanticTokensInspectorData; | ||
import com.redhat.devtools.lsp4ij.features.semanticTokens.inspector.SemanticTokensInspectorListener; | ||
import com.redhat.devtools.lsp4ij.features.semanticTokens.inspector.SemanticTokensInspectorManager; | ||
import com.redhat.devtools.lsp4ij.mock.MockLanguageServer; | ||
import org.eclipse.lsp4j.SemanticTokens; | ||
import org.eclipse.lsp4j.SemanticTokensWithRegistrationOptions; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
/** | ||
* Base class test case to test LSP 'textDocument/semanticTokens' feature. | ||
*/ | ||
public abstract class LSPSemanticTokensFixtureTestCase extends LSPCodeInsightFixtureTestCase { | ||
|
||
public LSPSemanticTokensFixtureTestCase(String... fileNamePatterns) { | ||
super(fileNamePatterns); | ||
} | ||
|
||
/** | ||
* Test LSP semanticTokens. | ||
* | ||
* @param fileName the file name used to match registered language servers. | ||
* @param editorContentText the editor content text. | ||
* @param jsonSemanticProvider the LSP SemanticTokensWithRegistrationOptions as JSON string. | ||
* @param jsonSemanticTokens the LSP SemanticTokens as JSON string. | ||
* @param expected the expected IJ SemanticTokens inspector data. | ||
*/ | ||
public void assertSemanticTokens(@NotNull String fileName, | ||
@NotNull String editorContentText, | ||
@NotNull String jsonSemanticProvider, | ||
@NotNull String jsonSemanticTokens, | ||
@NotNull String expected) { | ||
var semanticProvider = JSONUtils.getLsp4jGson().fromJson(jsonSemanticProvider, SemanticTokensWithRegistrationOptions.class); | ||
var semanticTokens = JSONUtils.getLsp4jGson().fromJson(jsonSemanticTokens, SemanticTokens.class); | ||
assertSemanticTokens(fileName, editorContentText, semanticProvider, semanticTokens, expected); | ||
} | ||
|
||
/** | ||
* Test LSP semanticTokens. | ||
* | ||
* @param fileName the file name used to match registered language servers. | ||
* @param editorContentText the editor content text. | ||
* @param semanticProvider the LSP SemanticTokensWithRegistrationOptions. | ||
* @param semanticTokens the LSP SemanticTokens. | ||
* @param expected the expected IJ SemanticTokens inspector data. | ||
*/ | ||
public void assertSemanticTokens(@NotNull String fileName, | ||
@NotNull String editorContentText, | ||
@NotNull SemanticTokensWithRegistrationOptions semanticProvider, | ||
@NotNull SemanticTokens semanticTokens, | ||
@NotNull String expected) { | ||
|
||
var project = myFixture.getProject(); | ||
var refData = new AtomicReference<SemanticTokensInspectorData>(); | ||
SemanticTokensInspectorListener listener = data -> refData.set(data); | ||
SemanticTokensInspectorManager.getInstance(project).addSemanticTokensInspectorListener(listener); | ||
try { | ||
|
||
MockLanguageServer.INSTANCE.setTimeToProceedQueries(200); | ||
|
||
var serverCapabilities = MockLanguageServer.INSTANCE.defaultServerCapabilities(); | ||
serverCapabilities.setSemanticTokensProvider(semanticProvider); | ||
MockLanguageServer.reset(() -> serverCapabilities); | ||
MockLanguageServer.INSTANCE.setSemanticTokens(semanticTokens); | ||
|
||
// Open editor for a given file name and content (which declares <caret> to know where the completion is triggered). | ||
myFixture.configureByText(fileName, editorContentText); | ||
myFixture.doHighlighting(); | ||
|
||
assertNotNull(refData.get()); | ||
String actual = SemanticTokensInspectorManager.format(refData.get(), true, false, false, project); | ||
assertEquals(expected, actual); | ||
|
||
} | ||
finally { | ||
SemanticTokensInspectorManager.getInstance(project).removeSemanticTokensInspectorListener(listener); | ||
} | ||
} | ||
|
||
} |
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