Skip to content

Commit

Permalink
address code review
Browse files Browse the repository at this point in the history
  • Loading branch information
matoous committed Nov 19, 2023
1 parent 6e99aec commit 7111025
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion helix-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ log = "0.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
toml = "0.7"
url = "2.3.1"

imara-diff = "0.1.0"

Expand Down
1 change: 0 additions & 1 deletion helix-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub use encoding_rs as encoding;
pub use url;

pub mod auto_pairs;
pub mod chars;
Expand Down
1 change: 1 addition & 0 deletions helix-term/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pulldown-cmark = { version = "0.9", default-features = false }
content_inspector = "0.2.4"
# openning URLs
open = "4.0.0"
url = "2.3.1"

# config
toml = "0.7"
Expand Down
82 changes: 62 additions & 20 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use helix_core::{
textobject,
tree_sitter::Node,
unicode::width::UnicodeWidthChar,
url::Url,
visual_offset_from_block, Deletion, LineEnding, Position, Range, Rope, RopeGraphemes,
RopeReader, RopeSlice, Selection, SmallVec, Tendril, Transaction,
};
Expand Down Expand Up @@ -61,8 +60,13 @@ use crate::{

use crate::job::{self, Jobs};
use futures_util::{stream::FuturesUnordered, TryStreamExt};
use std::{collections::HashMap, fmt, future::Future};
use std::{collections::HashSet, num::NonZeroUsize};
use std::{
collections::{HashMap, HashSet},
fmt,
future::Future,
io::Read,
num::NonZeroUsize,
};

use std::{
borrow::Cow,
Expand All @@ -71,6 +75,7 @@ use std::{

use once_cell::sync::Lazy;
use serde::de::{self, Deserialize, Deserializer};
use url::Url;

use grep_regex::RegexMatcherBuilder;
use grep_searcher::{sinks, BinaryDetection, SearcherBuilder};
Expand Down Expand Up @@ -1199,23 +1204,7 @@ fn goto_file_impl(cx: &mut Context, action: Action) {
}

if let Ok(url) = Url::parse(p) {
// file:// urls are opened inside helix
if url.scheme() != "file" {
let commands = open::commands(url.as_str());
cx.jobs.callback(async {
for cmd in commands {
let mut command = tokio::process::Command::new(cmd.get_program());
command.args(cmd.get_args());
if command.output().await.is_ok() {
return Ok(job::Callback::Editor(Box::new(|_| {})));
}
}
Ok(job::Callback::Editor(Box::new(move |editor| {
editor.set_error("Open file failed: no command found")
})))
});
return;
}
return open_url(cx, url, action);
}

let path = &rel_path.join(p);
Expand All @@ -1228,6 +1217,59 @@ fn goto_file_impl(cx: &mut Context, action: Action) {
}
}

/// Opens url. If the URL is a valid textual file it is open in helix, other
/// the file is open using external program.
fn open_url(cx: &mut Context, url: Url, action: Action) {
let (_, doc) = current_ref!(cx.editor);
let rel_path = doc
.relative_path()
.map(|path| path.parent().unwrap().to_path_buf())
.unwrap_or_default();

if url.scheme() != "file" {
return open_external_url(cx, url);
}

let content_type = std::fs::File::open(url.path()).and_then(|file| {
// Read up to 1kb to detect the content type
let mut read_buffer = Vec::new();
let n = file.take(1024).read_to_end(&mut read_buffer)?;
Ok(content_inspector::inspect(&read_buffer[..n]))
});

// we attempt to open binary files - files that can't be open in helix - using external
// program as well, e.g. pdf files or images
match content_type {
Ok(content_inspector::ContentType::BINARY) => open_external_url(cx, url),
Ok(_) | Err(_) => {
let path = &rel_path.join(url.path());
if path.is_dir() {
let picker = ui::file_picker(path.into(), &cx.editor.config());
cx.push_layer(Box::new(overlaid(picker)));
} else if let Err(e) = cx.editor.open(path, action) {
cx.editor.set_error(format!("Open file failed: {:?}", e));
}
}
}
}

/// Opens URL in external program.
fn open_external_url(cx: &mut Context, url: Url) {
let commands = open::commands(url.as_str());
cx.jobs.callback(async {
for cmd in commands {
let mut command = tokio::process::Command::new(cmd.get_program());
command.args(cmd.get_args());
if command.output().await.is_ok() {
return Ok(job::Callback::Editor(Box::new(|_| {})));
}
}
Ok(job::Callback::Editor(Box::new(move |editor| {
editor.set_error("Opening URL in external program failed")
})))
});
}

fn extend_word_impl<F>(cx: &mut Context, extend_fn: F)
where
F: Fn(RopeSlice, Range, usize) -> Range,
Expand Down
2 changes: 1 addition & 1 deletion helix-view/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ helix-vcs = { version = "0.6", path = "../helix-vcs" }

# Conversion traits
once_cell = "1.18"
url = "2"
url = "2.3.1"

arc-swap = { version = "1.6.0" }

Expand Down
2 changes: 1 addition & 1 deletion helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ where
}

use helix_lsp::{lsp, Client, LanguageServerName};
use helix_core::url::Url;
use url::Url;

impl Document {
pub fn from(
Expand Down

0 comments on commit 7111025

Please sign in to comment.