Skip to content

Commit

Permalink
Merge pull request #1603 from 1c-syntax/feature/definition-and-refere…
Browse files Browse the repository at this point in the history
…nces-providers
  • Loading branch information
nixel2007 authored Mar 26, 2021
2 parents 5476f7e + d64bf55 commit 47ff1f2
Show file tree
Hide file tree
Showing 8 changed files with 399 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.eclipse.lsp4j.CodeActionKind;
import org.eclipse.lsp4j.CodeActionOptions;
import org.eclipse.lsp4j.CodeLensOptions;
import org.eclipse.lsp4j.DefinitionOptions;
import org.eclipse.lsp4j.DocumentFormattingOptions;
import org.eclipse.lsp4j.DocumentLinkOptions;
import org.eclipse.lsp4j.DocumentRangeFormattingOptions;
Expand All @@ -40,6 +41,7 @@
import org.eclipse.lsp4j.HoverOptions;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4j.InitializeResult;
import org.eclipse.lsp4j.ReferenceOptions;
import org.eclipse.lsp4j.SaveOptions;
import org.eclipse.lsp4j.ServerCapabilities;
import org.eclipse.lsp4j.ServerInfo;
Expand Down Expand Up @@ -89,6 +91,8 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
capabilities.setDocumentLinkProvider(getDocumentLinkProvider());
capabilities.setWorkspaceSymbolProvider(getWorkspaceProvider());
capabilities.setHoverProvider(getHoverProvider());
capabilities.setReferencesProvider(getReferencesProvider());
capabilities.setDefinitionProvider(getDefinitionProvider());

InitializeResult result = new InitializeResult(capabilities, serverInfo);

Expand Down Expand Up @@ -227,6 +231,18 @@ private static HoverOptions getHoverProvider() {
return hoverOptions;
}

private static DefinitionOptions getDefinitionProvider() {
var definitionOptions = new DefinitionOptions();
definitionOptions.setWorkDoneProgress(Boolean.FALSE);
return definitionOptions;
}

private static ReferenceOptions getReferencesProvider() {
var referenceOptions = new ReferenceOptions();
referenceOptions.setWorkDoneProgress(Boolean.FALSE);
return referenceOptions;
}

private static WorkspaceSymbolOptions getWorkspaceProvider() {
var workspaceSymbolOptions = new WorkspaceSymbolOptions();
workspaceSymbolOptions.setWorkDoneProgress(Boolean.FALSE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
import com.github._1c_syntax.bsl.languageserver.jsonrpc.ProtocolExtension;
import com.github._1c_syntax.bsl.languageserver.providers.CodeActionProvider;
import com.github._1c_syntax.bsl.languageserver.providers.CodeLensProvider;
import com.github._1c_syntax.bsl.languageserver.providers.DefinitionProvider;
import com.github._1c_syntax.bsl.languageserver.providers.DiagnosticProvider;
import com.github._1c_syntax.bsl.languageserver.providers.DocumentLinkProvider;
import com.github._1c_syntax.bsl.languageserver.providers.DocumentSymbolProvider;
import com.github._1c_syntax.bsl.languageserver.providers.FoldingRangeProvider;
import com.github._1c_syntax.bsl.languageserver.providers.FormatProvider;
import com.github._1c_syntax.bsl.languageserver.providers.HoverProvider;
import com.github._1c_syntax.bsl.languageserver.providers.ReferencesProvider;
import com.github._1c_syntax.bsl.languageserver.utils.Ranges;
import lombok.RequiredArgsConstructor;
import org.eclipse.lsp4j.CodeAction;
Expand Down Expand Up @@ -67,6 +69,7 @@
import org.eclipse.lsp4j.services.TextDocumentService;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
Expand All @@ -85,6 +88,8 @@ public class BSLTextDocumentService implements TextDocumentService, ProtocolExte
private final FoldingRangeProvider foldingRangeProvider;
private final FormatProvider formatProvider;
private final HoverProvider hoverProvider;
private final ReferencesProvider referencesProvider;
private final DefinitionProvider definitionProvider;

@Override
public CompletableFuture<Hover> hover(HoverParams params) {
Expand All @@ -101,12 +106,24 @@ public CompletableFuture<Hover> hover(HoverParams params) {
public CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> definition(
DefinitionParams params
) {
throw new UnsupportedOperationException();
DocumentContext documentContext = context.getDocument(params.getTextDocument().getUri());
if (documentContext == null) {
return CompletableFuture.completedFuture(Either.forRight(Collections.emptyList()));
}

return CompletableFuture.supplyAsync(() ->
Either.forRight(definitionProvider.getDefinition(documentContext, params))
);
}

@Override
public CompletableFuture<List<? extends Location>> references(ReferenceParams params) {
throw new UnsupportedOperationException();
DocumentContext documentContext = context.getDocument(params.getTextDocument().getUri());
if (documentContext == null) {
return CompletableFuture.completedFuture(Collections.emptyList());
}

return CompletableFuture.supplyAsync(() -> referencesProvider.getReferences(documentContext, params));
}

@Override
Expand Down Expand Up @@ -172,7 +189,7 @@ public CompletableFuture<List<FoldingRange>> foldingRange(FoldingRangeRequestPar

return CompletableFuture.supplyAsync(() -> foldingRangeProvider.getFoldingRange(documentContext));
}

@Override
public void didOpen(DidOpenTextDocumentParams params) {
DocumentContext documentContext = context.addDocument(params.getTextDocument());
Expand Down
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()
);
}
}
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());
}
}
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));
}
}
Loading

0 comments on commit 47ff1f2

Please sign in to comment.