Search in sources :

Example 6 with IIdentifierNode

use of org.apache.flex.compiler.tree.as.IIdentifierNode 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 IIdentifierNode

use of org.apache.flex.compiler.tree.as.IIdentifierNode 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 IIdentifierNode

use of org.apache.flex.compiler.tree.as.IIdentifierNode in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method actionScriptCompletionWithNode.

private CompletableFuture<CompletionList> actionScriptCompletionWithNode(TextDocumentPositionParams position, IASNode offsetNode) {
    CompletionList result = new CompletionList();
    result.setIsIncomplete(false);
    result.setItems(new ArrayList<>());
    if (offsetNode == null) {
        //we couldn't find a node at the specified location
        autoCompletePackageBlock(result);
        return CompletableFuture.completedFuture(result);
    }
    IASNode parentNode = offsetNode.getParent();
    IASNode nodeAtPreviousOffset = null;
    if (parentNode != null) {
        nodeAtPreviousOffset = parentNode.getContainingNode(currentOffset - 1);
    }
    if (!isActionScriptCompletionAllowedInNode(position, offsetNode)) {
        //if we're inside a node that shouldn't have completion!
        return CompletableFuture.completedFuture(new CompletionList());
    }
    //variable types
    if (offsetNode instanceof IVariableNode) {
        IVariableNode variableNode = (IVariableNode) offsetNode;
        IExpressionNode nameExpression = variableNode.getNameExpressionNode();
        IExpressionNode typeNode = variableNode.getVariableTypeNode();
        int line = position.getPosition().getLine();
        int column = position.getPosition().getCharacter();
        if (line >= nameExpression.getEndLine() && line <= typeNode.getLine()) {
            if ((line != nameExpression.getEndLine() && line != typeNode.getLine()) || (line == nameExpression.getEndLine() && column > nameExpression.getEndColumn()) || (line == typeNode.getLine() && column <= typeNode.getColumn())) {
                autoCompleteTypes(offsetNode, result);
            }
            return CompletableFuture.completedFuture(result);
        }
    }
    if (parentNode != null && parentNode instanceof IVariableNode) {
        IVariableNode variableNode = (IVariableNode) parentNode;
        if (offsetNode == variableNode.getVariableTypeNode()) {
            autoCompleteTypes(parentNode, result);
            return CompletableFuture.completedFuture(result);
        }
    }
    //function return types
    if (offsetNode instanceof IFunctionNode) {
        IFunctionNode functionNode = (IFunctionNode) offsetNode;
        IContainerNode parameters = functionNode.getParametersContainerNode();
        IExpressionNode typeNode = functionNode.getReturnTypeNode();
        if (typeNode != null) {
            int line = position.getPosition().getLine();
            int column = position.getPosition().getCharacter();
            if (line >= parameters.getEndLine() && column > parameters.getEndColumn() && line <= typeNode.getLine() && column <= typeNode.getColumn()) {
                autoCompleteTypes(offsetNode, result);
                return CompletableFuture.completedFuture(result);
            }
        }
    }
    if (parentNode != null && parentNode instanceof IFunctionNode) {
        IFunctionNode functionNode = (IFunctionNode) parentNode;
        if (offsetNode == functionNode.getReturnTypeNode()) {
            autoCompleteTypes(parentNode, result);
            return CompletableFuture.completedFuture(result);
        }
    }
    //new keyword types
    if (parentNode != null && parentNode instanceof IFunctionCallNode) {
        IFunctionCallNode functionCallNode = (IFunctionCallNode) parentNode;
        if (functionCallNode.getNameNode() == offsetNode && functionCallNode.isNewExpression()) {
            autoCompleteTypes(parentNode, result);
            return CompletableFuture.completedFuture(result);
        }
    }
    if (nodeAtPreviousOffset != null && nodeAtPreviousOffset instanceof IKeywordNode && nodeAtPreviousOffset.getNodeID() == ASTNodeID.KeywordNewID) {
        autoCompleteTypes(nodeAtPreviousOffset, result);
        return CompletableFuture.completedFuture(result);
    }
    //as and is keyword types
    if (parentNode != null && parentNode instanceof IBinaryOperatorNode && (parentNode.getNodeID() == ASTNodeID.Op_AsID || parentNode.getNodeID() == ASTNodeID.Op_IsID)) {
        IBinaryOperatorNode binaryOperatorNode = (IBinaryOperatorNode) parentNode;
        if (binaryOperatorNode.getRightOperandNode() == offsetNode) {
            autoCompleteTypes(parentNode, result);
            return CompletableFuture.completedFuture(result);
        }
    }
    if (nodeAtPreviousOffset != null && nodeAtPreviousOffset instanceof IBinaryOperatorNode && (nodeAtPreviousOffset.getNodeID() == ASTNodeID.Op_AsID || nodeAtPreviousOffset.getNodeID() == ASTNodeID.Op_IsID)) {
        autoCompleteTypes(nodeAtPreviousOffset, result);
        return CompletableFuture.completedFuture(result);
    }
    //class extends keyword
    if (offsetNode instanceof IClassNode && nodeAtPreviousOffset != null && nodeAtPreviousOffset instanceof IKeywordNode && nodeAtPreviousOffset.getNodeID() == ASTNodeID.KeywordExtendsID) {
        autoCompleteTypes(offsetNode, result);
        return CompletableFuture.completedFuture(result);
    }
    //class implements keyword
    if (offsetNode instanceof IClassNode && nodeAtPreviousOffset != null && nodeAtPreviousOffset instanceof IKeywordNode && nodeAtPreviousOffset.getNodeID() == ASTNodeID.KeywordImplementsID) {
        autoCompleteTypes(offsetNode, result);
        return CompletableFuture.completedFuture(result);
    }
    //interface extends keyword
    if (offsetNode instanceof IInterfaceNode && nodeAtPreviousOffset != null && nodeAtPreviousOffset instanceof IKeywordNode && nodeAtPreviousOffset.getNodeID() == ASTNodeID.KeywordExtendsID) {
        autoCompleteTypes(offsetNode, result);
        return CompletableFuture.completedFuture(result);
    }
    //package (must be before member access)
    if (offsetNode instanceof IPackageNode) {
        IPackageNode packageNode = (IPackageNode) offsetNode;
        autoCompletePackageName(packageNode.getPackageName(), result);
        return CompletableFuture.completedFuture(result);
    }
    if (parentNode != null && parentNode instanceof FullNameNode) {
        IASNode gpNode = parentNode.getParent();
        if (gpNode != null && gpNode instanceof IPackageNode) {
            IPackageNode packageNode = (IPackageNode) gpNode;
            autoCompletePackageName(packageNode.getPackageName(), result);
        }
    }
    if (parentNode != null && parentNode instanceof IPackageNode) {
        //we'll get here if the last character in the package name is .
        IPackageNode packageNode = (IPackageNode) parentNode;
        IExpressionNode nameNode = packageNode.getNameExpressionNode();
        if (offsetNode == nameNode) {
            autoCompletePackageName(packageNode.getPackageName(), result);
            return CompletableFuture.completedFuture(result);
        }
    }
    //import (must be before member access)
    if (parentNode != null && parentNode instanceof IImportNode) {
        IImportNode importNode = (IImportNode) parentNode;
        IExpressionNode nameNode = importNode.getImportNameNode();
        if (offsetNode == nameNode) {
            String importName = importNode.getImportName();
            importName = importName.substring(0, position.getPosition().getCharacter() - nameNode.getColumn());
            autoCompleteImport(importName, result);
            return CompletableFuture.completedFuture(result);
        }
    }
    if (parentNode != null && parentNode instanceof FullNameNode) {
        IASNode gpNode = parentNode.getParent();
        if (gpNode != null && gpNode instanceof IImportNode) {
            IImportNode importNode = (IImportNode) gpNode;
            IExpressionNode nameNode = importNode.getImportNameNode();
            if (parentNode == nameNode) {
                String importName = importNode.getImportName();
                importName = importName.substring(0, position.getPosition().getCharacter() - nameNode.getColumn());
                autoCompleteImport(importName, result);
                return CompletableFuture.completedFuture(result);
            }
        }
    }
    if (nodeAtPreviousOffset != null && nodeAtPreviousOffset instanceof IImportNode) {
        autoCompleteImport("", result);
        return CompletableFuture.completedFuture(result);
    }
    //member access
    if (offsetNode instanceof IMemberAccessExpressionNode) {
        IMemberAccessExpressionNode memberAccessNode = (IMemberAccessExpressionNode) offsetNode;
        IExpressionNode leftOperand = memberAccessNode.getLeftOperandNode();
        IExpressionNode rightOperand = memberAccessNode.getRightOperandNode();
        int line = position.getPosition().getLine();
        int column = position.getPosition().getCharacter();
        if (line >= leftOperand.getEndLine() && line <= rightOperand.getLine()) {
            if ((line != leftOperand.getEndLine() && line != rightOperand.getLine()) || (line == leftOperand.getEndLine() && column > leftOperand.getEndColumn()) || (line == rightOperand.getLine() && column <= rightOperand.getColumn())) {
                autoCompleteMemberAccess(memberAccessNode, result);
                return CompletableFuture.completedFuture(result);
            }
        }
    }
    if (parentNode != null && parentNode instanceof IMemberAccessExpressionNode) {
        IMemberAccessExpressionNode memberAccessNode = (IMemberAccessExpressionNode) parentNode;
        //you would expect that the offset node could only be the right
        //operand, but it's actually possible for it to be the left operand,
        //even if the . has been typed! only sometimes, though.
        boolean isValidLeft = true;
        if (offsetNode == memberAccessNode.getLeftOperandNode() && memberAccessNode.getRightOperandNode() instanceof IIdentifierNode) {
            //if the left and right operands both exist, then this is not
            //member access and we should skip ahead
            isValidLeft = false;
        }
        if (offsetNode == memberAccessNode.getRightOperandNode() || isValidLeft) {
            autoCompleteMemberAccess(memberAccessNode, result);
            return CompletableFuture.completedFuture(result);
        }
    }
    if (nodeAtPreviousOffset != null && nodeAtPreviousOffset instanceof IMemberAccessExpressionNode) {
        //depending on the left operand, if a . is typed, the member access
        //may end up being the previous node instead of the parent or offset
        //node, so check if the right operand is empty
        IMemberAccessExpressionNode memberAccessNode = (IMemberAccessExpressionNode) nodeAtPreviousOffset;
        IExpressionNode rightOperandNode = memberAccessNode.getRightOperandNode();
        if (rightOperandNode instanceof IIdentifierNode) {
            IIdentifierNode identifierNode = (IIdentifierNode) rightOperandNode;
            if (identifierNode.getName().equals("")) {
                autoCompleteMemberAccess(memberAccessNode, result);
                return CompletableFuture.completedFuture(result);
            }
        }
    }
    //local scope
    do {
        //nodes to check
        if (offsetNode instanceof IScopedNode) {
            IScopedNode scopedNode = (IScopedNode) offsetNode;
            //include all members and local things that are in scope
            autoCompleteScope(scopedNode, false, result);
            //include all public definitions
            IASScope scope = scopedNode.getScope();
            IDefinition definitionToSkip = scope.getDefinition();
            autoCompleteDefinitions(result, false, false, null, definitionToSkip);
            return CompletableFuture.completedFuture(result);
        }
        offsetNode = offsetNode.getParent();
    } while (offsetNode != null);
    return CompletableFuture.completedFuture(result);
}
Also used : CompletionList(org.eclipse.lsp4j.CompletionList) IScopedNode(org.apache.flex.compiler.tree.as.IScopedNode) IPackageNode(org.apache.flex.compiler.tree.as.IPackageNode) IImportNode(org.apache.flex.compiler.tree.as.IImportNode) IIdentifierNode(org.apache.flex.compiler.tree.as.IIdentifierNode) IKeywordNode(org.apache.flex.compiler.tree.as.IKeywordNode) IVariableNode(org.apache.flex.compiler.tree.as.IVariableNode) IFunctionNode(org.apache.flex.compiler.tree.as.IFunctionNode) IContainerNode(org.apache.flex.compiler.tree.as.IContainerNode) IFunctionCallNode(org.apache.flex.compiler.tree.as.IFunctionCallNode) IInterfaceNode(org.apache.flex.compiler.tree.as.IInterfaceNode) IDefinition(org.apache.flex.compiler.definitions.IDefinition) IASScope(org.apache.flex.compiler.scopes.IASScope) IASNode(org.apache.flex.compiler.tree.as.IASNode) FullNameNode(org.apache.flex.compiler.internal.tree.as.FullNameNode) IClassNode(org.apache.flex.compiler.tree.as.IClassNode) IExpressionNode(org.apache.flex.compiler.tree.as.IExpressionNode) IBinaryOperatorNode(org.apache.flex.compiler.tree.as.IBinaryOperatorNode) IMemberAccessExpressionNode(org.apache.flex.compiler.tree.as.IMemberAccessExpressionNode)

