feat(editor): add autosave configuration (#1358)

This commit is contained in:
Paul Delafosse 2022-09-27 20:41:03 +02:00 committed by GitHub
parent 42a23afd35
commit 61dd0b6e4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 0 deletions

View File

@ -19,6 +19,7 @@ auto-closing-matching-pairs = true
hover-delay = 300 # ms
modal-mode-relative-line-numbers = true
format-on-save = true
autosave-interval = 0
enable-inlay-hints = true
inlay-hint-font-family = ""
inlay-hint-font-size = 0

View File

@ -236,6 +236,10 @@ pub struct EditorConfig {
desc = "How the editor should render whitespace characters.\nOptions: none, all, boundary, trailing."
)]
pub render_whitespace: String,
#[field_names(
desc = "Set the auto save delay (in milliseconds), Set to 0 to completely disable"
)]
pub autosave_interval: u64,
}
impl EditorConfig {

View File

@ -42,6 +42,7 @@ pub struct LapceEditorView {
pub editor: WidgetPod<LapceTabData, LapceEditorContainer>,
pub find: Option<WidgetPod<LapceTabData, Box<dyn Widget<LapceTabData>>>>,
cursor_blink_timer: TimerToken,
autosave_timer: TimerToken,
last_idle_timer: TimerToken,
display_border: bool,
background_color_name: &'static str,
@ -94,6 +95,7 @@ pub fn new(
editor: WidgetPod::new(editor),
find,
cursor_blink_timer: TimerToken::INVALID,
autosave_timer: TimerToken::INVALID,
last_idle_timer: TimerToken::INVALID,
display_border: true,
background_color_name: LapceTheme::EDITOR_BACKGROUND,
@ -636,6 +638,39 @@ fn event(
}
}
}
Event::Timer(id) if self.autosave_timer == *id => {
ctx.set_handled();
if let Some(editor) = data
.main_split
.active
.and_then(|active| data.main_split.editors.get(&active))
.cloned()
{
if data.config.editor.autosave_interval > 0 {
if ctx.is_focused() {
let doc = data.main_split.editor_doc(self.view_id);
if !doc.buffer().is_pristine() {
ctx.submit_command(Command::new(
LAPCE_COMMAND,
LapceCommand {
kind: CommandKind::Focus(FocusCommand::Save),
data: None,
},
Target::Widget(editor.view_id),
));
}
self.autosave_timer = ctx.request_timer(
Duration::from_millis(
data.config.editor.autosave_interval,
),
None,
);
} else {
self.cursor_blink_timer = TimerToken::INVALID;
}
}
}
}
_ => {}
}
@ -862,6 +897,15 @@ fn update(
}
}
if data.config.editor.autosave_interval > 0
&& editor_data.doc.rev() != old_editor_data.doc.rev()
{
self.autosave_timer = ctx.request_timer(
Duration::from_millis(data.config.editor.autosave_interval),
None,
);
}
if old_data.config.lapce.modal != data.config.lapce.modal
&& !editor_data.doc.content().is_input()
{