use of org.apache.flex.compiler.tree.as.IExpressionNode in project vscode-nextgenas by BowlerHatLLC.
the class ActionScriptTextDocumentService method actionScriptRenameWithNode.
private CompletableFuture<WorkspaceEdit> actionScriptRenameWithNode(RenameParams params, IASNode offsetNode) {
if (offsetNode == null) {
//we couldn't find a node at the specified location
return CompletableFuture.completedFuture(new WorkspaceEdit(new HashMap<>()));
}
IDefinition definition = null;
if (offsetNode instanceof IDefinitionNode) {
IDefinitionNode definitionNode = (IDefinitionNode) offsetNode;
IExpressionNode expressionNode = definitionNode.getNameExpressionNode();
definition = expressionNode.resolve(currentProject);
} else if (offsetNode instanceof IIdentifierNode) {
IIdentifierNode identifierNode = (IIdentifierNode) offsetNode;
definition = identifierNode.resolve(currentProject);
}
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 CompletableFuture.completedFuture(new WorkspaceEdit(new HashMap<>()));
}
WorkspaceEdit result = renameDefinition(definition, params.getNewName());
return CompletableFuture.completedFuture(result);
}
use of org.apache.flex.compiler.tree.as.IExpressionNode in project vscode-nextgenas by BowlerHatLLC.
the class ActionScriptTextDocumentService method memberAccessToPackageName.
private String memberAccessToPackageName(IMemberAccessExpressionNode memberAccess) {
String result = null;
IExpressionNode rightOperand = memberAccess.getRightOperandNode();
if (!(rightOperand instanceof IIdentifierNode)) {
return null;
}
IExpressionNode leftOperand = memberAccess.getLeftOperandNode();
if (leftOperand instanceof IMemberAccessExpressionNode) {
result = memberAccessToPackageName((IMemberAccessExpressionNode) leftOperand);
} else if (leftOperand instanceof IIdentifierNode) {
IIdentifierNode identifierNode = (IIdentifierNode) leftOperand;
result = identifierNode.getName();
} else {
return null;
}
IIdentifierNode identifierNode = (IIdentifierNode) rightOperand;
return result + "." + identifierNode.getName();
}
use of org.apache.flex.compiler.tree.as.IExpressionNode in project vscode-nextgenas by BowlerHatLLC.
the class ActionScriptTextDocumentService method signatureHelp.
/**
* Displays a function's parameters, including which one is currently
* active. Called automatically by VSCode any time that the user types "(",
* so be sure to check that a function call is actually happening at the
* current position.
*/
@Override
public CompletableFuture<SignatureHelp> signatureHelp(TextDocumentPositionParams position) {
String textDocumentUri = position.getTextDocument().getUri();
if (!textDocumentUri.endsWith(AS_EXTENSION) && !textDocumentUri.endsWith(MXML_EXTENSION)) {
//we couldn't find a node at the specified location
return CompletableFuture.completedFuture(new SignatureHelp(Collections.emptyList(), -1, -1));
}
IASNode offsetNode = null;
IMXMLTagData offsetTag = getOffsetMXMLTag(position);
if (offsetTag != null) {
IMXMLTagAttributeData attributeData = getMXMLTagAttributeWithValueAtOffset(offsetTag, currentOffset);
if (attributeData != null) {
//some attributes can have ActionScript completion, such as
//events and properties with data binding
IClassDefinition tagDefinition = (IClassDefinition) currentProject.resolveXMLNameToDefinition(offsetTag.getXMLName(), offsetTag.getMXMLDialect());
IDefinition attributeDefinition = currentProject.resolveSpecifier(tagDefinition, attributeData.getShortName());
if (attributeDefinition instanceof IEventDefinition) {
IMXMLInstanceNode mxmlNode = (IMXMLInstanceNode) getOffsetNode(position);
IMXMLEventSpecifierNode eventNode = mxmlNode.getEventSpecifierNode(attributeData.getShortName());
for (IASNode asNode : eventNode.getASNodes()) {
IASNode containingNode = getContainingNodeIncludingStart(asNode, currentOffset);
if (containingNode != null) {
offsetNode = containingNode;
}
}
if (offsetNode == null) {
offsetNode = eventNode;
}
}
}
}
if (offsetNode == null) {
offsetNode = getOffsetNode(position);
}
if (offsetNode == null) {
//we couldn't find a node at the specified location
return CompletableFuture.completedFuture(new SignatureHelp(Collections.emptyList(), -1, -1));
}
IFunctionCallNode functionCallNode = getAncestorFunctionCallNode(offsetNode);
IFunctionDefinition functionDefinition = null;
if (functionCallNode != null) {
IExpressionNode nameNode = functionCallNode.getNameNode();
IDefinition definition = nameNode.resolve(currentUnit.getProject());
if (definition instanceof IFunctionDefinition) {
functionDefinition = (IFunctionDefinition) definition;
} else if (definition instanceof IClassDefinition) {
IClassDefinition classDefinition = (IClassDefinition) definition;
functionDefinition = classDefinition.getConstructor();
} else if (nameNode instanceof IIdentifierNode) {
//special case for super()
IIdentifierNode identifierNode = (IIdentifierNode) nameNode;
if (identifierNode.getName().equals(IASKeywordConstants.SUPER)) {
ITypeDefinition typeDefinition = nameNode.resolveType(currentProject);
if (typeDefinition instanceof IClassDefinition) {
IClassDefinition classDefinition = (IClassDefinition) typeDefinition;
functionDefinition = classDefinitionToConstructor(classDefinition);
}
}
}
}
if (functionDefinition != null) {
SignatureHelp result = new SignatureHelp();
List<SignatureInformation> signatures = new ArrayList<>();
SignatureInformation signatureInfo = new SignatureInformation();
signatureInfo.setLabel(getSignatureLabel(functionDefinition));
List<ParameterInformation> parameters = new ArrayList<>();
for (IParameterDefinition param : functionDefinition.getParameters()) {
ParameterInformation paramInfo = new ParameterInformation();
paramInfo.setLabel(param.getBaseName());
parameters.add(paramInfo);
}
signatureInfo.setParameters(parameters);
signatures.add(signatureInfo);
result.setSignatures(signatures);
result.setActiveSignature(0);
int index = getFunctionCallNodeArgumentIndex(functionCallNode, offsetNode);
IParameterDefinition[] params = functionDefinition.getParameters();
int paramCount = params.length;
if (paramCount > 0 && index >= paramCount) {
if (index >= paramCount) {
IParameterDefinition lastParam = params[paramCount - 1];
if (lastParam.isRest()) {
//functions with rest parameters may accept any
//number of arguments, so continue to make the rest
//parameter active
index = paramCount - 1;
} else {
//if there's no rest parameter, and we're beyond the
//final parameter, none should be active
index = -1;
}
}
}
if (index != -1) {
result.setActiveParameter(index);
}
return CompletableFuture.completedFuture(result);
}
return CompletableFuture.completedFuture(new SignatureHelp(Collections.emptyList(), -1, -1));
}
use of org.apache.flex.compiler.tree.as.IExpressionNode in project vscode-nextgenas by BowlerHatLLC.
the class ActionScriptTextDocumentService method autoCompleteMemberAccess.
private void autoCompleteMemberAccess(IMemberAccessExpressionNode node, CompletionList result) {
ASScope scope = (ASScope) node.getContainingScope().getScope();
IExpressionNode leftOperand = node.getLeftOperandNode();
IDefinition leftDefinition = leftOperand.resolve(currentProject);
if (leftDefinition != null && leftDefinition instanceof ITypeDefinition) {
ITypeDefinition typeDefinition = (ITypeDefinition) leftDefinition;
TypeScope typeScope = (TypeScope) typeDefinition.getContainedScope();
addDefinitionsInTypeScopeToAutoCompleteActionScript(typeScope, scope, true, result);
return;
}
ITypeDefinition leftType = leftOperand.resolveType(currentProject);
if (leftType != null) {
TypeScope typeScope = (TypeScope) leftType.getContainedScope();
addDefinitionsInTypeScopeToAutoCompleteActionScript(typeScope, scope, false, result);
return;
}
if (leftOperand instanceof IMemberAccessExpressionNode) {
IMemberAccessExpressionNode memberAccess = (IMemberAccessExpressionNode) leftOperand;
String packageName = memberAccessToPackageName(memberAccess);
if (packageName != null) {
autoCompleteDefinitions(result, false, false, packageName, null);
return;
}
}
}
use of org.apache.flex.compiler.tree.as.IExpressionNode 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);
}
Aggregations