Search in sources :

Example 11 with TextDocumentContentChangeEvent

use of org.eclipse.lsp4j.TextDocumentContentChangeEvent in project sts4 by spring-projects.

the class SimpleTextDocumentService method didChange.

@Override
public final void didChange(DidChangeTextDocumentParams params) {
    async.execute(() -> {
        try {
            VersionedTextDocumentIdentifier docId = params.getTextDocument();
            String url = docId.getUri();
            // Log.debug("didChange: "+url);
            if (url != null) {
                TextDocument doc = getDocument(url);
                List<TextDocumentContentChangeEvent> changes = params.getContentChanges();
                doc.apply(params);
                didChangeContent(doc, changes);
            }
        } catch (BadLocationException e) {
            Log.log(e);
        }
    });
}
Also used : VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException)

Example 12 with TextDocumentContentChangeEvent

use of org.eclipse.lsp4j.TextDocumentContentChangeEvent in project sts4 by spring-projects.

the class LanguageServerHarness method changeDocument.

public synchronized TextDocumentInfo changeDocument(String uri, int start, int end, String replaceText) {
    TextDocumentInfo oldDoc = documents.get(uri);
    String oldContent = oldDoc.getText();
    String newContent = oldContent.substring(0, start) + replaceText + oldContent.substring(end);
    TextDocumentItem textDocument = setDocumentContent(uri, newContent);
    DidChangeTextDocumentParams didChange = new DidChangeTextDocumentParams();
    VersionedTextDocumentIdentifier version = new VersionedTextDocumentIdentifier();
    version.setUri(uri);
    version.setVersion(textDocument.getVersion());
    didChange.setTextDocument(version);
    switch(getDocumentSyncMode()) {
        case None:
            // nothing todo
            break;
        case Incremental:
            {
                TextDocumentContentChangeEvent change = new TextDocumentContentChangeEvent();
                change.setRange(new Range(oldDoc.toPosition(start), oldDoc.toPosition(end)));
                change.setRangeLength(end - start);
                change.setText(replaceText);
                didChange.setContentChanges(Collections.singletonList(change));
                break;
            }
        case Full:
            {
                TextDocumentContentChangeEvent change = new TextDocumentContentChangeEvent();
                change.setText(newContent);
                didChange.setContentChanges(Collections.singletonList(change));
                break;
            }
        default:
            throw new IllegalStateException("Unkown SYNC mode: " + getDocumentSyncMode());
    }
    if (getServer() != null) {
        getServer().getTextDocumentService().didChange(didChange);
    }
    return documents.get(uri);
}
Also used : DidChangeTextDocumentParams(org.eclipse.lsp4j.DidChangeTextDocumentParams) TextDocumentItem(org.eclipse.lsp4j.TextDocumentItem) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) Range(org.eclipse.lsp4j.Range) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent)

Example 13 with TextDocumentContentChangeEvent

use of org.eclipse.lsp4j.TextDocumentContentChangeEvent in project sts4 by spring-projects.

the class LanguageServerHarness method changeDocument.

public TextDocumentInfo changeDocument(String uri, String newContent) throws Exception {
    TextDocumentItem textDocument = setDocumentContent(uri, newContent);
    DidChangeTextDocumentParams didChange = new DidChangeTextDocumentParams();
    VersionedTextDocumentIdentifier version = new VersionedTextDocumentIdentifier();
    version.setUri(uri);
    version.setVersion(textDocument.getVersion());
    didChange.setTextDocument(version);
    switch(getDocumentSyncMode()) {
        case None:
            // nothing todo
            break;
        case Incremental:
        case Full:
            TextDocumentContentChangeEvent change = new TextDocumentContentChangeEvent();
            change.setText(newContent);
            didChange.setContentChanges(Collections.singletonList(change));
            break;
        default:
            throw new IllegalStateException("Unkown SYNC mode: " + getDocumentSyncMode());
    }
    if (getServer() != null) {
        getServer().getTextDocumentService().didChange(didChange);
    }
    return documents.get(uri);
}
Also used : DidChangeTextDocumentParams(org.eclipse.lsp4j.DidChangeTextDocumentParams) TextDocumentItem(org.eclipse.lsp4j.TextDocumentItem) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent)

Example 14 with TextDocumentContentChangeEvent

use of org.eclipse.lsp4j.TextDocumentContentChangeEvent in project eclipse.jdt.ls by eclipse.

