Closing brackets

Started by plazmon, April 21, 2015, 06:30:23 PM

Previous topic - Next topic

alex

Try to disable Tools->Options->Formatting->Smart Helpers->Auto-Brackets. Maybe it conflicts.
If does no help, paste the code here and describe how to reproduce -> I will check.
HippoEDIT team
[url="http://www.hippoedit.com/"]http://www.hippoedit.com/[/url]

plazmon

It did not help
How to reproduce:
Type '[]'
Put cursor inside '[|]'
Press ']'
Get '[]|]
Should be '[]|'
The same for quotes and double quotes.
For round and curved bracket everything work as I hope, except when the cursor is in a string (bordered by quotes)

Code (javascript) Select
Application.AddScriptInfo("CFBEB741-D13A-4058-BC05-CA082AC51EDC", "XBrackets", "1.01", "XBrackets plugin");

function onClose(oView, nKey, bUp, bShift, bControl, bAlt) {
        var sel = oView.Selection;
        if (sel.IsEmpty) {
                var style_range = oView.Document.GetStyleFromPos(oView.Position);
                if (!style_range.Style.Text) {
                        var line = oView.Document.GetLine(oView.Position.Line);
                        var caret = oView.Position;
                        if (caret.Pos < line.length  && nKey == ")" && line.charAt(caret.Pos) == ')') {
                            oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
                            return true;
                        }
                        if (caret.Pos < line.lengt && nKey == "]" && line.charAt(caret.Pos) == ']') {
                            oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
                            return true;
                        }
                        if (caret.Pos < line.length && nKey == '}' && line.charAt(caret.Pos) == '}') {
                            oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
                            return true;
                        }
                        if (caret.Pos < line.length && nKey == '\'') {
                            if (line.charAt(caret.Pos) == '\'') {
                                oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
                                return true;
                            }
                            //if (caret.Pos > 0 && line.charAt(caret.Pos-1) == '\'') {
                            //    oView.Document.InsertText(oView.Position, "\'", AddUndoActionType("Type \'"));
                            //    return true;
                            //}
                            //else {
                            //    oView.Document.InsertText(oView.Position, "\'\'", AddUndoActionType("Type double \'"));
                            //    return true;
                            //}
                        }
                        if (caret.Pos < line.length && nKey == '\"') {
                            if (line.charAt(caret.Pos) == '\"') {
                                oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
                                return true;
                            }
                            //if (caret.Pos > 0 && line.charAt(caret.Pos-1) == '\"') {
                            //    oView.Document.InsertText(oView.Position, "\"", AddUndoActionType("Type \""));
                            //    return true;
                            //}
                            //else {
                            //    oView.Document.InsertText(oView.Position, "\"\"", AddUndoActionType("Type double \""));
                            //    return true;
                            //}
                        }
                }
        }
        return false;
}

var oClose = new ShortKey(onClose);

Application.RegisterShortKey(")", oClose, "def_source");
Application.RegisterShortKey("]", oClose, "def_source");
Application.RegisterShortKey("}", oClose, "def_source");
Application.RegisterShortKey("\'", oClose, "def_source");
Application.RegisterShortKey("\"", oClose, "def_source");

alex

#17
In this example, even without testing I see mistyping ;)
Code (javascript) Select
if (caret.Pos < line.lengt && nKey == "]" && line.charAt(caret.Pos) == ']') {
   oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
   return true;
}


In JS you can skip escaping by using another quotes type as "'" or '"'.

It does not work for )]} in strings (and comments etc) by design:
"if (!style_range.Style.Text) {"
HippoEDIT team
[url="http://www.hippoedit.com/"]http://www.hippoedit.com/[/url]

alex

Code (javascript) Select
Application.AddScriptInfo("CFBEB741-D13A-4058-BC05-CA082AC51EDC", "XBrackets", "1.01", "XBrackets plugin");

