Saturday, January 22, 2011

PEM Editor tip: Go To Definition (Enhanced!)

'Go To Definition', introduced in Version 6.00, has been significantly enhanced in Version 6.50.

The 'things' for which you can 'Go To Definition' have gone beyond the original list of methods, properties, and objects to include creating new methods and properties, and finding PRGs, procedures, and functions, defined constants, classes, forms, and parent code. There is also a provision for customization using plug-ins.

The mechanics are simple:
  • Click on the name of the 'thing' (you can highlight it, if desired, but that is not necessary).
  • Initiate "Go To Definition", either by using the PEM Editor menu (in the VFP Main Menu) or by using the Hot Key (which you can assign in the Preferences form).

Note that Go To Definition will work in method code, PRGs, and also the Command Window.

Methods, Objects, and Properties
  • For methods, it will open up the method for editing, if there is custom code; otherwise it will open up a window showing all the parent code for the method.
  • For objects, it will act as if you selected the object in the form or from Document TreeView.
  • For properties, it will act as if you selected the object in the form or from Document TreeView and then highlights the property in the grid.
In the example below, the cursor is within the method name 'GridToExcelValue', and invoking 'Go To Definition' will bring up the code for that method.


Notes:

In the example below, the WITH/ENDWITH construction is analyzed so that the definition being searched for is actually This.Parent.cmdSave.DoClick.  This analysis will include embedded WITH/ENDWITH constructions.

However, VFP does not provide any way to know what 'THIS' refers to in a editor window; that is, there is no information available that gives a reference to the object that the method is attached to. Thus, PEM Editor changes the title bar of any method that it opens in anticipation of any use of Go To Definition.



Finally, there is no way to determine what object is referred to by code like  loObject.Refresh(). Thus, at times, the wrong definition may be shown.

Creating New Methods or Properties

If the search for the property, method, or object was unsuccessful, you will then be prompted about whether you want to create a new property or method. This allows you to do so, using only a few keystrokes, without leaving the method you are editing.

Consider this example, when creating the first few lines of the Init method for a form:


Using Go To Definition pops up the following form:


Note that this is a direct but not necessarily complete way of adding properties or methods.  If you want to change the default value of the property, set the description, etc, you will still have to use the normal method of doing so; of course, you can still use this technique and come back to edit the property or method later.

PRGs, Procedures, Functions

In the sample code below, 'OpenTable' could be a PRG, or a procedure or function within a PRG; In any of these cases, Go To Definition will open the PRG for you (and, if it's a procedure or function within a PRG, will highlight the line where the definition starts).


Defined Constants

Similarly, selecting a name of a defined constant (created by #Define), whether the definition occurs in the current code or in (nested) #include files, will find and highlight that definition.

Note that for all of these possibilities, the active project, if any, is searched, along with the current path and settings for 'Set Procedure' and 'Set ClassLib', as appropriate

Thanks to Doug Hennig for idea for finding PRGS, procedures, functions, and constants, and also providing the code to implement it!

Classes

Using Go To Definition on either NewObject of Createobject will open the class named in the parameters that follow.  In the example below, using Go To Definition on 'NewObject' would open class 'ParameterX'; using it on 'CreateObject' would use it on class 'clsUpdateFoxCode'.


Forms

Using Go To Definition on the name of a form (that is, following 'Do Form') will open that form for editing..


Parent Code (DoDefault)

Using Go To Definition on DoDefault will open up a window showing all the parent code for the current method.

Customization Using Plug-Ins

If none of the searches described above are successful, PEM Editor will look for a plug-in which can do its own search for the selected 'thing'. It looks first for a file named PEME_GoToDefinition.PRG anywhere in the path. If this fails, it then looks for a file named GoToDefinition.PRG in the 'Live Plug-Ins' folder. (There is a commented sample GoToDefinition.PRG in the 'Sample Plug-Ins' folder, which can be copied to the 'Live Plug-Ins' folder and modified).

The PRG is called with three parameters -- the text being searched, the text (on the same line) preceding the text being searched for, and the text (on the same line) following the text being searched for. The PRG can then do, well, just about anything -- bring up a form, open a table, modify the code in the method, etc.

The sample, for instance, will assume that the text being searched for is a name, either of a cursor, table, or class.  If an alias by that name is already in use, it will open a form which has a browse window and a grid showing the table structure; or, if a table by that name exists, it will open the table and show the same form; or, finally, it will attempt to open and edit a class by that name (using one of the methods from _oPEMEditor.oTools -- a set of tools, soon to be documented, that is also part of this release).

(I welcome comments about other inventive uses for this plug-ins.)

No comments:

Post a Comment