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);
}
});
}
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);
}
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);
}
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;
}
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);
}
Aggregations