Author Topic: Closing brackets  (Read 3134 times)

Offline plazmon

  • Newbie
  • *
  • Posts: 25
  • Karma: +0/-0
    • View Profile
Closing brackets
« on: April 21, 2015, 05:30:23 pm »
I want that HippoEdit go past closing brackets if they are already present in the text, such as akelpad  with the  XBraskets plugin does it.
I.e. when the user presses a closing parenthesis, and  to the right of the cursor the corresponding symbol is already present, I want that the editor simply moves the cursor to the right and inserts nothing.
Very annoying when the editor helpfully prints bracket pairs, and there are a heap of closing brackets and and quotes at the end of a line, and to position the cursor to the right place to insert the final semicolon I have to drag it manually over all these characters.
But in spite of this the HippoEdit is a great product, very nice and very comfortable.
Thank you.

Offline alex

  • Developer
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2181
  • Karma: +37/-3
    • View Profile
    • HippoEDIT
Re: Closing brackets
« Reply #1 on: April 21, 2015, 05:53:27 pm »
Hi Plazmon,

welcome on forum and thanks for comments.

Indeed HE does it (or did it before). Auto adding of the brackets is very un-trivial functionality where is difficult to find a balance of the feature to fit all users. Just as example:
http://forum.hippoedit.com/bug-reports/closing-parenthesis-')'-not-added/

There were a lot of reiterations of this functionality, when HE was skipping only auto added braces, than counting if brace is needed, than once more only auto added (and now I feel it is a best behaviour). So, any change that may be done in this area shall be well predictable for use and do not lead to misleading.

So, please describe here your example, and I will check if it is possible to change generic logic here.
In worst case you have always a chance to create your own plugin (binary or in JS) that will change the editor behaviour in the way you need (for example using Shortkey hook).

BR, Alex.


Offline plazmon

  • Newbie
  • *
  • Posts: 25
  • Karma: +0/-0
    • View Profile
Re: Closing brackets
« Reply #2 on: April 24, 2015, 04:13:14 pm »
Hi Alex!
I type
printf("Some text to output\n_")
The cursor is at position _ but I need to go to the end of line to finish operator by ;
So I must to move a finger to the cursor keys.
The scheme I want realised by AkelPad (with plaging) and also by PyCharm.
I have found that HE also do the same but only when you press open/close brackets (for example () ) very quickly.
It is strange for me when behavior depends on the speed of key-pressing.
And as you say in replay to http://forum.hippoedit.com/bug-reports/closing-parenthesis-%27)%27-not-added/ when someone want to insert anothe closing bracket it should position the cursor after (not before) the first one.
« Last Edit: April 24, 2015, 04:16:20 pm by plazmon »

Offline alex

  • Developer
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2181
  • Karma: +37/-3
    • View Profile
    • HippoEDIT
Re: Closing brackets
« Reply #3 on: April 26, 2015, 11:05:01 pm »
sorry, did not get the scenario yet.

1) you print "printf". Press "("
->  printf(|)
2) Press ' " ':
-> printf("|")
3) type "Some text to output\n"
-> printf("Some text to output\n|")

And now: what you press and what you expect?

HE tries to take care only about auto completed braces, not any. And auto completed also taken into account only if there was no significant delay and cursor movement in between. It is also not very good perceived if tool tries to be too smart, while probability to do something that user does not expect is rather high.

Offline plazmon

  • Newbie
  • *
  • Posts: 25
  • Karma: +0/-0
    • View Profile
Re: Closing brackets
« Reply #4 on: April 27, 2015, 12:52:35 pm »
4) press '"' and ')' and want
-> printf("Some text to output\n")|
I know, what I can turn off "Matching Braces" but it would be convinent to have this option on for reason not forgot close brackets.
I thick the algoriyhm should be lake that:

if pressed_close_bracket then
    if to_the_left_of_cursor_is_the_same_bracket then
        move_cursor_to_the_left
    else
        insert_bracket

For quote which is the same for open/close tha algorithm may be a little complex:

if pressed_quote then
    if to_the_left_of_cursor_is_the_same_quote then
        move_cursor_to_the_left
    else if to_the_lest_of_cursor_is_the_same_quote
        insert_quote
    else
         insert_two_quotes_and_position_cursor_between

Offline alex

  • Developer
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2181
  • Karma: +37/-3
    • View Profile
    • HippoEDIT
Re: Closing brackets
« Reply #5 on: April 27, 2015, 11:25:47 pm »
Quote
4) press '"' and ')' and want
-> printf("Some text to output\n")|
And what if I just want to type " and then escape it ?
or even:
print("\|") and now type '"'? Or print("\\|").
The same true for (1+abc|) -> type ")". What if I want to type ) after "abc" and than go in-front of it and put open "("?

Of course you always need to count not only left/right neighbours, but also nesting level:
you have:
((|) now type ")" -> it shall be "(()|)"
and for this:
(|) now type ")" -> it shall be "()|"

I have checked XBrackets, it also does not work as you expected. HE was working in similar way as XBrackets (or Sublime) in some stage, but later I have simplified/reverted the logic, to reduce number of "false positives" :)
« Last Edit: April 27, 2015, 11:29:09 pm by alex »

Offline plazmon

  • Newbie
  • *
  • Posts: 25
  • Karma: +0/-0
    • View Profile
Re: Closing brackets
« Reply #6 on: April 28, 2015, 02:14:47 pm »
Hi Alecs!

And what if I just want to type " and then escape it ?
I don't understend.

or even:
print("\|") and now type '"'? Or print("\\|").

Just press '"' two times.

The same true for (1+abc|) -> type ")". What if I want to type ) after "abc" and than go in-front of it and put open "("?

