Search in sources :

Example 1 with IMXMLDataManager

use of org.apache.flex.compiler.mxml.IMXMLDataManager 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 2 with IMXMLDataManager

use of org.apache.flex.compiler.mxml.IMXMLDataManager 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 3 with IMXMLDataManager

use of org.apache.flex.compiler.mxml.IMXMLDataManager in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method addMXMLTypeDefinitionAutoComplete.

private void addMXMLTypeDefinitionAutoComplete(ITypeDefinition definition, CompletionList result) {
    IMXMLDataManager mxmlDataManager = currentWorkspace.getMXMLDataManager();
    MXMLData mxmlData = (MXMLData) mxmlDataManager.get(fileSpecGetter.getFileSpecification(currentUnit.getAbsoluteFilename()));
    MXMLNamespace discoveredNS = getMXMLNamespaceForTypeDefinition(definition, mxmlData);
    addDefinitionAutoCompleteMXML(definition, discoveredNS.prefix, discoveredNS.uri, result);
}
Also used : IMXMLDataManager(org.apache.flex.compiler.mxml.IMXMLDataManager) MXMLData(org.apache.flex.compiler.internal.mxml.MXMLData)

Example 4 with IMXMLDataManager

use of org.apache.flex.compiler.mxml.IMXMLDataManager 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 5 with IMXMLDataManager

use of org.apache.flex.compiler.mxml.IMXMLDataManager in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method autoCompleteTypesForMXMLFromExistingTag.

/**
     * Using an existing tag, that may already have a prefix or short name,
     * populate the completion list.
     */
private void autoCompleteTypesForMXMLFromExistingTag(CompletionList result, IMXMLTagData offsetTag) {
    IMXMLDataManager mxmlDataManager = currentWorkspace.getMXMLDataManager();
    MXMLData mxmlData = (MXMLData) mxmlDataManager.get(fileSpecGetter.getFileSpecification(currentUnit.getAbsoluteFilename()));
    String tagStartShortName = offsetTag.getShortName();
    String tagPrefix = offsetTag.getPrefix();
    PrefixMap prefixMap = mxmlData.getRootTagPrefixMap();
    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)) {
                continue;
            }
            ITypeDefinition typeDefinition = (ITypeDefinition) definition;
            //or that the definition's base name matches the short name 
            if (tagStartShortName.length() == 0 || typeDefinition.getBaseName().startsWith(tagStartShortName)) {
                //in a namespace with that prefix
                if (tagPrefix.length() > 0) {
                    Collection<XMLName> tagNames = currentProject.getTagNamesForClass(typeDefinition.getQualifiedName());
                    for (XMLName tagName : tagNames) {
                        String tagNamespace = tagName.getXMLNamespace();
                        //not what we're using in this file
                        if (tagNamespace.equals(IMXMLLanguageConstants.NAMESPACE_MXML_2006)) {
                            //use the language namespace of the root tag instead
                            tagNamespace = mxmlData.getRootTag().getMXMLDialect().getLanguageNamespace();
                        }
                        String[] prefixes = prefixMap.getPrefixesForNamespace(tagNamespace);
                        for (String otherPrefix : prefixes) {
                            if (tagPrefix.equals(otherPrefix)) {
                                addDefinitionAutoCompleteMXML(typeDefinition, null, null, result);
                            }
                        }
                    }
                } else {
                    //no prefix yet, so complete the definition with a prefix
                    MXMLNamespace ns = getMXMLNamespaceForTypeDefinition(typeDefinition, mxmlData);
                    addDefinitionAutoCompleteMXML(typeDefinition, ns.prefix, ns.uri, result);
                }
            }
        }
    }
}
Also used : PrefixMap(org.apache.flex.compiler.common.PrefixMap) ICompilationUnit(org.apache.flex.compiler.units.ICompilationUnit) ITypeDefinition(org.apache.flex.compiler.definitions.ITypeDefinition) MXMLData(org.apache.flex.compiler.internal.mxml.MXMLData) 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) XMLName(org.apache.flex.compiler.common.XMLName)

Aggregations

MXMLData (org.apache.flex.compiler.internal.mxml.MXMLData)5 IMXMLDataManager (org.apache.flex.compiler.mxml.IMXMLDataManager)5 ICompilationUnit (org.apache.flex.compiler.units.ICompilationUnit)4 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 ConcurrentModificationException (java.util.ConcurrentModificationException)3 IMXMLTagData (org.apache.flex.compiler.mxml.IMXMLTagData)3 ArrayList (java.util.ArrayList)2 ISourceLocation (org.apache.flex.compiler.common.ISourceLocation)2 IDefinition (org.apache.flex.compiler.definitions.IDefinition)2 SWCCompilationUnit (org.apache.flex.compiler.internal.units.SWCCompilationUnit)2 IASNode (org.apache.flex.compiler.tree.as.IASNode)2 IIdentifierNode (org.apache.flex.compiler.tree.as.IIdentifierNode)2 StringReader (java.io.StringReader)1 URI (java.net.URI)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 List (java.util.List)1 PrefixMap (org.apache.flex.compiler.common.PrefixMap)1 XMLName (org.apache.flex.compiler.common.XMLName)1