Find QB function or sub based on cursor location
Posted: 06 Oct 2011 22:54
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
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);
}
}