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