bmalehorn / vscode-print-it

Wrap a print statment around current word or selection
4 stars 3 forks source link

Place PrintIt statement after multiline block not inside it. #12

Open xxxxxxbox opened 1 year ago

xxxxxxbox commented 1 year ago

Hi, there.

In my code i use a lot of multiline functions and classes. for example:

my_class.some_action(
    msg="...",
    meta="...",
    id="...",
    response="...",
    raw="...",
    comment="..."
)

If I select with a mouse "my_class" and press hot keys combination, PrintIt places print statement to the next line - right inside parenthesis. See example below.

my_class.some_action(
    print(f"{my_class=}")
    msg="...",
    meta="...",
    id="...",
    response="...",
    raw="...",
    comment="..."
)

This action breaks my code. I have to move print statement 8 lines down with Cut & Paste or with Vs Code hotkeys.

Not sure If it's possible. But it would be great if PrintIt can analyze nearest code and place print statement after multiline blocks, not inside them.

bmalehorn commented 1 year ago

Hey @xxxxxxbox, I'm definitely aware of this problem and run into it all the time. I do what you describe - I use the Move Line Downward keyboard shortcut (⌥ ↓ on Mac) to move it to the appropriate place after it's created.

print-it is a basic tool that just operates on text strings. It does not parse Python / JavaScript / Java / Ruby source code and does not understand a concept like "find the end of this statement". There are extensions, like the Python extension, that do parse Python code and tell VSCode how to syntax highlight each part, where function definitions are, etc.

However I'm not sure if there's any way in the VSCode extension API for print-it to get that language information from the Python extension. Correct me if I'm wrong, but I don't think there are any VSCode extensions that understand the Python language other than the Python extension itself.

So I probably will not implement this until VSCode extension API gives me a function like findStatementEnd({ line: 100, column: 5 }). If you find an API endpoint that does that sort of thing (or an extension that seems to understand Python code), feel free to let me know!

Also I could do some heuristic like trying to find a closing ) for the current line. I did that in another extension a while ago (https://github.com/bmalehorn/click-mode/blob/b94ea8cce89cf0e753b2ab915202d49ffc470fb6/click-mode.el#L66-L223), but most of that extension became parsing code which was always imperfect and could be tuned & special-cased forever. I'd rather insert the print statement in an obviously-wrong place rather than invite endless tweaks to the statement-finder code.

Does that all make sense?