Search in sources :

Example 66 with Range

use of org.eclipse.lsp4j.Range in project xtext-core by eclipse.

the class ContentAssistService method toCompletionItem.

protected CompletionItem toCompletionItem(ContentAssistEntry entry, int caretOffset, Position caretPosition, Document document) {
    CompletionItem completionItem = new CompletionItem();
    String label = entry.getLabel();
    if (label == null) {
        label = entry.getProposal();
    }
    completionItem.setLabel(label);
    completionItem.setDetail(entry.getDescription());
    completionItem.setDocumentation(entry.getDocumentation());
    String prefix = entry.getPrefix();
    if (prefix == null) {
        prefix = "";
    }
    int prefixOffset = caretOffset - prefix.length();
    Position prefixPosition = document.getPosition(prefixOffset);
    completionItem.setTextEdit(Either.forLeft(new TextEdit(new Range(prefixPosition, caretPosition), entry.getProposal())));
    completionItem.setKind(translateKind(entry));
    if (!entry.getTextReplacements().isEmpty()) {
        if (completionItem.getAdditionalTextEdits() == null) {
            completionItem.setAdditionalTextEdits(new ArrayList<>(entry.getTextReplacements().size()));
        }
        entry.getTextReplacements().forEach((ReplaceRegion it) -> completionItem.getAdditionalTextEdits().add(toTextEdit(it, document)));
    }
    if (ContentAssistEntry.KIND_SNIPPET.equals(entry.getKind())) {
        completionItem.setInsertTextFormat(InsertTextFormat.Snippet);
    }
    return completionItem;
}
Also used : CompletionItem(org.eclipse.lsp4j.CompletionItem) Position(org.eclipse.lsp4j.Position) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) TextEdit(org.eclipse.lsp4j.TextEdit) Range(org.eclipse.lsp4j.Range)

Example 67 with Range

use of org.eclipse.lsp4j.Range in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method renameDefinition.

private WorkspaceEdit renameDefinition(IDefinition definition, String newName) {
    if (definition == null) {
        if (languageClient != null) {
            MessageParams message = new MessageParams();
            message.setType(MessageType.Info);
            message.setMessage("You cannot rename this element.");
            languageClient.showMessage(message);
        }
        return new WorkspaceEdit(new HashMap<>());
    }
    WorkspaceEdit result = new WorkspaceEdit();
    Map<String, List<TextEdit>> changes = new HashMap<>();
    result.setChanges(changes);
    if (definition.getContainingFilePath().endsWith(SWC_EXTENSION)) {
        if (languageClient != null) {
            MessageParams message = new MessageParams();
            message.setType(MessageType.Info);
            message.setMessage("You cannot rename an element defined in a SWC file.");
            languageClient.showMessage(message);
        }
        return result;
    }
    if (definition instanceof IPackageDefinition) {
        if (languageClient != null) {
            MessageParams message = new MessageParams();
            message.setType(MessageType.Info);
            message.setMessage("You cannot rename a package.");
            languageClient.showMessage(message);
        }
        return result;
    }
    IDefinition parentDefinition = definition.getParent();
    if (parentDefinition != null && parentDefinition instanceof IPackageDefinition) {
        if (languageClient != null) {
            MessageParams message = new MessageParams();
            message.setType(MessageType.Info);
            message.setMessage("You cannot rename this element.");
            languageClient.showMessage(message);
        }
        return result;
    }
    for (ICompilationUnit compilationUnit : compilationUnits) {
        if (compilationUnit == null || compilationUnit instanceof SWCCompilationUnit) {
            continue;
        }
        ArrayList<TextEdit> textEdits = new ArrayList<>();
        if (compilationUnit.getAbsoluteFilename().endsWith(MXML_EXTENSION)) {
            IMXMLDataManager mxmlDataManager = currentWorkspace.getMXMLDataManager();
            MXMLData mxmlData = (MXMLData) mxmlDataManager.get(fileSpecGetter.getFileSpecification(compilationUnit.getAbsoluteFilename()));
            IMXMLTagData rootTag = mxmlData.getRootTag();
            if (rootTag != null) {
                ArrayList<ISourceLocation> units = new ArrayList<>();
                findMXMLUnits(mxmlData.getRootTag(), definition, units);
                for (ISourceLocation otherUnit : units) {
                    TextEdit textEdit = new TextEdit();
                    textEdit.setNewText(newName);
                    Range range = LanguageServerUtils.getRangeFromSourceLocation(otherUnit);
                    if (range == null) {
                        continue;
                    }
                    textEdit.setRange(range);
                    textEdits.add(textEdit);
                }
            }
        }
        IASNode ast = null;
        try {
            ast = compilationUnit.getSyntaxTreeRequest().get().getAST();
        } catch (Exception e) {
        //safe to ignore
        }
        if (ast != null) {
            ArrayList<IIdentifierNode> identifiers = new ArrayList<>();
            findIdentifiers(ast, definition, identifiers);
            for (IIdentifierNode identifierNode : identifiers) {
                TextEdit textEdit = new TextEdit();
                textEdit.setNewText(newName);
                Range range = LanguageServerUtils.getRangeFromSourceLocation(identifierNode);
                if (range == null) {
                    continue;
                }
                textEdit.setRange(range);
                textEdits.add(textEdit);
            }
        }
        if (textEdits.size() == 0) {
            continue;
        }
        URI uri = Paths.get(compilationUnit.getAbsoluteFilename()).toUri();
        changes.put(uri.toString(), textEdits);
    }
    return result;
}
Also used : HashMap(java.util.HashMap) IPackageDefinition(org.apache.flex.compiler.definitions.IPackageDefinition) MXMLData(org.apache.flex.compiler.internal.mxml.MXMLData) ArrayList(java.util.ArrayList) URI(java.net.URI) SWCCompilationUnit(org.apache.flex.compiler.internal.units.SWCCompilationUnit) IASNode(org.apache.flex.compiler.tree.as.IASNode) ArrayList(java.util.ArrayList) List(java.util.List) CompletionList(org.eclipse.lsp4j.CompletionList) ICompilationUnit(org.apache.flex.compiler.units.ICompilationUnit) MessageParams(org.eclipse.lsp4j.MessageParams) IIdentifierNode(org.apache.flex.compiler.tree.as.IIdentifierNode) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) Range(org.eclipse.lsp4j.Range) IMXMLTagData(org.apache.flex.compiler.mxml.IMXMLTagData) IDefinition(org.apache.flex.compiler.definitions.IDefinition) ConcurrentModificationException(java.util.ConcurrentModificationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) IMXMLDataManager(org.apache.flex.compiler.mxml.IMXMLDataManager) TextEdit(org.eclipse.lsp4j.TextEdit) ISourceLocation(org.apache.flex.compiler.common.ISourceLocation)

