cli: Move more of the tags code from main into the tags module

This commit is contained in:
Max Brunsfeld 2020-03-10 16:52:10 -07:00
parent 0e02ead0de
commit 4996cbe830
3 changed files with 40 additions and 38 deletions

View file

@ -267,35 +267,7 @@ fn run() -> error::Result<()> {
} else if let Some(matches) = matches.subcommand_matches("tags") {
loader.find_all_languages(&config.parser_directories)?;
let paths = collect_paths(matches.values_of("inputs").unwrap())?;
let mut lang = None;
if let Some(scope) = matches.value_of("scope") {
lang = loader.language_configuration_for_scope(scope)?;
if lang.is_none() {
return Error::err(format!("Unknown scope '{}'", scope));
}
}
for path in paths {
let path = Path::new(&path);
let (language, language_config) = match lang {
Some(v) => v,
None => match loader.language_configuration_for_file_name(path)? {
Some(v) => v,
None => {
eprintln!("No language found for path {:?}", path);
continue;
}
},
};
if let Some(tags_config) = language_config.tags_config(language)? {
let source = fs::read(path)?;
tags::generate_tags(tags_config, &source)?;
} else {
eprintln!("No tags config found for path {:?}", path);
}
}
tags::generate_tags(&loader, matches.value_of("scope"), &paths)?;
} else if let Some(matches) = matches.subcommand_matches("highlight") {
loader.configure_highlights(&config.theme.highlight_names);
loader.find_all_languages(&config.parser_directories)?;

View file

@ -1,15 +1,45 @@
use crate::error::Result;
use std::io;
use tree_sitter_tags::{TagsConfiguration, TagsContext};
use super::loader::Loader;
use crate::error::{Error, Result};
use std::fs;
use std::io::{self, Write};
use std::path::Path;
use tree_sitter_tags::TagsContext;
pub fn generate_tags(loader: &Loader, scope: Option<&str>, paths: &[String]) -> Result<()> {
let mut lang = None;
if let Some(scope) = scope {
lang = loader.language_configuration_for_scope(scope)?;
if lang.is_none() {
return Error::err(format!("Unknown scope '{}'", scope));
}
}
pub fn generate_tags(config: &TagsConfiguration, source: &[u8]) -> Result<()> {
let mut context = TagsContext::new();
let stdout = io::stdout();
let mut stdout = stdout.lock();
for tag in context.generate_tags(config, source) {
serde_json::to_writer(&mut stdout, &tag)?;
for path in paths {
let path = Path::new(&path);
let (language, language_config) = match lang {
Some(v) => v,
None => match loader.language_configuration_for_file_name(path)? {
Some(v) => v,
None => {
eprintln!("No language found for path {:?}", path);
continue;
}
},
};
if let Some(tags_config) = language_config.tags_config(language)? {
let source = fs::read(path)?;
for tag in context.generate_tags(tags_config, &source) {
serde_json::to_writer(&mut stdout, &tag)?;
stdout.write(b"\n")?;
}
} else {
eprintln!("No tags config found for path {:?}", path);
}
}
Ok(())