Skip to content

Commit e92fce6

Browse files
auto trust newly created user files
1 parent 5388810 commit e92fce6

File tree

6 files changed

+44
-7
lines changed

6 files changed

+44
-7
lines changed

helix-loader/src/trust_db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ fn trust_db_file() -> PathBuf {
142142

143143
pub fn is_workspace_trusted(path: impl AsRef<Path>) -> Result<Option<bool>> {
144144
let Ok(path) = path.as_ref().canonicalize() else {
145-
return Ok(Some(false));
145+
return Ok(None);
146146
};
147147

148148
TRUST_DB.inspect(|db| db.is_workspace_trusted(path))

helix-term/src/events.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use helix_event::{events, register_event};
22
use helix_view::document::Mode;
33
use helix_view::events::{
44
ConfigDidChange, DiagnosticsDidChange, DocumentDidChange, DocumentDidClose, DocumentDidOpen,
5-
DocumentFocusLost, LanguageServerExited, LanguageServerInitialized, SelectionDidChange,
5+
DocumentFocusLost, FileCreated, LanguageServerExited, LanguageServerInitialized,
6+
SelectionDidChange,
67
};
78

89
use crate::commands;
@@ -27,4 +28,5 @@ pub fn register() {
2728
register_event::<LanguageServerInitialized>();
2829
register_event::<LanguageServerExited>();
2930
register_event::<ConfigDidChange>();
31+
register_event::<FileCreated>();
3032
}

helix-term/src/handlers/trust.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use anyhow::anyhow;
88
use helix_stdx::env::set_current_working_dir;
99
use helix_view::editor::WorkspaceTrust;
1010
use helix_view::events::DocumentDidOpen;
11+
use helix_view::events::FileCreated;
1112
use helix_view::handlers::Handlers;
1213
use helix_view::theme::Modifier;
1314
use helix_view::theme::Style;
@@ -136,10 +137,9 @@ fn choose_parent_dialog(path: impl AsRef<Path>, trust: bool) -> impl Component +
136137
pub(super) fn register_hooks(_handlers: &Handlers) {
137138
helix_event::register_hook!(move |event: &mut DocumentDidOpen<'_>| {
138139
if event.editor.config.load().workspace_trust == WorkspaceTrust::Ask
139-
&& event
140-
.editor
141-
.document(event.doc)
142-
.is_some_and(|doc| doc.path().is_some() && doc.is_trusted.is_none())
140+
&& event.editor.document(event.doc).is_some_and(|doc| {
141+
doc.path().is_some_and(|p| p.exists()) && doc.is_trusted.is_none()
142+
})
143143
{
144144
// these unwraps are fine due to the above. TODO: change this to if let chains once rust is bumped to 1.88
145145
let path = event
@@ -157,4 +157,26 @@ pub(super) fn register_hooks(_handlers: &Handlers) {
157157

158158
Ok(())
159159
});
160+
161+
helix_event::register_hook!(move |event: &mut FileCreated| {
162+
let path = event.path.clone();
163+
crate::job::dispatch_blocking(move |editor, _compositor| {
164+
if !matches!(
165+
editor.config().workspace_trust,
166+
WorkspaceTrust::Ask | WorkspaceTrust::Manual
167+
) {
168+
return;
169+
}
170+
if let Some(doc) = editor.document_by_path(&path) {
171+
if doc.is_trusted.is_none() {
172+
if let Err(e) = editor.trust_workspace(path) {
173+
editor.set_error(format!(
174+
"Couldn't trust file: {e}; use :trust-workspace to trust it"
175+
))
176+
}
177+
}
178+
}
179+
});
180+
Ok(())
181+
});
160182
}

helix-view/src/document.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ pub struct DocumentSavedEvent {
119119
pub save_time: SystemTime,
120120
pub doc_id: DocumentId,
121121
pub path: PathBuf,
122+
pub is_newly_created: bool,
122123
pub text: Rope,
123124
}
124125

@@ -1051,9 +1052,10 @@ impl Document {
10511052
));
10521053
}
10531054

1055+
let path_exists = path.exists();
10541056
// Assume it is a hardlink to prevent data loss if the metadata cant be read (e.g. on certain Windows configurations)
10551057
let is_hardlink = helix_stdx::faccess::hardlink_count(&write_path).unwrap_or(2) > 1;
1056-
let backup = if path.exists() && atomic_save {
1058+
let backup = if path_exists && atomic_save {
10571059
let path_ = write_path.clone();
10581060
// hacks: we use tempfile to handle the complex task of creating
10591061
// non clobbered temporary path for us we don't want
@@ -1139,6 +1141,7 @@ impl Document {
11391141
save_time,
11401142
doc_id,
11411143
path,
1144+
is_newly_created: !path_exists,
11421145
text: text.clone(),
11431146
};
11441147

helix-view/src/editor.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,6 +2043,11 @@ impl Editor {
20432043
let res = doc_save_future.await;
20442044
if let Ok(event) = &res {
20452045
handler.file_changed(event.path.clone());
2046+
if event.is_newly_created {
2047+
helix_event::dispatch(crate::events::FileCreated {
2048+
path: event.path.clone(),
2049+
})
2050+
}
20462051
}
20472052
res
20482053
};

helix-view/src/events.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
use std::path::PathBuf;
2+
13
use helix_core::{ChangeSet, Rope};
24
use helix_event::events;
35
use helix_lsp::LanguageServerId;
46

57
use crate::{editor::Config, Document, DocumentId, Editor, ViewId};
68

79
events! {
10+
FileCreated {
11+
path: PathBuf
12+
}
813
DocumentDidOpen<'a> {
914
editor: &'a mut Editor,
1015
doc: DocumentId

0 commit comments

Comments
 (0)