Search in sources :

Example 86 with DocumentContext

use of com.github._1c_syntax.bsl.languageserver.context.DocumentContext in project bsl-language-server by 1c-syntax.

the class CodeActionProviderTest method testGetCodeActions.

@Test
void testGetCodeActions() {
    // given
    DiagnosticInfo diagnosticInfo = new DiagnosticInfo(CanonicalSpellingKeywordsDiagnostic.class, configuration);
    DiagnosticCode diagnosticCode = diagnosticInfo.getCode();
    List<Diagnostic> diagnostics = documentContext.getDiagnostics().stream().filter(diagnostic -> diagnostic.getCode().equals(diagnosticCode)).peek(diagnostic -> diagnostic.setTags(null)).collect(Collectors.toList());
    CodeActionParams params = new CodeActionParams();
    TextDocumentIdentifier textDocumentIdentifier = new TextDocumentIdentifier(documentContext.getUri().toString());
    CodeActionContext codeActionContext = new CodeActionContext();
    codeActionContext.setDiagnostics(diagnostics);
    params.setRange(new Range());
    params.setTextDocument(textDocumentIdentifier);
    params.setContext(codeActionContext);
    // when
    List<Either<Command, CodeAction>> codeActions = codeActionProvider.getCodeActions(params, documentContext);
    // then
    assertThat(codeActions).extracting(Either::getRight).hasSizeGreaterThanOrEqualTo(3).anyMatch(codeAction -> codeAction.getDiagnostics().contains(diagnostics.get(0))).anyMatch(codeAction -> codeAction.getDiagnostics().contains(diagnostics.get(1))).anyMatch(codeAction -> codeAction.getKind().equals(CodeActionKind.QuickFix)).allMatch(codeAction -> (codeAction.getDiagnostics().size() == 1) == toBoolean(codeAction.getIsPreferred()));
}
Also used : DiagnosticInfo(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo) CodeAction(org.eclipse.lsp4j.CodeAction) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Autowired(org.springframework.beans.factory.annotation.Autowired) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Collectors(java.util.stream.Collectors) CanonicalSpellingKeywordsDiagnostic(com.github._1c_syntax.bsl.languageserver.diagnostics.CanonicalSpellingKeywordsDiagnostic) Test(org.junit.jupiter.api.Test) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) List(java.util.List) LanguageServerConfiguration(com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration) Command(org.eclipse.lsp4j.Command) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) TestUtils(com.github._1c_syntax.bsl.languageserver.util.TestUtils) CheckForNull(javax.annotation.CheckForNull) Collections(java.util.Collections) DiagnosticCode(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) DiagnosticInfo(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo) Diagnostic(org.eclipse.lsp4j.Diagnostic) CanonicalSpellingKeywordsDiagnostic(com.github._1c_syntax.bsl.languageserver.diagnostics.CanonicalSpellingKeywordsDiagnostic) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) DiagnosticCode(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode) Range(org.eclipse.lsp4j.Range) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 87 with DocumentContext

use of com.github._1c_syntax.bsl.languageserver.context.DocumentContext in project bsl-language-server by 1c-syntax.

the class CodeLensProviderTest method testGetCodeLens.

