Page 1 of 1

Find QB function or sub based on cursor location

Posted: 06 Oct 2011 22:54
by jgodfrey
In an attempt to assist with this topic:

http://www.rjsoftware.se/Forum/viewtopi ... =11&t=1981

Here's a script that will find a QB function or subroutine named like the word that's currently selected or near the cursor. Since I'm not a QB programmer it may require some additional seasoning, but I think it works as described in the referenced post.

Jeff

Code: Select all

#language C++Script

// Locate the QB FUNCTION or SUB named like the word that's currently
// selected or near the cursor.

// Jeff Godfrey, Sep-2011

WideString GetSelection()
{
   // If there is no selection, create one
   if (Document.SelLength == 0)
   {
      int col = Document.CursorX;
      WideString thisLine = Document.Lines(Document.CursorY);
      // Word is to our right
      if (ScriptUtils.WPosAt(" ", thisLine, col)) { Document.CursorWordRight(true); }
      // Word is to our left
      else if (ScriptUtils.WPosAt(" ", thisLine, col + 1)) { Document.CursorWordLeft(true); }
      // We're within a word
      else { Document.CursorWordLeft(false); Document.CursorWordRight(true); }
   }
   return Trim(Document.SelText()); 
}

// Search through the file looking for either:
// FUNCTION <selection> 
// SUB <selection>
boolean FindMatchingCodeBlock(WideString selection)
{
   for (int i = 0; i < Document.LineCount; i++)
   {
      WideString currLine = Trim(Document.Lines(i));
      if ((ScriptUtils.WPosAt("FUNCTION " + selection, currLine, 1)) ||
          (ScriptUtils.WPosAt("SUB " + selection, currLine, 1)))
      {
         Document.CursorX = 0;
         Document.CursorY = i;
         return true;
      }   
   }
   return false;
}

{
   // Get the selection
   WideString selection = GetSelection();
   
   // If we were unable to get the selection, get out
   if (selection == "") 
   {
      ShowMessage("Unable to determine selection - Exiting...");
      exit;
   }
   
   // Find the FUNCTION or SUB matching our selection
   if (!FindMatchingCodeBlock(selection))
   {
     ShowMessage("Unable to find code block named " + selection);
   }
}



Re: Find QB function or sub based on cursor location

Posted: 07 Oct 2011 01:00
by rjbill
Thanks for doing that.

It should work for QB64 programs, and other Quick BASIC-like programming languages.
(I suppose there could be times when SUB or FUNCTION don't start at the first character)

And it won't be hard to make it find NEXT and PREVIOUS routines, too.

Re: Find QB function or sub based on cursor location

Posted: 07 Oct 2011 01:48
by jgodfrey
rjbill wrote:(I suppose there could be times when SUB or FUNCTION don't start at the first character)
That isn't a problem, as all leading and trailing spaces are removed from the current line before the search is performed. So, as long as SUB or FUNCTION is the first non-whitespace char (which I assume it has to be), it should work fine.
rjbill wrote:And it won't be hard to make it find NEXT and PREVIOUS routines, too.
You're just looking for the next or previous SUB or FUNCTION, unrelated to the any specific SUB or FUNCTION, right? In that case, that seems like a different (and easier) problem, but a separate script. I'd just search up or down from the current cursor location for whatever make sense...

Jeff

Re: Find QB function or sub based on cursor location

Posted: 13 Oct 2011 09:49
by rjbill
I'm thinking I want to assign these functions to a 'command prefix key',
probably F11. So you would press F11, and then another key to execute
the appropriate function, like:

UpArrow = Find Previous Sub/Function
DownArrow = Find Next Sub/Function
F11 = Execute Previous Find (so you can F11-Up, F11-F11, F11-F11, ...)
F12 = Find Sub/Function under cursor or selection
ESC = Cancel F11 'mode'

So I would need to have a "Master Function" assigned to F11,
and have it call the other functions based on the second keypress.

Rather than assign each function to its own command key.

Is that hard to do?

Re: Find QB function or sub based on cursor location

Posted: 13 Oct 2011 17:19
by jgodfrey
rjbill wrote:So I would need to have a "Master Function" assigned to F11,
and have it call the other functions based on the second keypress.

Rather than assign each function to its own command key.

Is that hard to do?
I'm not aware of a way to do that via scripting right now, though maybe Rickard has some tricks up his sleeve. If you could somehow read keystrokes via a script, you could have F11 start the master script and then have it (the script) react appropriately to the various keystrokes. However, I'm not sure that's possible right now.

With that said though, if it were me, I'd probably prefer some separate key assignments for the 3 functions. For instance, I might do something like this:
  • Ctrl+Shift+Up - find previous function or sub
  • Ctrl+Shift+Down - find next function or sub
  • Ctrl+Shift+Right - find function or sub beneath cursor
That way, they're all related and very similar (Ctrl+Shift+<SomeArrow>), but easy to trigger separately.

Jeff