Search in sources :

Example 16 with TextDocument

use of org.springframework.ide.vscode.commons.util.text.TextDocument in project sts4 by spring-projects.

the class VscodeCompletionEngineAdapter method adaptEdits.

private static Optional<TextEdit> adaptEdits(TextDocument doc, DocumentEdits edits) {
    try {
        TextReplace replaceEdit = edits.asReplacement(doc);
        if (replaceEdit == null) {
            // The original edit does nothing.
            return Optional.empty();
        } else {
            TextDocument newDoc = doc.copy();
            edits.apply(newDoc);
            TextEdit vscodeEdit = new TextEdit();
            vscodeEdit.setRange(doc.toRange(replaceEdit.start, replaceEdit.end - replaceEdit.start));
            if (Boolean.getBoolean("lsp.completions.indentation.enable")) {
                vscodeEdit.setNewText(replaceEdit.newText);
            } else {
                vscodeEdit.setNewText(vscodeIndentFix(doc, vscodeEdit.getRange().getStart(), replaceEdit.newText));
            }
            // TODO: cursor offset within newText? for now we assume its always at the end.
            return Optional.of(vscodeEdit);
        }
    } catch (Exception e) {
        LOG.get().error("{}", e);
        return Optional.empty();
    }
}
Also used : TextReplace(org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits.TextReplace) TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) TextEdit(org.eclipse.lsp4j.TextEdit) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException)

Example 17 with TextDocument

use of org.springframework.ide.vscode.commons.util.text.TextDocument in project sts4 by spring-projects.

the class SimpleDefinitionFinder method findDefinitions.

/**
 * This is meant to be overridden by subclass. This method provides a simple implementation
 * of 'goto definition' (which is not one you probably want to use in practice, but it
 * might be usful just to test whether things are wired up correctly to make the
 * 'goto definition' action in vscode work.
 * <p>
 * The implementation provided here simply looks for the first occurrence of the word
 * currently pointed at in the current document using String.indexOf.
 */
protected Flux<Location> findDefinitions(TextDocumentPositionParams params) {
    try {
        TextDocument doc = server.getTextDocumentService().get(params);
        if (doc != null) {
            int offset = doc.toOffset(params.getPosition());
            int start = offset;
            while (Character.isLetter(doc.getSafeChar(start))) {
                start--;
            }
            start = start + 1;
            int end = offset;
            while (Character.isLetter(doc.getSafeChar(end))) {
                end++;
            }
            String word = doc.textBetween(start, end);
            Log.log("Looking for definition of '" + word + "'");
            String text = doc.get();
            int def = text.indexOf(word);
            if (def >= 0) {
                return Flux.just(new Location(params.getTextDocument().getUri(), doc.toRange(def, word.length()))).doOnNext((Location loc) -> {
                    Log.log("definition: " + loc);
                });
            }
        }
    } catch (Exception e) {
        Log.log(e);
    }
    return Flux.empty();
}
Also used : TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) Location(org.eclipse.lsp4j.Location)

Example 18 with TextDocument

use of org.springframework.ide.vscode.commons.util.text.TextDocument in project sts4 by spring-projects.

the class BootJavaCodeLensEngine method handle.

@Override
public List<? extends CodeLens> handle(CodeLensParams params) {
    SimpleTextDocumentService documents = server.getTextDocumentService();
    String docURI = params.getTextDocument().getUri();
    if (documents.get(docURI) != null) {
        TextDocument doc = documents.get(docURI).copy();
        try {
            List<? extends CodeLens> codeLensesResult = provideCodeLenses(doc);
            if (codeLensesResult != null) {
                return codeLensesResult;
            }
        } catch (Exception e) {
        }
    }
    return SimpleTextDocumentService.NO_CODELENS;
}
Also used : TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) SimpleTextDocumentService(org.springframework.ide.vscode.commons.languageserver.util.SimpleTextDocumentService)

Example 19 with TextDocument

use of org.springframework.ide.vscode.commons.util.text.TextDocument in project sts4 by spring-projects.

the class CompilationUnitCacheTest method cu_cache_invalidated_by_project_change.

@Test
public void cu_cache_invalidated_by_project_change() throws Exception {
    File directory = new File(ProjectsHarness.class.getResource("/test-projects/test-request-mapping-live-hover/").toURI());
    String docUri = directory.toPath().resolve("src/main/java/example/HelloWorldController.java").toUri().toString();
    harness.intialize(directory);
    URI fileUri = new URI(docUri);
    Path path = Paths.get(fileUri);
    String content = new String(Files.readAllBytes(path));
    TextDocument document = new TextDocument(docUri, LanguageId.JAVA, 0, content);
    CompilationUnit cu = getCompilationUnit(document);
    assertNotNull(cu);
    CompilationUnit cuAnother = getCompilationUnit(document);
    assertTrue(cu == cuAnother);
    harness.changeFile(directory.toPath().resolve(MavenCore.POM_XML).toUri().toString());
    cuAnother = getCompilationUnit(document);
    assertNotNull(cuAnother);
    assertFalse(cu == cuAnother);
}
Also used : Path(java.nio.file.Path) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) File(java.io.File) URI(java.net.URI) Test(org.junit.Test)

Example 20 with TextDocument

use of org.springframework.ide.vscode.commons.util.text.TextDocument in project sts4 by spring-projects.

the class CompilationUnitCacheTest method cu_cache_invalidated_by_project_deletion.

@Test
public void cu_cache_invalidated_by_project_deletion() throws Exception {
    File directory = new File(ProjectsHarness.class.getResource("/test-projects/test-request-mapping-live-hover/").toURI());
    String docUri = directory.toPath().resolve("src/main/java/example/HelloWorldController.java").toUri().toString();
    harness.intialize(directory);
    URI fileUri = new URI(docUri);
    Path path = Paths.get(fileUri);
    String content = new String(Files.readAllBytes(path));
    TextDocument document = new TextDocument(docUri, LanguageId.JAVA, 0, content);
    CompilationUnit cu = getCompilationUnit(document);
    assertNotNull(cu);
    CompilationUnit cuAnother = getCompilationUnit(document);
    assertTrue(cu == cuAnother);
    harness.deleteFile(directory.toPath().resolve(MavenCore.POM_XML).toUri().toString());
    cuAnother = getCompilationUnit(document);
    assertNotNull(cuAnother);
    assertFalse(cu == cuAnother);
}
Also used : Path(java.nio.file.Path) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) File(java.io.File) URI(java.net.URI) Test(org.junit.Test)

Aggregations

TextDocument (org.springframework.ide.vscode.commons.util.text.TextDocument)43 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)12 URI (java.net.URI)11 File (java.io.File)10 Location (org.eclipse.lsp4j.Location)8 Test (org.junit.Test)8 Path (java.nio.file.Path)7 ArrayList (java.util.ArrayList)7 SymbolInformation (org.eclipse.lsp4j.SymbolInformation)7 IClasspath (org.springframework.ide.vscode.commons.java.IClasspath)7 IJavaProject (org.springframework.ide.vscode.commons.java.IJavaProject)7 ImmutableList (com.google.common.collect.ImmutableList)6 List (java.util.List)6 Range (org.eclipse.lsp4j.Range)6 Collection (java.util.Collection)5 Map (java.util.Map)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 Stream (java.util.stream.Stream)5 ASTParser (org.eclipse.jdt.core.dom.ASTParser)5 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)5