Comments (6)
“Save without formatting” might not prevent ...
Are you sure? From my experience it always just saves the file without any modifications.
Just look here:
- The command implementation (note skipSaveParticipants): https://github.com/microsoft/vscode/blob/2f9cc59a89bf39b709452f11c0f83c51bc0ce844/src/vs/workbench/contrib/files/browser/fileCommands.ts#L488
- How this option is used: https://github.com/microsoft/vscode/blob/973684056e67153952f495fce93bf50d0ec0b892/src/vs/workbench/services/textfile/common/textFileEditorModel.ts#L782
- Who are save particpants? https://github.com/microsoft/vscode/blob/973684056e67153952f495fce93bf50d0ec0b892/src/vs/workbench/contrib/codeEditor/browser/saveParticipants.ts#L395-L399
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 toggleformattingToggle.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.
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.
Alright; since you’re the only one that requested something like this, I’ll close for now. Thanks anyway!
from vscode-formatting-toggle.
Or maybe we should introduce a setting like editor.disableFormatting
in VSCode?
from vscode-formatting-toggle.
👋 Interesting. First thoughts:
-
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
andformatOnType
. I know that’s my case and that’s why I created this extension in the first place. -
“Save without formatting” might not prevent the
editor.codeActionsOnSave
orvscode.workspace.onWillSaveTextDocument
updates you mentioned. -
If no one complained except about #54 (which only 3 people upvoted), it’s probably not actually a change that many people actually need?
-
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.
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)
- Please add "deactivateFor" HOT 4
- Infer `formattingToggle.affects` from the initial user configuration HOT 2
- Does not work with ESLint extension HOT 2
- Please support "editor.codeActionsOnSave" HOT 10
- Support toggling `files` formatting options HOT 3
- Does not work in Remote - Containers or GitHub Codespaces HOT 8
- [Feature Request] Remember the format on settings instead of reset it? HOT 1
- Handle workspace settings / mention it in the documentation HOT 7
- No toggle in my status bar HOT 6
- bisect results HOT 3
- Please add scope parameter to disable formatting for current workspace HOT 15
- Toggle is not working. Error visible in the VSC console HOT 3
- Formatting Toggle does not work? HOT 3
- Add Setting for Status Bar Text HOT 1
- [Feature Request] Indentation of Preprocess Instructions HOT 1
- Incorrect Formatting Issue HOT 1
- feature request: github dev support HOT 1
- Issues with Version: 1.85.0 and/or workspace settings HOT 2
- The status bar element disappears from the status bar when switching to a code editor HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vscode-formatting-toggle.