Skip to content

Commit 906a841

Browse files
committed
Переход к модулю класса
1 parent f1c47cc commit 906a841

File tree

5 files changed

+161
-3
lines changed

5 files changed

+161
-3
lines changed

src/main/java/com/github/_1c_syntax/bsl/languageserver/providers/SelectionRangeProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ private static boolean ifBranchMatchesIfStatement(BSLParserRuleContext ctx) {
216216
return ifStatement.elseBranch() == null && ifStatement.elsifBranch().isEmpty();
217217
}
218218

219-
private static Optional<TerminalNode> findNodeContainsPosition(BSLParserRuleContext tree, Position position) {
219+
public static Optional<TerminalNode> findNodeContainsPosition(BSLParserRuleContext tree, Position position) {
220220

221221
if (tree.getTokens().isEmpty()) {
222222
return Optional.empty();
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* This file is a part of BSL Language Server.
3+
*
4+
* Copyright (c) 2018-2022
5+
* Alexey Sosnoviy <labotamy@gmail.com>, Nikita Fedkin <nixel2007@gmail.com> and contributors
6+
*
7+
* SPDX-License-Identifier: LGPL-3.0-or-later
8+
*
9+
* BSL Language Server is free software; you can redistribute it and/or
10+
* modify it under the terms of the GNU Lesser General Public
11+
* License as published by the Free Software Foundation; either
12+
* version 3.0 of the License, or (at your option) any later version.
13+
*
14+
* BSL Language Server is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
* Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public
20+
* License along with BSL Language Server.
21+
*/
22+
package com.github._1c_syntax.bsl.languageserver.references;
23+
24+
import com.github._1c_syntax.bsl.languageserver.context.DocumentContext;
25+
import com.github._1c_syntax.bsl.languageserver.context.ServerContext;
26+
import com.github._1c_syntax.bsl.languageserver.providers.SelectionRangeProvider;
27+
import com.github._1c_syntax.bsl.languageserver.references.model.OccurrenceType;
28+
import com.github._1c_syntax.bsl.languageserver.references.model.Reference;
29+
import com.github._1c_syntax.bsl.languageserver.utils.Ranges;
30+
import com.github._1c_syntax.bsl.parser.BSLParser;
31+
import lombok.RequiredArgsConstructor;
32+
import org.eclipse.lsp4j.Position;
33+
import org.springframework.stereotype.Component;
34+
35+
import java.net.URI;
36+
import java.util.Optional;
37+
38+
39+
@Component
40+
@RequiredArgsConstructor
41+
public class OSModulesReferenceFinder implements ReferenceFinder {
42+
43+
private final ServerContext serverContext;
44+
45+
@Override
46+
public Optional<Reference> findReference(URI uri, Position position) {
47+
48+
DocumentContext document = serverContext.getDocument(uri);
49+
if (document == null) {
50+
return Optional.empty();
51+
}
52+
53+
var node = SelectionRangeProvider.findNodeContainsPosition(document.getAst(), position);
54+
55+
if (node.isEmpty()) {
56+
return Optional.empty();
57+
}
58+
59+
if (!(node.get().getParent() instanceof BSLParser.TypeNameContext)) {
60+
return Optional.empty();
61+
}
62+
63+
var className = node.get().getText();
64+
65+
var dd = serverContext.getDocuments().values().stream()
66+
.filter(d -> d.getTypeName().equals(className))
67+
.findFirst();
68+
69+
if (dd.isEmpty()) {
70+
return Optional.empty();
71+
}
72+
73+
return Optional.of(new Reference(
74+
dd.get().getSymbolTree().getModule(),
75+
dd.get().getSymbolTree().getModule(),
76+
dd.get().getUri(),
77+
Ranges.create(0, 0, 0, 0),
78+
OccurrenceType.DEFINITION
79+
)
80+
);
81+
82+
}
83+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* This file is a part of BSL Language Server.
3+
*
4+
* Copyright (c) 2018-2022
5+
* Alexey Sosnoviy <labotamy@gmail.com>, Nikita Fedkin <nixel2007@gmail.com> and contributors
6+
*
7+
* SPDX-License-Identifier: LGPL-3.0-or-later
8+
*
9+
* BSL Language Server is free software; you can redistribute it and/or
10+
* modify it under the terms of the GNU Lesser General Public
11+
* License as published by the Free Software Foundation; either
12+
* version 3.0 of the License, or (at your option) any later version.
13+
*
14+
* BSL Language Server is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
* Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public
20+
* License along with BSL Language Server.
21+
*/
22+
package com.github._1c_syntax.bsl.languageserver.references;
23+
24+
import com.github._1c_syntax.bsl.languageserver.context.ServerContext;
25+
import com.github._1c_syntax.bsl.languageserver.util.TestUtils;
26+
import com.github._1c_syntax.utils.Absolute;
27+
import org.eclipse.lsp4j.Position;
28+
import org.junit.jupiter.api.Test;
29+
import org.springframework.beans.factory.annotation.Autowired;
30+
import org.springframework.boot.test.context.SpringBootTest;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
34+
@SpringBootTest
35+
class OSModulesReferenceFinderTest {
36+
37+
@Autowired
38+
private OSModulesReferenceFinder referenceFinder;
39+
40+
@Autowired
41+
private ServerContext serverContext;
42+
43+
@Test
44+
void testFindReferenceToClass() {
45+
// given
46+
serverContext.setConfigurationRoot(Absolute.path("src/test/resources/metadata/oscript"));
47+
serverContext.populateContext();
48+
var documentContext = TestUtils.getDocumentContextFromFile("./src/test/resources/metadata/oscript/Классы/ТестовыйКласс.os");
49+
var mainOsContext = TestUtils.getDocumentContextFromFile("./src/test/resources/metadata/oscript/main.os");
50+
51+
// when
52+
var optionalReference = referenceFinder.findReference(
53+
mainOsContext.getUri(),
54+
new Position(1, 25)
55+
);
56+
57+
// then
58+
assertThat(optionalReference)
59+
.isPresent()
60+
// .hasValueSatisfying(reference -> assertThat(reference.getSymbol().getSymbolKind()).isEqualTo())
61+
;
62+
}
63+
64+
65+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

2-
Переменная = Новый ТестовыйКласс;
2+
ТестовыйКласс = Новый ТестовыйКласс;
33

4-
Другая = 1 + Переменная;
4+
Другая = 1 + ТестовыйКласс;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
Процедура ПростоПроцедура()
3+
4+
5+
КонецПроцедуры
6+
7+
Процедура ПриСозданииОбъекта()
8+
9+
10+
КонецПроцедуры

0 commit comments

Comments
 (0)