Search in sources :

Example 46 with DocumentContext

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

the class CommonModuleNameServerCallDiagnosticTest method getDocumentContextFromFile.

@SneakyThrows
void getDocumentContextFromFile() {
    Path path = Absolute.path(PATH_TO_METADATA);
    Path testFile = Paths.get(PATH_TO_MODULE_FILE).toAbsolutePath();
    initServerContext(path);
    var configuration = context.getConfiguration();
    documentContext = spy(TestUtils.getDocumentContext(testFile.toUri(), FileUtils.readFileToString(testFile.toFile(), StandardCharsets.UTF_8), context));
    module = spy((MDCommonModule) configuration.getModulesByObject().get(documentContext.getUri()));
}
Also used : Path(java.nio.file.Path) MDCommonModule(com.github._1c_syntax.mdclasses.mdo.MDCommonModule) SneakyThrows(lombok.SneakyThrows)

Example 47 with DocumentContext

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

the class ConsecutiveEmptyLinesDiagnosticTest method checkQuickFixes.

private void checkQuickFixes(String module, boolean haveFix) {
    final DocumentContext documentContext = TestUtils.getDocumentContext(module);
    List<Diagnostic> diagnostics = getDiagnostics(documentContext);
    diagnostics.forEach(diagnostic -> checkFix(documentContext, diagnostic, haveFix));
}
Also used : Diagnostic(org.eclipse.lsp4j.Diagnostic) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext)

Example 48 with DocumentContext

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

the class DeprecatedTypeManagedFormDiagnosticTest method testQuickFix.

@Test
void testQuickFix() {
    final DocumentContext documentContext = getDocumentContext();
    List<Diagnostic> diagnostics = getDiagnostics();
    final Diagnostic ruDiagnostic = diagnostics.get(0);
    List<CodeAction> quickFixes = getQuickFixes(ruDiagnostic);
    assertThat(quickFixes).hasSize(1);
    final CodeAction quickFix = quickFixes.get(0);
    assertThat(quickFix).of(diagnosticInstance).in(documentContext).fixes(ruDiagnostic);
    assertThat(quickFix).in(documentContext).hasChanges(1).hasNewText("\"ФормаКлиентскогоПриложения\"");
}
Also used : CodeAction(org.eclipse.lsp4j.CodeAction) Diagnostic(org.eclipse.lsp4j.Diagnostic) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) Test(org.junit.jupiter.api.Test)

Example 49 with DocumentContext

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

the class AnalyzeCommand method getFileInfoFromFile.

private FileInfo getFileInfoFromFile(Path srcDir, File file) {
    String textDocumentContent;
    try {
        textDocumentContent = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    DocumentContext documentContext = context.addDocument(file.toURI(), textDocumentContent, 1);
    Path filePath = srcDir.relativize(Absolute.path(file));
    List<Diagnostic> diagnostics = documentContext.getDiagnostics();
    MetricStorage metrics = documentContext.getMetrics();
    String mdoRef = "";
    Optional<AbstractMDObjectBase> mdObjectBase = documentContext.getMdObject();
    if (mdObjectBase.isPresent()) {
        mdoRef = mdObjectBase.get().getMdoReference().getMdoRef();
    }
    FileInfo fileInfo = new FileInfo(filePath, mdoRef, diagnostics, metrics);
    // clean up AST after diagnostic computing to free up RAM.
    documentContext.clearSecondaryData();
    return fileInfo;
}
Also used : Path(java.nio.file.Path) FileInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo) AbstractMDObjectBase(com.github._1c_syntax.mdclasses.mdo.AbstractMDObjectBase) MetricStorage(com.github._1c_syntax.bsl.languageserver.context.MetricStorage) Diagnostic(org.eclipse.lsp4j.Diagnostic) IOException(java.io.IOException) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext)

Example 50 with DocumentContext

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

the class FormatCommand method formatFile.

@SneakyThrows
private void formatFile(File file) {
    String textDocumentContent = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
    final URI uri = file.toURI();
    DocumentContext documentContext = serverContext.addDocument(uri, textDocumentContent, 1);
    DocumentFormattingParams params = new DocumentFormattingParams();
    FormattingOptions options = new FormattingOptions();
    options.setInsertSpaces(false);
    params.setOptions(options);
    final List<TextEdit> formatting = formatProvider.getFormatting(params, documentContext);
    serverContext.removeDocument(uri);
    if (formatting.isEmpty()) {
        return;
    }
    final TextEdit textEdit = formatting.get(0);
    final String newText = textEdit.getNewText();
    FileUtils.writeStringToFile(file, newText, StandardCharsets.UTF_8);
}
Also used : FormattingOptions(org.eclipse.lsp4j.FormattingOptions) DocumentFormattingParams(org.eclipse.lsp4j.DocumentFormattingParams) TextEdit(org.eclipse.lsp4j.TextEdit) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) URI(java.net.URI) SneakyThrows(lombok.SneakyThrows)

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