the class BaseDocumentLifeCycleHandler method handleChanged.

public ICompilationUnit handleChanged(DidChangeTextDocumentParams params) {
    String uri = params.getTextDocument().getUri();
    ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri);
    if (unit == null || !unit.isWorkingCopy() || params.getContentChanges().isEmpty() || unit.getResource().isDerived()) {
        return unit;
    }
    try {
        if (unit.equals(sharedASTProvider.getActiveJavaElement())) {
            sharedASTProvider.disposeAST();
        }
        List<TextDocumentContentChangeEvent> contentChanges = params.getContentChanges();
        for (TextDocumentContentChangeEvent changeEvent : contentChanges) {
            Range range = changeEvent.getRange();
            int length;
            IDocument document = JsonRpcHelpers.toDocument(unit.getBuffer());
            final int startOffset;
            if (range != null) {
                Position start = range.getStart();
                startOffset = JsonRpcHelpers.toOffset(document, start.getLine(), start.getCharacter());
                length = DiagnosticsHelper.getLength(unit, range);
            } else {
                // range is optional and if not given, the whole file content is replaced
                length = unit.getSource().length();
                startOffset = 0;
            }
            TextEdit edit = null;
            String text = changeEvent.getText();
            if (length == 0) {
                edit = new InsertEdit(startOffset, text);
            } else if (text.isEmpty()) {
                edit = new DeleteEdit(startOffset, length);
            } else {
                edit = new ReplaceEdit(startOffset, length, text);
            }
            edit.apply(document, TextEdit.NONE);
        }
        triggerValidation(unit);
    } catch (JavaModelException | MalformedTreeException | BadLocationException e) {
        JavaLanguageServerPlugin.logException("Error while handling document change. URI: " + uri, e);
    }
    return unit;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) InsertEdit(org.eclipse.text.edits.InsertEdit) JavaModelException(org.eclipse.jdt.core.JavaModelException) Position(org.eclipse.lsp4j.Position) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) Range(org.eclipse.lsp4j.Range) DeleteEdit(org.eclipse.text.edits.DeleteEdit) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 15 with TextDocumentContentChangeEvent

use of org.eclipse.lsp4j.TextDocumentContentChangeEvent in project eclipse.jdt.ls by eclipse.

the class CompletionHandlerTest method changeDocument.

private void changeDocument(ICompilationUnit unit, String content, int version) throws JavaModelException {
    DidChangeTextDocumentParams changeParms = new DidChangeTextDocumentParams();
    VersionedTextDocumentIdentifier textDocument = new VersionedTextDocumentIdentifier();
    textDocument.setUri(JDTUtils.toURI(unit));
    textDocument.setVersion(version);
    changeParms.setTextDocument(textDocument);
    TextDocumentContentChangeEvent event = new TextDocumentContentChangeEvent();
    event.setText(content);
    List<TextDocumentContentChangeEvent> contentChanges = new ArrayList<>();
    contentChanges.add(event);
    changeParms.setContentChanges(contentChanges);
    lifeCycleHandler.didChange(changeParms);
}
Also used : DidChangeTextDocumentParams(org.eclipse.lsp4j.DidChangeTextDocumentParams) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) ArrayList(java.util.ArrayList) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent)

Aggregations

TextDocumentContentChangeEvent (org.eclipse.lsp4j.TextDocumentContentChangeEvent)16 VersionedTextDocumentIdentifier (org.eclipse.lsp4j.VersionedTextDocumentIdentifier)9 DidChangeTextDocumentParams (org.eclipse.lsp4j.DidChangeTextDocumentParams)7 Range (org.eclipse.lsp4j.Range)7 TextDocumentItem (org.eclipse.lsp4j.TextDocumentItem)4 ArrayList (java.util.ArrayList)3 Position (org.eclipse.lsp4j.Position)3 Test (org.junit.Test)3 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 IDocument (org.eclipse.jface.text.IDocument)2 DeleteEdit (org.eclipse.text.edits.DeleteEdit)2 InsertEdit (org.eclipse.text.edits.InsertEdit)2 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)2 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)2 TextEdit (org.eclipse.text.edits.TextEdit)2 TextDocument (org.springframework.ide.vscode.commons.util.text.TextDocument)2 Path (java.nio.file.Path)1 List (java.util.List)1