Search in sources :

Example 86 with Range

use of org.eclipse.lsp4j.Range in project eclipse.jdt.ls by eclipse.

the class NavigateToDefinitionHandler method definition.

public List<? extends Location> definition(TextDocumentPositionParams position, IProgressMonitor monitor) {
    ITypeRoot unit = JDTUtils.resolveTypeRoot(position.getTextDocument().getUri());
    Location location = null;
    if (unit != null && !monitor.isCanceled()) {
        location = computeDefinitionNavigation(unit, position.getPosition().getLine(), position.getPosition().getCharacter(), monitor);
    }
    if (location == null) {
        location = new Location();
        location.setRange(new Range());
    }
    return Arrays.asList(location);
}
Also used : ITypeRoot(org.eclipse.jdt.core.ITypeRoot) Range(org.eclipse.lsp4j.Range) Location(org.eclipse.lsp4j.Location)

Example 87 with Range

use of org.eclipse.lsp4j.Range in project eclipse.jdt.ls by eclipse.

the class WorkspaceDiagnosticsHandler method convertRange.

/**
 * @param marker
 * @return
 */
private static Range convertRange(IDocument document, IMarker marker) {
    int line = marker.getAttribute(IMarker.LINE_NUMBER, -1) - 1;
    int cStart = 0;
    int cEnd = 0;
    try {
        // Buildship doesn't provide markers for gradle files, Maven does
        if (marker.isSubtypeOf(IMavenConstants.MARKER_ID)) {
            cStart = marker.getAttribute(IMavenConstants.MARKER_COLUMN_START, -1);
            cEnd = marker.getAttribute(IMavenConstants.MARKER_COLUMN_END, -1);
        } else {
            int lineOffset = 0;
            try {
                lineOffset = document.getLineOffset(line);
            } catch (BadLocationException unlikelyException) {
                JavaLanguageServerPlugin.logException(unlikelyException.getMessage(), unlikelyException);
                return new Range(new Position(line, 0), new Position(line, 0));
            }
            cEnd = marker.getAttribute(IMarker.CHAR_END, -1) - lineOffset;
            cStart = marker.getAttribute(IMarker.CHAR_START, -1) - lineOffset;
        }
    } catch (CoreException e) {
        JavaLanguageServerPlugin.logException(e.getMessage(), e);
    }
    cStart = Math.max(0, cStart);
    cEnd = Math.max(0, cEnd);
    return new Range(new Position(line, cStart), new Position(line, cEnd));
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) Position(org.eclipse.lsp4j.Position) Range(org.eclipse.lsp4j.Range) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 88 with Range

use of org.eclipse.lsp4j.Range in project eclipse.jdt.ls by eclipse.

the class CompletionProposalReplacementProvider method updateReplacement.

/**
 * Updates the replacement and any additional replacement for the given item.
 *
 * @param proposal
 * @param item
 * @param trigger
 */
public void updateReplacement(CompletionProposal proposal, CompletionItem item, char trigger) {
    // reset importRewrite
    this.importRewrite = TypeProposalUtils.createImportRewrite(compilationUnit);
    List<org.eclipse.lsp4j.TextEdit> additionalTextEdits = new ArrayList<>();
    StringBuilder completionBuffer = new StringBuilder();
    Range range = null;
    if (isSupportingRequiredProposals(proposal)) {
        CompletionProposal[] requiredProposals = proposal.getRequiredProposals();
        if (requiredProposals != null) {
            for (CompletionProposal requiredProposal : requiredProposals) {
                switch(requiredProposal.getKind()) {
                    case CompletionProposal.TYPE_IMPORT:
                    case CompletionProposal.METHOD_IMPORT:
                    case CompletionProposal.FIELD_IMPORT:
                        appendImportProposal(completionBuffer, requiredProposal, proposal.getKind());
                        break;
                    case CompletionProposal.TYPE_REF:
                        org.eclipse.lsp4j.TextEdit edit = toRequiredTypeEdit(requiredProposal, trigger, proposal.canUseDiamond(context));
                        if (proposal.getKind() == CompletionProposal.CONSTRUCTOR_INVOCATION || proposal.getKind() == CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION || proposal.getKind() == CompletionProposal.ANONYMOUS_CLASS_DECLARATION) {
                            completionBuffer.append(edit.getNewText());
                            range = edit.getRange();
                        } else {
                            additionalTextEdits.add(edit);
                        }
                        break;
                    default:
                        /*
						 * In 3.3 we only support the above required proposals, see
						 * CompletionProposal#getRequiredProposals()
						 */
                        Assert.isTrue(false);
                }
            }
        }
    }
    if (range == null) {
        range = toReplacementRange(proposal);
    }
    if (proposal.getKind() == CompletionProposal.METHOD_DECLARATION) {
        appendMethodOverrideReplacement(completionBuffer, proposal);
    } else if (proposal.getKind() == CompletionProposal.POTENTIAL_METHOD_DECLARATION && proposal instanceof GetterSetterCompletionProposal) {
        appendMethodPotentialReplacement(completionBuffer, (GetterSetterCompletionProposal) proposal);
    } else if (proposal.getKind() == CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION || proposal.getKind() == CompletionProposal.ANONYMOUS_CLASS_DECLARATION) {
        appendAnonymousClass(completionBuffer, proposal, range);
    } else {
        appendReplacementString(completionBuffer, proposal);
    }
    // select insertTextFormat.
    if (client.isCompletionSnippetsSupported()) {
        item.setInsertTextFormat(InsertTextFormat.Snippet);
    } else {
        item.setInsertTextFormat(InsertTextFormat.PlainText);
    }
    String text = completionBuffer.toString();
    if (range != null) {
        item.setTextEdit(new org.eclipse.lsp4j.TextEdit(range, text));
    } else {
        // fallback
        item.setInsertText(text);
    }
    addImports(additionalTextEdits);
    if (!additionalTextEdits.isEmpty()) {
        item.setAdditionalTextEdits(additionalTextEdits);
    }
}
Also used : CompletionProposal(org.eclipse.jdt.core.CompletionProposal) ArrayList(java.util.ArrayList) Range(org.eclipse.lsp4j.Range) TextEdit(org.eclipse.text.edits.TextEdit)

