Function List Plugin Part 2: Python Parsing Rules
The previous article on Function List Plugin dealt with setting up the plugin on Notepad++ 5.3. In this article, I show you how to set up parsing rules and tree view icons for the Function List Plugin in Notepad++ 5.3. The rules shown here are for code written in Python.
Parsing Classes
The default Python parsing rules in Function List only identify functions and don’t parse the context of the function. The rules I will show here are only a slight improvement, but an improvement nonetheless. When you are done updating the parsing rules for Python, you will be able to break your code into constituent Classes, the functions within the classes, and functions defined in the global context. The rules won’t be able to parse nested functions or classes. If you come up with a clever way of parsing those, please do let me know and I’ll update this article with that information. The challenge is that Python does not use explicit code block delimiters, and it looks like the parser does not support backreferences.
Now on to setting up the Class parsing rule. Bring up the Language Parsing Rules dialog box from Plugins->Function List->Language Parsing Rules. Select ‘Python’ from the list box on the left. Select the default parsing group (DEFINE) from the Group list box and click ‘Delete’. Now, click ‘Add’ and move the cursor over to the ‘Name’ text box under Group Properties. Change the name to ‘Class’. Check the ‘Auto Expand’ and ‘Match Case’ check boxes. Type the following regular expressions in the corresponding text boxes under :
Function Begin: ^class\s+
Function List Name: [\w_]+
Leave all the other text boxes blank.
Parsing Functions
At this point, if you open a Python source code file in Notepad++ and bring up the Function List pane (Plugins->Function List->List), you will get a list of all top level classes. Make sure you have the ‘View as Tree’ option selected. Now, we will add rules to parse all functions within the classes. Add a new Group as above, and set the name to ‘Function’. Set the ‘Subgroup of’ value to ‘Class’. Check ‘Auto Expand’ and ‘Match Case’ and type in the following regular expressions:
Function Begin: ^[\s\t]+def\s+
Function List Name: [\w_]+
Body Begin: :
Body End: $
Parsing Global Functions
If you use global functions, you need to add one more parsing rule to list them separately. Add a new Group and name it ‘GlobalFunction’. Check ‘Auto Expand’ and ‘Match Case’ and type in the following regular expressions:
Function Begin: ^def\s+
Function List Name: [\w_]+
Body Begin: :
Body End: $
Now you are all set to view the classes, class functions and global functions.
Adding Icons
If you have followed the tutorial till now, you will have a list of classes and functions. But the list looks very bland. There is a way to snazz it up by assigning icons to the listed items. Unfortunately, the icon assignment cannot be done through the user interface. You will have to manually edit the rules XML file (FunctionListRules.xml) in a text editor other than Notepad++. If you open the XML file within Notepad++, the plugin writes its current settings when you exit Notepad++. So, any changes you make to the XML file will get overwritten. I’ve used Windows Notepad with success, but any other plain text editor should work as well.
The icons are read from a bitmap file, renamed with the extension ‘.flb’ instead of ‘.bmp’. A sample icon list is included in the plugin package – C++.flb. We will assign icons from that file for our python configuration. You can also create your own icon list – just make sure it’s a standard windows Bitmap image of height 16 pixels and width 16xN where N is the number of icons. Also, make sure you change the extension of the bitmap to .flb.
When assigning the icons in the XML file, it helps to have the icon bitmap file open. Keep in mind that the first 16×16 square is icon 0. Open the XML file in a text editor and search for ‘python’. When you locate the correct section, update the ‘icon’ and ‘child’ values to match the ones in the listing below.
<Language name="Python" imagelistpath="C:Program FilesNotepad++pluginsConfigC++.flb"> <CommList param1="#" param2="" /> <Group name="Class" subgroup="" icon="1" child="11" autoexp="1" matchcase="1" fendtobbeg="" bbegtobend=""> <Rules regexbeg="^classs+" regexfunc="[w_]+" regexend="" bodybegin="" bodyend="" sep="" /> </Group> <Group name="Function" subgroup="Class" icon="9" child="17" autoexp="1" matchcase="1" fendtobbeg="" bbegtobend=""> <Rules regexbeg="^[st]+defs+" regexfunc="[w_]+" regexend="" bodybegin=":" bodyend="$" sep="" /> </Group> <Group name="GlobalFunction" subgroup="" icon="9" child="17" autoexp="1" matchcase="1" fendtobbeg="" bbegtobend=""> <Rules regexbeg="^defs+" regexfunc="[w_]+" regexend="" bodybegin=":" bodyend="$" sep="" /> </Group> </Language> |
I’m assuming you are using C++.flb for your icon list – if you have your own icon list, adjust the values accordingly. The ‘child’ icon number indicates which icon will be used for each item when you expand the group in Tree view. Also, notice the ‘imagelistpath’ value. You can either type in the value in the XML file directly, or select the .flb file by checking the ‘Bitmap List’ checkbox under ‘Language Settings’ in the Language Parsing Rules dialog box.
That is it! You are done. The resulting Function List pane should now look something like this:

