| 1 |
use std::{ |
| 2 |
collections::HashSet, |
| 3 |
path::PathBuf, |
| 4 |
sync::{Arc, Mutex}, |
| 5 |
}; |
| 6 |
|
| 7 |
use helix_event::register_hook; |
| 8 |
use helix_loader::workspace_trust::TrustStatus; |
| 9 |
use helix_view::{events::DocumentDidOpen, handlers::Handlers, DocumentId}; |
| 10 |
|
| 11 |
use crate::{compositor::Compositor, job, ui}; |
| 12 |
|
| 13 |
const ID: &str = "workspace-trust-select"; |
| 14 |
|
| 15 |
pub(super) fn register_hooks(_handlers: &Handlers) { |
| 16 |
|
| 17 |
— |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
let prompted: Arc<Mutex<HashSet<PathBuf>>> = Arc::default(); |
| 22 |
register_hook!(move |event: &mut DocumentDidOpen<'_>| { |
| 23 |
let doc_id = event.doc; |
| 24 |
|
| 25 |
let (workspace, servers_to_load) = { |
| 26 |
let doc = doc!(event.editor, &doc_id); |
| 27 |
(doc.workspace_root().to_path_buf(), doc.servers_to_load()) |
| 28 |
}; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
if event.editor.workspace_trust.status(&workspace) == TrustStatus::Stale { |
| 34 |
event.editor.set_status( |
| 35 |
"Workspace `.helix/` config changed since `:workspace-trust`. \ |
| 36 |
Local config not loaded. Run `:workspace-trust` to re-allow.", |
| 37 |
); |
| 38 |
return Ok(()); |
| 39 |
} |
| 40 |
|
| 41 |
if !event |
| 42 |
.editor |
| 43 |
.workspace_trust |
| 44 |
.restricted_for_doc(&workspace, servers_to_load) |
| 45 |
{ |
| 46 |
return Ok(()); |
| 47 |
} |
| 48 |
|
| 49 |
ct |
| 50 |
|
| 51 |
if !event.editor.workspace_trust.prompts_enabled() { |
| 52 |
return Ok(()); |
| 53 |
} |
| 54 |
|
| 55 |
e. |
| 56 |
if !prompted.lock().unwrap().insert(workspace.clone()) { |
| 57 |
return Ok(()); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
event.editor.workspace_trust.deny_once(&workspace); |
| 62 |
let workspace = workspace.clone(); |
| 63 |
job::dispatch_blocking(move |_editor, compositor| prompt(workspace, compositor)); |
| 64 |
|
| 65 |
Ok(()) |
| 66 |
}); |
| 67 |
} |
| 68 |
|
| 69 |
fn prompt(workspace: PathBuf, compositor: &mut Compositor) { |
| 70 |
let select = select(workspace); |
| 71 |
compositor.replace_or_push(ID, select); |
| 72 |
} |
| 73 |
|
| 74 |
const TRUST_MESSAGE: &str = "Trust this workspace? |
| 75 |
|
| 76 |
Trusted workspaces may load local Helix config files (`.helix/*`) and auto-start language servers. \ |
| 77 |
Both can execute arbitrary code. Only trust workspaces whose contents you have inspected."; |
| 78 |
|
| 79 |
#[derive(Default, Clone, Copy, Debug)] |
| 80 |
pub enum TrustChoice { |
| 81 |
#[default] |
| 82 |
Trust, |
| 83 |
Never, |
| 84 |
} |
| 85 |
|
| 86 |
fn select(workspace: PathBuf) -> ui::Select<TrustChoice> { |
| 87 |
ui::Select::new( |
| 88 |
TRUST_MESSAGE, |
| 89 |
[TrustChoice::Trust, TrustChoice::Never], |
| 90 |
(), |
| 91 |
move |editor, option, event| { |
| 92 |
if event != ui::PromptEvent::Validate { |
| 93 |
return; |
| 94 |
} |
| 95 |
match option { |
| 96 |
TrustChoice::Trust => { |
| 97 |
editor.workspace_trust.trust(&workspace); |
| 98 |
let documents: Vec<DocumentId> = editor.documents.keys().cloned().collect(); |
| 99 |
for document_id in documents.iter() { |
| 100 |
editor.launch_language_servers(*document_id); |
| 101 |
} |
| 102 |
let _ = editor |
| 103 |
.config_events |
| 104 |
.0 |
| 105 |
.send(helix_view::editor::ConfigEvent::Refresh); |
| 106 |
} |
| 107 |
TrustChoice::Never => { |
| 108 |
editor.workspace_trust.exclude(&workspace); |
| 109 |
|
| 110 |
|
| 111 |
let _ = editor |
| 112 |
.config_events |
| 113 |
.0 |
| 114 |
.send(helix_view::editor::ConfigEvent::Refresh); |
| 115 |
} |
| 116 |
} |
| 117 |
}, |
| 118 |
) |
| 119 |
} |
| 120 |
|
| 121 |
impl crate::ui::menu::Item for TrustChoice { |
| 122 |
type Data = (); |
| 123 |
|
| 124 |
fn format(&self, _data: &Self::Data) -> tui::widgets::Row<'_> { |
| 125 |
match self { |
| 126 |
TrustChoice::Trust => "Trust", |
| 127 |
TrustChoice::Never => "Never", |
| 128 |
} |
| 129 |
.into() |
| 130 |
} |
| 131 |
} |