cohama / lexima.vim

Auto close parentheses and repeat by dot dot dot...
996 stars 46 forks source link

Closing chars in multiple lines #14

Closed dai-shi closed 9 years ago

dai-shi commented 9 years ago

Hi, I like this plugin pretty much. Now, the ideal behavior for me is to type code just the same as in the case that the plugin is disabled.

This issue is about closing the char like '}' in the next line.

Suppose the following case

func() {|}

And if you enter, you get

func() {
  |
}

Then you type '}' to get

func() {
}|
}

But what I really want is

func() {
}|

I tried to write rules for this without any success. Maybe it is out of the scope of just rules.

Any suggestions? Thanks.

dai-shi commented 9 years ago

OK, I spent some time on this. My first trial was the following rule.

call lexima#add_rule({'at': '\%#\n\s*}', 'char': '}', 'leave': '}', 'delete': 1})

This does not work right away, because you can't specify leave and delete at the same time. So, I modified a bit to support leave and delete options. Here is the patch.

--- insmode.vim.orig    2015-01-30 19:05:03.828498360 +0900
+++ insmode.vim 2015-01-30 19:20:23.933437517 +0900
@@ -50,16 +50,9 @@
   if rule == {}
     return fallback
   else
-    if has_key(rule, 'leave')
-      if type(rule.leave) ==# type('')
-        let input = printf('<C-r>=lexima#insmode#leave_till(%s, %s)<CR>', string(rule.leave), string(lexima#string#to_mappable(a:fallback)))
-      elseif type(rule.leave) ==# type(0)
-        let input = printf('<C-r>=lexima#insmode#leave(%d, %s)<CR>', rule.leave, string(lexima#string#to_mappable(a:fallback)))
-      else
-        throw 'lexima: Not applicable rule (' . string(rule) . ')'
-      endif
-      let input_after = ''
-    elseif has_key(rule, 'delete')
+    let input = ''
+    let input_after = ''
+    if has_key(rule, 'delete')
       if type(rule.delete) ==# type('')
         let input = printf('<C-r>=lexima#insmode#delete_till(%s, %s)<CR>', string(rule.delete), string(lexima#string#to_mappable(a:fallback)))
       elseif type(rule.delete) ==# type(0)
@@ -67,10 +60,19 @@
       else
         throw 'lexima: Not applicable rule (' . string(rule) . ')'
       endif
-      let input = input . rule.input
-      let input_after = ''
+    endif
+    if has_key(rule, 'leave')
+      if type(rule.leave) ==# type('')
+        let input = input . printf('<C-r>=lexima#insmode#leave_till(%s, %s)<CR>', string(rule.leave), string(lexima#string#to_mappable(a:fallback)))
+      elseif type(rule.leave) ==# type(0)
+        let input = input . printf('<C-r>=lexima#insmode#leave(%d, %s)<CR>', rule.leave, string(lexima#string#to_mappable(a:fallback)))
+      else
+        throw 'lexima: Not applicable rule (' . string(rule) . ')'
+      endif
     else
-      let input = rule.input
+      let input = input . rule.input
+    endif
+    if has_key(rule, 'input_after')
       let input_after = rule.input_after
     endif
     return s:input(lexima#string#to_inputtable(input), lexima#string#to_inputtable(input_after))

This works to some extent, but unfortunately, this causes a trouble when you edit code. You can't type '}', ouch.

continued...

dai-shi commented 9 years ago

My second trial was the following.

call lexima#add_rule({'at': '\%#\n\s*}', 'char': '}', 'input': '}', 'delete': '}'})

This works as I have expected, and I'm happy now.

After then, I noticed the commit 47ab82d7b0e025dbe1c8d7b1d6dfec4de43e472e. Oh no, @cohama you have already tried that. But it's slightly different.

I'm curious what you mean by "often noisy." Do you still see any problem with my rule above?

Thanks.

cohama commented 9 years ago

Thanks and sorry for slow response. The reason why that rule was deleted is that some people complained about unintended leaving. For exmaple,

{
  "right": "}"
}

In case of lexima's interface, adding a rule is easy but deleting is not so I disabled that rule by default.

Of cource, there is no problem to define your own rule at all.

dai-shi commented 9 years ago

Thanks for the example. The exact example is not a big problem, because it's in the double quotation, but I see what you mean.

{
  "right brace char code": 125 // this means }
}

This causes the problem.

And,I understand why it's disabled by default. Thanks.