mirror of
https://github.com/helix-editor/helix.git
synced 2024-11-22 01:16:18 +04:00
Add more context; Editor::open doesn't need to own path
This commit is contained in:
parent
1533f48934
commit
ed950fcc56
@ -25,7 +25,7 @@
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
use anyhow::Error;
|
||||
use anyhow::{Context, Error};
|
||||
|
||||
use crossterm::{
|
||||
event::{DisableMouseCapture, EnableMouseCapture, Event},
|
||||
@ -122,7 +122,7 @@ pub fn new(args: Args, config: Config) -> Result<Self, Error> {
|
||||
});
|
||||
let syn_loader = std::sync::Arc::new(syntax::Loader::new(syn_loader_conf));
|
||||
|
||||
let mut compositor = Compositor::new()?;
|
||||
let mut compositor = Compositor::new().context("build compositor")?;
|
||||
let config = Arc::new(ArcSwap::from_pointee(config));
|
||||
let mut editor = Editor::new(
|
||||
compositor.size(),
|
||||
@ -141,26 +141,28 @@ pub fn new(args: Args, config: Config) -> Result<Self, Error> {
|
||||
|
||||
if args.load_tutor {
|
||||
let path = helix_loader::runtime_dir().join("tutor.txt");
|
||||
editor.open(path, Action::VerticalSplit)?;
|
||||
editor.open(&path, Action::VerticalSplit)?;
|
||||
// Unset path to prevent accidentally saving to the original tutor file.
|
||||
doc_mut!(editor).set_path(None)?;
|
||||
} else if !args.files.is_empty() {
|
||||
let first = &args.files[0].0; // we know it's not empty
|
||||
if first.is_dir() {
|
||||
std::env::set_current_dir(&first)?;
|
||||
std::env::set_current_dir(&first).context("set current dir")?;
|
||||
editor.new_file(Action::VerticalSplit);
|
||||
let picker = ui::file_picker(".".into(), &config.load().editor);
|
||||
compositor.push(Box::new(overlayed(picker)));
|
||||
} else {
|
||||
let nr_of_files = args.files.len();
|
||||
editor.open(first.to_path_buf(), Action::VerticalSplit)?;
|
||||
editor.open(first, Action::VerticalSplit)?;
|
||||
for (file, pos) in args.files {
|
||||
if file.is_dir() {
|
||||
return Err(anyhow::anyhow!(
|
||||
"expected a path to file, found a directory. (to open a directory pass it as first argument)"
|
||||
));
|
||||
} else {
|
||||
let doc_id = editor.open(file, Action::Load)?;
|
||||
let doc_id = editor
|
||||
.open(&file, Action::Load)
|
||||
.context(format!("open '{}'", file.to_string_lossy()))?;
|
||||
// with Action::Load all documents have the same view
|
||||
let view_id = editor.tree.focus;
|
||||
let doc = editor.document_mut(doc_id).unwrap();
|
||||
@ -192,7 +194,8 @@ pub fn new(args: Args, config: Config) -> Result<Self, Error> {
|
||||
#[cfg(windows)]
|
||||
let signals = futures_util::stream::empty();
|
||||
#[cfg(not(windows))]
|
||||
let signals = Signals::new(&[signal::SIGTSTP, signal::SIGCONT])?;
|
||||
let signals =
|
||||
Signals::new(&[signal::SIGTSTP, signal::SIGCONT]).context("build signal handler")?;
|
||||
|
||||
let app = Self {
|
||||
compositor,
|
||||
|
@ -1024,7 +1024,7 @@ fn goto_file_impl(cx: &mut Context, action: Action) {
|
||||
for sel in paths {
|
||||
let p = sel.trim();
|
||||
if !p.is_empty() {
|
||||
if let Err(e) = cx.editor.open(PathBuf::from(p), action) {
|
||||
if let Err(e) = cx.editor.open(&PathBuf::from(p), action) {
|
||||
cx.editor.set_error(format!("Open file failed: {:?}", e));
|
||||
}
|
||||
}
|
||||
@ -1849,7 +1849,7 @@ fn global_search(cx: &mut Context) {
|
||||
}
|
||||
},
|
||||
move |cx, (line_num, path), action| {
|
||||
match cx.editor.open(path.into(), action) {
|
||||
match cx.editor.open(path, action) {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
cx.editor.set_error(format!(
|
||||
|
@ -61,7 +61,7 @@ fn jump_to_location(
|
||||
return;
|
||||
}
|
||||
};
|
||||
let _id = editor.open(path, action).expect("editor.open failed");
|
||||
let _id = editor.open(&path, action).expect("editor.open failed");
|
||||
let (view, doc) = current!(editor);
|
||||
let definition_pos = location.range.start;
|
||||
// TODO: convert inside server
|
||||
@ -114,7 +114,7 @@ fn sym_picker(
|
||||
return;
|
||||
}
|
||||
};
|
||||
if let Err(err) = cx.editor.open(path, action) {
|
||||
if let Err(err) = cx.editor.open(&path, action) {
|
||||
let err = format!("failed to open document: {}: {}", uri, err);
|
||||
log::error!("{}", err);
|
||||
cx.editor.set_error(err);
|
||||
@ -385,7 +385,7 @@ pub fn apply_workspace_edit(
|
||||
};
|
||||
|
||||
let current_view_id = view!(editor).id;
|
||||
let doc_id = match editor.open(path, Action::Load) {
|
||||
let doc_id = match editor.open(&path, Action::Load) {
|
||||
Ok(doc_id) => doc_id,
|
||||
Err(err) => {
|
||||
let err = format!("failed to open document: {}: {}", uri, err);
|
||||
|
@ -50,7 +50,7 @@ fn open(
|
||||
ensure!(!args.is_empty(), "wrong argument count");
|
||||
for arg in args {
|
||||
let (path, pos) = args::parse_file(arg);
|
||||
let _ = cx.editor.open(path, Action::Replace)?;
|
||||
let _ = cx.editor.open(&path, Action::Replace)?;
|
||||
let (view, doc) = current!(cx.editor);
|
||||
let pos = Selection::point(pos_at_coords(doc.text().slice(..), pos, true));
|
||||
doc.set_selection(view.id, pos);
|
||||
@ -819,7 +819,7 @@ fn vsplit(
|
||||
} else {
|
||||
for arg in args {
|
||||
cx.editor
|
||||
.open(PathBuf::from(arg.as_ref()), Action::VerticalSplit)?;
|
||||
.open(&PathBuf::from(arg.as_ref()), Action::VerticalSplit)?;
|
||||
}
|
||||
}
|
||||
|
||||
@ -838,7 +838,7 @@ fn hsplit(
|
||||
} else {
|
||||
for arg in args {
|
||||
cx.editor
|
||||
.open(PathBuf::from(arg.as_ref()), Action::HorizontalSplit)?;
|
||||
.open(&PathBuf::from(arg.as_ref()), Action::HorizontalSplit)?;
|
||||
}
|
||||
}
|
||||
|
||||
@ -923,7 +923,7 @@ fn tutor(
|
||||
_event: PromptEvent,
|
||||
) -> anyhow::Result<()> {
|
||||
let path = helix_loader::runtime_dir().join("tutor.txt");
|
||||
cx.editor.open(path, Action::Replace)?;
|
||||
cx.editor.open(&path, Action::Replace)?;
|
||||
// Unset path to prevent accidentally saving to the original tutor file.
|
||||
doc_mut!(cx.editor).set_path(None)?;
|
||||
Ok(())
|
||||
@ -1150,7 +1150,7 @@ fn open_config(
|
||||
_event: PromptEvent,
|
||||
) -> anyhow::Result<()> {
|
||||
cx.editor
|
||||
.open(helix_loader::config_file(), Action::Replace)?;
|
||||
.open(&helix_loader::config_file(), Action::Replace)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -1159,7 +1159,7 @@ fn open_log(
|
||||
_args: &[Cow<str>],
|
||||
_event: PromptEvent,
|
||||
) -> anyhow::Result<()> {
|
||||
cx.editor.open(helix_loader::log_file(), Action::Replace)?;
|
||||
cx.editor.open(&helix_loader::log_file(), Action::Replace)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -63,7 +63,7 @@ fn id(&self) -> Option<&'static str> {
|
||||
}
|
||||
}
|
||||
|
||||
use anyhow::Error;
|
||||
use anyhow::Context as AnyhowContext;
|
||||
use std::io::stdout;
|
||||
use tui::backend::{Backend, CrosstermBackend};
|
||||
type Terminal = tui::terminal::Terminal<CrosstermBackend<std::io::Stdout>>;
|
||||
@ -76,9 +76,9 @@ pub struct Compositor {
|
||||
}
|
||||
|
||||
impl Compositor {
|
||||
pub fn new() -> Result<Self, Error> {
|
||||
pub fn new() -> anyhow::Result<Self> {
|
||||
let backend = CrosstermBackend::new(stdout());
|
||||
let terminal = Terminal::new(backend)?;
|
||||
let terminal = Terminal::new(backend).context("build terminal")?;
|
||||
Ok(Self {
|
||||
layers: Vec::new(),
|
||||
terminal,
|
||||
|
@ -175,7 +175,7 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi
|
||||
path.strip_prefix(&root).unwrap_or(path).to_string_lossy()
|
||||
},
|
||||
move |cx, path: &PathBuf, action| {
|
||||
if let Err(e) = cx.editor.open(path.into(), action) {
|
||||
if let Err(e) = cx.editor.open(path, action) {
|
||||
let err = if let Some(err) = e.source() {
|
||||
format!("{}", err)
|
||||
} else {
|
||||
|
@ -779,8 +779,8 @@ pub fn new_file_from_stdin(&mut self, action: Action) -> Result<DocumentId, Erro
|
||||
}
|
||||
|
||||
// ??? possible use for integration tests
|
||||
pub fn open(&mut self, path: PathBuf, action: Action) -> Result<DocumentId, Error> {
|
||||
let path = helix_core::path::get_canonicalized_path(&path)?;
|
||||
pub fn open(&mut self, path: &Path, action: Action) -> Result<DocumentId, Error> {
|
||||
let path = helix_core::path::get_canonicalized_path(path)?;
|
||||
let id = self.document_by_path(&path).map(|doc| doc.id);
|
||||
|
||||
let id = if let Some(id) = id {
|
||||
|
@ -62,7 +62,7 @@ pub fn jump_to_stack_frame(editor: &mut Editor, frame: &helix_dap::StackFrame) {
|
||||
return;
|
||||
};
|
||||
|
||||
if let Err(e) = editor.open(path, Action::Replace) {
|
||||
if let Err(e) = editor.open(&path, Action::Replace) {
|
||||
editor.set_error(format!("Unable to jump to stack frame: {}", e));
|
||||
return;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user