Function List Plugin Part 2: Python Parsing Rules

notepadpp 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 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 name="GlobalFunction" subgroup="" icon="9" child="17" autoexp="1" matchcase="1" fendtobbeg="" bbegtobend="">	 
		<Rules regexbeg="^defs+" regexfunc="[w_]+" regexend="" bodybegin=":" bodyend="$" sep="" />	 

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:

Function List with Icons
Function List with Icons

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!


Leave a Reply to Neale Morison Cancel reply

Your email address will not be published. Required fields are marked *