Search in sources :

Example 1 with TextDocumentContentChangeEvent

use of org.graalvm.tools.lsp.server.types.TextDocumentContentChangeEvent in project graal by oracle.

the class TextDocumentContentChangeTest method assertDocumentChanges.

private static void assertDocumentChanges(String oldText, String replacement, Range range, String expectedText) {
    TextDocumentContentChangeEvent event = TextDocumentContentChangeEvent.create(replacement).setRange(range).setRangeLength(replacement.length());
    String actualText = SourceUtils.applyTextDocumentChanges(Arrays.asList(event), createSource(oldText), null, null);
    assertEquals(expectedText, actualText);
}
Also used : TextDocumentContentChangeEvent(org.graalvm.tools.lsp.server.types.TextDocumentContentChangeEvent)

Example 2 with TextDocumentContentChangeEvent

use of org.graalvm.tools.lsp.server.types.TextDocumentContentChangeEvent in project graal by oracle.

the class TextDocumentContentChangeTest method applyTextDocumentChangesList.

@Test
public void applyTextDocumentChangesList() {
    String oldText = "";
    String replacement1 = "a";
    TextDocumentContentChangeEvent event1 = TextDocumentContentChangeEvent.create(replacement1).setRange(Range.create(Position.create(0, 0), Position.create(0, 0))).setRangeLength(replacement1.length());
    String replacement2 = "c";
    TextDocumentContentChangeEvent event2 = TextDocumentContentChangeEvent.create(replacement2).setRange(Range.create(Position.create(0, 1), Position.create(0, 1))).setRangeLength(replacement2.length());
    String replacement3 = "b";
    TextDocumentContentChangeEvent event3 = TextDocumentContentChangeEvent.create(replacement3).setRange(Range.create(Position.create(0, 1), Position.create(0, 1))).setRangeLength(replacement3.length());
    String replacement4 = "\nefg\nhij";
    TextDocumentContentChangeEvent event4 = TextDocumentContentChangeEvent.create(replacement4).setRange(Range.create(Position.create(0, 3), Position.create(0, 3))).setRangeLength(replacement4.length());
    String replacement5 = "####";
    TextDocumentContentChangeEvent event5 = TextDocumentContentChangeEvent.create(replacement5).setRange(Range.create(Position.create(1, 0), Position.create(2, 0))).setRangeLength(replacement5.length());
    String replacement6 = "\n";
    TextDocumentContentChangeEvent event6 = TextDocumentContentChangeEvent.create(replacement6).setRange(Range.create(Position.create(1, 7), Position.create(1, 7))).setRangeLength(replacement6.length());
    String actualText = SourceUtils.applyTextDocumentChanges(Arrays.asList(event1, event2, event3, event4, event5, event6), createSource(oldText), null, null);
    assertEquals("abc\n####hij\n", actualText);
}
Also used : TextDocumentContentChangeEvent(org.graalvm.tools.lsp.server.types.TextDocumentContentChangeEvent) Test(org.junit.Test)

Example 3 with TextDocumentContentChangeEvent

use of org.graalvm.tools.lsp.server.types.TextDocumentContentChangeEvent in project graal by oracle.

the class SourceUtils method applyTextDocumentChanges.

public static String applyTextDocumentChanges(List<? extends TextDocumentContentChangeEvent> list, Source source, TextDocumentSurrogate surrogate, TruffleLogger logger) {
    Source currentSource = null;
    String text = source.getCharacters().toString();
    StringBuilder sb = new StringBuilder(text);
    for (TextDocumentContentChangeEvent event : list) {
        if (currentSource == null) {
            currentSource = source;
        } else {
            currentSource = Source.newBuilder(currentSource.getLanguage(), sb, currentSource.getName()).cached(false).build();
        }
        Range range = event.getRange();
        if (range == null) {
            // The whole file has changed
            // Clear StringBuilder
            sb.setLength(0);
            sb.append(event.getText());
            continue;
        }
        Position start = range.getStart();
        Position end = range.getEnd();
        int startLine = start.getLine() + 1;
        int endLine = end.getLine() + 1;
        int replaceBegin = currentSource.getLineStartOffset(startLine) + start.getCharacter();
        int replaceEnd = currentSource.getLineStartOffset(endLine) + end.getCharacter();
        sb.replace(replaceBegin, replaceEnd, event.getText());
        if (surrogate != null && surrogate.hasCoverageData()) {
            updateCoverageData(surrogate, currentSource, event.getText(), range, replaceBegin, replaceEnd, logger);
        }
    }
    return sb.toString();
}
Also used : Position(org.graalvm.tools.lsp.server.types.Position) Range(org.graalvm.tools.lsp.server.types.Range) Source(com.oracle.truffle.api.source.Source) TextDocumentContentChangeEvent(org.graalvm.tools.lsp.server.types.TextDocumentContentChangeEvent)

