jiangmiao / auto-pairs

Vim plugin, insert or delete brackets, parens, quotes in pair
http://www.vim.org/scripts/script.php?script_id=3599
4.09k stars 373 forks source link

Why does auto-pairs remove my indentation when pressing <CR> between curly brackets #347

Closed Hubro closed 2 years ago

Hubro commented 2 years ago

In file test.yang:

module test {
  ncs:plan-outline mock-nano-cfs-plan {
    ncs:component-type "ncs:self" {
      ncs:state "ncs:init" {
        ncs:create "ncs:init" {|}
      }
      ncs:state "ncs:ready";
    }
  }
}

(Cursor is marked with | on line 5)

When I press Enter now, the result is:

module test {
  ncs:plan-outline mock-nano-cfs-plan {
    ncs:component-type "ncs:self" {
      ncs:state "ncs:init" {
ncs:create "ncs:init" {
|
    }
      }
      ncs:state "ncs:ready";
    }
  }
}

I would expect the result to be:

module test {
  ncs:plan-outline mock-nano-cfs-plan {
    ncs:component-type "ncs:self" {
      ncs:state "ncs:init" {
        ncs:create "ncs:init" {
          |
        }
      }
      ncs:state "ncs:ready";
    }
  }
}

Why does this happen?


Neovim v0.6.1 auto-pairs master branch (39f06b873a8449af8ff6a3eee716d3da14d63a76)

autoindent
nosmartindent
nocindent
expandtab
shiftwidth=2
softtabstop=4
tabstop=8
LunarWatcher commented 2 years ago

It doesn't. Your indent system does.

Off the top of my head, autoindent probably registers ncs: as a label, moves that to the front, and does whatever thing with with the other bracket based on what autoindent sees as logical. That said, I don't understand the inner workings of any of the indent systems, so I'm not sure what the exact reason is.

However, you can trivially prove it is the indent system by copying the last example, highlighting the three lines, and pressing =. You'll get your exact output; and that's on Vim, not auto-pairs

Hubro commented 2 years ago

@LunarWatcher I see, thanks. I thought I had eliminated that possibility by disabling cindent, but I guess not :thinking:

Removing : from cinkeys had no effect, nor does removing it from indentkeys... Any combination of autoindent, smartindent and cindent also doesn't appear to have any effect.

LunarWatcher commented 2 years ago

Vim indentation is... tricky, to say the least. All three built-in options have a lot of limitations outside C-like languages, unfortunately. You're better off either finding or creating an indentexpr in most other cases. Or, of course, just dealing with the indentation being weird. Unfortunately, there doesn't seem to be any indentexprs for yang at this time, so no easy way out there.

That said, tree-sitter supports indentation, and a search for tree-sitter yang parsers reveals a repo you made :p Tree-sitter is still somewhat outside my area of expertise, but you might be able to fix this by enabling tree-sitter indentation (assuming you haven't already, and assuming the yang parser supports it, if it needs to explicitly). In general, you just want a more sane indentexpr than Vim's built-in system, and tree-sitter should be able to provide that.

Hubro commented 2 years ago

Alright, thanks a lot! I'll try to mess around with tree-sitter indentation.