Giter Club home page Giter Club logo

Comments (6)

zardoy avatar zardoy commented on September 21, 2024 1

“Save without formatting” might not prevent ...

Are you sure? From my experience it always just saves the file without any modifications.

Just look here:

As you can see forementioned settings, code actions and vscode.workspace.onWillSaveTextDocument are skipped with that command.

probably not actually a change that many people actually need

I just expect from extension that it works in 100% cases. You know, it might be annoying to have a lot of pitfals and I don't want to learn where it works and where it doesn't.

... I know that’s my case and that’s why I created this extension in the first place.

In my case I just want to avoid any unexpected diffs in forks. For example I'm globally using Prettier and forementioned on-save formatting settings. Sometimes formatter isn't configured properly, sometimes files.trimTrailingWhitespace just removes all whitespaces from the file, which in turn creates huge diffs on GitHub.

Here is my final proposal:

Since it's not possible to modify user shortcuts, introduce a formattingToggle.toggleFormat command and formattingToggle.disableFormatting setting. Suggest user to bind this command to his save shortcut (ctrl+s). Whenever formattingToggle.disableFormatting is enabled (either in workspace level (#61) or globally) it would call workbench.action.files.saveWithoutFormatting or default workbench.action.files.save otherwise.

As you can see, it would resolve all issues in this repo and more importantly my needs.

Of course I could implement this in a new extension as it is absolutely different behavior, however maybe you don't mind of introducing formattingToggle.toggleMode setting:

  • when setting: use existing behavior
  • when command: just toggle formattingToggle.disableFormatting setting

Then we can introduce a command to toggle the setting on workspace-level.

I’d happily support you from the sidelines but if I’m being honest I don’t really want to spend time looking into that myself right now.

No worries I can do anything myself.

from vscode-formatting-toggle.

Tom-Bonnike avatar Tom-Bonnike commented on September 21, 2024 1

As you can see forementioned settings, code actions and vscode.workspace.onWillSaveTextDocument are skipped with that command.

Ah, looks like it indeed. Thanks for looking.

Since it's not possible to modify user shortcuts

Aw, that would have been a much nicer solution. Maybe there’s a way to hook into a save event and somehow prevent the default behaviour? Let’s just make really sure there isn’t a more elegant solution before forcing people to rebind their shortcuts.
Might be worth asking the folks over at VSCode if they have an opinion? The extension has quite a lot of installs so I’m sure we could get some eyes from them.

introduce a formattingToggle.toggleFormat command and formattingToggle.disableFormatting setting. Suggest user to bind this command to his save shortcut (ctrl+s). Whenever formattingToggle.disableFormatting is enabled (either in workspace level (#61) or globally) it would call workbench.action.files.saveWithoutFormatting or default workbench.action.files.save otherwise.

  • when setting: use existing behavior
  • when command: just toggle formattingToggle.disableFormatting setting

Sounds like a decent workaround yeah! Let’s make sure we get the naming right, the extension will be offering two ways, we should make the pros and cons obvious in the README (I can help with that once the PR ships).

I think we don’t need formattingToggle.toggleMode. What do you think of:

  // Super clear name, this way if someone is just going through the VSCode settings
  // they’ll know they need to do something else for it to work.
  // Probably we can add some description to the setting with instructions.
  "formattingToggle.forceFormattingStatusViaKeybindOverride": true,

  // Would have no effect if the above is set, we could make that clear in the README.
  "formattingToggle.affects": [
    "editor.formatOnPaste",
    "editor.formatOnSave",
    "editor.formatOnType"
  ]

from vscode-formatting-toggle.

Tom-Bonnike avatar Tom-Bonnike commented on September 21, 2024 1

Alright; since you’re the only one that requested something like this, I’ll close for now. Thanks anyway!

from vscode-formatting-toggle.

zardoy avatar zardoy commented on September 21, 2024

Or maybe we should introduce a setting like editor.disableFormatting in VSCode?

from vscode-formatting-toggle.

Tom-Bonnike avatar Tom-Bonnike commented on September 21, 2024

👋 Interesting. First thoughts:

  1. I propose to add a boolean setting which would also enable this behavior.

    Indeed, it should be an option. There are probably people that want to leave some of the formatting settings enabled. Most people probably really only care about disabling formatOnSave, formatOnPaste and formatOnType. I know that’s my case and that’s why I created this extension in the first place.

  2. “Save without formatting” might not prevent the editor.codeActionsOnSave or vscode.workspace.onWillSaveTextDocument updates you mentioned.

  3. If no one complained except about #54 (which only 3 people upvoted), it’s probably not actually a change that many people actually need?

  4. Or maybe we should introduce a setting like editor.disableFormatting in VSCode?

    That would make it reaaally simple to implement using the existing formattingToggle.affects setup and would make sure we are covering all formatters at once.


I'd like to implement this, however I still in doubt how we can write to keybindings.json.
AFAIK we don't have direct access to neither settings.json nor keybindings.json via API, but we want this to also work in web.

I’d happily support you from the sidelines but if I’m being honest I don’t really want to spend time looking into that myself right now.

from vscode-formatting-toggle.

zardoy avatar zardoy commented on September 21, 2024

Sorry for that delay.
I just found that it's possible to achieve this without any extension.

Just add this to keybindings.json

{
    "key": "cmd+s", //ctrl+s for win / linux
    "command": "workbench.action.files.saveWithoutFormatting",
    "when": "config.saveWithoutFormatting"
}

And just toggle (true/false) saveWithoutFormatting setting globally / per project.

Its possible to toggle that setting with toggle extension via keybinding, or with commands to toggle via statusbar.

As an alternative solution for this issue, we probably can:

  • Add this to default keybindings of extension:
{
    "key": "ctrl+s",
    "command": "workbench.action.files.saveWithoutFormatting",
    "when": "config.formattingToggle.forceDisableFormattingViaKeybinding"
}
  • Or suggest user to add that to his keybindings.json with custom keybindng.

Anyway, we can't just hook into other commands. Of course, you can try to open feature request for that, but I won't even try. I can imagine this is an obvious security issue (for the same reason we can't read arbitrary when context from extension host).

However, solution from previous comments is much better.

Also, I don't understand the purpose of formattingToggle.forceFormattingStatusViaKeybindOverride, I wanted to add formattingToggle.toggleMode to toggle the mode of the extension (which setting will toggle statusbar button).

At the moment this is not in priority list, as I've found workaround that I described above, sorry.

from vscode-formatting-toggle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.