What has this to do with open (?

Of course you always need to count not only left/right neighbours, but also nesting level:
you have:
((|) now type ")" -> it shall be "(()|)"

Just type two ")"

and for this:
(|) now type ")" -> it shall be "()|"

I have checked XBrackets, it also does not work as you expected. HE was working in similar way as XBrackets (or Sublime) in some stage, but later I have simplified/reverted the logic, to reduce number of "false positives" :)

Yes, it implements some more complex logic. It seems it counts  pairs of brackets that it inserts by itself.
I propose more simple logic. For me the situation when I need additional closed brackets is rarer than skipping an existing bracket. Moreover to press the same key is easier then look for cursor keys.
But maybe I want a strange thing. :)

Offline alex

  • Developer
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2181
  • Karma: +37/-3
    • View Profile
    • HippoEDIT
Re: Closing brackets
« Reply #7 on: April 28, 2015, 05:54:46 pm »
Quote
Just type two...
Just use an arrow key ;)

My suggestion is: implement the plugin, doing what you think is right and share it with others ;) I will appreciate it with a free license, if you still do not have one.
I will provide all necessary support in plugin development and documentation. Even can do for you some API extension, if will be needed (for example changing of standard settings, as auto brackets, when your plugin is instantiated).
As an example check shortkey plugin - it does very similar things.

Offline alex

  • Developer
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2181
  • Karma: +37/-3
    • View Profile
    • HippoEDIT
Re: Closing brackets
« Reply #8 on: April 30, 2015, 08:28:52 pm »
And... ?
Do not give up so fats ;) In reality it is easier than you may think.

Below is a base for your plugin. Just execute it once with Tools->Execute <your_file>. It will skip every ")", if there is another ")" right side to place where you insert it.

Code: Javascript
  1. Application.AddScriptInfo("1DBE5CC3-B040-45DF-875D-19E6F2E26EC3", "XBrackets", "1.0", "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 && line.charAt(caret.Pos) == ')') {
  11.                                 oView.Position = new Position(oView.Position.Line, caret.Pos + 1);
  12.                                 return true;
  13.                         }
  14.                 }
  15.         }
  16.         return false;
  17. }
  18.  
  19. Application.RegisterShortKey(")", new ShortKey(onClose), "def_source");

Offline alex

  • Developer
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2181
  • Karma: +37/-3
    • View Profile
    • HippoEDIT
Re: Closing brackets
« Reply #9 on: April 30, 2015, 08:29:24 pm »
Alex - thank you very much! :)

Offline plazmon

  • Newbie
  • *
  • Posts: 25
  • Karma: +0/-0
    • View Profile
Re: Closing brackets
« Reply #10 on: May 05, 2015, 03:15:03 pm »
Alex, thank you very much!  :D
In the nears future, as I will have a little time, I will adapt you script yo all my needs.
It is real easy when you know how  :)
Thank you again.


Offline plazmon

  • Newbie
  • *
  • Posts: 25
  • Karma: +0/-0
    • View Profile
Re: Closing brackets
« Reply #11 on: May 06, 2015, 03:44:56 pm »
Hi Alex!
I am trying to adapt your code to serve all closing brackets, and obtained "Unspecified error" at the 7th line
Code: Javascript
  1. function onClose(oView, nKey, bUp, bShift, bControl, bAlt) {
  2.     // bla-bla-bla
  3. }
  4.  
  5. var oClose = new ShortKey(onClose);
  6.  
  7. Application.RegisterShortKey(")", oClose, "def_source");
  8. Application.RegisterShortKey("]", oClose, "def_source");
  9. Application.RegisterShortKey("}", oClose, "def_source");
  10. Application.RegisterShortKey("\'", oClose, "def_source");
  11. Application.RegisterShortKey("\"", oClose, "def_source");
what I do wrong?

Offline alex

  • Developer
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2181
  • Karma: +37/-3
    • View Profile
    • HippoEDIT
Re: Closing brackets
« Reply #12 on: May 06, 2015, 04:22:12 pm »
:) That also took some time for me to get this :)

So:
- Error reporting in this case is confusing, and shall be extended (I will provided a fix in next build)
- It looks like that I have allowed to run only single instance of the shortkey for same key (")") and syntax ("def_source"). Actually, because every shortkey can report if it process key or not, it would be OK to allow multiple shortkey to be called for same shortkey. (I will provided a fix in next build Not yet, too much to change)
- There is a "XBrackets plugin" you have tried and kept be loading -> Tools->Options->Plugins->XBrackets ;) Uncheck it and retry. Unfortunately check is not persisted between sessions and plugin will be reloaded next start. This is a bug I will also fix. But there is a way: right click on XBrackets and select -> Forget

Such problem not appearing when you restart same plugin, because editor does unload of previous instance if same plugin script once more executed.

BR, Alex.
« Last Edit: May 07, 2015, 11:28:52 pm by alex »

Offline plazmon

  • Newbie
  • *
  • Posts: 25
  • Karma: +0/-0
    • View Profile
Re: Closing brackets
« Reply #13 on: May 06, 2015, 04:51:32 pm »
Evrything is OK now,
thank you!
« Last Edit: May 07, 2015, 02:04:20 pm by plazmon »

Offline plazmon

  • Newbie
  • *
  • Posts: 25
  • Karma: +0/-0
    • View Profile
Re: Closing brackets
« Reply #14 on: May 08, 2015, 12:43:53 pm »
Hi Alex!
With closing brackets both round ')' and curled '}' everything work well, but with a square bracket ']' and quotes the same code behaves strange. It don't want to skip the char and simply add another.
UPD: Strange enough but with round brackets sometimes the same strange behavior. Sometime the script skips it,some times not. I have noticed that when not skipping, I saw that HE select matching brackets.
« Last Edit: May 08, 2015, 01:36:30 pm by plazmon »

 

Related Topics

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