@Test
void testGetCodeLens() {
    // given
    String filePath = "./src/test/resources/providers/codeLens.bsl";
    DocumentContext documentContext = TestUtils.getDocumentContextFromFile(filePath);
    // when
    List<CodeLens> codeLenses = codeLensProvider.getCodeLens(documentContext);
    // then
    Map<MethodSymbol, Integer> methodsCognitiveComplexity = documentContext.getCognitiveComplexityData().getMethodsComplexity();
    Map<MethodSymbol, Integer> methodsCyclomaticComplexity = documentContext.getCyclomaticComplexityData().getMethodsComplexity();
    MethodSymbol firstMethod = documentContext.getSymbolTree().getMethods().get(0);
    MethodSymbol secondMethod = documentContext.getSymbolTree().getMethods().get(1);
    int cognitiveComplexityFirstMethod = methodsCognitiveComplexity.get(firstMethod);
    int cognitiveComplexitySecondMethod = methodsCognitiveComplexity.get(secondMethod);
    int cyclomaticComplexityFirstMethod = methodsCyclomaticComplexity.get(firstMethod);
    int cyclomaticComplexitySecondMethod = methodsCyclomaticComplexity.get(secondMethod);
    assertThat(codeLenses).hasSize(4).anyMatch(codeLens -> codeLens.getRange().equals(firstMethod.getSubNameRange())).anyMatch(codeLens -> codeLens.getCommand().getTitle().contains(String.valueOf(cognitiveComplexityFirstMethod))).anyMatch(codeLens -> codeLens.getCommand().getTitle().contains(String.valueOf(cyclomaticComplexityFirstMethod))).anyMatch(codeLens -> codeLens.getRange().equals(secondMethod.getSubNameRange())).anyMatch(codeLens -> codeLens.getCommand().getTitle().contains(String.valueOf(cognitiveComplexitySecondMethod))).anyMatch(codeLens -> codeLens.getCommand().getTitle().contains(String.valueOf(cyclomaticComplexitySecondMethod)));
}
Also used : Test(org.junit.jupiter.api.Test) List(java.util.List) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) CodeLens(org.eclipse.lsp4j.CodeLens) Map(java.util.Map) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Autowired(org.springframework.beans.factory.annotation.Autowired) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) TestUtils(com.github._1c_syntax.bsl.languageserver.util.TestUtils) MethodSymbol(com.github._1c_syntax.bsl.languageserver.context.symbol.MethodSymbol) CodeLens(org.eclipse.lsp4j.CodeLens) MethodSymbol(com.github._1c_syntax.bsl.languageserver.context.symbol.MethodSymbol) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 88 with DocumentContext

use of com.github._1c_syntax.bsl.languageserver.context.DocumentContext in project bsl-language-server by 1c-syntax.

the class DefinitionProviderTest method testDefinitionOfCommonModule.

@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));
}
Also used : Position(org.eclipse.lsp4j.Position) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) DefinitionParams(org.eclipse.lsp4j.DefinitionParams) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 89 with DocumentContext

use of com.github._1c_syntax.bsl.languageserver.context.DocumentContext in project bsl-language-server by 1c-syntax.

the class DiagnosticProviderTest method testComputeDiagnostics.

@Test
void testComputeDiagnostics() {
    // given
    // TODO: это тест на новый getDiagnostics, а не на DiagnosticProvider
    final DocumentContext documentContext = TestUtils.getDocumentContextFromFile("./src/test/resources/providers/diagnosticProvider.bsl");
    // when
    final List<Diagnostic> diagnostics = documentContext.getDiagnostics();
    // then
    assertThat(diagnostics.size()).isPositive();
}
Also used : Diagnostic(org.eclipse.lsp4j.Diagnostic) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 90 with DocumentContext

use of com.github._1c_syntax.bsl.languageserver.context.DocumentContext in project bsl-language-server by 1c-syntax.

the class ReferenceIndexFillerTest method testFindCalledMethod.

@Test
void testFindCalledMethod() {
    // given
    DocumentContext documentContext = TestUtils.getDocumentContextFromFile("./src/test/resources/references/ReferenceIndexFillerTest.bsl");
    referenceIndexFiller.fill(documentContext);
    // when
    Optional<Reference> referencedSymbol = referenceIndex.getReference(documentContext.getUri(), new Position(4, 0));
    // then
    assertThat(referencedSymbol).isPresent();
    assertThat(referencedSymbol).get().extracting(Reference::getSymbol).extracting(Symbol::getName).isEqualTo("Локальная");
    assertThat(referencedSymbol).get().extracting(Reference::getSelectionRange).isEqualTo(Ranges.create(4, 0, 4, 9));
}
Also used : Position(org.eclipse.lsp4j.Position) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

DocumentContext (com.github._1c_syntax.bsl.languageserver.context.DocumentContext)86 Test (org.junit.jupiter.api.Test)69 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)57 Diagnostic (org.eclipse.lsp4j.Diagnostic)36 List (java.util.List)22 Position (org.eclipse.lsp4j.Position)21 SneakyThrows (lombok.SneakyThrows)17 Path (java.nio.file.Path)16 CodeAction (org.eclipse.lsp4j.CodeAction)16 TestUtils (com.github._1c_syntax.bsl.languageserver.util.TestUtils)15 Autowired (org.springframework.beans.factory.annotation.Autowired)15 Collectors (java.util.stream.Collectors)14 Range (org.eclipse.lsp4j.Range)13 ArrayList (java.util.ArrayList)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)12 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)12 LanguageServerConfiguration (com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration)11 MethodSymbol (com.github._1c_syntax.bsl.languageserver.context.symbol.MethodSymbol)10 MDCommonModule (com.github._1c_syntax.mdclasses.mdo.MDCommonModule)10