Example 68 with Range

use of org.eclipse.lsp4j.Range in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method definitionToSymbol.

private SymbolInformation definitionToSymbol(IDefinition definition) {
    SymbolInformation symbol = new SymbolInformation();
    if (definition instanceof IClassDefinition) {
        symbol.setKind(SymbolKind.Class);
    } else if (definition instanceof IInterfaceDefinition) {
        symbol.setKind(SymbolKind.Interface);
    } else if (definition instanceof IFunctionDefinition) {
        IFunctionDefinition functionDefinition = (IFunctionDefinition) definition;
        if (functionDefinition.isConstructor()) {
            symbol.setKind(SymbolKind.Constructor);
        } else {
            symbol.setKind(SymbolKind.Function);
        }
    } else if (definition instanceof IFunctionDefinition) {
        symbol.setKind(SymbolKind.Function);
    } else if (definition instanceof IConstantDefinition) {
        symbol.setKind(SymbolKind.Constant);
    } else {
        symbol.setKind(SymbolKind.Variable);
    }
    symbol.setName(definition.getQualifiedName());
    Location location = new Location();
    String sourcePath = definition.getSourcePath();
    if (sourcePath == null) {
        //I'm not sure why getSourcePath() can sometimes return null, but
        //getContainingFilePath() seems to work as a fallback -JT
        sourcePath = definition.getContainingFilePath();
    }
    Path definitionPath = Paths.get(sourcePath);
    location.setUri(definitionPath.toUri().toString());
    Position start = new Position();
    Position end = new Position();
    //getLine() and getColumn() may include things like metadata, so it
    //makes more sense to jump to where the definition name starts
    int line = definition.getNameLine();
    int column = definition.getNameColumn();
    if (line < 0 || column < 0) {
        //this is not ideal, but MXML variable definitions may not have a
        //node associated with them, so we need to figure this out from the
        //offset instead of a pre-calculated line and column -JT
        String code = sourceByPath.get(Paths.get(sourcePath));
        offsetToLineAndCharacter(new StringReader(code), definition.getNameStart(), start);
        end.setLine(start.getLine());
        end.setCharacter(start.getCharacter());
    } else {
        start.setLine(line);
        start.setCharacter(column);
        end.setLine(line);
        end.setCharacter(column);
    }
    Range range = new Range();
    range.setStart(start);
    range.setEnd(end);
    location.setRange(range);
    symbol.setLocation(location);
    return symbol;
}
Also used : Path(java.nio.file.Path) IClassDefinition(org.apache.flex.compiler.definitions.IClassDefinition) IInterfaceDefinition(org.apache.flex.compiler.definitions.IInterfaceDefinition) IFunctionDefinition(org.apache.flex.compiler.definitions.IFunctionDefinition) Position(org.eclipse.lsp4j.Position) StringReader(java.io.StringReader) Range(org.eclipse.lsp4j.Range) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) IConstantDefinition(org.apache.flex.compiler.definitions.IConstantDefinition) Location(org.eclipse.lsp4j.Location) ISourceLocation(org.apache.flex.compiler.common.ISourceLocation)

