New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Would it be possible to support a 'Smart indent on yank' feature (at least for Python)? #61
Comments
Nice work. python-mode already has code to determine the right indentation level. It should also work for things like indentation inside lists and so on. The hard part is turning a region into a coherent one that has the correct indentation for the first line. There's a bunch of overly complicated code in combobulate that tries to do this: That might give you some ideas. |
That sums it up, yup 😄 Thanks, I'll check out your suggestions. To me it would be amazing to see an interactive |
I stumbled upon this problem too, while working on a similar package. The basic idea is to preserve in kill ring indentation of the first line, since it is crucial for proper indentation of the whole block. I have some code in my config With this code you can copy/paste code blocks that not start with spaces preserving its indentations. this_is = func(1, |temp={'1':1,
'2':2}) for |
I've made a large number of simplifications to this in development, which I am due to finish merging some time next week. The problem is ensuring, as you say, the first line is properly indented relative to the other lines, and then adjusting indentation to match point when you yank. |
Thanks for this library! I really enjoy editing Python code with it. One 'killer' feature of PyCharm I am very used to, is its 'Smart Indent pasted lines' feature.
Here is a video demonstrating its behavior in PyCharm:
CleanShot.2023-09-24.at.20.15.24.mp4
Here is how Emacs does it by default on a yank (no fun, one usually has to manually reindent stuff):
CleanShot.2023-09-24.at.20.16.40.mp4
Using my code below, it behaves identical to PyCharm:
CleanShot.2023-09-24.at.20.19.04.mp4
I thought a bit about it and came up with those functions to mimick its behavior (it's not perfect, but works fine for me so far).
@mickeynp since you are quite experienced with Python, how do you approach this issue? What do you think of this approach? Is there an easier solution to this? I don't know if this can even be implemented based on the treesit node structures and then indent the code based on that (but this would be a lot of work I think).
The code checks if the first line ends with a
:
then tries to best-guess apply indents based on that.dima-python-indent-for-current-line
indents the entire pasted code block based on the(current-indentation)
.The text was updated successfully, but these errors were encountered: