vscode-as3mxml icon indicating copy to clipboard operation
vscode-as3mxml copied to clipboard

Generate event listener

Open joshtynjala opened this issue 8 years ago • 4 comments

Completion:

  • [ ] In addEventListener() call
  • [ ] In MXML event attribute

Code actions:

  • [X] In addEventListener() call
  • [X] In MXML event attribute

joshtynjala avatar Dec 14 '16 16:12 joshtynjala

Some quick and dirty code that generates an event listener for MXML event specifiers in a completion request:

if (offsetNode instanceof IMXMLEventSpecifierNode)
{
    IMXMLEventSpecifierNode eventNode = (IMXMLEventSpecifierNode) offsetNode;
    IParameterDefinition paramDef = eventNode.getEventParameterDefinition();
    ITypeDefinition paramTypeDef = paramDef.resolveType(project);
    if (paramTypeDef == null)
    {
        return result;
    }
    IMXMLClassDefinitionNode classNode = eventNode.getClassDefinitionNode();
    IClassDefinition classDef = classNode.getClassDefinition();
    String functionName = eventNode.getName() + "Handler";
    IDefinitionSet handlerDefSet = classDef.getContainedScope().getLocalDefinitionSetByName(functionName);
    String currentFunctionName = functionName;
    int count = 2;
    while(handlerDefSet != null)
    {
        currentFunctionName = functionName + count;
        handlerDefSet = classDef.getContainedScope().getLocalDefinitionSetByName(currentFunctionName);
        count++;
    }
    functionName = currentFunctionName;
    String uri = path.toUri().toString();
    WorkspaceEdit workspaceEdit = CodeActionsUtils.createWorkspaceEditForGenerateEventListener(offsetNode, functionName, paramTypeDef.getQualifiedName(), uri, fileText, project);
    if(workspaceEdit == null)
    {
        return result;
    }
    List<TextEdit> textEdits = workspaceEdit.getChanges().get(uri);
    CompletionItem item = new CompletionItem();
    item.setLabel("Generate event listener");
    item.setInsertText(functionName + "(event)");
    item.setKind(CompletionItemKind.Event);
    item.setAdditionalTextEdits(textEdits);
    result.getItems().add(item);
    return result;
}

joshtynjala avatar Jun 07 '19 22:06 joshtynjala

For my future reference, Flash Builder generates an event listener in MXML during a completion request like this:

<s:List change="list1_changeHandler(event)"/>
<fx:Script>
	<![CDATA[
		import spark.events.IndexChangeEvent;
		
		protected function list1_changeHandler(event:IndexChangeEvent):void
		{
			// TODO Auto-generated method stub
			
		}
		
	]]>
</fx:Script>

joshtynjala avatar Jun 07 '19 22:06 joshtynjala

Flash Builder generates an event listener in MXML during a completion request like this:

<s:List change="list1_changeHandler(event)"/>
<fx:Script>
	<![CDATA[
		import spark.events.IndexChangeEvent;
		
		protected function list1_changeHandler(event:IndexChangeEvent):void
		{
			// TODO Auto-generated method stub
			
		}
		
	]]>
</fx:Script>

Yes, if there is no </fx:Script> block yet created, right? If it is, it adds import(s) line(s) to the top of the block and appends event handler function at the end of the block.

neminovno avatar Jun 07 '19 22:06 neminovno

Correct. Determining whether a Script element is needed or not is already implemented. It's mostly generating a name for the event listener that I still need to flesh out.

joshtynjala avatar Jun 07 '19 22:06 joshtynjala