function onClose(oView, nKey, bUp, bShift, bControl, bAlt) {
        var sel = oView.Selection;
        if (sel.IsEmpty) {
                var style_range = oView.Document.GetStyleFromPos(oView.Position);
var line = oView.Document.GetLine(oView.Position.Line);
var caret = oView.Position;
                if (!style_range.Style.Text) {
                        if (caret.Pos < line.length  && nKey == ")" && line.charAt(caret.Pos) == ')') {
                            oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
                            return true;
                        }
                        if (caret.Pos < line.length && nKey == "]" && line.charAt(caret.Pos) == ']') {
                            oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
                            return true;
                        }
                        if (caret.Pos < line.length && nKey == '}' && line.charAt(caret.Pos) == '}') {
                            oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
                            return true;
                        }
                } else {
if (caret.Pos < line.length && nKey == "'") {
                            if (line.charAt(caret.Pos) == "'") {
                                oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
                                return true;
                            }
                            //if (caret.Pos > 0 && line.charAt(caret.Pos-1) == '\'') {
                            //    oView.Document.InsertText(oView.Position, "\'", AddUndoActionType("Type \'"));
                            //    return true;
                            //}
                            //else {
                            //    oView.Document.InsertText(oView.Position, "\'\'", AddUndoActionType("Type double \'"));
                            //    return true;
                            //}
                        }
                        if (caret.Pos < line.length && nKey == '"') {
                            if (line.charAt(caret.Pos) == '"') {
                                oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
                                return true;
                            }
                            //if (caret.Pos > 0 && line.charAt(caret.Pos-1) == '\"') {
                            //    oView.Document.InsertText(oView.Position, "\"", AddUndoActionType("Type \""));
                            //    return true;
                            //}
                            //else {
                            //    oView.Document.InsertText(oView.Position, "\"\"", AddUndoActionType("Type double \""));
                            //    return true;
                            //}
                        }
}
        }
        return false;
}

var oClose = new ShortKey(onClose);

Application.RegisterShortKey(")", oClose, "def_source");
Application.RegisterShortKey("]", oClose, "def_source");
Application.RegisterShortKey("}", oClose, "def_source");
Application.RegisterShortKey("'", oClose, "def_source");
Application.RegisterShortKey('"', oClose, "def_source");
HippoEDIT team
[url="http://www.hippoedit.com/"]http://www.hippoedit.com/[/url]

plazmon

Hi Alex!
Thank you very much for a lesson of JS, and finding my typos (shame on me)  :)
May I continue my education?
1. What is a sense of moving defenises of var line and caret upper, just aesthetics?
2. What "style_range.Style.Text"  means and how to understand that  Pos is in string?
3. Even in current variant script must work for situation ""|" and skips last quote, (it is out of string), but it does not.
Thank you again for you support.

alex

Quote1. What is a sense of moving definitions of var line and caret upper, just aesthetics?
Yes. Only aesthetics, and to keep code smaller while, this parts shall be duplicated for both branches.

Quote2. What "style_range.Style.Text"  means and how to understand that  Pos is in string?
You can get more information about scripting objects in wiki.
Here properties of style object correspond to properties of style definition from syntax schema.
To find out that current style range is "string" you can check "id" property.

Quote3.  Even in current variant script must work for situation ""|" and skips last quote, (it is out of string), but it does not.?
In this case you can avoid at all checking of id or Text property and do the code always. Now, because I have moved the code to be only checked inside of the "text" styles, your example will not work.


HippoEDIT team
[url="http://www.hippoedit.com/"]http://www.hippoedit.com/[/url]

plazmon

Hi!
Thank you for your help in my eduction of JS programming!
I don't know why but my code don't work correctly at this situation
"\n"|)
After pressing ) it don't skip it but add another
"\n")|)
And after pressing ")" all string in quotes are highlighted for a while.

alex

If your code corresponds to one pasted by me before, than probably the reason specific of the GetStyleFromPos... It works same way as internal function and searches for matching style regions inclusive end borders of the range (n..m]. And in example case position corresponds to m, and so returned.

I will evaluate, if this can be changed for script calls (such behaviour may be not really obvious) but workaround may be to check this special case on style check call as this:
Code (javascript) Select
Application.AddScriptInfo("CFBEB741-D13A-4058-BC05-CA082AC51EDC", "XBrackets", "1.01", "XBrackets plugin");

function onClose(oView, nKey, bUp, bShift, bControl, bAlt) {
var sel = oView.Selection;
if (sel.IsEmpty) {
var line = oView.Document.GetLine(oView.Position.Line);
var caret = oView.Position;
var style_range = oView.Document.GetStyleFromPos(caret);
if (style_range.Range.Right == caret.Pos) style_range = oView.Document.GetStyleFromPos(new Position(caret.Line, caret.Pos + 1));
if (!style_range.Style.Text) {
Output().writeln("Not-Text");
if (caret.Pos < line.length && nKey == ")" && line.charAt(caret.Pos) == ')') {
oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
return true;
}
if (caret.Pos < line.length && nKey == "]" && line.charAt(caret.Pos) == ']') {
oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
return true;
}
if (caret.Pos < line.length && nKey == '}' && line.charAt(caret.Pos) == '}') {
oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
return true;
}
} else {
Output().writeln("Text");
if (caret.Pos < line.length && nKey == "'") {
if (line.charAt(caret.Pos) == "'") {
oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
return true;
}
//if (caret.Pos > 0 && line.charAt(caret.Pos-1) == '\'') {
//    oView.Document.InsertText(oView.Position, "\'", AddUndoActionType("Type \'"));
//    return true;
//}
//else {
//    oView.Document.InsertText(oView.Position, "\'\'", AddUndoActionType("Type double \'"));
//    return true;
//}
}
if (caret.Pos < line.length && nKey == '"') {
if (line.charAt(caret.Pos) == '"') {
oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
return true;
}
//if (caret.Pos > 0 && line.charAt(caret.Pos-1) == '\"') {
//    oView.Document.InsertText(oView.Position, "\"", AddUndoActionType("Type \""));
//    return true;
//}
//else {
//    oView.Document.InsertText(oView.Position, "\"\"", AddUndoActionType("Type double \""));
//    return true;
//}
}
}
}
return false;
}

