Skip to content

Commit

Permalink
Add save without formatting; Allow toggling of formatting for autosave (
Browse files Browse the repository at this point in the history
lapce#1722)

* Add save without formatting

* Allow disabling formatting on autosave
  • Loading branch information
MinusGix authored Nov 20, 2022
1 parent 5a903c9 commit 5b10fea
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features/Changes
- [#1720](https://github.com/lapce/lapce/pull/1720): Display signature/parameter information from LSP
- [#1723](https://github.com/lapce/lapce/pull/1723): In the palette, display the keybind for a command adjacent to it
- [#1722](https://github.com/lapce/lapce/pull/1722): Add 'Save without Formatting'; Add option to disable formatting on autosave

### Bug Fixes

Expand Down
1 change: 1 addition & 0 deletions defaults/settings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ modal-mode-relative-line-numbers = true
format-on-save = false
highlight-matching-brackets = true
autosave-interval = 0
format-on-autosave = true
enable-inlay-hints = true
inlay-hint-font-family = ""
inlay-hint-font-size = 0
Expand Down
3 changes: 3 additions & 0 deletions lapce-core/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ pub enum FocusCommand {
#[strum(message = "Save")]
#[strum(serialize = "save")]
Save,
#[strum(message = "Save Without Formatting")]
#[strum(serialize = "save_without_format")]
SaveWithoutFormatting,
#[strum(serialize = "save_and_exit")]
SaveAndExit,
#[strum(serialize = "force_exit")]
Expand Down
4 changes: 4 additions & 0 deletions lapce-data/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@ pub struct EditorConfig {
desc = "Set the auto save delay (in milliseconds), Set to 0 to completely disable"
)]
pub autosave_interval: u64,
#[field_names(
desc = "Whether the document should be formatted when an autosave is triggered (required Format on Save)"
)]
pub format_on_autosave: bool,
#[field_names(
desc = "If enabled the cursor treats leading soft tabs as if they are hard tabs."
)]
Expand Down
12 changes: 8 additions & 4 deletions lapce-data/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,7 @@ impl LapceEditorBufferData {
}
}

fn save(&mut self, ctx: &mut EventCtx, exit: bool) {
fn save(&mut self, ctx: &mut EventCtx, exit: bool, allow_formatting: bool) {
if self.doc.buffer().is_pristine() && self.doc.content().is_file() {
if exit {
ctx.submit_command(Command::new(
Expand All @@ -1358,7 +1358,8 @@ impl LapceEditorBufferData {
}

if let BufferContent::File(path) = self.doc.content() {
let format_on_save = self.config.editor.format_on_save;
let format_on_save =
allow_formatting && self.config.editor.format_on_save;
let path = path.clone();
let proxy = self.proxy.clone();
let rev = self.doc.rev();
Expand Down Expand Up @@ -2278,10 +2279,13 @@ impl LapceEditorBufferData {
}
}
SaveAndExit => {
self.save(ctx, true);
self.save(ctx, true, true);
}
Save => {
self.save(ctx, false);
self.save(ctx, false, true);
}
SaveWithoutFormatting => {
self.save(ctx, false, false);
}
Rename => {
if let BufferContent::File(path) = self.doc.content() {
Expand Down
9 changes: 8 additions & 1 deletion lapce-ui/src/editor/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,10 +628,17 @@ impl Widget<LapceTabData> for LapceEditorView {
if ctx.is_focused() {
let doc = data.main_split.editor_doc(self.view_id);
if !doc.buffer().is_pristine() {
let save_cmd =
if data.config.editor.format_on_autosave {
FocusCommand::Save
} else {
FocusCommand::SaveWithoutFormatting
};

ctx.submit_command(Command::new(
LAPCE_COMMAND,
LapceCommand {
kind: CommandKind::Focus(FocusCommand::Save),
kind: CommandKind::Focus(save_cmd),
data: None,
},
Target::Widget(editor.view_id),
Expand Down

0 comments on commit 5b10fea

Please sign in to comment.