Example 89 with Range

use of org.eclipse.lsp4j.Range in project ballerina by ballerina-lang.

the class DefinitionUtil method getDefinitionPosition.

/**
 * Get definition position for the given definition context.
 *
 * @param definitionContext   context of the definition.
 * @param lSPackageCache package context for language server.
 * @return position
 */
public static List<Location> getDefinitionPosition(TextDocumentServiceContext definitionContext, LSPackageCache lSPackageCache) {
    List<Location> contents = new ArrayList<>();
    if (definitionContext.get(NodeContextKeys.SYMBOL_KIND_OF_NODE_KEY) == null) {
        return contents;
    }
    String nodeKind = definitionContext.get(NodeContextKeys.SYMBOL_KIND_OF_NODE_KEY);
    BLangPackage bLangPackage = getPackageOfTheOwner(definitionContext.get(NodeContextKeys.NODE_OWNER_PACKAGE_KEY), definitionContext, lSPackageCache);
    BLangNode bLangNode = null;
    switch(nodeKind) {
        case ContextConstants.FUNCTION:
            bLangNode = bLangPackage.functions.stream().filter(function -> function.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            break;
        case ContextConstants.STRUCT:
            bLangNode = bLangPackage.structs.stream().filter(struct -> struct.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            break;
        case ContextConstants.OBJECT:
            bLangNode = bLangPackage.objects.stream().filter(object -> object.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            break;
        case ContextConstants.ENUM:
            bLangNode = bLangPackage.enums.stream().filter(enm -> enm.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            // Fixing the position issue with enum node.
            bLangNode.getPosition().eLine = bLangNode.getPosition().sLine;
            bLangNode.getPosition().eCol = bLangNode.getPosition().sCol;
            break;
        case ContextConstants.CONNECTOR:
            bLangNode = bLangPackage.connectors.stream().filter(bConnector -> bConnector.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            break;
        case ContextConstants.ACTION:
            bLangNode = bLangPackage.connectors.stream().filter(bConnector -> bConnector.name.getValue().equals(((BLangInvocation) definitionContext.get(NodeContextKeys.PREVIOUSLY_VISITED_NODE_KEY)).symbol.owner.name.getValue())).flatMap(connector -> connector.actions.stream()).filter(bAction -> bAction.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            break;
        case ContextConstants.TRANSFORMER:
            bLangNode = bLangPackage.transformers.stream().filter(bTransformer -> bTransformer.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            break;
        case ContextConstants.ENDPOINT:
            bLangNode = bLangPackage.globalEndpoints.stream().filter(globalEndpoint -> globalEndpoint.name.value.equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            if (bLangNode == null) {
                DefinitionTreeVisitor definitionTreeVisitor = new DefinitionTreeVisitor(definitionContext);
                definitionContext.get(NodeContextKeys.NODE_STACK_KEY).pop().accept(definitionTreeVisitor);
                if (definitionContext.get(NodeContextKeys.NODE_KEY) != null) {
                    bLangNode = definitionContext.get(NodeContextKeys.NODE_KEY);
                }
            }
            break;
        case ContextConstants.VARIABLE:
            bLangNode = bLangPackage.globalVars.stream().filter(globalVar -> globalVar.name.getValue().equals(definitionContext.get(NodeContextKeys.VAR_NAME_OF_NODE_KEY))).findAny().orElse(null);
            // BLangNode is null only when node at the cursor position is a local variable.
            if (bLangNode == null) {
                DefinitionTreeVisitor definitionTreeVisitor = new DefinitionTreeVisitor(definitionContext);
                definitionContext.get(NodeContextKeys.NODE_STACK_KEY).pop().accept(definitionTreeVisitor);
                if (definitionContext.get(NodeContextKeys.NODE_KEY) != null) {
                    bLangNode = definitionContext.get(NodeContextKeys.NODE_KEY);
                    break;
                }
            }
            break;
        default:
            break;
    }
    if (bLangNode == null) {
        return contents;
    }
    Location l = new Location();
    TextDocumentPositionParams position = definitionContext.get(DocumentServiceKeys.POSITION_KEY);
    Path parentPath = CommonUtil.getPath(new LSDocument(position.getTextDocument().getUri())).getParent();
    if (parentPath != null) {
        String fileName = bLangNode.getPosition().getSource().getCompilationUnitName();
        Path filePath = Paths.get(CommonUtil.getPackageURI(definitionContext.get(NodeContextKeys.PACKAGE_OF_NODE_KEY).name.getValue(), parentPath.toString(), definitionContext.get(NodeContextKeys.PACKAGE_OF_NODE_KEY).name.getValue()), fileName);
        l.setUri(filePath.toUri().toString());
        Range r = new Range();
        // Subtract 1 to convert the token lines and char positions to zero based indexing
        r.setStart(new Position(bLangNode.getPosition().getStartLine() - 1, bLangNode.getPosition().getStartColumn() - 1));
        r.setEnd(new Position(bLangNode.getPosition().getEndLine() - 1, bLangNode.getPosition().getEndColumn() - 1));
        l.setRange(r);
        contents.add(l);
    }
    return contents;
}
Also used : CommonUtil(org.ballerinalang.langserver.common.utils.CommonUtil) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) TextDocumentPositionParams(org.eclipse.lsp4j.TextDocumentPositionParams) PackageID(org.ballerinalang.model.elements.PackageID) Range(org.eclipse.lsp4j.Range) LSPackageCache(org.ballerinalang.langserver.LSPackageCache) NodeContextKeys(org.ballerinalang.langserver.common.constants.NodeContextKeys) ArrayList(java.util.ArrayList) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) DefinitionTreeVisitor(org.ballerinalang.langserver.definition.DefinitionTreeVisitor) LSDocument(org.ballerinalang.langserver.common.LSDocument) List(java.util.List) Paths(java.nio.file.Paths) ContextConstants(org.ballerinalang.langserver.common.constants.ContextConstants) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) Location(org.eclipse.lsp4j.Location) Position(org.eclipse.lsp4j.Position) Path(java.nio.file.Path) DocumentServiceKeys(org.ballerinalang.langserver.DocumentServiceKeys) Path(java.nio.file.Path) Position(org.eclipse.lsp4j.Position) ArrayList(java.util.ArrayList) Range(org.eclipse.lsp4j.Range) DefinitionTreeVisitor(org.ballerinalang.langserver.definition.DefinitionTreeVisitor) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) LSDocument(org.ballerinalang.langserver.common.LSDocument) TextDocumentPositionParams(org.eclipse.lsp4j.TextDocumentPositionParams) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) Location(org.eclipse.lsp4j.Location)

Example 90 with Range

use of org.eclipse.lsp4j.Range in project ballerina by ballerina-lang.

the class ReferencesTreeVisitor method getLocation.

/**
 * Get the physical source location of the given package.
 *
 * @param bLangNode          ballerina language node references are requested for
 * @param ownerPackageName   list of name compositions of the node's package name
 * @param currentPackageName list of name compositions of the current package
 * @return location of the package of the given node
 */
private Location getLocation(BLangNode bLangNode, String ownerPackageName, String currentPackageName) {
    Location l = new Location();
    Range r = new Range();
    TextDocumentPositionParams position = this.context.get(DocumentServiceKeys.POSITION_KEY);
    Path parentPath = CommonUtil.getPath(new LSDocument(position.getTextDocument().getUri())).getParent();
    if (parentPath != null) {
        String fileName = bLangNode.getPosition().getSource().getCompilationUnitName();
        Path filePath = Paths.get(CommonUtil.getPackageURI(currentPackageName, parentPath.toString(), ownerPackageName), fileName);
        l.setUri(filePath.toUri().toString());
        // Subtract 1 to convert the token lines and char positions to zero based indexing
        r.setStart(new Position(bLangNode.getPosition().getStartLine() - 1, bLangNode.getPosition().getStartColumn() - 1));
        r.setEnd(new Position(bLangNode.getPosition().getEndLine() - 1, bLangNode.getPosition().getEndColumn() - 1));
        l.setRange(r);
    }
    return l;
}
Also used : Path(java.nio.file.Path) Position(org.eclipse.lsp4j.Position) LSDocument(org.ballerinalang.langserver.common.LSDocument) TextDocumentPositionParams(org.eclipse.lsp4j.TextDocumentPositionParams) Range(org.eclipse.lsp4j.Range) Location(org.eclipse.lsp4j.Location)

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