var oClose = new ShortKey(onClose);

Application.RegisterShortKey(")", oClose, "def_source");
Application.RegisterShortKey("]", oClose, "def_source");
Application.RegisterShortKey("}", oClose, "def_source");
Application.RegisterShortKey("'", oClose, "def_source");
Application.RegisterShortKey('"', oClose, "def_source");


I have added Output().writeln("") for debug purposes. I think this (or similar alert("") ) is only a way to "debug" the flow now. 

Highlighting of the text between pair brackets (only if distance is more than 5 chars as far as I remember) is by design. Controlled with Code Completion -> Matching Braces.

BR, Alex.
HippoEDIT team
[url="http://www.hippoedit.com/"]http://www.hippoedit.com/[/url]

plazmon

Hi!
This is the code

Code (javascript) Select
Application.AddScriptInfo("CFBEB741-D13A-4058-BC05-CA082AC51EDC", "XBrackets", "1.01", "XBrackets plugin");

function onClose(oView, nKey, bUp, bShift, bControl, bAlt) {
        var sel = oView.Selection;
        if (sel.IsEmpty) {
                var style_range = oView.Document.GetStyleFromPos(oView.Position);
                var line = oView.Document.GetLine(oView.Position.Line);
                var caret = oView.Position;
                if (!style_range.Style.Text) {
                    Output().writeln("Non-Text");
                    if (caret.Pos < line.length  && nKey == ")" && line.charAt(caret.Pos) == ')') {
                        oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
                        return true;
                    }
                    if (caret.Pos < line.length && nKey == "]" && line.charAt(caret.Pos) == ']') {
                        oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
                        return true;
                    }
                    if (caret.Pos < line.length && nKey == '}' && line.charAt(caret.Pos) == '}') {
                        oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
                        return true;
                    }
                }
                if (caret.Pos < line.length && nKey == "'") {
                    if (line.charAt(caret.Pos) == "'") {
                        oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
                        return true;
                    }
                    if (caret.Pos > 0 && line.charAt(caret.Pos-1) == "'") {
                        oView.Document.InsertText(oView.Position, "'", AddUndoActionType("Type '"));
                        return true;
                    }
                    else {
                        oView.Document.InsertText(oView.Position, "''", AddUndoActionType("Type double '"));
                        oView.Position = new Position(oView.Position.Line, caret.Pos+1);
                        return true;
                    }
                }
                if (caret.Pos < line.length && nKey == '"') {
                    if (line.charAt(caret.Pos) == '"') {
                        oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
                        return true;
                    }
                if (caret.Pos > 0 && line.charAt(caret.Pos-1) == '"') {
                    oView.Document.InsertText(oView.Position, '"', AddUndoActionType('Type "'));
                    return true;
                }
                else {
                    oView.Document.InsertText(oView.Position, '""', AddUndoActionType('Type double "'));
                    oView.Position = new Position(oView.Position.Line, caret.Pos+1);
                    return true;
                }
                }
        }
        return false;
}

var oClose = new ShortKey(onClose);

Application.RegisterShortKey(")", oClose, "def_source");
Application.RegisterShortKey("]", oClose, "def_source");
Application.RegisterShortKey("}", oClose, "def_source");
Application.RegisterShortKey("'", oClose, "def_source");
Application.RegisterShortKey('"', oClose, "def_source");


After pressing )-key in the situation ("\n"|) it output nothing
in the situation ("\ n"|) it output "Non-Text and behaves as expected
It seems that \n blocks finding of text ent

alex

Yes, your are right, there is a bug. I have alsoe noticed that some time ago during testing.
Not \n blocks it, but additional style inside (escaped symbol marked in bold style).
I will fix that. In addition to that, search for style from pos takes ranges as [n..m) (by design), but not as (n..m] as I have written before.
So, maybe if the bug will be fixed, you will not need any workaround.
HippoEDIT team
[url="http://www.hippoedit.com/"]http://www.hippoedit.com/[/url]