PS: It looks like WordPress ate up the XML code for configuring the icons at some point! I’ve restored the actual code, but if you see anomalies, please do leave me a comment.
PPS: WordPress keeps eating up the XML code without even me touching the post!! How Bizzare!
A great post, thank you!
I followed your instructions, step by step using NP++ 5.1.1 with FunctionList 2 alpha and couldn’t make Functions to be nested in Classes. As soon as I set Function as ‘Subgroup of’ Classes, Function list disappears. When I deselect Subgroup of, Functions are displayed normally, but as a separate branch of the tree.
On NP++ 5.3.*, FunctionList’s window is absolutely blank. I’m using Vista.
Do you have the ‘view as tree’ option selected? That is necessary for displaying subgroups. Check Part 1 of the article. Also, I hope you are trying to parse Python code – the parse regexs will obviously be different for other languages. Sometimes, people overlook obvious things, so just want to make sure.
Yes, in Plugins/Function List menu I have selected: List, Show All Functions, View List as Tree. I’ve copied your xml fragment to FunctionListRules.xml in plugins/Config, so I can see all classes, functions and global functions with icons. And I doublechecked each of them have the same regexes and checked checkboxes as you provided.
I’m working with valid python code.
It’s only that when I set Function’s ‘subgroup of’ to ‘Class’, the list of Functions disappears. Then I see only Classes and Globals..
oh, wow. I don’t really understand what has just happened, I was just playing around with menu items under some class name in Function List, I randomly checked/unchecked sort alphabetically, insert to position and suddenly the list of class methods appeared under their class name. Looks like it needed a slight push to get everything in place.
Keep in mind you are using an alpha build of the Function List plugin. When I get some time on hand, I would like to get the code from Jens Lorenz and bring it at least to a beta build. It is a very useful plugin for programmers, so there is great value in keeping it updated.
I second the idea of keeping the Function List plugin updated.
I can’t understand why it is not even a standard plugin in Notepad++.
Hi,
I like your post 😉
I’m trying to configure the rules for Javascript, and I don’t get it 🙁
I have defined next:
GROUP NAME: Functions
RULE 1:
======
Function Begin: function[s&]+
Function List Name: ["w_]+
Function End: s*(.*)
Separator Between: ;
Body Begin: {
Body End: }
RULE 2:
======
Function Begin: ^s*
Function List Name: ["w_.]+
Function End: s*[=:]s*functions*([w_$&=',s]*)
Separator Between: ;
Body Begin: {
Body End: }
———————————————
Only I’m see “Functions” name 🙁 🙁 🙁
Any idea??
Thanks a lot!!
@albert
Hi Albert,
I have been somewhat occupied the last few days, so haven’t had a chance to explore your parsing rules. I will probably be able to look at it in a few days. Just in case you aren’t reading the comments from this post, I’ll email you to let you know what I find out. I cleaned up your comment post, so no worries about the double pasting.
Hi…your post was great — I think I understand Regex more now…however, for ASP, I can’t get the Function List to display. I have:
Basically, this should be identical to VB…but no matter what I do, nothing shows up. This is FunctionList 2 Beta, Notepad++ 5.4.3. I see “Function List – 100%”, but no functions or subroutines are visible.
Would you be kind enough to help?
Thank you,
.eric
Whoops…something ate all my Regex stuff; here it is:
LESSTHANLanguage name=”ASP” imagelistpath=””GREATERTHAN
LESSTHANCommList param1=”'” param2=”” /GREATERTHAN
LESSTHANCommList param1=”//” param2=”” /GREATERTHAN
LESSTHANGroup name=”FUNCTION” subgroup=”” icon=”0″ child=”0″ autoexp=”1″ matchcase=”0″ fendtobbeg=”” bbegtobend=”” keywords=””GREATERTHAN
LESSTHANRules regexbeg=”<functions+” regexfunc=”[w_]+” regexend=”s*(.*)” bodybegin=”” bodyend=”<ends+function>” sep=”” /GREATERTHAN
LESSTHANRules regexbeg=”<subs+” regexfunc=”[w_]+” regexend=”s*(.*)” bodybegin=”” bodyend=”<ends+sub>” sep=”” /GREATERTHAN
LESSTHAN/GroupGREATERTHAN
LESSTHAN/LanguageGREATERTHAN
@hooeezit Ok. Thanks!!
On Notepad++ v5.4.4 and Function List v 2.0 BETA I couldn’t make class functions get nested into class branches, they were shown just as a separate branch in the list.
This is what helped me to fix it:
– ‘Subgroup’ field for ‘Class’ is ‘Function’
– ‘Subgroup’ field for ‘Function’ is empty
– with Python source opened in NP++ select another language scheme in Plugins->Function List->Language Parsing Rules and press ‘Try!’ (I tried Perl scheme). Then select Python scheme and press ‘Try!’. At this point all class functions were shown correctly.
Obviously there are some issues with the plugin but I’m happy I’ve managed to set it up.
Just to let you know: in version 2 beta, in “language parsing rule” section you can set the bitmap you want to use for every “group”.
for every language in the “MISC settings” (is the first option) you can set the flb file to use. once selected, near the name of the group will appear an icon. clicking on it you can change the assigned icon.
@cioma
A great post. Thanks hooeezit and cioma!
hooeezit, you might want to verify the comment of cioma. It helped me set-up Function List v2.0 BETA on Notepad++ v5.4.5. I guess there might be a misunderstanding about the meaning of the ‘subgroup’ field: ‘subgroup of’ vs. ‘has subgroup’
Hi,
I tried this for php with no success.
Anyone ideas ? Solutions ?
Thanks in advance.
notepad++ @ windows 7
C:UsersLixasAppDataRoamingNotepad++pluginsconfigFunctionListRules.xml
Working configuration:
In case if this do not look right, you can get working verson here
http://pastebin.com/mcd1df81
TiGeR, you can follow Lixas’ suggestion. Open FunctionListRules.xml and see what are the matches on PHP.
I did more simple because I like to see the parameters expected by the function, so:
Function Begin:function[ ]
Function List Name:.+
Seperator Between:;
Body Begin:{
Body End:}
albert, it can also works for javascript.
After fooling around a little I came up with this, which is based on this and other python rules. It works for me:
I’ll try that again. Here is a set of Python rules that works. Here’s hoping the code doesn’t get eaten by this content management system.
<Language name=”Python” imagelistpath=”%APPDATA%Notepad++pluginsconfigC++.flb”>
<CommList param1=”#” param2=”” />
<Group name=”Class” subgroup=”Method” icon=”1″ child=”11″ autoexp=”4″ matchcase=”1″ fendtobbeg=”” bbegtobend=”” keywords=””>
<Rules regexbeg=”^classs+” regexfunc=”[w_]+” regexend=”” bodybegin=”” bodyend=”” sep=”” />
</Group>
<Group name=”Method” subgroup=”” icon=”9″ child=”17″ autoexp=”1″ matchcase=”1″ fendtobbeg=”” bbegtobend=”” keywords=””>
<Rules regexbeg=”^s+defs+” regexfunc=”[w_]+” regexend=”” bodybegin=”:” bodyend=”$” sep=”” />
</Group>
<Group name=”GlobalFunction” subgroup=”” icon=”9″ child=”17″ autoexp=”1″ matchcase=”1″ fendtobbeg=”” bbegtobend=”” keywords=””>
<Rules regexbeg=”^defs+” regexfunc=”[w_]+” regexend=”” bodybegin=”:” bodyend=”$” sep=”” />
</Group>
</Language>
Note:
If an < appears above you need to replace it with a left pointing angle bracket.
Replace > with a right pointing angle bracket.
%APPDATA% should be replaced with the dir in your APPDATA environment variable, e.g.
C:Documents and SettingsYourNameApplication Data
Hooeezit and Neil great post and fix.
I want to thank you both.
Now finally I can enjoy working on python with Notepad++.
🙂 -> that’s mee smiling