Search in sources :

Example 61 with Range

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);
    }
}
Also used : TextEdit(org.eclipse.lsp4j.TextEdit) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Document(org.eclipse.xtext.ide.server.Document) Range(org.eclipse.lsp4j.Range) ExecutionException(java.util.concurrent.ExecutionException) ILanguageServerAccess(org.eclipse.xtext.ide.server.ILanguageServerAccess)

Example 62 with Range

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;
}
Also used : ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) Position(org.eclipse.lsp4j.Position) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) XtextResource(org.eclipse.xtext.resource.XtextResource) IParseResult(org.eclipse.xtext.parser.IParseResult) Range(org.eclipse.lsp4j.Range)

Example 63 with Range

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;
}
Also used : DocumentHighlightKind(org.eclipse.lsp4j.DocumentHighlightKind) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) FoldingRange(org.eclipse.lsp4j.FoldingRange) Range(org.eclipse.lsp4j.Range)

Example 64 with Range

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);
    }
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Position(org.eclipse.lsp4j.Position) CodeAction(org.eclipse.lsp4j.CodeAction) FoldingRange(org.eclipse.lsp4j.FoldingRange) Range(org.eclipse.lsp4j.Range) Endpoint(org.eclipse.lsp4j.jsonrpc.Endpoint) Extension(org.eclipse.xtext.xbase.lib.Extension) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) List(java.util.List) CompletionList(org.eclipse.lsp4j.CompletionList)

Example 65 with Range

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;
}
Also used : TextEdit(org.eclipse.lsp4j.TextEdit) Range(org.eclipse.lsp4j.Range)

Aggregations

Range (org.eclipse.lsp4j.Range)126 Position (org.eclipse.lsp4j.Position)59 Test (org.junit.Test)38 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)23 TextEdit (org.eclipse.lsp4j.TextEdit)23 ArrayList (java.util.ArrayList)22 List (java.util.List)17 Location (org.eclipse.lsp4j.Location)16 Document (org.eclipse.xtext.ide.server.Document)16 Diagnostic (org.eclipse.lsp4j.Diagnostic)15 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)13 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)13 PrepareRenameParams (org.eclipse.lsp4j.PrepareRenameParams)12 BadLocationException (org.springframework.ide.vscode.commons.util.BadLocationException)12 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)9 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)9 Command (org.eclipse.lsp4j.Command)9 CompletionList (org.eclipse.lsp4j.CompletionList)9 RenameParams (org.eclipse.lsp4j.RenameParams)9 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)8