Search in sources :

Example 1 with Location

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

the class ActionScriptTextDocumentService method mxmlReferences.

private CompletableFuture<List<? extends Location>> mxmlReferences(ReferenceParams params, IMXMLTagData offsetTag) {
    IDefinition definition = getDefinitionForMXMLNameAtOffset(offsetTag, currentOffset);
    if (definition != null) {
        if (isInsideTagPrefix(offsetTag, currentOffset)) {
            //ignore the tag's prefix
            return CompletableFuture.completedFuture(Collections.emptyList());
        }
        ArrayList<Location> result = new ArrayList<>();
        referencesForDefinition(definition, result);
        return CompletableFuture.completedFuture(result);
    }
    //finally, check if we're looking for references to a tag's id
    IMXMLTagAttributeData attributeData = getMXMLTagAttributeWithValueAtOffset(offsetTag, currentOffset);
    if (attributeData == null || !attributeData.getName().equals(IMXMLLanguageConstants.ATTRIBUTE_ID)) {
        //definition referenced at the current position.
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    Path path = LanguageServerUtils.getPathFromLanguageServerURI(params.getTextDocument().getUri());
    if (path == null) {
        //this probably shouldn't happen, but check just to be safe
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    ICompilationUnit unit = getCompilationUnit(path);
    Collection<IDefinition> definitions = null;
    try {
        definitions = unit.getFileScopeRequest().get().getExternallyVisibleDefinitions();
    } catch (Exception e) {
    //safe to ignore
    }
    if (definitions == null || definitions.size() == 0) {
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    IClassDefinition classDefinition = null;
    for (IDefinition currentDefinition : definitions) {
        if (currentDefinition instanceof IClassDefinition) {
            classDefinition = (IClassDefinition) currentDefinition;
            break;
        }
    }
    if (classDefinition == null) {
        //this probably shouldn't happen, but check just to be safe
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    IASScope scope = classDefinition.getContainedScope();
    for (IDefinition currentDefinition : scope.getAllLocalDefinitions()) {
        if (currentDefinition.getBaseName().equals(attributeData.getRawValue())) {
            definition = currentDefinition;
            break;
        }
    }
    if (definition == null) {
        //definition referenced at the current position.
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    ArrayList<Location> result = new ArrayList<>();
    referencesForDefinition(definition, result);
    return CompletableFuture.completedFuture(result);
}
Also used : Path(java.nio.file.Path) ICompilationUnit(org.apache.flex.compiler.units.ICompilationUnit) IClassDefinition(org.apache.flex.compiler.definitions.IClassDefinition) IASScope(org.apache.flex.compiler.scopes.IASScope) ArrayList(java.util.ArrayList) IDefinition(org.apache.flex.compiler.definitions.IDefinition) ConcurrentModificationException(java.util.ConcurrentModificationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Location(org.eclipse.lsp4j.Location) ISourceLocation(org.apache.flex.compiler.common.ISourceLocation) IMXMLTagAttributeData(org.apache.flex.compiler.mxml.IMXMLTagAttributeData)

Example 2 with Location

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

the class ActionScriptTextDocumentService method referencesForDefinition.

private void referencesForDefinition(IDefinition definition, List<Location> result) {
    for (ICompilationUnit compilationUnit : compilationUnits) {
        if (compilationUnit == null || compilationUnit instanceof SWCCompilationUnit) {
            continue;
        }
        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) {
                    Location location = LanguageServerUtils.getLocationFromSourceLocation(otherUnit);
                    if (location == null) {
                        continue;
                    }
                    result.add(location);
                }
            }
        }
        IASNode ast;
        try {
            ast = compilationUnit.getSyntaxTreeRequest().get().getAST();
        } catch (Exception e) {
            continue;
        }
        ArrayList<IIdentifierNode> identifiers = new ArrayList<>();
        findIdentifiers(ast, definition, identifiers);
        for (IIdentifierNode otherNode : identifiers) {
            Location location = LanguageServerUtils.getLocationFromSourceLocation(otherNode);
            if (location == null) {
                continue;
            }
            result.add(location);
        }
    }
}
Also used : ICompilationUnit(org.apache.flex.compiler.units.ICompilationUnit) MXMLData(org.apache.flex.compiler.internal.mxml.MXMLData) ArrayList(java.util.ArrayList) IIdentifierNode(org.apache.flex.compiler.tree.as.IIdentifierNode) IMXMLTagData(org.apache.flex.compiler.mxml.IMXMLTagData) ConcurrentModificationException(java.util.ConcurrentModificationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SWCCompilationUnit(org.apache.flex.compiler.internal.units.SWCCompilationUnit) IMXMLDataManager(org.apache.flex.compiler.mxml.IMXMLDataManager) IASNode(org.apache.flex.compiler.tree.as.IASNode) ISourceLocation(org.apache.flex.compiler.common.ISourceLocation) Location(org.eclipse.lsp4j.Location) ISourceLocation(org.apache.flex.compiler.common.ISourceLocation)

Example 3 with Location

use of org.eclipse.lsp4j.Location 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 4 with Location

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

the class ActionScriptTextDocumentService method actionScriptDefinitionWithNode.

private CompletableFuture<List<? extends Location>> actionScriptDefinitionWithNode(TextDocumentPositionParams position, IASNode offsetNode) {
    if (offsetNode == null) {
        //we couldn't find a node at the specified location
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    IDefinition definition = null;
    if (offsetNode instanceof IIdentifierNode) {
        IIdentifierNode expressionNode = (IIdentifierNode) offsetNode;
        definition = expressionNode.resolve(currentProject);
    }
    if (definition == null) {
        //definition referenced at the current position.
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    List<Location> result = new ArrayList<>();
    resolveDefinition(definition, result);
    return CompletableFuture.completedFuture(result);
}
Also used : ArrayList(java.util.ArrayList) IIdentifierNode(org.apache.flex.compiler.tree.as.IIdentifierNode) IDefinition(org.apache.flex.compiler.definitions.IDefinition) Location(org.eclipse.lsp4j.Location) ISourceLocation(org.apache.flex.compiler.common.ISourceLocation)

Example 5 with Location

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

the class ActionScriptTextDocumentService method actionScriptReferencesWithNode.

private CompletableFuture<List<? extends Location>> actionScriptReferencesWithNode(ReferenceParams params, IASNode offsetNode) {
    if (offsetNode == null) {
        //we couldn't find a node at the specified location
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    if (offsetNode instanceof IIdentifierNode) {
        IIdentifierNode identifierNode = (IIdentifierNode) offsetNode;
        IDefinition resolved = identifierNode.resolve(currentProject);
        if (resolved == null) {
            return CompletableFuture.completedFuture(Collections.emptyList());
        }
        List<Location> result = new ArrayList<>();
        referencesForDefinition(resolved, result);
        return CompletableFuture.completedFuture(result);
    }
    //definition referenced at the current position.
    return CompletableFuture.completedFuture(Collections.emptyList());
}
Also used : ArrayList(java.util.ArrayList) IIdentifierNode(org.apache.flex.compiler.tree.as.IIdentifierNode) IDefinition(org.apache.flex.compiler.definitions.IDefinition) Location(org.eclipse.lsp4j.Location) ISourceLocation(org.apache.flex.compiler.common.ISourceLocation)

Aggregations

ISourceLocation (org.apache.flex.compiler.common.ISourceLocation)8 Location (org.eclipse.lsp4j.Location)8 ArrayList (java.util.ArrayList)5 Path (java.nio.file.Path)4 IDefinition (org.apache.flex.compiler.definitions.IDefinition)4 IIdentifierNode (org.apache.flex.compiler.tree.as.IIdentifierNode)3 ICompilationUnit (org.apache.flex.compiler.units.ICompilationUnit)3 Range (org.eclipse.lsp4j.Range)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 ConcurrentModificationException (java.util.ConcurrentModificationException)2 IClassDefinition (org.apache.flex.compiler.definitions.IClassDefinition)2 Position (org.eclipse.lsp4j.Position)2 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 Reader (java.io.Reader)1 ABCParser (org.apache.flex.abc.ABCParser)1 PoolingABCVisitor (org.apache.flex.abc.PoolingABCVisitor)1 IConstantDefinition (org.apache.flex.compiler.definitions.IConstantDefinition)1