[script] Easy goto source file shown in app's error message

Ask questions about how to create a script or swap scripts with other users.
Post Reply
User avatar
pjj
Posts: 2109
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

[script] Easy goto source file shown in app's error message

Post by pjj »

Sometimes I got PHP error/warning message (in the browser), that reads like this:

Code: Select all

Notice: Undefined offset: 0 in D:\wwwJobs\my_project\app\themes\default\admin.tpl.php on line 117
It would be cool to be able to copy this message to clipboard, switch back to RJ TE and immediately be taken to the appropriate line of the file in question. Luckily, current script engine enables this; all you need to do is to add script presented below to RJ TE and then click on its icon (I have also requested that scripts could be run on program focus; hopefully Rickard will add it and then no click on icon would be even necessary). Here's the appropriate code:

Code: Select all

Clipboard.Open;
var clipContent = Clipboard.AsText;
Clipboard.Close;

if (clipContent == "") {
    ShowMessage("*** Clipboard is empty!");
}
else {
    var regex1 = "in [C-Z]:\\\\([a-zA-Z0-9\-\_\.\\\\]*)";
    var regex2 = "on line [0-9]{1,5}";

    var n1, n2;
    var fileNameStr = "";
    var lineNumberStr = "";

    n1 = ScriptUtils.RegExPos(regex1, clipContent, fileNameStr);

    if (n1 > 0) {
        var fileName = Copy(fileNameStr, 4, Length(fileNameStr));
        n2 = ScriptUtils.RegExPos(regex2, clipContent, lineNumberStr);
        if (n2 > 0) {
            var lineNumber = Copy(lineNumberStr, 9, Length(lineNumberStr));
            lineNumber = StrToInt(lineNumber);
            // open file
            MainApp.OpenFile(fileName);
            Document.CursorY = lineNumber + 15;
            Document.CursorY = lineNumber - 1;
            Clipboard.Clear;
        }
        else {
            ShowMessage("ERROR: Line number not found in the clipboard content!");
        }
    }
}
If first regex (i.e. file path & name) isn't found in clipboard's content, script gracefully terminates. If file path is found, but no line number, script shows error message. If both are found, appropriate file is open, and caret is placed on the offending line; clipboard is then cleared.

You may need to tweak it a bit, though, namely change regex1 and regex2 variables (if error messages in the programming language you use look differently), and then second arguments of two Copy() calls.

Btw. these two lines

Code: Select all

            Document.CursorY = lineNumber + 15;
            Document.CursorY = lineNumber - 1;
are my (somewhat miserable) attempt at placing found line in the middle of the screen.

Finally, when it is possible to run this script on program focus, message stating that clipboard is empty would be a nuisance (obviously), so it should be removed.

I hope you'll find it useful.
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
Post Reply