Skip to content

Commit

Permalink
Use utils::io::*
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchengxu committed Dec 24, 2024
1 parent 74dd02e commit 646f1a7
Show file tree
Hide file tree
Showing 20 changed files with 25 additions and 30 deletions.
2 changes: 1 addition & 1 deletion crates/cli/benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rayon::prelude::*;
use std::io::BufRead;
use std::sync::Arc;
use types::ClapItem;
use utils::line_count;
use utils::io::line_count;

fn prepare_source_items() -> Vec<SourceItem> {
let largest_cache = find_largest_cache_digest().expect("Cache is empty");
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/command/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use maple_core::dirs::Dirs;
use std::fs::read_dir;
use std::io::Write;
use std::path::{PathBuf, MAIN_SEPARATOR};
use utils::remove_dir_contents;
use utils::io::remove_dir_contents;

/// List and remove all the cached contents.
#[derive(Subcommand, Debug, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/command/helptags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::Parser;
use maple_core::helptags::generate_tag_lines;
use maple_core::paths::AbsPathBuf;
use std::io::Write;
use utils::read_lines;
use utils::io::read_lines;

/// Parse and display Vim helptags.
#[derive(Parser, Debug, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use maple_core::process::ShellCommand;
use printer::{println_json, println_json_with_length};
use std::path::{Path, PathBuf};
use std::process::Command as StdCommand;
use utils::{line_count, read_first_lines};
use utils::io::{line_count, read_first_lines};

#[derive(Debug, Clone)]
#[allow(unused)]
Expand Down
4 changes: 2 additions & 2 deletions crates/maple_core/src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl CacheInfo {
&& digest.cached_path.exists()
&& now.signed_duration_since(digest.last_visit).num_days() < MAX_DAYS
// In case the cache was not created completely.
&& utils::line_count(&digest.cached_path)
&& utils::io::line_count(&digest.cached_path)
.map(|total| total == digest.total)
.unwrap_or(false)
{
Expand Down Expand Up @@ -210,7 +210,7 @@ pub fn store_cache_digest(
shell_cmd: ShellCommand,
new_created_cache: PathBuf,
) -> std::io::Result<Digest> {
let total = utils::line_count(&new_created_cache)?;
let total = utils::io::line_count(&new_created_cache)?;

let digest = Digest::new(shell_cmd, total, new_created_cache);

Expand Down
2 changes: 1 addition & 1 deletion crates/maple_core/src/datastore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fn load_json<T: DeserializeOwned, P: AsRef<Path>>(path: Option<P>) -> Option<T>

fn write_json<T: Serialize, P: AsRef<Path>>(obj: T, path: Option<P>) -> std::io::Result<()> {
if let Some(json_path) = path.as_ref() {
utils::create_or_overwrite(json_path, serde_json::to_string(&obj)?.as_bytes())?;
utils::io::create_or_overwrite(json_path, serde_json::to_string(&obj)?.as_bytes())?;
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/maple_core/src/helptags.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashMap;
use utils::read_lines;
use utils::io::read_lines;

#[inline]
fn strip_trailing_slash(x: &str) -> &str {
Expand Down
5 changes: 2 additions & 3 deletions crates/maple_core/src/previewer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use std::fs::File;
use std::io::Read;
use std::path::Path;
use utils::bytelines::ByteLines;
use utils::io::FileSizeTier;
use utils::read_first_lines;
use utils::io::{read_first_lines, FileSizeTier};

/// Preview of a text file.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -65,7 +64,7 @@ fn read_text_lines<P: AsRef<Path>>(
// XXX: is megabyte enough for any text file?
const MEGABYTE: usize = 32 * 1_048_576;

let filesize = utils::file_size(&file);
let filesize = utils::io::file_size(&file);
if filesize > MEGABYTE {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
Expand Down
2 changes: 1 addition & 1 deletion crates/maple_core/src/previewer/vim_help.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::path::Path;
use utils::{read_lines, read_lines_from_small};
use utils::io::{read_lines, read_lines_from_small};

#[derive(Debug, Clone)]
pub struct HelpTagPreview<'a> {
Expand Down
2 changes: 1 addition & 1 deletion crates/maple_core/src/stdio_server/plugin/colorizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn find_colors(input_file: impl AsRef<Path>) -> std::io::Result<BTreeMap<usize,
};

// 0-based line_number
for (line_number, line) in utils::read_lines(input_file)?
for (line_number, line) in utils::io::read_lines(input_file)?
.map_while(Result::ok)
.enumerate()
{
Expand Down
2 changes: 1 addition & 1 deletion crates/maple_core/src/stdio_server/plugin/ctags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use icon::IconType;
use serde::Serialize;
use std::collections::HashMap;
use std::path::Path;
use utils::SizeChecker;
use utils::io::SizeChecker;

#[derive(Serialize, Debug)]
struct ScopeRef<'a> {
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 @@ -468,7 +468,7 @@ impl LspPlugin {

let maybe_character_index = if lines.is_empty() {
// Buffer may not be loaded, read the local file directly.
let Some(line) = utils::read_line_at(filepath, line)? else {
let Some(line) = utils::io::read_line_at(filepath, line)? else {
return Ok(None);
};
utils::char_index_for(&line, col - 1)
Expand Down Expand Up @@ -611,7 +611,7 @@ impl LspPlugin {
let path = loc.uri.path();
let row = loc.range.start.line + 1;
let column = loc.range.start.character + 1;
let text = utils::read_line_at(path, row as usize)
let text = utils::io::read_line_at(path, row as usize)
.ok()
.flatten()
.unwrap_or_default();
Expand Down
2 changes: 1 addition & 1 deletion crates/maple_core/src/stdio_server/plugin/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn parse_vim_which_key_map(config_file: &str) -> HashMap<char, HashMap<char, Str

let mut map = HashMap::new();

if let Ok(lines) = utils::read_lines(config_file) {
if let Ok(lines) = utils::io::read_lines(config_file) {
lines.for_each(|line| {
if let Ok(line) = line {
if let Some(caps) = COMMENT_DOC.captures(&line) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rgb2ansi256::rgb_to_ansi256;
use std::collections::HashMap;
use std::fmt::Debug;
use std::path::PathBuf;
use utils::read_lines_from_small;
use utils::io::read_lines_from_small;

#[derive(Debug, serde::Serialize)]
struct KeywordHighlight {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::time::Duration;
use types::ClapItem;
use utils::line_count;
use utils::io::line_count;

async fn execute_and_write_cache(
cmd: &str,
Expand Down
3 changes: 2 additions & 1 deletion crates/maple_core/src/stdio_server/provider/hooks/on_move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use std::sync::atomic::Ordering;
use std::time::Duration;
use sublime_syntax::TokenHighlight;
use tokio::sync::oneshot;
use utils::{display_width, SizeChecker};
use utils::display_width;
use utils::io::SizeChecker;

type SublimeHighlightData = Vec<(usize, Vec<TokenHighlight>)>;

Expand Down
4 changes: 2 additions & 2 deletions crates/maple_core/src/stdio_server/provider/impls/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl LocationItem {
// location is 0-based.
let start_line = location.range.start.line + 1;
let start_character = location.range.start.character;
let line = utils::read_line_at(&file_path, start_line as usize)
let line = utils::io::read_line_at(&file_path, start_line as usize)
.ok()
.flatten()?;

Expand Down Expand Up @@ -463,7 +463,7 @@ impl ClapProvider for LspProvider {
.into_iter()
.filter_map(|line_number| self.fetch_location_at(line_number - 1))
.filter_map(|loc| {
let text = utils::read_line_at(&loc.path, loc.row).ok().flatten()?;
let text = utils::io::read_line_at(&loc.path, loc.row).ok().flatten()?;
Some(serde_json::json!({
"filename": loc.path,
"lnum": loc.row,
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 @@ -825,7 +825,7 @@ impl ProviderSource {
.collect(),
),
Self::File { ref path, .. } | Self::CachedFile { ref path, .. } => {
let lines_iter = utils::read_first_lines(path, n).ok()?;
let lines_iter = utils::io::read_first_lines(path, n).ok()?;
if provider_id == "blines" {
let items = lines_iter
.enumerate()
Expand Down
4 changes: 2 additions & 2 deletions crates/maple_markdown/src/toc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fn parse_toc(
line_start: usize,
) -> std::io::Result<Vec<String>> {
let mut code_fence = None;
Ok(utils::read_lines(input_file)?
Ok(utils::io::read_lines(input_file)?
.skip(line_start)
.filter_map(Result::ok)
.filter(|line| match &code_fence {
Expand Down Expand Up @@ -176,7 +176,7 @@ pub fn generate_toc(
pub fn find_toc_range(input_file: impl AsRef<Path>) -> std::io::Result<Option<(usize, usize)>> {
let mut start = 0;

for (idx, line) in utils::read_lines(input_file)?
for (idx, line) in utils::io::read_lines(input_file)?
.map_while(Result::ok)
.enumerate()
{
Expand Down
5 changes: 0 additions & 5 deletions crates/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ use std::process::{Command, Output};
pub mod bytelines;
pub mod io;

pub use self::io::{
count_lines, create_or_overwrite, file_size, line_count, read_first_lines, read_line_at,
read_lines, read_lines_from_small, remove_dir_contents, SizeChecker,
};

/// Returns the width of displaying `n` on the screen.
///
/// Same with `n.to_string().len()` but without allocation.
Expand Down

0 comments on commit 646f1a7

Please sign in to comment.