Community
    • Login

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

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    4 Posts 4 Posters 1.5k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • netoale netoaleN Offline
      netoale netoale
      last edited by

      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>
      
      N 1 Reply Last reply Reply Quote 0
      • MAPJe71M Offline
        MAPJe71
        last edited by

        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.
        1 Reply Last reply Reply Quote 4
        • AriFijanA Offline
          AriFijan
          last edited by

          Thanks for your code, it helped me to finally get the function list working for Javascript classes.

          As a reference for others who find this topic, I got the code to work by following this page of the manual. It referenced this page, which mentions that the file structure for use with Notepad++ v7.9.1 and later should be like this:

          <NotepadPlus><functionList> [your parser code goes here] </functionList></NotepadPlus>
          

          I don’t know about the points mentioned by MAPJe71 but it works for me. :-)

          1 Reply Last reply Reply Quote 0
          • N Offline
            NESblast @netoale netoale
            last edited by

            @netoale-netoale This worked fairly well, thank you, but was picking up a few keywords i didn’t want [if, for, switch, …etc]. If you’re having a similar issue, i got Claude to improve it like so:

            <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+)+(?!(if|for|while|do|switch|try|catch|finally|return|break|continue|var|let|const|function|class|import|export|default|throw|delete|typeof|instanceof|in|new|this|super|true|false|null|undefined|async|await|yield|with|debugger|case|else|elseif|endif)\b)[A-Za-z_$][\w$]*\s*(\(|\=|:|\?))|(^(\t|[ ]{2,4})(?!(if|for|while|do|switch|try|catch|finally|return|break|continue|var|let|const|function|class|import|export|default|throw|delete|typeof|instanceof|in|new|this|super|true|false|null|undefined|async|await|yield|with|debugger|case|else|elseif|endif)\b)[A-Za-z_$][\w$]*\s*(\(|:|\=|\?))">
                           <functionName>
                               <funcNameExpr expr="(^(\t|[ ]{2,4})((static)\s+)+(?!(if|for|while|do|switch|try|catch|finally|return|break|continue|var|let|const|function|class|import|export|default|throw|delete|typeof|instanceof|in|new|this|super|true|false|null|undefined|async|await|yield|with|debugger|case|else|elseif|endif)\b)[A-Za-z_$][\w$]*\s*(\(|\=|:|\?))|([A-Za-z_$][\w$]*\s*(\(|:|\=|\?))"/>
                               <funcNameExpr expr="(\s+[A-Za-z_$][\w$]*\s*(\(|\=|:|\?))|([A-Za-z_$][\w$]*\s*(\(|:|\=|\?))"/>
                               <funcNameExpr expr="(\s+[A-Za-z_$][\w$]*\s*(\(|\=|:|\?))|([A-Za-z_$][\w$]*)"/>
                               <funcNameExpr expr="(\s+[A-Za-z_$][\w$]*)|([A-Za-z_$][\w$]*)"/>
                               <funcNameExpr expr="[A-Za-z_$][\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>
                   <!-- Original 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 Reply Last reply Reply Quote 0

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            • First post
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors