Function List Javascript working with both EC6 'class' Syntax and prototype syntax



  • Hi! I’m using notepad++ from many years, and I’ve never managed to make function list working with objects an JavaScipt. I’ve combined some code I’ve found around the web and I managed to make it works with both EC6 ‘class’ Syntax and prototype syntax. Of course, It’s not perfect, but I wanted to share this with you.
    Have fun !!

        <parser
            displayName="JavaScript"
            id         ="javascript_function"
            commentExpr="(?s:/\*.*?\*/)|(?m-s://.*?$)"
        >
            <!-- <classRange>, ES6 "class" Syntax inspired of typescript : https://github.com/chai2010/notepadplus-typescript/blob/master/functionList.xml   -->
            <classRange
                mainExpr="^\s*(export\s+)?(class|interface)\s+\w+\s*((extends|implements)\s+(\w|\s|,|\.|[^{])*)?\{"
                openSymbole = "\{"
                closeSymbole = "\}"
                displayMode="node">
                <className>
                    <nameExpr expr="(export\s+)?(class|interface)\s+\w+"/>
                    <nameExpr expr="(class|interface)\s+\w+"/>
                    <nameExpr expr="\s+\w+"/>
                    <nameExpr expr="\w+"/>
                </className>
                <!-- Indent only support tab/2space/4space!!! -->
                <!-- tab/2space is best choice!  -->
                <!-- regexp: ^(\t|[ ]{2,4})  -->
                <function
                    mainExpr="(^(\t|[ ]{2,4})((static)\s+)+\w+\s*(\(|\=|:|\?))|(^(\t|[ ]{2,4})\w+\s*(\(|:|\=|\?))">
                    <functionName>
                        <funcNameExpr expr="(^(\t|[ ]{2,4})((static)\s+)+\w+\s*(\(|\=|:|\?))|(\w+\s*(\(|:|\=|\?))"/>
                        <funcNameExpr expr="(\s+\w+\s*(\(|\=|:|\?))|(\w+\s*(\(|:|\=|\?))"/>
                        <funcNameExpr expr="(\s+\w+\s*(\(|\=|:|\?))|(\w+)"/>
                        <funcNameExpr expr="(\s+\w+)|(\w+)"/>
                        <funcNameExpr expr="\w+"/>
                    </functionName>
                </function>
            </classRange>
            <!-- <classRange>, "prototype" syntax imported from :https://community.notepad-plus-plus.org/topic/8647/configure-function-list-to-show-javascript-class-functions-created-with-prototype -->
            <classRange 
                mainExpr="^[\t ]*([_A-Za-z]?[\w_]*)(\.prototype)+[\s]+(=)+[\s]*\{" openSymbole = "\{" closeSymbole = "\}" displayMode="node"
            >
                <className>
                    <nameExpr expr="[_A-Za-z]+[\w_]*"/>
                </className>
                <function mainExpr="^[\t ]*([_A-Za-z]?[\w_])+[\s]*+:+[\s]*+function+[\s]*\("> 
                    <functionName>
                        <funcNameExpr expr="^[\t ]*([_A-Za-z]?[\w_]*)"/>
                    </functionName>
                    </function>
            </classRange>
            <!-- Orgiginal notepad++ functionlist -->
            <function
                mainExpr="((^|\s+|[;\}\.])([A-Za-z_$][\w$]*\.)*[A-Za-z_$][\w$]*\s*[=:]|^|[\s;\}]+)\s*function(\s+[A-Za-z_$][\w$]*)?\s*\([^\)\(]*\)[\n\s]*\{"
            >
                <functionName>
                    <nameExpr expr="[A-Za-z_$][\w$]*\s*[=:]|[A-Za-z_$][\w$]*\s*\(" />
                    <nameExpr expr="[A-Za-z_$][\w$]*" />
                </functionName>
                <className>
                    <nameExpr expr="([A-Za-z_$][\w$]*\.)*[A-Za-z_$][\w$]*\." />
                    <nameExpr expr="([A-Za-z_$][\w$]*\.)*[A-Za-z_$][\w$]*" />
                </className>
            </function>
        </parser>
    


    1. The RegEx (\t|[ ]{2,4}) tries to match a tab, 2 spaces, 3 spaces (!) or 4 spaces.
      To only support “tab/2space/4space” you have to write the RegEx as (\t|\x20{2}|\x20{4}) (allthough not necessary replaced [ ] with \x20).
    2. A <parser> nodes can only contain ONE <classRange>-node.
    3. In [\n\s] the \s makes the \n superfluous as \n is included in \s.

Log in to reply