Author Topic: Closing brackets  (Read 2905 times)

Offline alex

  • Developer
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2172
  • Karma: +37/-3
    • View Profile
    • HippoEDIT
Re: Closing brackets
« Reply #15 on: May 08, 2015, 02:10:17 pm »
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.

Offline plazmon

  • Newbie
  • *
  • Posts: 25
  • Karma: +0/-0
    • View Profile
Re: Closing brackets
« Reply #16 on: May 08, 2015, 03:31:54 pm »
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
  1. Application.AddScriptInfo("CFBEB741-D13A-4058-BC05-CA082AC51EDC", "XBrackets", "1.01", "XBrackets plugin");
  2.  
  3. function onClose(oView, nKey, bUp, bShift, bControl, bAlt) {
  4.         var sel = oView.Selection;
  5.         if (sel.IsEmpty) {
  6.                 var style_range = oView.Document.GetStyleFromPos(oView.Position);
  7.                 if (!style_range.Style.Text) {
  8.                         var line = oView.Document.GetLine(oView.Position.Line);
  9.                         var caret = oView.Position;
  10.                         if (caret.Pos < line.length  && nKey == ")" && line.charAt(caret.Pos) == ')') {
  11.                             oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
  12.                             return true;
  13.                         }
  14.                         if (caret.Pos < line.lengt && nKey == "]" && line.charAt(caret.Pos) == ']') {
  15.                             oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
  16.                             return true;
  17.                         }
  18.                         if (caret.Pos < line.length && nKey == '}' && line.charAt(caret.Pos) == '}') {
  19.                             oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
  20.                             return true;
  21.                         }
  22.                         if (caret.Pos < line.length && nKey == '\'') {
  23.                             if (line.charAt(caret.Pos) == '\'') {
  24.                                 oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
  25.                                 return true;
  26.                             }
  27.                             //if (caret.Pos > 0 && line.charAt(caret.Pos-1) == '\'') {
  28.                             //    oView.Document.InsertText(oView.Position, "\'", AddUndoActionType("Type \'"));
  29.                             //    return true;
  30.                             //}
  31.                             //else {
  32.                             //    oView.Document.InsertText(oView.Position, "\'\'", AddUndoActionType("Type double \'"));
  33.                             //    return true;
  34.                             //}
  35.                         }
  36.                         if (caret.Pos < line.length && nKey == '\"') {
  37.                             if (line.charAt(caret.Pos) == '\"') {
  38.                                 oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
  39.                                 return true;
  40.                             }
  41.                             //if (caret.Pos > 0 && line.charAt(caret.Pos-1) == '\"') {
  42.                             //    oView.Document.InsertText(oView.Position, "\"", AddUndoActionType("Type \""));
  43.                             //    return true;
  44.                             //}
  45.                             //else {
  46.                             //    oView.Document.InsertText(oView.Position, "\"\"", AddUndoActionType("Type double \""));
  47.                             //    return true;
  48.                             //}
  49.                         }
  50.                 }
  51.         }
  52.         return false;
  53. }
  54.  
  55. var oClose = new ShortKey(onClose);
  56.  
  57. Application.RegisterShortKey(")", oClose, "def_source");
  58. Application.RegisterShortKey("]", oClose, "def_source");
  59. Application.RegisterShortKey("}", oClose, "def_source");
  60. Application.RegisterShortKey("\'", oClose, "def_source");
  61. Application.RegisterShortKey("\"", oClose, "def_source");

Offline alex

  • Developer
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2172
  • Karma: +37/-3
    • View Profile
    • HippoEDIT
