Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchengxu committed Dec 24, 2024
1 parent d7526e9 commit 106ad9a
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 20 deletions.
4 changes: 2 additions & 2 deletions crates/maple_core/src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl ShellCommand {
}

pub fn cache_file_path(&self) -> std::io::Result<PathBuf> {
let cached_filename = utils::calculate_hash(self);
let cached_filename = utils::compute_hash(self);
generate_cache_file_path(cached_filename.to_string())
}

Expand All @@ -122,7 +122,7 @@ impl ShellCommand {
pub fn write_cache(self, total: usize, cmd_stdout: &[u8]) -> std::io::Result<PathBuf> {
use std::io::Write;

let cache_filename = utils::calculate_hash(&self);
let cache_filename = utils::compute_hash(&self);
let cache_file = generate_cache_file_path(cache_filename.to_string())?;

std::fs::File::create(&cache_file)?.write_all(cmd_stdout)?;
Expand Down
4 changes: 2 additions & 2 deletions crates/maple_core/src/stdio_server/plugin/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,9 @@ impl LspPlugin {
let Some(line) = utils::io::read_line_at(filepath, line)? else {
return Ok(None);
};
utils::char_index_for(&line, col - 1)
utils::char_index_at_byte(&line, col - 1)
} else {
utils::char_index_for(&lines[0], col - 1)
utils::char_index_at_byte(&lines[0], col - 1)
};

let Some(character) = maybe_character_index else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl WordHighlighter {

let is_word = |c: char| c.is_ascii_alphanumeric() || c == '_';

if let Some(cursor_char) = utils::char_at(&curline, col - 1) {
if let Some(cursor_char) = utils::char_at_byte(&curline, col - 1) {
if !is_word(cursor_char) {
return Ok(None);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ pub async fn initialize_provider(ctx: &Context, init_display: bool) -> Result<()
let context = context.clone();
let rg_cmd = RgTokioCommand::new(context.cwd.to_path_buf());
let job_id = utils::calculate_hash(&rg_cmd);
let job_id = utils::compute_hash(&rg_cmd);
job::try_start(
async move {
if let Ok(digest) = rg_cmd.create_cache().await {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ impl<'a> CachedPreviewImpl<'a> {
tracing::debug!(?latest_line, ?cache_line, "The cache is probably outdated");

let shell_cmd = crate::tools::rg::rg_shell_command(&self.ctx.cwd);
let job_id = utils::calculate_hash(&shell_cmd);
let job_id = utils::compute_hash(&shell_cmd);

if job::reserve(job_id) {
let ctx = self.ctx.clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl DumbJumpProvider {
}

async fn initialize_tags(&self, extension: String, cwd: AbsPathBuf) -> VimResult<()> {
let job_id = utils::calculate_hash(&(&cwd, "dumb_jump"));
let job_id = utils::compute_hash(&(&cwd, "dumb_jump"));

if job::reserve(job_id) {
let ctags_future = {
Expand Down
2 changes: 1 addition & 1 deletion crates/maple_core/src/stdio_server/provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ impl Context {

/// Executes the command `cmd` and returns the raw bytes of stdout.
pub fn exec_cmd(&self, cmd: &str) -> std::io::Result<Vec<u8>> {
let out = utils::execute_at(cmd, Some(&self.cwd))?;
let out = utils::execute_shell_command(cmd, Some(&self.cwd))?;
Ok(out.stdout)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/maple_core/src/tools/ctags/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl<'a, P: AsRef<Path> + Hash> TagsGenerator<'a, P> {
/// The file path of generated tags is determined by the hash of command itself.
pub fn tags_path(&self) -> PathBuf {
let mut tags_path = CTAGS_TAGS_DIR.deref().clone();
tags_path.push(utils::calculate_hash(self).to_string());
tags_path.push(utils::compute_hash(self).to_string());
tags_path
}

Expand Down
24 changes: 14 additions & 10 deletions crates/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ pub mod io;
/// Returns the width of displaying `n` on the screen.
///
/// Same with `n.to_string().len()` but without allocation.
pub fn display_width(n: usize) -> usize {
pub fn display_width(mut n: usize) -> usize {
if n == 0 {
return 1;
}

let mut n = n;
let mut len = 0;
while n > 0 {
len += 1;
Expand All @@ -32,14 +31,17 @@ pub fn is_git_repo(dir: &Path) -> bool {
dir.join(".git").exists()
}

pub fn calculate_hash<T: Hash>(t: &T) -> u64 {
pub fn compute_hash<T: Hash>(t: &T) -> u64 {
let mut s = DefaultHasher::new();
t.hash(&mut s);
s.finish()
}

/// Converts `shell_cmd` to `Command` with optional working directory.
pub fn as_std_command<P: AsRef<Path>>(shell_cmd: impl AsRef<OsStr>, dir: Option<P>) -> Command {
/// Constructs a `Command` for executing a shell command.
pub fn build_shell_command<P: AsRef<Path>>(
shell_cmd: impl AsRef<OsStr>,
dir: Option<P>,
) -> Command {
let mut cmd = if cfg!(target_os = "windows") {
let mut cmd = Command::new("cmd");
cmd.arg("/C").arg(shell_cmd.as_ref());
Expand All @@ -58,16 +60,18 @@ pub fn as_std_command<P: AsRef<Path>>(shell_cmd: impl AsRef<OsStr>, dir: Option<
}

/// Executes the `shell_cmd` and returns the output.
pub fn execute_at<S, P>(shell_cmd: S, dir: Option<P>) -> std::io::Result<Output>
pub fn execute_shell_command<S, P>(shell_cmd: S, dir: Option<P>) -> std::io::Result<Output>
where
S: AsRef<OsStr>,
P: AsRef<Path>,
{
let mut cmd = as_std_command(shell_cmd, dir);
let mut cmd = build_shell_command(shell_cmd, dir);
cmd.output()
}

/// Converts the char positions to byte positions as Vim and Neovim highlights is byte-positioned.
/// Converts the char positions to byte positions for use in Vim/NeoVim.
///
/// Vim and Neovim highlights use byte positions, this utility translate char positions.
pub fn char_indices_to_byte_indices(s: &str, char_indices: &[usize]) -> Vec<usize> {
s.char_indices()
.enumerate()
Expand All @@ -82,7 +86,7 @@ pub fn char_indices_to_byte_indices(s: &str, char_indices: &[usize]) -> Vec<usiz
}

/// Returns the char index of given byte index (0-based) in a line.
pub fn char_index_for(line: &str, byte_idx: usize) -> Option<usize> {
pub fn char_index_at_byte(line: &str, byte_idx: usize) -> Option<usize> {
line.char_indices().enumerate().find_map(
|(c_idx, (b_idx, _c))| {
if byte_idx == b_idx {
Expand All @@ -95,7 +99,7 @@ pub fn char_index_for(line: &str, byte_idx: usize) -> Option<usize> {
}

/// Returns the char at given byte index (0-based) in a line.
pub fn char_at(line: &str, byte_idx: usize) -> Option<char> {
pub fn char_at_byte(line: &str, byte_idx: usize) -> Option<char> {
line.char_indices()
.find_map(|(b_idx, c)| if byte_idx == b_idx { Some(c) } else { None })
}

0 comments on commit 106ad9a

Please sign in to comment.