Example 69 with Range

use of org.eclipse.lsp4j.Range in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method createDiagnosticWithoutRange.

private Diagnostic createDiagnosticWithoutRange() {
    Diagnostic diagnostic = new Diagnostic();
    Range range = new Range();
    range.setStart(new Position());
    range.setEnd(new Position());
    diagnostic.setRange(range);
    return diagnostic;
}
Also used : Position(org.eclipse.lsp4j.Position) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range)

Example 70 with Range

use of org.eclipse.lsp4j.Range in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method resolveDefinition.

private void resolveDefinition(IDefinition definition, List<Location> result) {
    String definitionPath = definition.getSourcePath();
    if (definitionPath == null) {
        //if the definition is in an MXML file, getSourcePath() may return
        //null, but getContainingFilePath() will return something
        definitionPath = definition.getContainingFilePath();
        if (definitionPath == null) {
            //if everything is null, there's nothing to do
            return;
        }
        //however, getContainingFilePath() also works for SWCs
        if (!definitionPath.endsWith(AS_EXTENSION) && !definitionPath.endsWith(MXML_EXTENSION) && (definitionPath.contains(SDK_LIBRARY_PATH_SIGNATURE_UNIX) || definitionPath.contains(SDK_LIBRARY_PATH_SIGNATURE_WINDOWS))) {
            //if it's a framework SWC, we're going to attempt to resolve
            //the source file 
            ICompilationUnit unit = currentProject.getScope().getCompilationUnitForDefinition(definition);
            try {
                byte[] abcBytes = unit.getABCBytesRequest().get().getABCBytes();
                ABCParser parser = new ABCParser(abcBytes);
                PoolingABCVisitor visitor = new PoolingABCVisitor();
                parser.parseABC(visitor);
                Pool<String> pooledStrings = visitor.getStringPool();
                for (String pooledString : pooledStrings.getValues()) {
                    if (pooledString.contains(SDK_SOURCE_PATH_SIGNATURE_UNIX) || pooledString.contains(SDK_SOURCE_PATH_SIGNATURE_WINDOWS)) {
                        //just go with the first one that we find
                        definitionPath = transformDebugFilePath(pooledString);
                        break;
                    }
                }
            } catch (InterruptedException e) {
            //safe to ignore
            }
        }
        if (!definitionPath.endsWith(AS_EXTENSION) && !definitionPath.endsWith(MXML_EXTENSION)) {
            //if it's in a SWC or something, we don't know how to resolve
            return;
        }
    }
    Path resolvedPath = Paths.get(definitionPath);
    Location location = new Location();
    location.setUri(resolvedPath.toUri().toString());
    int nameLine = definition.getNameLine();
    int nameColumn = definition.getNameColumn();
    if (nameLine == -1 || nameColumn == -1) {
        //getNameLine() and getNameColumn() will both return -1 for a
        //variable definition created by an MXML tag with an id.
        //so we need to figure them out from the offset instead.
        int nameOffset = definition.getNameStart();
        if (nameOffset == -1) {
            //we can't find the name, so give up
            return;
        }
        Reader reader = getReaderForPath(resolvedPath);
        if (reader == null) {
            //we can't get the code at all
            return;
        }
        Position position = new Position();
        offsetToLineAndCharacter(reader, nameOffset, position);
        nameLine = position.getLine();
        nameColumn = position.getCharacter();
    }
    if (nameLine == -1 || nameColumn == -1) {
        //we can't find the name, so give up
        return;
    }
    Position start = new Position();
    start.setLine(nameLine);
    start.setCharacter(nameColumn);
    Position end = new Position();
    end.setLine(nameLine);
    end.setCharacter(nameColumn + definition.getNameEnd() - definition.getNameStart());
    Range range = new Range();
    range.setStart(start);
    range.setEnd(end);
    location.setRange(range);
    result.add(location);
}
Also used : Path(java.nio.file.Path) ICompilationUnit(org.apache.flex.compiler.units.ICompilationUnit) Position(org.eclipse.lsp4j.Position) Reader(java.io.Reader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Range(org.eclipse.lsp4j.Range) PoolingABCVisitor(org.apache.flex.abc.PoolingABCVisitor) ABCParser(org.apache.flex.abc.ABCParser) Location(org.eclipse.lsp4j.Location) ISourceLocation(org.apache.flex.compiler.common.ISourceLocation)

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