Skip to content

Commit 5b6f518

Browse files
authored
fix(buffers/#3521): Add 'save without formatting' command (#3539)
Fixes #3521
1 parent 5686023 commit 5b6f518

File tree

6 files changed

+43
-5
lines changed

6 files changed

+43
-5
lines changed

CHANGES_CURRENT.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- #3506 - Theme: Add 'workbench.colorCustomizations' setting (related #3495)
1616
- #3526 - Explorer: Enable auto-refresh by default (fixes #3399)
1717
- #3529 - Keybindings: Allow remaps to be defined in keybindings.json
18+
- #3539 - Buffers: Add Save without formatting commaaand (fixes #3521)
1819

1920
### Bug Fixes
2021

src/Core/SaveReason.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[@deriving show]
22
type t =
3-
| UserInitiated
3+
| UserInitiated({allowFormatting: bool})
44
| AutoSave;

src/Feature/Buffers/Feature_Buffers.re

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ type command =
154154
| ChangeFiletype({maybeBufferId: option(int)})
155155
| ConvertIndentationToTabs
156156
| ConvertIndentationToSpaces
157-
| DetectIndentation;
157+
| DetectIndentation
158+
| SaveWithoutFormatting;
158159

159160
[@deriving show({with_path: false})]
160161
type msg =
@@ -554,7 +555,9 @@ let update = (~activeBufferId, ~config, msg: msg, model: model) => {
554555
| Saved(bufferId) =>
555556
let saveReason =
556557
model.pendingSaveReason
557-
|> Option.value(~default=SaveReason.UserInitiated);
558+
|> Option.value(
559+
~default=SaveReason.UserInitiated({allowFormatting: true}),
560+
);
558561
let model' =
559562
update(bufferId, Option.map(Buffer.incrementSaveTick), model);
560563
let eff =
@@ -725,6 +728,19 @@ let update = (~activeBufferId, ~config, msg: msg, model: model) => {
725728
let updatedBuffer = Buffer.setIndentation(indentation, buffer);
726729
(add(updatedBuffer, model), Nothing);
727730
};
731+
732+
| SaveWithoutFormatting => (
733+
{
734+
...model,
735+
pendingSaveReason:
736+
Some(SaveReason.UserInitiated({allowFormatting: false})),
737+
},
738+
Effect(
739+
Service_Vim.Effects.save(~bufferId=activeBufferId, () =>
740+
Saved(activeBufferId)
741+
),
742+
),
743+
)
728744
}
729745

730746
| LargeFileOptimizationsApplied({buffer}) =>
@@ -933,6 +949,14 @@ module Commands = {
933949
"workbench.action.editor.changeLanguageMode",
934950
Command(ChangeFiletype({maybeBufferId: None})),
935951
);
952+
953+
let saveWithoutFormatting =
954+
define(
955+
~category="File",
956+
~title="Save without formatting",
957+
"workbench.action.files.saveWithoutFormatting",
958+
Command(SaveWithoutFormatting),
959+
);
936960
};
937961

938962
let sub = (~isWindowFocused, ~maybeFocusedBuffer, model) => {
@@ -992,6 +1016,7 @@ module Contributions = {
9921016
detectIndentation,
9931017
indentUsingSpaces,
9941018
indentUsingTabs,
1019+
saveWithoutFormatting,
9951020
]
9961021
|> Command.Lookup.fromList;
9971022

src/Feature/LanguageSupport/Feature_LanguageSupport.re

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,8 @@ let bufferSaved =
608608
~activeBufferId,
609609
model,
610610
) =>
611-
if (reason == Oni_Core.SaveReason.AutoSave) {
611+
if (reason == Oni_Core.SaveReason.AutoSave
612+
|| reason == Oni_Core.SaveReason.UserInitiated({allowFormatting: false})) {
612613
(model, Isolinear.Effect.none);
613614
} else {
614615
let (formatting', formattingEffect) =

src/Service/Vim/Service_Vim.re

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,21 @@ module Effects = {
245245
};
246246

247247
let saveAll = toMsg => {
248-
Isolinear.Effect.createWithDispatch(~name="loadBuffer", dispatch => {
248+
Isolinear.Effect.createWithDispatch(~name="saveAll", dispatch => {
249249
let (_: Vim.Context.t, _effects: list(Vim.Effect.t)) =
250250
Vim.command("wa!");
251251
dispatch(toMsg());
252252
});
253253
};
254+
255+
let save = (~bufferId, toMsg) => {
256+
Isolinear.Effect.createWithDispatch(~name="saveBuffer", dispatch => {
257+
let context = {...Vim.Context.current(), bufferId};
258+
let (_: Vim.Context.t, _effects: list(Vim.Effect.t)) =
259+
Vim.command(~context, "w!");
260+
dispatch(toMsg());
261+
});
262+
};
254263
};
255264

256265
module Sub = {

src/Service/Vim/Service_Vim.rei

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ module Effects: {
3939
let loadBuffer:
4040
(~filePath: string, (~bufferId: int) => 'msg) => Isolinear.Effect.t('msg);
4141

42+
let save: (~bufferId: int, unit => 'msg) => Isolinear.Effect.t('msg);
43+
4244
let saveAll: (unit => 'msg) => Isolinear.Effect.t('msg);
4345

4446
let applyCompletion:

0 commit comments

Comments
 (0)