Search in sources :

Example 1 with ICompilationUnit

use of org.apache.flex.compiler.units.ICompilationUnit 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 ICompilationUnit

use of org.apache.flex.compiler.units.ICompilationUnit in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method getOffsetNode.

private IASNode getOffsetNode(TextDocumentIdentifier textDocument, Position position) {
    currentOffset = -1;
    if (!textDocument.getUri().endsWith(MXML_EXTENSION)) {
        //if we're in an <fx:Script> element, these will have been
        //previously calculated, so don't clear them
        importStartIndex = -1;
        importEndIndex = -1;
    }
    Path path = LanguageServerUtils.getPathFromLanguageServerURI(textDocument.getUri());
    if (path == null) {
        return null;
    }
    if (!isInWorkspaceOrSourcePath(path)) {
        //the path must be in the workspace or source-path
        return null;
    }
    String code;
    if (sourceByPath.containsKey(path)) {
        code = sourceByPath.get(path);
    } else {
        System.err.println("Could not find source " + path.toAbsolutePath().toString());
        System.err.println(sourceByPath.keySet().size());
        return null;
    }
    ICompilationUnit unit = getCompilationUnit(path);
    if (unit == null) {
        //should have been logged already
        return null;
    }
    IASNode ast = null;
    try {
        ast = unit.getSyntaxTreeRequest().get().getAST();
    } catch (InterruptedException e) {
        System.err.println("Interrupted while getting AST: " + path.toAbsolutePath().toString());
        return null;
    }
    if (ast == null) {
        //we couldn't find the root node for this file
        System.err.println("Could not find AST: " + path.toAbsolutePath().toString());
        return null;
    }
    currentOffset = lineAndCharacterToOffset(new StringReader(code), position.getLine(), position.getCharacter());
    if (currentOffset == -1) {
        System.err.println("Could not find code at position " + position.getLine() + ":" + position.getCharacter() + " in file " + path.toAbsolutePath().toString());
        return null;
    }
    IASNode offsetNode = getContainingNodeIncludingStart(ast, currentOffset);
    if (offsetNode != null) {
        //if we have an offset node, try to find where imports may be added
        IPackageNode packageNode = (IPackageNode) offsetNode.getAncestorOfType(IPackageNode.class);
        if (packageNode == null) {
            IFileNode fileNode = (IFileNode) offsetNode.getAncestorOfType(IFileNode.class);
            if (fileNode != null) {
                boolean foundPackage = false;
                for (int i = 0; i < fileNode.getChildCount(); i++) {
                    IASNode childNode = fileNode.getChild(i);
                    if (foundPackage) {
                        //this is the node following the package
                        importStartIndex = childNode.getAbsoluteStart();
                        break;
                    }
                    if (childNode instanceof IPackageNode) {
                        //use the start of the the next node after the
                        //package as the place where the import can be added
                        foundPackage = true;
                    }
                }
            }
        } else {
            importEndIndex = packageNode.getAbsoluteEnd();
        }
    }
    return offsetNode;
}
Also used : Path(java.nio.file.Path) ICompilationUnit(org.apache.flex.compiler.units.ICompilationUnit) IPackageNode(org.apache.flex.compiler.tree.as.IPackageNode) IFileNode(org.apache.flex.compiler.tree.as.IFileNode) IASNode(org.apache.flex.compiler.tree.as.IASNode) StringReader(java.io.StringReader)

Example 3 with ICompilationUnit

use of org.apache.flex.compiler.units.ICompilationUnit in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method autoCompleteImport.

