use of org.eclipse.lsp4j.Range in project xtext-core by eclipse.
the class FormattingService method toTextEdit.
protected TextEdit toTextEdit(Document document, String formattedText, int startOffset, int length) {
TextEdit textEdit = new TextEdit();
textEdit.setNewText(formattedText);
textEdit.setRange(new Range(document.getPosition(startOffset), document.getPosition((startOffset + length))));
return textEdit;
}
use of org.eclipse.lsp4j.Range in project xtext-core by eclipse.
the class ChangeConverter2 method _handleReplacements.
protected void _handleReplacements(ITextDocumentChange change) {
try {
if (change.getReplacements().size() > 0) {
String uri = uriExtensions.toUriString(change.getNewURI());
access.doRead(uri, (ILanguageServerAccess.Context context) -> {
Document document = context.getDocument();
List<TextEdit> textEdits = Lists.transform(change.getReplacements(), (ITextReplacement replacement) -> {
Position start = document.getPosition(replacement.getOffset());
Position end = document.getPosition(replacement.getOffset() + replacement.getLength());
Range range = new Range(start, end);
return new TextEdit(range, replacement.getReplacementText());
});
return addTextEdit(uri, document, textEdits.toArray(new TextEdit[textEdits.size()]));
}).get();
}
} catch (InterruptedException | ExecutionException e) {
throw Exceptions.sneakyThrow(e);
}
}
use of org.eclipse.lsp4j.Range in project xtext-core by eclipse.
the class ChangeConverter2 method _handleReplacements.
protected void _handleReplacements(IEmfResourceChange change) {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
String uri = uriExtensions.toUriString(change.getResource().getURI());
change.getResource().save(outputStream, null);
String newContent = new String(outputStream.toByteArray(), getCharset(change.getResource()));
access.doRead(uri, (ILanguageServerAccess.Context context) -> {
Document document = context.getDocument();
Range range = new Range(document.getPosition(0), document.getPosition(document.getContents().length()));
TextEdit textEdit = new TextEdit(range, newContent);
return addTextEdit(uri, document, textEdit);
}).get();
} catch (InterruptedException | ExecutionException | IOException e) {
throw Exceptions.sneakyThrow(e);
}
}
use of org.eclipse.lsp4j.Range in project xtext-core by eclipse.
the class RenameService2 method doPrepareRename.
protected Either<Range, PrepareRenameResult> doPrepareRename(Resource resource, Document document, PrepareRenameParams params, CancelIndicator cancelIndicator) {
String uri = params.getTextDocument().getUri();
if (resource instanceof XtextResource) {
ICompositeNode rootNode = null;
XtextResource xtextResource = (XtextResource) resource;
if (xtextResource != null) {
IParseResult parseResult = xtextResource.getParseResult();
if (parseResult != null) {
rootNode = parseResult.getRootNode();
}
}
if (rootNode == null) {
RenameService2.LOG.trace("Could not retrieve root node for resource. URI: " + uri);
return null;
}
Position caretPosition = params.getPosition();
try {
int caretOffset = document.getOffSet(caretPosition);
EObject element = null;
int candidateOffset = caretOffset;
do {
element = getElementWithIdentifierAt(xtextResource, candidateOffset);
if (element != null && !element.eIsProxy()) {
ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(rootNode, candidateOffset);
if (leaf != null && isIdentifier(leaf)) {
String convertedNameValue = getConvertedValue(leaf.getGrammarElement(), leaf);
String elementName = getElementName(element);
if (!Strings.isEmpty(convertedNameValue) && !Strings.isEmpty(elementName) && Objects.equal(convertedNameValue, elementName)) {
Position start = document.getPosition(leaf.getOffset());
Position end = document.getPosition(leaf.getEndOffset());
return Either.forLeft(new Range(start, end));
}
}
}
candidateOffset = (candidateOffset - 1);
} while (((candidateOffset >= 0) && ((candidateOffset + 1) >= caretOffset)));
} catch (IndexOutOfBoundsException e) {
RenameService2.LOG.trace("Invalid document " + toPositionFragment(caretPosition, uri));
return null;
}
RenameService2.LOG.trace("No element found at " + toPositionFragment(caretPosition, uri));
} else {
RenameService2.LOG.trace("Loaded resource is not an XtextResource. URI: " + resource.getURI());
}
return null;
}
use of org.eclipse.lsp4j.Range in project xtext-core by eclipse.
the class DocumentExtensions method newLocation.
public Location newLocation(Resource resource, ITextRegion textRegion) {
Range range = newRange(resource, textRegion);
if (range == null) {
return null;
}
String uri = uriExtensions.toUriString(resource.getURI());
return new Location(uri, range);
}
Aggregations