Vimjas / vim-python-pep8-indent

A nicer Python indentation style for vim.
Creative Commons Zero v1.0 Universal
790 stars 69 forks source link

Consider using Python AST #99

Open blueyed opened 6 years ago

blueyed commented 6 years ago

Via https://github.com/Vimjas/vim-python-pep8-indent/pull/71#issue-110134510.

It might make sense to use a library like https://github.com/ambv/black/

However, this would likely involve quite some overhead if the whole text would need to be sent/used/tokenized everytime.

/cc @purplep

blueyed commented 6 years ago

Via #71:

I will experiment with using ast module or parso or maybe creating my own parser

Yay! \o/ Would be great if parso fits this need - otherwise I think this would be something @davidhalter might be interested to support.

to correctly determine indent you only need parse one paragraph

Depends on what you mean with "paragraph" .. ;)

If I remember correctly this library searches the whole file for matching quotes by the way, which also isn't very efficient.

Not the whole file necessarily, but it uses searchpairpos, which might also be used by an AST parser then to determine what input should be sent to it.

This indent plugin is certainly not very well optimized for performance though indeed. I've started looking into this a while ago, but stopped there.

Anyway in 99% of cases parsing one paragraph is totally acceptable and would remove error-prone dependency on on syntax files.

Yes.

Fortunately there are a lot of tests already - and although they are clumsy by themselves it helps to avoid regressions.

davidhalter commented 6 years ago

Please let me know if you are missing features in parso.

blueyed commented 6 years ago

@davidhalter Thanks.

Can you provide us any pointers already to get to the information that is needed for getting the indent of a line?

davidhalter commented 6 years ago

I don't think that you can easily determine what the indentation of a line is, if you think about newlines and other empty space or multi line statements. However once you have a node in parso you can just use .column to parse the node.

What you could always do is scan for suite nodes that imply there's an indentation for it, check if we're in it's range and check the first statement for it's column. However it's probably more complicated if you care about error nodes.

blueyed commented 5 years ago

btw: I've found this https://github.com/sillybun/autoformatpythonstatement. It uses cpp code to get the indent (called via Python): https://github.com/sillybun/autoformatpythonstatement/blob/master/ftplugin/python/afpython.cpp.