Skip to content

Commit 330620d

Browse files
Copilotnixel2007
andcommitted
test: Add tests to verify variable with common module is not highlighted as namespace
Co-authored-by: nixel2007 <[email protected]>
1 parent f7f1a53 commit 330620d

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

src/test/java/com/github/_1c_syntax/bsl/languageserver/providers/SemanticTokensProviderTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222
package com.github._1c_syntax.bsl.languageserver.providers;
2323

2424
import com.github._1c_syntax.bsl.languageserver.context.DocumentContext;
25+
import com.github._1c_syntax.bsl.languageserver.context.ServerContext;
2526
import com.github._1c_syntax.bsl.languageserver.references.ReferenceIndexFiller;
2627
import com.github._1c_syntax.bsl.languageserver.util.CleanupContextBeforeClassAndAfterEachTestMethod;
2728
import com.github._1c_syntax.bsl.languageserver.util.TestUtils;
29+
import com.github._1c_syntax.utils.Absolute;
30+
import org.apache.commons.io.FileUtils;
2831
import org.eclipse.lsp4j.Position;
2932
import org.eclipse.lsp4j.Range;
3033
import org.eclipse.lsp4j.SemanticTokenModifiers;
@@ -39,7 +42,10 @@
3942
import org.springframework.beans.factory.annotation.Autowired;
4043
import org.springframework.boot.test.context.SpringBootTest;
4144

45+
import java.io.File;
46+
import java.io.IOException;
4247
import java.net.URI;
48+
import java.nio.charset.StandardCharsets;
4349
import java.util.ArrayList;
4450
import java.util.List;
4551
import java.util.Set;
@@ -59,6 +65,9 @@ class SemanticTokensProviderTest {
5965
@Autowired
6066
private ReferenceIndexFiller referenceIndexFiller;
6167

68+
@Autowired
69+
private ServerContext serverContext;
70+
6271
// region Helper types and methods
6372

6473
/**
@@ -1661,5 +1670,59 @@ void rangeTokens_withSdblQuery() {
16611670
}
16621671

16631672
// endregion
1673+
1674+
// region Common module namespace tests
1675+
1676+
@Test
1677+
void variableWithCommonModuleNotHighlightedAsNamespace() throws IOException {
1678+
// given - code with variable that holds reference to common module via ОбщегоНазначения.ОбщийМодуль
1679+
// The variable itself should NOT be highlighted as namespace
1680+
// Pattern: Модуль = ОбщегоНазначения.ОбщийМодуль("..."); Модуль.Метод();
1681+
var path = Absolute.path("src/test/resources/metadata/designer");
1682+
serverContext.setConfigurationRoot(path);
1683+
1684+
// Load the common module
1685+
var file = new File("src/test/resources/metadata/designer",
1686+
"CommonModules/ПервыйОбщийМодуль/Ext/Module.bsl");
1687+
var uri = Absolute.uri(file);
1688+
TestUtils.getDocumentContext(
1689+
uri,
1690+
FileUtils.readFileToString(file, StandardCharsets.UTF_8),
1691+
serverContext
1692+
);
1693+
1694+
// Load a document with the pattern
1695+
var documentContext = TestUtils.getDocumentContextFromFile(
1696+
"./src/test/resources/references/ReferenceIndexCommonModuleVariable.bsl"
1697+
);
1698+
referenceIndexFiller.fill(documentContext);
1699+
1700+
// when
1701+
TextDocumentIdentifier textDocumentIdentifier = TestUtils.getTextDocumentIdentifier(documentContext.getUri());
1702+
SemanticTokens tokens = provider.getSemanticTokensFull(documentContext, new SemanticTokensParams(textDocumentIdentifier));
1703+
var decoded = decode(tokens.getData());
1704+
1705+
// then
1706+
int namespaceTypeIdx = legend.getTokenTypes().indexOf(SemanticTokenTypes.Namespace);
1707+
1708+
// Find all namespace tokens
1709+
var namespaceTokens = decoded.stream()
1710+
.filter(t -> t.type() == namespaceTypeIdx)
1711+
.toList();
1712+
1713+
// Variable "МодульУправлениеДоступом" should NOT be highlighted as namespace
1714+
// It appears on lines 6, 7, 10, 13 (0-indexed) in ReferenceIndexCommonModuleVariable.bsl
1715+
// Lines 7, 10, 13 are where the variable is used for method calls
1716+
for (var token : namespaceTokens) {
1717+
// Namespace tokens should not be at position 4 (start of "МодульУправлениеДоступом") on variable usage lines
1718+
if (token.line() == 7 || token.line() == 10 || token.line() == 13) {
1719+
assertThat(token.start())
1720+
.as("Variable 'МодульУправлениеДоступом' at line %d should not be namespace", token.line())
1721+
.isNotEqualTo(4);
1722+
}
1723+
}
1724+
}
1725+
1726+
// endregion
16641727
}
16651728

src/test/java/com/github/_1c_syntax/bsl/languageserver/semantictokens/ModuleReferenceSemanticTokensSupplierTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,47 @@ void testNoTokensWithoutCommonModuleReference() {
107107
// then
108108
assertThat(tokens).isEmpty();
109109
}
110+
111+
@Test
112+
void testVariableWithCommonModuleNotHighlightedAsNamespace() throws IOException {
113+
// given - code with variable that holds reference to common module via ОбщегоНазначения.ОбщийМодуль
114+
// The variable itself should NOT be highlighted as namespace
115+
var path = Absolute.path("src/test/resources/metadata/designer");
116+
serverContext.setConfigurationRoot(path);
117+
118+
// Load the common module
119+
var file = new File("src/test/resources/metadata/designer",
120+
"CommonModules/ПервыйОбщийМодуль/Ext/Module.bsl");
121+
var uri = Absolute.uri(file);
122+
TestUtils.getDocumentContext(
123+
uri,
124+
FileUtils.readFileToString(file, StandardCharsets.UTF_8),
125+
serverContext
126+
);
127+
128+
// Load a document with the pattern: Модуль = ОбщегоНазначения.ОбщийМодуль("..."); Модуль.Метод();
129+
var documentContext = TestUtils.getDocumentContextFromFile(
130+
"./src/test/resources/references/ReferenceIndexCommonModuleVariable.bsl"
131+
);
132+
referenceIndexFiller.fill(documentContext);
133+
134+
// when
135+
var tokens = supplier.getSemanticTokens(documentContext);
136+
137+
// then
138+
int namespaceTypeIdx = legend.getTokenTypes().indexOf(SemanticTokenTypes.Namespace);
139+
var namespaceTokens = tokens.stream()
140+
.filter(t -> t.type() == namespaceTypeIdx)
141+
.toList();
142+
143+
// Variable names like "МодульУправлениеДоступом" should NOT appear as namespace tokens
144+
// Only direct common module names should be namespace tokens
145+
for (var token : namespaceTokens) {
146+
// Check that the token is not on a line where variable is used (lines 7, 8, 10, 13 in the test file)
147+
// Line 7 is where the variable is assigned - "МодульУправлениеДоступом" should not be namespace
148+
// The only namespace token should be on line 7 for expression "ОбщегоНазначения.ОбщийМодуль(...)"
149+
// but that's a method call pattern, not a direct module reference
150+
assertThat(token.line()).as("Namespace token should not be on variable usage lines").isNotIn(7, 10, 13);
151+
}
152+
}
110153
}

0 commit comments

Comments
 (0)