sansarip / owlbear

An experimental Visual Studio Code extension that supports paredit-like structural-editing features for HTML, TypeScript, TypeScript-React, JavaScript, and JavaScript-React.
https://sansarip.github.io/owlbear/
MIT License
36 stars 2 forks source link

Apply formatting a bit more locally to the edit #113

Closed PEZ closed 1 year ago

PEZ commented 1 year ago

When I delete over a closing bracket when there is content inside, the cursor is moved inside it. This is as it should. However, I notice that the document was marked as dirty, and it wasn't obvious why. The Compare with Saved command revealed that somewhere completely else in my file, some whitespace was added. Probably my formatter that was invoked? In Calva, formatting is invoked only on the edited form (or its parent in some cases). Something to consider for Owlbear as well?

sansarip commented 1 year ago

Sorry for the late response and inconvenience, I just noticed this issue 🤦‍♂️!!

Owlbear currently invokes VS Code's configured formatter for the entire file after Owlbear commands are run.

I'll look into only applying the formatter for the edited scope 🔎

In the meanwhile, you can turn off auto-formatting with Owlbear: Toggle Autoformat!

sansarip commented 1 year ago

I gave it a shot hoping it would be a relatively simple improvement, but the VS Code API is unfortunately a blocker 🛑.

What I tried I augmented the extension to select and format code from the "scope" object that the Owlbear API returns. It almost worked acceptably; the issue is that selecting via the VS Code API in this way loses the cursor position. So, while the formatting would be scoped to the selection, the user's cursor position in that scope/context would be lost 😢.

Potential solution Unless I'm missing something obvious with the VS Code API, I think I'd need to import a formatter library like Prettier and move formatting entirely to that library instead of relying on VS Code as a middleman.

Conclusion Scoped formatting looks like it'll require Owlbear to buy into a particular formatter instead of relying on VS Code e.g. Prettier. I originally didn't even intend to include formatting as part of Owlbear and only threw in a cheeky commands.executeCommand("editor.action.formatDocument"); because it was so easy 😆!

As scoped formatting involves more work than anticipated and was not originally a primary goal for Owlbear, I'm unfortunately going to close this issue 😔 Owlbear's autoformatting can be turned off via Owlbear: Toggle Autoformat and performed manually instead!

PEZ commented 1 year ago

Yes. This is much easier for Calva, which already bundles a formatter.