Search in sources :

Example 31 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)

Example 32 with Range

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);
    }
}
Also used : Position(org.eclipse.lsp4j.Position) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) TextEdit(org.eclipse.lsp4j.TextEdit) ArrayList(java.util.ArrayList) List(java.util.List) 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 33 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 34 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 35 with Range

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

Aggregations

Range (org.eclipse.lsp4j.Range)293 Test (org.junit.Test)178 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)153 Position (org.eclipse.lsp4j.Position)111 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)105 AbstractSelectionTest (org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest)54 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)47 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)47 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)41 ArrayList (java.util.ArrayList)39 Command (org.eclipse.lsp4j.Command)34 List (java.util.List)33 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)31 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)30 Diagnostic (org.eclipse.lsp4j.Diagnostic)29 TextEdit (org.eclipse.lsp4j.TextEdit)29 CodeAction (org.eclipse.lsp4j.CodeAction)28 AbstractQuickFixTest (org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)27 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)25 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)21