Re: Closing brackets
« Reply #17 on: May 08, 2015, 03:41:58 pm »
In this example, even without testing I see mistyping ;)
Code: Javascript
  1. if (caret.Pos < line.lengt && nKey == "]" && line.charAt(caret.Pos) == ']') {
  2.    oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
  3.    return true;
  4. }

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) {"
« Last Edit: May 08, 2015, 03:45:40 pm by alex »

Offline alex

  • Developer
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2172
  • Karma: +37/-3
    • View Profile
    • HippoEDIT
Re: Closing brackets
« Reply #18 on: May 08, 2015, 03:50:38 pm »
Code: Javascript
  1. Application.AddScriptInfo("CFBEB741-D13A-4058-BC05-CA082AC51EDC", "XBrackets", "1.01", "XBrackets plugin");
  2.  
  3. function onClose(oView, nKey, bUp, bShift, bControl, bAlt) {
  4.         var sel = oView.Selection;
  5.         if (sel.IsEmpty) {
  6.                 var style_range = oView.Document.GetStyleFromPos(oView.Position);
  7.                                 var line = oView.Document.GetLine(oView.Position.Line);
  8.                                 var caret = oView.Position;
  9.                 if (!style_range.Style.Text) {
  10.                         if (caret.Pos < line.length  && nKey == ")" && line.charAt(caret.Pos) == ')') {
  11.                             oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
  12.                             return true;
  13.                         }
  14.                         if (caret.Pos < line.length && nKey == "]" && line.charAt(caret.Pos) == ']') {
  15.                             oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
  16.                             return true;
  17.                         }
  18.                         if (caret.Pos < line.length && nKey == '}' && line.charAt(caret.Pos) == '}') {
  19.                             oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
  20.                             return true;
  21.                         }
  22.                 } else {
  23.                                                 if (caret.Pos < line.length && nKey == "'") {
  24.                             if (line.charAt(caret.Pos) == "'") {
  25.                                 oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
  26.                                 return true;
  27.                             }
  28.                             //if (caret.Pos > 0 && line.charAt(caret.Pos-1) == '\'') {
  29.                             //    oView.Document.InsertText(oView.Position, "\'", AddUndoActionType("Type \'"));
  30.                             //    return true;
  31.                             //}
  32.                             //else {
  33.                             //    oView.Document.InsertText(oView.Position, "\'\'", AddUndoActionType("Type double \'"));
  34.                             //    return true;
  35.                             //}
  36.                         }
  37.                         if (caret.Pos < line.length && nKey == '"') {
  38.                             if (line.charAt(caret.Pos) == '"') {
  39.                                 oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
  40.                                 return true;
  41.                             }
  42.                             //if (caret.Pos > 0 && line.charAt(caret.Pos-1) == '\"') {
  43.                             //    oView.Document.InsertText(oView.Position, "\"", AddUndoActionType("Type \""));
  44.                             //    return true;
  45.                             //}
  46.                             //else {
  47.                             //    oView.Document.InsertText(oView.Position, "\"\"", AddUndoActionType("Type double \""));
  48.                             //    return true;
  49.                             //}
  50.                         }
  51.                                 }
  52.         }
  53.         return false;
  54. }
  55.  
  56. var oClose = new ShortKey(onClose);
  57.  
  58. Application.RegisterShortKey(")", oClose, "def_source");
  59. Application.RegisterShortKey("]", oClose, "def_source");
  60. Application.RegisterShortKey("}", oClose, "def_source");
  61. Application.RegisterShortKey("'", oClose, "def_source");
  62. Application.RegisterShortKey('"', oClose, "def_source");

Offline plazmon

  • Newbie
  • *
  • Posts: 25
  • Karma: +0/-0
    • View Profile
Re: Closing brackets
« Reply #19 on: May 08, 2015, 05:21:16 pm »
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.

Offline alex

  • Developer
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2172
  • Karma: +37/-3
    • View Profile
    • HippoEDIT
Re: Closing brackets
« Reply #20 on: May 09, 2015, 02:08:01 pm »
Quote
1. 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.

Quote
2. 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.

Quote
3.  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.



Offline plazmon

  • Newbie
  • *
  • Posts: 25
  • Karma: +0/-0
    • View Profile
Re: Closing brackets
« Reply #21 on: May 14, 2015, 05:54:27 pm »
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.

Offline alex

  • Developer
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2172
  • Karma: +37/-3
    • View Profile
    • HippoEDIT
Re: Closing brackets
« Reply #22 on: May 14, 2015, 11:23:07 pm »
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
  1. Application.AddScriptInfo("CFBEB741-D13A-4058-BC05-CA082AC51EDC", "XBrackets", "1.01", "XBrackets plugin");
  2.  
  3. function onClose(oView, nKey, bUp, bShift, bControl, bAlt) {
  4.         var sel = oView.Selection;
  5.         if (sel.IsEmpty) {
  6.                 var line = oView.Document.GetLine(oView.Position.Line);
  7.                 var caret = oView.Position;
  8.                 var style_range = oView.Document.GetStyleFromPos(caret);
  9.                 if (style_range.Range.Right == caret.Pos) style_range = oView.Document.GetStyleFromPos(new Position(caret.Line, caret.Pos + 1));
  10.                 if (!style_range.Style.Text) {
  11.                         Output().writeln("Not-Text");
  12.                         if (caret.Pos < line.length && nKey == ")" && line.charAt(caret.Pos) == ')') {
  13.                                 oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
  14.                                 return true;
  15.                         }
  16.                         if (caret.Pos < line.length && nKey == "]" && line.charAt(caret.Pos) == ']') {
  17.                                 oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
  18.                                 return true;
  19.                         }
  20.                         if (caret.Pos < line.length && nKey == '}' && line.charAt(caret.Pos) == '}') {
  21.                                 oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
  22.                                 return true;
  23.                         }
  24.                 } else {
  25.                         Output().writeln("Text");
  26.                         if (caret.Pos < line.length && nKey == "'") {
  27.                                 if (line.charAt(caret.Pos) == "'") {
  28.                                         oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
  29.                                         return true;
  30.                                 }
  31.                                 //if (caret.Pos > 0 && line.charAt(caret.Pos-1) == '\'') {
  32.                                 //    oView.Document.InsertText(oView.Position, "\'", AddUndoActionType("Type \'"));
  33.                                 //    return true;
  34.                                 //}
  35.                                 //else {
  36.                                 //    oView.Document.InsertText(oView.Position, "\'\'", AddUndoActionType("Type double \'"));
  37.                                 //    return true;
  38.                                 //}
  39.                         }
  40.                         if (caret.Pos < line.length && nKey == '"') {
  41.                                 if (line.charAt(caret.Pos) == '"') {
  42.                                         oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
  43.                                         return true;
  44.                                 }
  45.                                 //if (caret.Pos > 0 && line.charAt(caret.Pos-1) == '\"') {
  46.                                 //    oView.Document.InsertText(oView.Position, "\"", AddUndoActionType("Type \""));
  47.                                 //    return true;
  48.                                 //}
  49.                                 //else {
  50.                                 //    oView.Document.InsertText(oView.Position, "\"\"", AddUndoActionType("Type double \""));
  51.                                 //    return true;
  52.                                 //}
  53.                         }
  54.                 }
  55.         }
  56.         return false;
  57. }
  58.  
  59. var oClose = new ShortKey(onClose);
  60.  
  61. Application.RegisterShortKey(")", oClose, "def_source");
  62. Application.RegisterShortKey("]", oClose, "def_source");
  63. Application.RegisterShortKey("}", oClose, "def_source");
  64. Application.RegisterShortKey("'", oClose, "def_source");
  65. 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.

Offline plazmon

  • Newbie
  • *
  • Posts: 25
  • Karma: +0/-0
    • View Profile
Re: Closing brackets
« Reply #23 on: May 15, 2015, 05:35:14 pm »
Hi!
This is the code

Code: Javascript
  1. Application.AddScriptInfo("CFBEB741-D13A-4058-BC05-CA082AC51EDC", "XBrackets", "1.01", "XBrackets plugin");
  2.  
  3. function onClose(oView, nKey, bUp, bShift, bControl, bAlt) {
  4.         var sel = oView.Selection;
  5.         if (sel.IsEmpty) {
  6.                 var style_range = oView.Document.GetStyleFromPos(oView.Position);
  7.                 var line = oView.Document.GetLine(oView.Position.Line);
  8.                 var caret = oView.Position;
  9.                 if (!style_range.Style.Text) {
  10.                     Output().writeln("Non-Text");
  11.                     if (caret.Pos < line.length  && nKey == ")" && line.charAt(caret.Pos) == ')') {
  12.                         oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
  13.                         return true;
  14.                     }
  15.                     if (caret.Pos < line.length && nKey == "]" && line.charAt(caret.Pos) == ']') {
  16.                         oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
  17.                         return true;
  18.                     }
  19.                     if (caret.Pos < line.length && nKey == '}' && line.charAt(caret.Pos) == '}') {
  20.                         oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
  21.                         return true;
  22.                     }
  23.                 }
  24.                 if (caret.Pos < line.length && nKey == "'") {
  25.                     if (line.charAt(caret.Pos) == "'") {
  26.                         oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
  27.                         return true;
  28.                     }
  29.                     if (caret.Pos > 0 && line.charAt(caret.Pos-1) == "'") {
  30.                         oView.Document.InsertText(oView.Position, "'", AddUndoActionType("Type '"));
  31.                         return true;
  32.                     }
  33.                     else {
  34.                         oView.Document.InsertText(oView.Position, "''", AddUndoActionType("Type double '"));
  35.                         oView.Position = new Position(oView.Position.Line, caret.Pos+1);
  36.                         return true;
  37.                     }
  38.                 }
  39.                 if (caret.Pos < line.length && nKey == '"') {
  40.                     if (line.charAt(caret.Pos) == '"') {
  41.                         oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
  42.                         return true;
  43.                     }
  44.                 if (caret.Pos > 0 && line.charAt(caret.Pos-1) == '"') {
  45.                     oView.Document.InsertText(oView.Position, '"', AddUndoActionType('Type "'));
  46.                     return true;
  47.                 }
  48.                 else {
  49.                     oView.Document.InsertText(oView.Position, '""', AddUndoActionType('Type double "'));
  50.                     oView.Position = new Position(oView.Position.Line, caret.Pos+1);
  51.                     return true;
  52.                 }
  53.                 }
  54.         }
  55.         return false;
  56. }
  57.  
  58. var oClose = new ShortKey(onClose);
  59.  
  60. Application.RegisterShortKey(")", oClose, "def_source");
  61. Application.RegisterShortKey("]", oClose, "def_source");
  62. Application.RegisterShortKey("}", oClose, "def_source");
  63. Application.RegisterShortKey("'", oClose, "def_source");
  64. 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

Offline alex

  • Developer
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2172
  • Karma: +37/-3
    • View Profile
    • HippoEDIT
Re: Closing brackets
« Reply #24 on: May 15, 2015, 08:11:39 pm »
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.

 

Related Topics

  Subject / Started by Replies Last post
3 Replies
1846 Views
Last post March 17, 2009, 12:17:53 pm
by alex
0 Replies
2231 Views
Last post November 06, 2009, 11:06:13 am
by alex
3 Replies
1549 Views
Last post August 02, 2010, 12:18:30 am
by alex
1 Replies
1382 Views
Last post July 24, 2011, 06:52:04 pm
by alex
2 Replies
969 Views
Last post November 26, 2015, 04:54:19 am
by junrall