Notepad++ Autocomplete generation script

Discussion in 'CommandHelper Scripts' started by Raven0, Dec 8, 2016.

  1. Raven0

    Raven0 New Member

    Okay, think that for my first post I should do something useful for this community, as I code in CH for quite a while, plus had help from IRC guys and girls multiple times so far.

    So, this will generate XML file that you could basically place in "instalation_folder\Notepad++\plugins\APIs\" and from then on, everytime you work in CH files(.ms, .msa), it should use call tip to show you further info.

    It's not perfect by any means, I'm sure there's better ways to pull this off, but as I needed it to further speed up my coding, I had to give it a shot.
    There's no way for me to return event documentation, and in some cases I had to actually triple check what's being generated to I can include it in replacement/removal portions of the code. But, for me it works with over 15+ extensions installed, as it checks all their functions & events and then just goes and sorts it out.

    Aaaanyway, here's the code. Hope you enjoy it. ^^

    Requirements:
    1 - CHDangerous extension (so it actually writes the file);
    2 - Run from console - optional, not really a requirement, as output is sent there;

    Pastie: http://paste.thezomg.com/19960/14812215/
    Code (Text):
    #Author: Raven0 @ buxville.net
    *:/generate_AC_xml = >>>
        /* Create file to write into */
            @file = "MethodScript.xml";
            try(read(@file),chd_create(@file));
         
        /* Prepare start of the file */
            @file_Write = array(
                "<?xml version=\"1.0\" encoding=\"Windows-1252\" ?>",
                "<!-- author: Raven0 \@ www.buxville.net -->",
                "<NotepadPlus>",
                "   <AutoComplete language=\"MethodScript\">",
                "        <Environment ignoreCase=\"no\" startFunc=\"(\" stopFunc=\")\" paramSeparator=\",\" terminal=\";\"/>"
            );
        /* Prepare JSON file, as we will use it to quickly get rid of some escapes */
            @file_JSON_prep = array();
            @file_JSON_prep["functions"] = array();
            @file_JSON_prep["events"] = array();
         
        /* Grab extensions */
            @CH_extensions = extension_info();
     
        /* START OF THE PROCESSING TO JSON FILE */
            for(@x = 0, @x < array_size(@CH_extensions),@x++){
         
            /* Define extension name and version */
                @extension_Name = array_keys(@CH_extensions)[@x];
                @extension_Version = @CH_extensions[@extension_Name]['version'];
            /* Define extension functions and events */
                @functions = @CH_extensions[@extension_Name]['functions'];
                @events    = @CH_extensions[@extension_Name]['events'];
             
             /* Catch all functions and prepare for JSON */
                for(@y = 0, @y < array_size(@functions), @y++){
                    @function_Name   = @functions[@y];
                    if(!string_starts_with(@function_Name,"_")){
                        try {
                        /* Declare function and it's arguments */
                           @file_JSON_prep["functions"][@function_Name]                = array();
                           @file_JSON_prep["functions"][@function_Name]['args']        = reflect_docs(@function_Name,"args");
                           @file_JSON_prep["functions"][@function_Name]['return']      = reflect_docs(@function_Name,"return");
                           @file_JSON_prep["functions"][@function_Name]['description'] = reflect_docs(@function_Name,"description");
                       } catch(FormatException @ex) {console(colorize("&eCould not show information for function: &a"[email protected]_Name."&r"),false);}
                   }
               }
             
           /* Catch all events and prepare for JSON */
               for(@z = 0, @z < array_size(@events), @z++){
                   @event_Name   = @events[@z];
                   if(!string_starts_with(@event_Name,"_")){
                       try {
                       /* Declare event and it's arguments */
                            @file_JSON_prep["events"][] = @event_Name;
                        } catch(FormatException @ex) {console(colorize("&eCould not show information for event: &a"[email protected]_Name."&r"),false);}
                    }
                }
            }
        /* ENCODE TO JSON FORMAT */
            @file_JSON_prep1 = json_encode(@file_JSON_prep);
            @file_JSON = json_decode(@file_JSON_prep1);
         
        /* Start of the processing from JSON to XML */
            /* FUNCTIONS */
                @process_Functions = @file_JSON["functions"];
             
                foreach(@function: @function_Args in @process_Functions){
                /* FUNCTION NAME */
                    /* Process function name and write to XML */
                        @process_Function_Name = @function;
                        @file_Write[]          = "        <KeyWord name=\""[email protected]_Function_Name."\" func=\"yes\">";
                     
                /* FUNCTION RETURNS & DESCRIPTION */
                    /* RETURNS */
                        @process_Function_Return      = @function_Args["return"];
                    /* DESCRIPTION */
                        @process_Function_Description = @function_Args["description"];
                     
                        /* REMOVE UNNECESSARY CHARACTERS/TAGS/ETC FROM DESCRIPTION */
                            # List of unwanted onesin order of removal: <code> <\/code> \u0003 <li> <\/li> <pre> <\/pre> <ul> <\/ul>
                            @process_Function_Description_prep_1 = replace(replace(replace(replace(replace(replace(replace(replace(replace(@process_Function_Description,"<code>",""),"<\\//code>",""),"\u0003",""),"<li>",""),"<\\//li>",""),"<pre>",""),"<\\//pre>",""),"<ul>",""),"<\\//ul>","");
                     
                        /* REPLACE end of line and escape some characters */
                            # List in order of replacement: \n -> &#x0a, @ -> \@, \/ -> \//, < -> &lt;, > -> &gt;, ' -> &apos;, " -> &quot;
                            @process_Function_Description_prep_2 = replace(replace(replace(replace(replace(replace(@process_Function_Description_prep_1,"\n","&#x0a;"),"\@","\\@"),">","&gt;"),"<","&lt;"),"'","&apos;"),'"',"&quot;");
                     
                        /* REPLACE all {{function|some_name}} description tags meant for forum links */
                            @find_Code_prep      = reg_match('\\{\\{function\\|.+\\}\\}',@process_Function_Description_prep_2)
                            if(array_index_exists(@find_Code_prep,0)){
                                @find_Code = @find_Code_prep[0];
                                @find_CodeValue = reg_match('(?<=\\|)(.*)(?=.\\})',@find_Code)[0];
                                @process_Function_Description_prep_3 = replace(@process_Function_Description_prep_2,@find_Code,@find_CodeValue);
                            }
                            else{
                                @process_Function_Description_prep_3 = @process_Function_Description_prep_2;
                            }
                         
                        /* GET RID of all way too long descriptions, which may have html tables/tags in them */
                            /* Long descriptions that start with ----  */
                                if(string_position(@process_Function_Description_prep_3,"----") != -1){
                                    @newString = substr(@process_Function_Description_prep_3,0,string_position(@process_Function_Description_prep_3,"----"));
                                    @process_Function_Description_prep_4 = @newString;
                                } else{ @process_Function_Description_prep_4 = @process_Function_Description_prep_3; }
                             
                            /* HTML bb codes for table */
                                if(string_position(@process_Function_Description_prep_4,"{|") != -1 && string_position(@process_Function_Description_prep_4,"|}") != -1){
                                    @newString2 = substr(@process_Function_Description_prep_4,0,string_position(@process_Function_Description_prep_4,"{|")).substr(@process_Function_Description_prep_4,(string_position(@process_Function_Description_prep_4,"|}") + 1));
                                    @process_Function_Description_prep_5 = @newString2;
                                } else{ @process_Function_Description_prep_5 = @process_Function_Description_prep_4; }
                             
                        /* Simply aesthetic, but replace single dot as end of line */
                            @process_Function_Description_prep_6 = replace(@process_Function_Description_prep_5,". ",".&#x0a;");
                         
                        /* Replace next line triggers with nothing if on the end of description */
                            if(string_ends_with(@process_Function_Description_prep_6,"&#x0a;")){
                                @process_Function_Description_prepared = replace(@process_Function_Description_prep_6,"&#x0a;","");
                            }
                            else { @process_Function_Description_prepared = @process_Function_Description_prep_6;}
                         
                        /* WRITE TO XML */
                            @file_Write[] = "            <Overload retVal=\""[email protected]_Function_Return."\" descr=\""[email protected]_Function_Description_prepared."\">";
                         
                /* FUNCTION ARGS */
                    /* Process function arguments and write to XML */
                        @process_Function_Args = @function_Args["args"];
                     
                    /* Prepare counters, as we will split parentheses from other arguments */
                        @open_parentheses   = 0;
                        @closed_parentheses = 0;
                        @word = 0;
                        @arguments = array();
                     
                    /* Process arguments */
                        @processArgs = @process_Function_Args;
                        for(@countChar = 0, @countChar < length(@processArgs), @countChar++){
                            @character = @processArgs[@countChar];
                            if(@character == ","){
                                if(@open_parentheses != @closed_parentheses){ @skipWrite = false; }
                                else{ @word++; @skipWrite = true;}
                            }
                            else if(@character == "[") { @open_parentheses++; @skipWrite = false; }
                            else if(@character == "]") { @closed_parentheses++; @skipWrite = false; }
                            else { @skipWrite = false; }
                         
                            if(@skipWrite == false) {
                                if(!array_index_exists(@arguments,@word)){ array_set(@arguments,@word,@character); }
                                else{ @arguments[@word] .= @character; }
                            }
                        }
                    /* Push arguments into XML */
                        foreach(@argument in @arguments){
                            @file_Write[] = "                <Param name=\""[email protected]"\"/>";
                        }
                /* WRITE REST OF XML PORTION for function */
                    @file_Write[] = "            </Overload>"
                    @file_Write[] = "        </KeyWord>"
                 
                }
            /* EVENTS */
                @process_Events = @file_JSON["events"];
             
                foreach(@event in @process_Events){
                /* EVENT NAME */
                    /* Process event name and write to XML */
                        @process_Event_Name = @event;
                        @file_Write[] = "        <KeyWord name=\""[email protected]_Event_Name."\" func=\"yes\">";
                /* WRITE REST OF XML PORTION for event */
                    @file_Write[] = "            <Overload retVal=\"mixed\" descr=\"One of CH&apos;s or extension events.&#x0a;Consult it&apos;s wiki for further information.\">";
                    @file_Write[] = "                <Param name=\"eventData\"/>";
                    @file_Write[] = "            </Overload>"
                    @file_Write[] = "        </KeyWord>"
                }
        /* Finish XML writing */
            @file_Write[] = "   </AutoComplete>";
            @file_Write[] = "</NotepadPlus>";
        /* Write XML file  */
            @writeFile = "";
            foreach(@stringLine in @file_Write){
                @writeFile .= @stringLine."\n";
            }
            chd_write(@file,@writeFile,"OVERWRITE");
            console(colorize("All done! AutoComplete XML file is generated!"),false);
    <<<