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 AbstractLanguageServerTest method _toExpectation.
protected String _toExpectation(final DocumentHighlight it) {
String _xblockexpression = null;
{
StringConcatenation _builder = new StringConcatenation();
{
Range _range = it.getRange();
boolean _tripleEquals = (_range == null);
if (_tripleEquals) {
_builder.append("[NaN, NaN]:[NaN, NaN]");
} else {
String _expectation = this.toExpectation(it.getRange());
_builder.append(_expectation);
}
}
final String rangeString = _builder.toString();
StringConcatenation _builder_1 = new StringConcatenation();
{
DocumentHighlightKind _kind = it.getKind();
boolean _tripleEquals_1 = (_kind == null);
if (_tripleEquals_1) {
_builder_1.append("NaN");
} else {
String _expectation_1 = this.toExpectation(it.getKind());
_builder_1.append(_expectation_1);
}
}
_builder_1.append(" ");
_builder_1.append(rangeString);
_xblockexpression = _builder_1.toString();
}
return _xblockexpression;
}
use of org.eclipse.lsp4j.Range in project xtext-core by eclipse.
the class AbstractLanguageServerTest method testCodeAction.
protected void testCodeAction(final Procedure1<? super AbstractLanguageServerTest.TestCodeActionConfiguration> configurator) {
try {
@Extension final AbstractLanguageServerTest.TestCodeActionConfiguration configuration = new AbstractLanguageServerTest.TestCodeActionConfiguration();
configuration.setFilePath(("MyModel." + this.fileExtension));
configurator.apply(configuration);
final String filePath = this.initializeContext(configuration).getUri();
CodeActionParams _codeActionParams = new CodeActionParams();
final Procedure1<CodeActionParams> _function = (CodeActionParams it) -> {
TextDocumentIdentifier _textDocumentIdentifier = new TextDocumentIdentifier(filePath);
it.setTextDocument(_textDocumentIdentifier);
Range _range = new Range();
final Procedure1<Range> _function_1 = (Range it_1) -> {
int _line = configuration.getLine();
int _column = configuration.getColumn();
Position _position = new Position(_line, _column);
it_1.setStart(_position);
it_1.setEnd(it_1.getStart());
};
Range _doubleArrow = ObjectExtensions.<Range>operator_doubleArrow(_range, _function_1);
it.setRange(_doubleArrow);
CodeActionContext _codeActionContext = new CodeActionContext();
final Procedure1<CodeActionContext> _function_2 = (CodeActionContext it_1) -> {
it_1.setDiagnostics(this.getDiagnostics().get(filePath));
};
CodeActionContext _doubleArrow_1 = ObjectExtensions.<CodeActionContext>operator_doubleArrow(_codeActionContext, _function_2);
it.setContext(_doubleArrow_1);
};
CodeActionParams _doubleArrow = ObjectExtensions.<CodeActionParams>operator_doubleArrow(_codeActionParams, _function);
final CompletableFuture<List<Either<Command, CodeAction>>> result = this.languageServer.codeAction(_doubleArrow);
if ((configuration.assertCodeActions != null)) {
configuration.assertCodeActions.apply(result.get());
} else {
this.assertEquals(configuration.expectedCodeActions, this.toExpectation(result.get()));
}
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
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;
}
Aggregations