Example 4 with TextDocumentContentChangeEvent

use of org.graalvm.tools.lsp.server.types.TextDocumentContentChangeEvent in project graal by oracle.

the class SourceUtils method removeLastTextInsertion.

public static SourceFix removeLastTextInsertion(TextDocumentSurrogate surrogate, int originalCharacter, TruffleLogger logger) {
    TextDocumentContentChangeEvent lastChange = surrogate.getLastChange();
    if (lastChange == null) {
        return null;
    }
    Range range = lastChange.getRange();
    TextDocumentContentChangeEvent replacementEvent = // 
    TextDocumentContentChangeEvent.create("").setRange(// 
    Range.create(range.getStart(), Position.create(range.getEnd().getLine(), range.getEnd().getCharacter() + lastChange.getText().length()))).setRangeLength(lastChange.getText().length());
    String codeBeforeLastChange = applyTextDocumentChanges(Arrays.asList(replacementEvent), surrogate.getSource(), surrogate, logger);
    int characterIdx = originalCharacter - (originalCharacter - range.getStart().getCharacter());
    return new SourceFix(codeBeforeLastChange, lastChange.getText(), characterIdx);
}
Also used : Range(org.graalvm.tools.lsp.server.types.Range) TextDocumentContentChangeEvent(org.graalvm.tools.lsp.server.types.TextDocumentContentChangeEvent)

Example 5 with TextDocumentContentChangeEvent

use of org.graalvm.tools.lsp.server.types.TextDocumentContentChangeEvent in project graal by oracle.

the class LanguageServerImpl method processChanges.

private void processChanges(final String documentUri, final List<? extends TextDocumentContentChangeEvent> list) {
    String langId = openedFileUri2LangId.get(URI.create(documentUri));
    if (langId == null) {
        truffleAdapter.getLogger().warning("Changed document that was not opened: " + documentUri);
        return;
    }
    URI uri = URI.create(documentUri);
    Future<?> future;
    switch(TEXT_DOCUMENT_SYNC_KIND) {
        case Full:
            // Only need the first element, as long as sync mode isTextDocumentSyncKind.Full
            TextDocumentContentChangeEvent e = list.iterator().next();
            future = truffleAdapter.parse(e.getText(), langId, uri);
            break;
        case Incremental:
            future = truffleAdapter.processChangesAndParse(list, uri);
            break;
        default:
            throw new IllegalStateException("Unknown TextDocumentSyncKind: " + TEXT_DOCUMENT_SYNC_KIND);
    }
    CompletableFuture.runAsync(() -> waitForResultAndHandleExceptions(future, null, uri));
}
Also used : URI(java.net.URI) TextDocumentContentChangeEvent(org.graalvm.tools.lsp.server.types.TextDocumentContentChangeEvent)

Aggregations

TextDocumentContentChangeEvent (org.graalvm.tools.lsp.server.types.TextDocumentContentChangeEvent)7 Range (org.graalvm.tools.lsp.server.types.Range)2 Source (com.oracle.truffle.api.source.Source)1 URI (java.net.URI)1 Collection (java.util.Collection)1 ExecutionException (java.util.concurrent.ExecutionException)1 Position (org.graalvm.tools.lsp.server.types.Position)1 TextDocumentSurrogate (org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate)1 Test (org.junit.Test)1