-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1603 from 1c-syntax/feature/definition-and-refere…
…nces-providers
- Loading branch information
Showing
8 changed files
with
399 additions
and
3 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
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
63 changes: 63 additions & 0 deletions
63
src/main/java/com/github/_1c_syntax/bsl/languageserver/providers/DefinitionProvider.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,63 @@ | ||
/* | ||
* This file is a part of BSL Language Server. | ||
* | ||
* Copyright © 2018-2021 | ||
* Alexey Sosnoviy <[email protected]>, Nikita Gryzlov <[email protected]> and contributors | ||
* | ||
* SPDX-License-Identifier: LGPL-3.0-or-later | ||
* | ||
* BSL Language Server is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3.0 of the License, or (at your option) any later version. | ||
* | ||
* BSL Language Server is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with BSL Language Server. | ||
*/ | ||
package com.github._1c_syntax.bsl.languageserver.providers; | ||
|
||
import com.github._1c_syntax.bsl.languageserver.context.DocumentContext; | ||
import com.github._1c_syntax.bsl.languageserver.context.symbol.SourceDefinedSymbol; | ||
import com.github._1c_syntax.bsl.languageserver.references.Reference; | ||
import com.github._1c_syntax.bsl.languageserver.references.ReferenceResolver; | ||
import lombok.RequiredArgsConstructor; | ||
import org.eclipse.lsp4j.DefinitionParams; | ||
import org.eclipse.lsp4j.LocationLink; | ||
import org.eclipse.lsp4j.Position; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class DefinitionProvider { | ||
|
||
private final ReferenceResolver referenceResolver; | ||
|
||
public List<LocationLink> getDefinition(DocumentContext documentContext, DefinitionParams params) { | ||
Position position = params.getPosition(); | ||
|
||
return referenceResolver.findReference(documentContext.getUri(), position) | ||
.filter(Reference::isSourceDefinedSymbolReference) | ||
.map(DefinitionProvider::toLocationLink) | ||
.map(Collections::singletonList) | ||
.orElse(Collections.emptyList()); | ||
} | ||
|
||
private static LocationLink toLocationLink(Reference reference) { | ||
SourceDefinedSymbol symbol = (SourceDefinedSymbol) reference.getSymbol(); | ||
|
||
return new LocationLink( | ||
symbol.getOwner().getUri().toString(), | ||
symbol.getRange(), | ||
symbol.getSelectionRange(), | ||
reference.getSelectionRange() | ||
); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/com/github/_1c_syntax/bsl/languageserver/providers/ReferencesProvider.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,55 @@ | ||
/* | ||
* This file is a part of BSL Language Server. | ||
* | ||
* Copyright © 2018-2021 | ||
* Alexey Sosnoviy <[email protected]>, Nikita Gryzlov <[email protected]> and contributors | ||
* | ||
* SPDX-License-Identifier: LGPL-3.0-or-later | ||
* | ||
* BSL Language Server is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3.0 of the License, or (at your option) any later version. | ||
* | ||
* BSL Language Server is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with BSL Language Server. | ||
*/ | ||
package com.github._1c_syntax.bsl.languageserver.providers; | ||
|
||
import com.github._1c_syntax.bsl.languageserver.context.DocumentContext; | ||
import com.github._1c_syntax.bsl.languageserver.references.Reference; | ||
import com.github._1c_syntax.bsl.languageserver.references.ReferenceIndex; | ||
import com.github._1c_syntax.bsl.languageserver.references.ReferenceResolver; | ||
import lombok.RequiredArgsConstructor; | ||
import org.eclipse.lsp4j.Location; | ||
import org.eclipse.lsp4j.ReferenceParams; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class ReferencesProvider { | ||
|
||
private final ReferenceResolver referenceResolver; | ||
private final ReferenceIndex referenceIndex; | ||
|
||
public List<Location> getReferences(DocumentContext documentContext, ReferenceParams params) { | ||
var position = params.getPosition(); | ||
|
||
return referenceResolver.findReference(documentContext.getUri(), position) | ||
.flatMap(Reference::getSourceDefinedSymbol) | ||
.stream() | ||
.map(referenceIndex::getReferencesTo) | ||
.flatMap(Collection::stream) | ||
.map(Reference::toLocation) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
src/test/java/com/github/_1c_syntax/bsl/languageserver/providers/DefinitionProviderTest.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 @@ | ||
/* | ||
* This file is a part of BSL Language Server. | ||
* | ||
* Copyright © 2018-2021 | ||
* Alexey Sosnoviy <[email protected]>, Nikita Gryzlov <[email protected]> and contributors | ||
* | ||
* SPDX-License-Identifier: LGPL-3.0-or-later | ||
* | ||
* BSL Language Server is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3.0 of the License, or (at your option) any later version. | ||
* | ||
* BSL Language Server is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with BSL Language Server. | ||
*/ | ||
package com.github._1c_syntax.bsl.languageserver.providers; | ||
|
||
import com.github._1c_syntax.bsl.languageserver.context.DocumentContext; | ||
import com.github._1c_syntax.bsl.languageserver.context.ServerContext; | ||
import com.github._1c_syntax.bsl.languageserver.util.CleanupContextBeforeClassAndAfterClass; | ||
import com.github._1c_syntax.bsl.languageserver.util.TestUtils; | ||
import com.github._1c_syntax.bsl.languageserver.utils.Ranges; | ||
import com.github._1c_syntax.mdclasses.metadata.additional.ModuleType; | ||
import org.eclipse.lsp4j.DefinitionParams; | ||
import org.eclipse.lsp4j.Position; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.nio.file.Paths; | ||
|
||
import static com.github._1c_syntax.bsl.languageserver.util.TestUtils.PATH_TO_METADATA; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@SpringBootTest | ||
@CleanupContextBeforeClassAndAfterClass | ||
class DefinitionProviderTest { | ||
|
||
@Autowired | ||
private DefinitionProvider definitionProvider; | ||
|
||
@Autowired | ||
private ServerContext serverContext; | ||
|
||
private static final String PATH_TO_FILE = "./src/test/resources/providers/definition.bsl"; | ||
|
||
@PostConstruct | ||
void prepareServerContext() { | ||
serverContext.setConfigurationRoot(Paths.get(PATH_TO_METADATA)); | ||
serverContext.populateContext(); | ||
} | ||
|
||
@Test | ||
void testEmptyDefinition() { | ||
DocumentContext documentContext = TestUtils.getDocumentContextFromFile(PATH_TO_FILE); | ||
|
||
var params = new DefinitionParams(); | ||
params.setPosition(new Position(1, 0)); | ||
|
||
// when | ||
var definitions = definitionProvider.getDefinition(documentContext, params); | ||
|
||
// then | ||
assertThat(definitions).isEmpty(); | ||
} | ||
|
||
@Test | ||
void testDefinitionOfLocalMethod() { | ||
DocumentContext documentContext = TestUtils.getDocumentContextFromFile(PATH_TO_FILE); | ||
var methodSymbol = documentContext.getSymbolTree().getMethodSymbol("ИмяФункции").orElseThrow(); | ||
|
||
var params = new DefinitionParams(); | ||
params.setPosition(new Position(4, 10)); | ||
|
||
// when | ||
var definitions = definitionProvider.getDefinition(documentContext, params); | ||
|
||
// then | ||
assertThat(definitions).hasSize(1); | ||
|
||
var definition = definitions.get(0); | ||
|
||
assertThat(definition.getTargetUri()).isEqualTo(documentContext.getUri().toString()); | ||
assertThat(definition.getTargetSelectionRange()).isEqualTo(methodSymbol.getSelectionRange()); | ||
assertThat(definition.getTargetRange()).isEqualTo(methodSymbol.getRange()); | ||
assertThat(definition.getOriginSelectionRange()).isEqualTo(Ranges.create(4, 0, 10)); | ||
} | ||
|
||
@Test | ||
void testDefinitionOfCommonModule() { | ||
DocumentContext documentContext = TestUtils.getDocumentContextFromFile(PATH_TO_FILE); | ||
var managerModule = serverContext.getDocument("Catalog.Справочник1", ModuleType.ManagerModule).orElseThrow(); | ||
var methodSymbol = managerModule.getSymbolTree().getMethodSymbol("ТестЭкспортная").orElseThrow(); | ||
|
||
var params = new DefinitionParams(); | ||
params.setPosition(new Position(6, 30)); | ||
|
||
// when | ||
var definitions = definitionProvider.getDefinition(documentContext, params); | ||
|
||
// then | ||
assertThat(definitions).hasSize(1); | ||
|
||
var definition = definitions.get(0); | ||
|
||
assertThat(definition.getTargetUri()).isEqualTo(managerModule.getUri().toString()); | ||
assertThat(definition.getTargetSelectionRange()).isEqualTo(methodSymbol.getSelectionRange()); | ||
assertThat(definition.getTargetRange()).isEqualTo(methodSymbol.getRange()); | ||
assertThat(definition.getOriginSelectionRange()).isEqualTo(Ranges.create(6, 24, 38)); | ||
} | ||
} |
Oops, something went wrong.