private void autoCompleteImport(String importName, CompletionList result) {
    List<CompletionItem> items = result.getItems();
    for (ICompilationUnit unit : compilationUnits) {
        if (unit == null) {
            continue;
        }
        Collection<IDefinition> definitions = null;
        try {
            definitions = unit.getFileScopeRequest().get().getExternallyVisibleDefinitions();
        } catch (Exception e) {
            //safe to ignore
            continue;
        }
        for (IDefinition definition : definitions) {
            if (definition instanceof ITypeDefinition) {
                String qualifiedName = definition.getQualifiedName();
                if (qualifiedName.equals(definition.getBaseName())) {
                    //this definition is top-level. no import required.
                    continue;
                }
                if (qualifiedName.startsWith(importName)) {
                    int index = importName.lastIndexOf(".");
                    if (index != -1) {
                        qualifiedName = qualifiedName.substring(index + 1);
                    }
                    index = qualifiedName.indexOf(".");
                    if (index > 0) {
                        qualifiedName = qualifiedName.substring(0, index);
                    }
                    CompletionItem item = new CompletionItem();
                    item.setLabel(qualifiedName);
                    if (definition.getBaseName().equals(qualifiedName)) {
                        ITypeDefinition typeDefinition = (ITypeDefinition) definition;
                        if (typeDefinition instanceof IInterfaceDefinition) {
                            item.setKind(CompletionItemKind.Interface);
                        } else if (typeDefinition instanceof IClassDefinition) {
                            item.setKind(CompletionItemKind.Class);
                        } else {
                            item.setKind(CompletionItemKind.Text);
                        }
                    } else {
                        item.setKind(CompletionItemKind.Text);
                    }
                    if (!items.contains(item)) {
                        items.add(item);
                    }
                }
            }
        }
    }
}
Also used : ICompilationUnit(org.apache.flex.compiler.units.ICompilationUnit) IInterfaceDefinition(org.apache.flex.compiler.definitions.IInterfaceDefinition) IClassDefinition(org.apache.flex.compiler.definitions.IClassDefinition) CompletionItem(org.eclipse.lsp4j.CompletionItem) ITypeDefinition(org.apache.flex.compiler.definitions.ITypeDefinition) IDefinition(org.apache.flex.compiler.definitions.IDefinition) ConcurrentModificationException(java.util.ConcurrentModificationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 4 with ICompilationUnit

use of org.apache.flex.compiler.units.ICompilationUnit in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method getOffsetMXMLTag.

private IMXMLTagData getOffsetMXMLTag(TextDocumentIdentifier textDocument, Position position) {
    namespaceStartIndex = -1;
    namespaceEndIndex = -1;
    String uri = textDocument.getUri();
    if (!uri.endsWith(MXML_EXTENSION)) {
        // don't try to parse ActionScript files as MXML
        return null;
    }
    currentOffset = -1;
    importStartIndex = -1;
    importEndIndex = -1;
    Path path = LanguageServerUtils.getPathFromLanguageServerURI(uri);
    if (path == null) {
        return null;
    }
    if (!isInWorkspaceOrSourcePath(path)) {
        //the path must be in the workspace or source-path
        return null;
    }
    String code;
    if (sourceByPath.containsKey(path)) {
        code = sourceByPath.get(path);
    } else {
        System.err.println("Could not find source " + path.toAbsolutePath().toString());
        System.err.println(sourceByPath.keySet().size());
        return null;
    }
    //need to ensure that the compilation unit exists, even though we don't
    //use it directly
    ICompilationUnit unit = getCompilationUnit(path);
    if (unit == null) {
        //should have been logged already
        return null;
    }
    IMXMLDataManager mxmlDataManager = currentWorkspace.getMXMLDataManager();
    MXMLData mxmlData = (MXMLData) mxmlDataManager.get(fileSpecGetter.getFileSpecification(path.toAbsolutePath().toString()));
    if (mxmlData == null) {
        return null;
    }
    currentOffset = lineAndCharacterToOffset(new StringReader(code), position.getLine(), position.getCharacter());
    if (currentOffset == -1) {
        System.err.println("Could not find code at position " + position.getLine() + ":" + position.getCharacter() + " in file " + path.toAbsolutePath().toString());
        return null;
    }
    //calculate the location for automatically generated xmlns tags
    IMXMLTagData rootTag = mxmlData.getRootTag();
    IMXMLTagAttributeData[] attributeDatas = rootTag.getAttributeDatas();
    for (IMXMLTagAttributeData attributeData : attributeDatas) {
        if (!attributeData.getName().startsWith("xmlns")) {
            if (namespaceStartIndex == -1) {
                namespaceStartIndex = attributeData.getStart();
                namespaceEndIndex = namespaceStartIndex;
            }
            break;
        }
        int start = attributeData.getAbsoluteStart();
        int end = attributeData.getValueEnd() + 1;
        if (brokenMXMLValueEnd) {
            end--;
        }
        if (namespaceStartIndex == -1 || namespaceStartIndex > start) {
            namespaceStartIndex = start;
        }
        if (namespaceEndIndex == -1 || namespaceEndIndex < end) {
            namespaceEndIndex = end;
        }
    }
    IMXMLUnitData unitData = mxmlData.findContainmentReferenceUnit(currentOffset);
    IMXMLUnitData currentUnitData = unitData;
    while (currentUnitData != null) {
        if (currentUnitData instanceof IMXMLTagData) {
            IMXMLTagData tagData = (IMXMLTagData) currentUnitData;
            if (tagData.getXMLName().equals(tagData.getMXMLDialect().resolveScript()) && unitData instanceof IMXMLTextData) {
                IMXMLTextData textUnitData = (IMXMLTextData) unitData;
                if (textUnitData.getTextType() == IMXMLTextData.TextType.CDATA) {
                    importStartIndex = textUnitData.getCompilableTextStart();
                    importEndIndex = textUnitData.getCompilableTextEnd();
                }
            }
            return tagData;
        }
        currentUnitData = currentUnitData.getParentUnitData();
    }
    return null;
}
Also used : Path(java.nio.file.Path) ICompilationUnit(org.apache.flex.compiler.units.ICompilationUnit) IMXMLDataManager(org.apache.flex.compiler.mxml.IMXMLDataManager) IMXMLUnitData(org.apache.flex.compiler.mxml.IMXMLUnitData) MXMLData(org.apache.flex.compiler.internal.mxml.MXMLData) StringReader(java.io.StringReader) IMXMLTextData(org.apache.flex.compiler.mxml.IMXMLTextData) IMXMLTagData(org.apache.flex.compiler.mxml.IMXMLTagData) IMXMLTagAttributeData(org.apache.flex.compiler.mxml.IMXMLTagAttributeData)

Example 5 with ICompilationUnit

use of org.apache.flex.compiler.units.ICompilationUnit in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method autoCompleteDefinitions.

private void autoCompleteDefinitions(CompletionList result, boolean forMXML, boolean typesOnly, String packageName, IDefinition definitionToSkip) {
    String skipQualifiedName = null;
    if (definitionToSkip != null) {
        skipQualifiedName = definitionToSkip.getQualifiedName();
    }
    for (ICompilationUnit unit : compilationUnits) {
        if (unit == null) {
            continue;
        }
        Collection<IDefinition> definitions = null;
        try {
            definitions = unit.getFileScopeRequest().get().getExternallyVisibleDefinitions();
        } catch (Exception e) {
            //safe to ignore
            continue;
        }
        for (IDefinition definition : definitions) {
            boolean isType = definition instanceof ITypeDefinition;
            if (!typesOnly || isType) {
                if (packageName == null || definition.getPackageName().equals(packageName)) {
                    if (skipQualifiedName != null && skipQualifiedName.equals(definition.getQualifiedName())) {
                        continue;
                    }
                    if (isType) {
                        IMetaTag excludeClassMetaTag = definition.getMetaTagByName(IMetaAttributeConstants.ATTRIBUTE_EXCLUDECLASS);
                        if (excludeClassMetaTag != null) {
                            //skip types with [ExcludeClass] metadata
                            continue;
                        }
                    }
                    if (forMXML && isType) {
                        ITypeDefinition typeDefinition = (ITypeDefinition) definition;
                        addMXMLTypeDefinitionAutoComplete(typeDefinition, result);
                    } else {
                        addDefinitionAutoCompleteActionScript(definition, result);
                    }
                }
            }
        }
    }
    if (packageName == null || packageName.equals("")) {
        CompletionItem item = new CompletionItem();
        item.setKind(CompletionItemKind.Class);
        item.setLabel(IASKeywordConstants.VOID);
        result.getItems().add(item);
    }
}
Also used : ICompilationUnit(org.apache.flex.compiler.units.ICompilationUnit) IMetaTag(org.apache.flex.compiler.definitions.metadata.IMetaTag) ITypeDefinition(org.apache.flex.compiler.definitions.ITypeDefinition) CompletionItem(org.eclipse.lsp4j.CompletionItem) IDefinition(org.apache.flex.compiler.definitions.IDefinition) ConcurrentModificationException(java.util.ConcurrentModificationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

ICompilationUnit (org.apache.flex.compiler.units.ICompilationUnit)15 FileNotFoundException (java.io.FileNotFoundException)11 IOException (java.io.IOException)11 ConcurrentModificationException (java.util.ConcurrentModificationException)11 ArrayList (java.util.ArrayList)6 IDefinition (org.apache.flex.compiler.definitions.IDefinition)6 Path (java.nio.file.Path)5 IASNode (org.apache.flex.compiler.tree.as.IASNode)5 ISourceLocation (org.apache.flex.compiler.common.ISourceLocation)4 ITypeDefinition (org.apache.flex.compiler.definitions.ITypeDefinition)4 MXMLData (org.apache.flex.compiler.internal.mxml.MXMLData)4 SWCCompilationUnit (org.apache.flex.compiler.internal.units.SWCCompilationUnit)4 IMXMLDataManager (org.apache.flex.compiler.mxml.IMXMLDataManager)4 IASScope (org.apache.flex.compiler.scopes.IASScope)4 StringReader (java.io.StringReader)3 IMXMLTagData (org.apache.flex.compiler.mxml.IMXMLTagData)3 Location (org.eclipse.lsp4j.Location)3 URI (java.net.URI)2 HashMap (java.util.HashMap)2 IClassDefinition (org.apache.flex.compiler.definitions.IClassDefinition)2