Search in sources :

Example 1 with SWCCompilationUnit

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

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

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

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

the class ActionScriptTextDocumentService method workspaceSymbol.

/**
     * Searches by name for a symbol in the workspace.
     */
public CompletableFuture<List<? extends SymbolInformation>> workspaceSymbol(WorkspaceSymbolParams params) {
    if (compilationUnits == null) {
        //if we haven't successfully compiled the project, we can't do this
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    List<SymbolInformation> result = new ArrayList<>();
    String query = params.getQuery();
    for (ICompilationUnit unit : compilationUnits) {
        if (unit == null || unit instanceof SWCCompilationUnit) {
            continue;
        }
        IASScope[] scopes;
        try {
            scopes = unit.getFileScopeRequest().get().getScopes();
        } catch (Exception e) {
            return CompletableFuture.completedFuture(Collections.emptyList());
        }
        for (IASScope scope : scopes) {
            querySymbolsInScope(query, scope, result);
        }
    }
    return CompletableFuture.completedFuture(result);
}
Also used : ICompilationUnit(org.apache.flex.compiler.units.ICompilationUnit) SWCCompilationUnit(org.apache.flex.compiler.internal.units.SWCCompilationUnit) IASScope(org.apache.flex.compiler.scopes.IASScope) ArrayList(java.util.ArrayList) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) ConcurrentModificationException(java.util.ConcurrentModificationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)4 IOException (java.io.IOException)4 ConcurrentModificationException (java.util.ConcurrentModificationException)4 SWCCompilationUnit (org.apache.flex.compiler.internal.units.SWCCompilationUnit)4 ICompilationUnit (org.apache.flex.compiler.units.ICompilationUnit)4 ArrayList (java.util.ArrayList)3 IASNode (org.apache.flex.compiler.tree.as.IASNode)3 URI (java.net.URI)2 HashMap (java.util.HashMap)2 ISourceLocation (org.apache.flex.compiler.common.ISourceLocation)2 MXMLData (org.apache.flex.compiler.internal.mxml.MXMLData)2 IMXMLDataManager (org.apache.flex.compiler.mxml.IMXMLDataManager)2 IMXMLTagData (org.apache.flex.compiler.mxml.IMXMLTagData)2 IIdentifierNode (org.apache.flex.compiler.tree.as.IIdentifierNode)2 List (java.util.List)1 IDefinition (org.apache.flex.compiler.definitions.IDefinition)1 IPackageDefinition (org.apache.flex.compiler.definitions.IPackageDefinition)1 CompilerProject (org.apache.flex.compiler.internal.projects.CompilerProject)1 ICompilerProblem (org.apache.flex.compiler.problems.ICompilerProblem)1 IASScope (org.apache.flex.compiler.scopes.IASScope)1