Unify path:row:column parsing, use it in CLI

This commit is contained in:
Kirill Bulatov 2023-05-12 16:52:07 +03:00
parent 89fe5c6b09
commit d719352152
9 changed files with 127 additions and 65 deletions

View file

@ -16,6 +16,7 @@ use std::{
path::{Path, PathBuf},
ptr,
};
use util::paths::PathLikeWithPosition;
#[derive(Parser)]
#[clap(name = "zed", global_setting(clap::AppSettings::NoAutoVersion))]
@ -24,8 +25,11 @@ struct Args {
#[clap(short, long)]
wait: bool,
/// A sequence of space-separated paths that you want to open.
#[clap()]
paths: Vec<PathBuf>,
///
/// Use `path:line:row` syntax to open a file at a specific location.
/// Non-existing paths and directories will ignore `:line:row` suffix.
#[clap(value_parser = parse_path_with_position)]
paths_with_position: Vec<PathLikeWithPosition<PathBuf>>,
/// Print Zed's version and the app path.
#[clap(short, long)]
version: bool,
@ -34,6 +38,14 @@ struct Args {
bundle_path: Option<PathBuf>,
}
fn parse_path_with_position(
argument_str: &str,
) -> Result<PathLikeWithPosition<PathBuf>, std::convert::Infallible> {
PathLikeWithPosition::parse_str(argument_str, |path_str| {
Ok(Path::new(path_str).to_path_buf())
})
}
#[derive(Debug, Deserialize)]
struct InfoPlist {
#[serde(rename = "CFBundleShortVersionString")]
@ -50,7 +62,11 @@ fn main() -> Result<()> {
return Ok(());
}
for path in args.paths.iter() {
for path in args
.paths_with_position
.iter()
.map(|path_with_position| &path_with_position.path_like)
{
if !path.exists() {
touch(path.as_path())?;
}
@ -60,9 +76,13 @@ fn main() -> Result<()> {
tx.send(CliRequest::Open {
paths: args
.paths
.paths_with_position
.into_iter()
.map(|path| fs::canonicalize(path).map_err(|error| anyhow!(error)))
// TODO kb continue sendint path with the position further
.map(|path_with_position| path_with_position.path_like)
.map(|path| {
fs::canonicalize(&path).with_context(|| format!("path {path:?} canonicalization"))
})
.collect::<Result<Vec<PathBuf>>>()?,
wait: args.wait,
})?;