TiredTumblrina 2 weeks ago committed by GitHub
commit 872770d051
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -9,7 +9,7 @@ use helix_lsp::{
use helix_stdx::path::get_relative_path;
use helix_view::{
align_view,
document::DocumentSavedEventResult,
document::{DocumentSavedEventResult, IrregularFileError},
editor::{ConfigEvent, EditorEvent},
graphics::Rect,
theme,
@ -186,9 +186,17 @@ impl Application {
Some(Layout::Horizontal) => Action::HorizontalSplit,
None => Action::Load,
};
let doc_id = editor
let doc_id = match editor
.open(&file, action)
.context(format!("open '{}'", file.to_string_lossy()))?;
.context(format!("open '{}'", file.to_string_lossy()))
{
// Ignore irregular files during application init.
Err(e) if e.is::<IrregularFileError>() => {
nr_of_files -= 1;
continue;
}
a => a,
}?;
// with Action::Load all documents have the same view
// NOTE: this isn't necessarily true anymore. If
// `--vsplit` or `--hsplit` are used, the file which is
@ -199,15 +207,21 @@ impl Application {
doc.set_selection(view_id, pos);
}
}
editor.set_status(format!(
"Loaded {} file{}.",
nr_of_files,
if nr_of_files == 1 { "" } else { "s" } // avoid "Loaded 1 files." grammo
));
// align the view to center after all files are loaded,
// does not affect views without pos since it is at the top
let (view, doc) = current!(editor);
align_view(doc, view, Align::Center);
// if all files were invalid, replace with empty buffer
if nr_of_files == 0 {
editor.new_file(Action::VerticalSplit);
} else {
editor.set_status(format!(
"Loaded {} file{}.",
nr_of_files,
if nr_of_files == 1 { "" } else { "s" } // avoid "Loaded 1 files." grammo
));
// align the view to center after all files are loaded,
// does not affect views without pos since it is at the top
let (view, doc) = current!(editor);
align_view(doc, view, Align::Center);
}
} else {
editor.new_file(Action::VerticalSplit);
}

@ -117,6 +117,17 @@ pub struct SavePoint {
revert: Mutex<Transaction>,
}
#[derive(Debug)]
pub struct IrregularFileError {
msg: String,
}
impl Display for IrregularFileError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.msg)
}
}
pub struct Document {
pub(crate) id: DocumentId,
text: Rope,
@ -686,6 +697,16 @@ impl Document {
config_loader: Option<Arc<ArcSwap<syntax::Loader>>>,
config: Arc<dyn DynAccess<Config>>,
) -> Result<Self, Error> {
// if the path is not a regular file (e.g.: /dev/random) it should not be opened
if path
.metadata()
.map_or(false, |metadata| !metadata.is_file())
{
return Err(anyhow::anyhow!(IrregularFileError {
msg: "Path argument must be a regular file, a directory, or a symlink.".to_string()
}));
}
// Open the file if it exists, otherwise assume it is a new file (and thus empty).
let (rope, encoding, has_bom) = if path.exists() {
let mut file =

Loading…
Cancel
Save