Search in sources :

Example 6 with Location

use of org.eclipse.lsp4j.Location 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 7 with Location

use of org.eclipse.lsp4j.Location in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method mxmlDefinition.

private CompletableFuture<List<? extends Location>> mxmlDefinition(TextDocumentPositionParams position, IMXMLTagData offsetTag) {
    IDefinition definition = getDefinitionForMXMLNameAtOffset(offsetTag, currentOffset);
    if (definition == null) {
        //definition referenced at the current position.
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    if (isInsideTagPrefix(offsetTag, currentOffset)) {
        //ignore the tag's prefix
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    List<Location> result = new ArrayList<>();
    resolveDefinition(definition, result);
    return CompletableFuture.completedFuture(result);
}
Also used : ArrayList(java.util.ArrayList) IDefinition(org.apache.flex.compiler.definitions.IDefinition) Location(org.eclipse.lsp4j.Location) ISourceLocation(org.apache.flex.compiler.common.ISourceLocation)

Example 8 with Location

use of org.eclipse.lsp4j.Location in project vscode-nextgenas by BowlerHatLLC.

the class LanguageServerUtils method getLocationFromSourceLocation.

/**
     * Converts a compiler source location to a language server location. May
     * return null if the line or column of the source location is -1.
     */
public static Location getLocationFromSourceLocation(ISourceLocation sourceLocation) {
    Path sourceLocationPath = Paths.get(sourceLocation.getSourcePath());
    Location location = new Location();
    location.setUri(sourceLocationPath.toUri().toString());
    Range range = getRangeFromSourceLocation(sourceLocation);
    if (range == null) {
        //this is probably generated by the compiler somehow
        return null;
    }
    location.setRange(range);
    return location;
}
Also used : Path(java.nio.file.Path) Range(org.eclipse.lsp4j.Range) ISourceLocation(org.apache.flex.compiler.common.ISourceLocation) Location(org.eclipse.lsp4j.Location)

Aggregations

ISourceLocation (org.apache.flex.compiler.common.ISourceLocation)8 Location (org.eclipse.lsp4j.Location)8 ArrayList (java.util.ArrayList)5 Path (java.nio.file.Path)4 IDefinition (org.apache.flex.compiler.definitions.IDefinition)4 IIdentifierNode (org.apache.flex.compiler.tree.as.IIdentifierNode)3 ICompilationUnit (org.apache.flex.compiler.units.ICompilationUnit)3 Range (org.eclipse.lsp4j.Range)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 ConcurrentModificationException (java.util.ConcurrentModificationException)2 IClassDefinition (org.apache.flex.compiler.definitions.IClassDefinition)2 Position (org.eclipse.lsp4j.Position)2 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 Reader (java.io.Reader)1 ABCParser (org.apache.flex.abc.ABCParser)1 PoolingABCVisitor (org.apache.flex.abc.PoolingABCVisitor)1 IConstantDefinition (org.apache.flex.compiler.definitions.IConstantDefinition)1