Search in sources :

Example 6 with ICompilationUnit

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

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

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

Example 9 with ICompilationUnit

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

the class ActionScriptTextDocumentService method checkFilePathForAllProblems.

private boolean checkFilePathForAllProblems(Path path, Boolean quick) {
    ICompilationUnit mainUnit = getCompilationUnit(path);
    if (mainUnit == null) {
        return false;
    }
    CompilerProject project = (CompilerProject) mainUnit.getProject();
    Collection<ICompilerProblem> fatalProblems = project.getFatalProblems();
    if (fatalProblems == null || fatalProblems.size() == 0) {
        fatalProblems = project.getProblems();
    }
    if (fatalProblems != null && fatalProblems.size() > 0) {
        URI uri = path.toUri();
        PublishDiagnosticsParams publish = new PublishDiagnosticsParams();
        publish.setDiagnostics(new ArrayList<>());
        publish.setUri(uri.toString());
        codeProblemTracker.trackFileWithProblems(uri);
        for (ICompilerProblem problem : fatalProblems) {
            addCompilerProblem(problem, publish);
        }
        codeProblemTracker.cleanUpStaleProblems();
        if (languageClient != null) {
            languageClient.publishDiagnostics(publish);
        }
        return true;
    }
    IASNode ast = null;
    try {
        ast = mainUnit.getSyntaxTreeRequest().get().getAST();
    } catch (Exception e) {
        System.err.println("Exception during build: " + e);
        return false;
    }
    if (ast == null) {
        return false;
    }
    Map<URI, PublishDiagnosticsParams> files = new HashMap<>();
    try {
        if (quick) {
            PublishDiagnosticsParams params = checkCompilationUnitForAllProblems(mainUnit);
            URI uri = Paths.get(mainUnit.getAbsoluteFilename()).toUri();
            files.put(uri, params);
        } else {
            boolean continueCheckingForErrors = true;
            while (continueCheckingForErrors) {
                try {
                    for (ICompilationUnit unit : compilationUnits) {
                        if (unit == null || unit instanceof SWCCompilationUnit) {
                            //compiled compilation units won't have problems
                            continue;
                        }
                        PublishDiagnosticsParams params = checkCompilationUnitForAllProblems(unit);
                        URI uri = Paths.get(unit.getAbsoluteFilename()).toUri();
                        files.put(uri, params);
                    }
                    continueCheckingForErrors = false;
                } catch (ConcurrentModificationException e) {
                //when we finished building one of the compilation
                //units, more were added to the collection, so we need
                //to start over because we can't iterate over a modified
                //collection.
                }
            }
            //only clean up stale errors on a full check
            codeProblemTracker.cleanUpStaleProblems();
        }
    } catch (Exception e) {
        System.err.println("Exception during build: " + e);
        e.printStackTrace();
        return false;
    }
    if (languageClient != null) {
        files.values().forEach(languageClient::publishDiagnostics);
    }
    return true;
}
Also used : ICompilationUnit(org.apache.flex.compiler.units.ICompilationUnit) ConcurrentModificationException(java.util.ConcurrentModificationException) HashMap(java.util.HashMap) CompilerProject(org.apache.flex.compiler.internal.projects.CompilerProject) URI(java.net.URI) ConcurrentModificationException(java.util.ConcurrentModificationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ICompilerProblem(org.apache.flex.compiler.problems.ICompilerProblem) SWCCompilationUnit(org.apache.flex.compiler.internal.units.SWCCompilationUnit) IASNode(org.apache.flex.compiler.tree.as.IASNode) PublishDiagnosticsParams(org.eclipse.lsp4j.PublishDiagnosticsParams)

Example 10 with ICompilationUnit

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

the class ActionScriptTextDocumentService method createCodeActionsForImport.

private void createCodeActionsForImport(Diagnostic diagnostic, List<Command> commands) {
    String message = diagnostic.getMessage();
    int start = message.lastIndexOf(" ") + 1;
    int end = message.length() - 1;
    String typeString = message.substring(start, end);
    ArrayList<IDefinition> types = new ArrayList<>();
    for (ICompilationUnit unit : compilationUnits) {
        if (unit == null) {
            continue;
        }
        try {
            Collection<IDefinition> definitions = unit.getFileScopeRequest().get().getExternallyVisibleDefinitions();
            if (definitions == null) {
                continue;
            }
            for (IDefinition definition : definitions) {
                if (definition instanceof ITypeDefinition) {
                    ITypeDefinition typeDefinition = (ITypeDefinition) definition;
                    String baseName = typeDefinition.getBaseName();
                    if (typeDefinition.getQualifiedName().equals(baseName)) {
                        //this definition is top-level. no import required.
                        continue;
                    }
                    if (baseName.equals(typeString)) {
                        types.add(typeDefinition);
                    }
                }
            }
        } catch (Exception e) {
        //safe to ignore
        }
    }
    for (IDefinition definitionToImport : types) {
        Command command = createImportCommand(definitionToImport);
        if (command != null) {
            commands.add(command);
        }
    }
}
Also used : ICompilationUnit(org.apache.flex.compiler.units.ICompilationUnit) ITypeDefinition(org.apache.flex.compiler.definitions.ITypeDefinition) Command(org.eclipse.lsp4j.Command) ArrayList(java.util.ArrayList) 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