Page 1 of 1

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

Posted: 19 Jul 2018 14:19
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

var clipContent = Clipboard.AsText;

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
            Document.CursorY = lineNumber + 15;
            Document.CursorY = lineNumber - 1;
        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.