Notepad++ Making FunctionList work with .js

Viewed 2453

I have installed Notepad++ on Win7, versions 7.3.3 and 7.4.2, with both versions i open a javascript file, navitage to View>FunctionList

Function list appears blank.

I found another Function list on sourcefourge, plugin version 2.1 which works a bit, however, i have to update the language rules and press "try!" button for every JS code file, and then the functions appear.

Please, does someone know how to install Notepad++ and have FunctionList working for .js?

I didn't use AppData, I installed all notepad++ files to the notepad++ directory.

4 Answers

Finaly!! I managed to make function list working with both "prototype" syntax and ECMAScript 6 "class" syntax for object declaration. For that, I used this thread for prototype syntax and typescript detection for EC6 class Syntax

The result is there :

<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>

Tested with notepad++ v7.8.1 Function list to edit is here : C:\Users[YourSessionUser]\AppData\Roaming\Notepad++\functionList.xml

I did not have success with top answer in Notepad 7.6.6 so made the alternative below.

I want to capture functions inside a class (introduced in ECMAScript 2015; see MDN docs) and therefore it can't rely on a function keyword to trigger a match.

The parser I present below is a nasty hack that does not work in all situations and produces occasional false matches. However it is getting the job done for me, and I couldn't find anything better elsewhere online... I hope it helps someone :)


    <parser
        displayName="JavaScript"
        id         ="javascript_function"
        commentExpr="(?s:/\*.*?\*/)|(?m-s://.*?$)"
    >
        <function
            mainExpr="((^|\s+|[;\}\.])([A-Za-z_$][\w$]*\.)*[A-Za-z_$][\w$]*\s*[=:]|^|[\s;\}]+)\s*((function)|[$\t]*)(?(?![\s]*(for|if|while))(\s+[A-Za-z_$][\w$]*)?|(failtomatch))\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>

The changes I made the the mainExpr regular expression have the following effect

  • (function)|[$\t]*) instead of just checking that the keyword function appears before a function name, also allow for zero or more end-lines or tabs to signal the start of a function.
  • (?(?![\s]*(for|if|while))(\s+[A-Za-z_$][\w$]*)?|(failtomatch)) Then, as long as the characters which appear before the (...arguments) are not for, if or while, we can assume that this is a function name. Otherwise cause the entire match to fail by trying to match the string "failtomatch" (which hopefully isn't the actual name of your function) (also if your function name starts with for, if or while it will also be discarded.)
  • Hopefully posting this will get someone to help solve this a bit better - I did my best with my limited knowledge of regex :P
Related