Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

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

    Help wanted · · · – – – · · ·
    3
    3
    168
    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 netoale
      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>
      
      1 Reply Last reply Reply Quote 0
      • MAPJe71
        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
        • AriFijan
          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
          • First post
            Last post
          Copyright © 2014 NodeBB Forums | Contributors