Example 9 with IIdentifierNode

use of org.apache.flex.compiler.tree.as.IIdentifierNode in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method actionScriptDefinitionWithNode.

private CompletableFuture<List<? extends Location>> actionScriptDefinitionWithNode(TextDocumentPositionParams position, IASNode offsetNode) {
    if (offsetNode == null) {
        //we couldn't find a node at the specified location
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    IDefinition definition = null;
    if (offsetNode instanceof IIdentifierNode) {
        IIdentifierNode expressionNode = (IIdentifierNode) offsetNode;
        definition = expressionNode.resolve(currentProject);
    }
    if (definition == null) {
        //definition referenced at the current position.
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    List<Location> result = new ArrayList<>();
    resolveDefinition(definition, result);
    return CompletableFuture.completedFuture(result);
}
Also used : ArrayList(java.util.ArrayList) IIdentifierNode(org.apache.flex.compiler.tree.as.IIdentifierNode) IDefinition(org.apache.flex.compiler.definitions.IDefinition) Location(org.eclipse.lsp4j.Location) ISourceLocation(org.apache.flex.compiler.common.ISourceLocation)

Example 10 with IIdentifierNode

use of org.apache.flex.compiler.tree.as.IIdentifierNode in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method actionScriptReferencesWithNode.

private CompletableFuture<List<? extends Location>> actionScriptReferencesWithNode(ReferenceParams params, IASNode offsetNode) {
    if (offsetNode == null) {
        //we couldn't find a node at the specified location
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    if (offsetNode instanceof IIdentifierNode) {
        IIdentifierNode identifierNode = (IIdentifierNode) offsetNode;
        IDefinition resolved = identifierNode.resolve(currentProject);
        if (resolved == null) {
            return CompletableFuture.completedFuture(Collections.emptyList());
        }
        List<Location> result = new ArrayList<>();
        referencesForDefinition(resolved, result);
        return CompletableFuture.completedFuture(result);
    }
    //definition referenced at the current position.
    return CompletableFuture.completedFuture(Collections.emptyList());
}
Also used : ArrayList(java.util.ArrayList) IIdentifierNode(org.apache.flex.compiler.tree.as.IIdentifierNode) IDefinition(org.apache.flex.compiler.definitions.IDefinition) Location(org.eclipse.lsp4j.Location) ISourceLocation(org.apache.flex.compiler.common.ISourceLocation)

Aggregations

IIdentifierNode (org.apache.flex.compiler.tree.as.IIdentifierNode)10 IDefinition (org.apache.flex.compiler.definitions.IDefinition)7 ArrayList (java.util.ArrayList)6 IASNode (org.apache.flex.compiler.tree.as.IASNode)5 ISourceLocation (org.apache.flex.compiler.common.ISourceLocation)4 IExpressionNode (org.apache.flex.compiler.tree.as.IExpressionNode)4 IMXMLTagData (org.apache.flex.compiler.mxml.IMXMLTagData)3 Location (org.eclipse.lsp4j.Location)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 ConcurrentModificationException (java.util.ConcurrentModificationException)2 HashMap (java.util.HashMap)2 MXMLData (org.apache.flex.compiler.internal.mxml.MXMLData)2 SWCCompilationUnit (org.apache.flex.compiler.internal.units.SWCCompilationUnit)2 IMXMLDataManager (org.apache.flex.compiler.mxml.IMXMLDataManager)2 IFunctionCallNode (org.apache.flex.compiler.tree.as.IFunctionCallNode)2 IMemberAccessExpressionNode (org.apache.flex.compiler.tree.as.IMemberAccessExpressionNode)2 ICompilationUnit (org.apache.flex.compiler.units.ICompilationUnit)2 CompletionList (org.eclipse.lsp4j.CompletionList)2 URI (java.net.URI)1