zkry / yaml-pro

Edit YAML in Emacs like a pro
GNU General Public License v3.0
137 stars 9 forks source link

yaml-pro-indent-subtree at beginning of line indents element on line above #26

Closed antifuchs closed 7 months ago

antifuchs commented 1 year ago

First I thought yaml-pro-indent-subtree was broken, but it has a kind of twisted logic! With the following code:

baz:
  bar:
    x: 1
    y:
  foo:
    x: 2
    y: 4

point at column 0:

With point at the very beginning of the line of the foo: key, a yaml-pro-indent-subtree will result in:

baz:
  bar:
    x: 1
      y:
  foo:
    x: 2
    y: 4

point at column 1:

With point one character ahead, the tree results in:

baz:
    bar:
      x: 1
      y:
    foo:
      x: 2
      y: 4

... it indents both the bar: and foo: subtree. Weird, also not what I meant to do. But at least the syntax checks out!

point at column 2:

That's what I actually meant to do!

baz:
  bar:
    x: 1
    y:
    foo:
      x: 2
      y: 4

Special case: foo at column zero

This does the right thing, resulting in a buffer transform from:

baz:
  bar:
    x: 1
    y:
foo:
  x: 2
  y: 4

to:

baz:
  bar:
    x: 1
    y:
  foo:
    x: 2
    y: 4

but point remains at column 0, which means that if I attempt to unindent the tree again, it'll catch the wrong target & unindent the y key above it.

zkry commented 1 year ago

This is a helpful observation and I think this could definitely be improved. It sort of makes sense that this is what's happening, considering how the tree-sitter nodes are parsed. I think a more intuitive parse functionality could definitely be made. Perhaps if the point is on whitespace at the beginning of the line, it could count as if it was the node of the first character on the line.

zkry commented 7 months ago

Sorry for not getting to this sooner. The version of the function without treesitter (yaml-pro-indent-subtree) does have some weird bugs. I think that moving the point to the first character on the line and then doing the indent should fix this and remove strange behavior.

If it's possible with our current version, I would definitely recommend switching to the treesitter version of the package (yaml-pro-ts-mode and yaml-pro-ts-indent-subtree) as the tree-sitter parser is much faster and results in fewer bugs.