Search in sources :

Example 11 with ICompilationUnit

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

the class ActionScriptTextDocumentService method getCompilationUnit.

private ICompilationUnit getCompilationUnit(Path path) {
    String absolutePath = path.toAbsolutePath().toString();
    currentUnit = null;
    currentProject = getProject();
    if (currentProject == null) {
        return null;
    }
    currentWorkspace = currentProject.getWorkspace();
    //we're going to start with the files passed into the compiler
    String[] files = currentProjectOptions.files;
    if (files != null) {
        for (int i = files.length - 1; i >= 0; i--) {
            String file = files[i];
            //a previous file may have created a compilation unit for the
            //current file, so use that, if available
            ICompilationUnit existingUnit = findCompilationUnit(file);
            if (existingUnit != null) {
                if (file.equals(absolutePath)) {
                    currentUnit = existingUnit;
                }
                continue;
            }
            IInvisibleCompilationUnit unit = currentProject.createInvisibleCompilationUnit(file, fileSpecGetter);
            if (unit == null) {
                System.err.println("Could not create compilation unit for file: " + file);
                continue;
            }
            invisibleUnits.add(unit);
            if (file.equals(absolutePath)) {
                currentUnit = unit;
            }
        }
    }
    compilationUnits = currentProject.getCompilationUnits();
    //if we didn't find the unit already, search the complete set of units
    if (currentUnit == null) {
        //might already be created
        for (ICompilationUnit unit : compilationUnits) {
            if (unit != null && unit.getAbsoluteFilename().equals(absolutePath)) {
                currentUnit = unit;
                break;
            }
        }
    }
    //if we still haven't found it, create it manually
    if (currentUnit == null) {
        //if all else fails, create the compilation unit manually
        IInvisibleCompilationUnit unit = currentProject.createInvisibleCompilationUnit(absolutePath, fileSpecGetter);
        if (unit == null) {
            System.err.println("Could not create compilation unit for file: " + absolutePath);
            return null;
        }
        invisibleUnits.add(unit);
        currentUnit = unit;
    }
    //for some reason, function nodes may not always be populated, but we
    //can force them to be populated
    IASNode ast = null;
    try {
        ast = currentUnit.getSyntaxTreeRequest().get().getAST();
    } catch (InterruptedException e) {
        System.err.println("Interrupted while getting AST");
    }
    if (ast instanceof IFileNode) {
        IFileNode fileNode = (IFileNode) ast;
        fileNode.populateFunctionNodes();
    }
    return currentUnit;
}
Also used : IInvisibleCompilationUnit(org.apache.flex.compiler.units.IInvisibleCompilationUnit) ICompilationUnit(org.apache.flex.compiler.units.ICompilationUnit) IFileNode(org.apache.flex.compiler.tree.as.IFileNode) IASNode(org.apache.flex.compiler.tree.as.IASNode)

Example 12 with ICompilationUnit

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

the class ActionScriptTextDocumentService method documentSymbol.

/**
     * Searches by name for a symbol in a specific document (not the whole
     * workspace)
     */
@Override
public CompletableFuture<List<? extends SymbolInformation>> documentSymbol(DocumentSymbolParams params) {
    TextDocumentIdentifier textDocument = params.getTextDocument();
    Path path = LanguageServerUtils.getPathFromLanguageServerURI(textDocument.getUri());
    if (path == null) {
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    ICompilationUnit unit = getCompilationUnit(path);
    if (unit == null) {
        //we couldn't find a compilation unit with the specified path
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    IASScope[] scopes;
    try {
        scopes = unit.getFileScopeRequest().get().getScopes();
    } catch (Exception e) {
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    List<SymbolInformation> result = new ArrayList<>();
    for (IASScope scope : scopes) {
        scopeToSymbols(scope, result);
    }
    return CompletableFuture.completedFuture(result);
}
Also used : Path(java.nio.file.Path) ICompilationUnit(org.apache.flex.compiler.units.ICompilationUnit) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) 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)

Example 13 with ICompilationUnit

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

Example 14 with ICompilationUnit

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

the class ActionScriptTextDocumentService method resolveDefinition.

private void resolveDefinition(IDefinition definition, List<Location> result) {
    String definitionPath = definition.getSourcePath();
    if (definitionPath == null) {
        //if the definition is in an MXML file, getSourcePath() may return
        //null, but getContainingFilePath() will return something
        definitionPath = definition.getContainingFilePath();
        if (definitionPath == null) {
            //if everything is null, there's nothing to do
            return;
        }
        //however, getContainingFilePath() also works for SWCs
        if (!definitionPath.endsWith(AS_EXTENSION) && !definitionPath.endsWith(MXML_EXTENSION) && (definitionPath.contains(SDK_LIBRARY_PATH_SIGNATURE_UNIX) || definitionPath.contains(SDK_LIBRARY_PATH_SIGNATURE_WINDOWS))) {
            //if it's a framework SWC, we're going to attempt to resolve
            //the source file 
            ICompilationUnit unit = currentProject.getScope().getCompilationUnitForDefinition(definition);
            try {
                byte[] abcBytes = unit.getABCBytesRequest().get().getABCBytes();
                ABCParser parser = new ABCParser(abcBytes);
                PoolingABCVisitor visitor = new PoolingABCVisitor();
                parser.parseABC(visitor);
                Pool<String> pooledStrings = visitor.getStringPool();
                for (String pooledString : pooledStrings.getValues()) {
                    if (pooledString.contains(SDK_SOURCE_PATH_SIGNATURE_UNIX) || pooledString.contains(SDK_SOURCE_PATH_SIGNATURE_WINDOWS)) {
                        //just go with the first one that we find
                        definitionPath = transformDebugFilePath(pooledString);
                        break;
                    }
                }
            } catch (InterruptedException e) {
            //safe to ignore
            }
        }
        if (!definitionPath.endsWith(AS_EXTENSION) && !definitionPath.endsWith(MXML_EXTENSION)) {
            //if it's in a SWC or something, we don't know how to resolve
            return;
        }
    }
    Path resolvedPath = Paths.get(definitionPath);
    Location location = new Location();
    location.setUri(resolvedPath.toUri().toString());
    int nameLine = definition.getNameLine();
    int nameColumn = definition.getNameColumn();
    if (nameLine == -1 || nameColumn == -1) {
        //getNameLine() and getNameColumn() will both return -1 for a
        //variable definition created by an MXML tag with an id.
        //so we need to figure them out from the offset instead.
        int nameOffset = definition.getNameStart();
        if (nameOffset == -1) {
            //we can't find the name, so give up
            return;
        }
        Reader reader = getReaderForPath(resolvedPath);
        if (reader == null) {
            //we can't get the code at all
            return;
        }
        Position position = new Position();
        offsetToLineAndCharacter(reader, nameOffset, position);
        nameLine = position.getLine();
        nameColumn = position.getCharacter();
    }
    if (nameLine == -1 || nameColumn == -1) {
        //we can't find the name, so give up
        return;
    }
    Position start = new Position();
    start.setLine(nameLine);
    start.setCharacter(nameColumn);
    Position end = new Position();
    end.setLine(nameLine);
    end.setCharacter(nameColumn + definition.getNameEnd() - definition.getNameStart());
    Range range = new Range();
    range.setStart(start);
    range.setEnd(end);
    location.setRange(range);
    result.add(location);
}
Also used : Path(java.nio.file.Path) ICompilationUnit(org.apache.flex.compiler.units.ICompilationUnit) Position(org.eclipse.lsp4j.Position) Reader(java.io.Reader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Range(org.eclipse.lsp4j.Range) PoolingABCVisitor(org.apache.flex.abc.PoolingABCVisitor) ABCParser(org.apache.flex.abc.ABCParser) Location(org.eclipse.lsp4j.Location) ISourceLocation(org.apache.flex.compiler.common.ISourceLocation)

Example 15 with ICompilationUnit

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

the class ActionScriptTextDocumentService method addMembersForMXMLTypeToAutoComplete.

private void addMembersForMXMLTypeToAutoComplete(IClassDefinition definition, IMXMLTagData offsetTag, boolean includePrefix, CompletionList result) {
    ICompilationUnit unit = getCompilationUnit(Paths.get(offsetTag.getSourcePath()));
    if (unit == null) {
        return;
    }
    IASScope[] scopes;
    try {
        scopes = unit.getFileScopeRequest().get().getScopes();
    } catch (Exception e) {
        return;
    }
    if (scopes != null && scopes.length > 0) {
        String propertyElementPrefix = null;
        if (includePrefix) {
            String prefix = offsetTag.getPrefix();
            if (prefix.length() > 0) {
                propertyElementPrefix = prefix;
            }
        }
        TypeScope typeScope = (TypeScope) definition.getContainedScope();
        ASScope scope = (ASScope) scopes[0];
        addDefinitionsInTypeScopeToAutoCompleteMXML(typeScope, scope, propertyElementPrefix, result);
        addStyleMetadataToAutoCompleteMXML(typeScope, propertyElementPrefix, result);
        addEventMetadataToAutoCompleteMXML(typeScope, propertyElementPrefix, result);
    }
}
Also used : ICompilationUnit(org.apache.flex.compiler.units.ICompilationUnit) IASScope(org.apache.flex.compiler.scopes.IASScope) IASScope(org.apache.flex.compiler.scopes.IASScope) ASScope(org.apache.flex.compiler.internal.scopes.ASScope) TypeScope(org.apache.flex.compiler.internal.scopes.TypeScope) 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