Skip to content

Commit

Permalink
refactor(rust): misc fixes & tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
amaanq committed Apr 12, 2024
1 parent 5825e24 commit abc7910
Show file tree
Hide file tree
Showing 23 changed files with 131 additions and 121 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions cli/loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ once_cell.workspace = true
regex.workspace = true
serde.workspace = true
serde_json.workspace = true
tempfile.workspace = true

tree-sitter.workspace = true
tree-sitter-highlight.workspace = true
Expand Down
28 changes: 18 additions & 10 deletions cli/loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,12 @@ pub struct CompileConfig<'a> {
}

impl<'a> CompileConfig<'a> {
#[must_use]
pub fn new(
src_path: &'a Path,
externals: Option<&'a [PathBuf]>,
output_path: Option<PathBuf>,
) -> CompileConfig<'a> {
) -> Self {
Self {
src_path,
header_paths: vec![src_path],
Expand Down Expand Up @@ -449,7 +450,7 @@ impl Loader {
let parser_path = config.src_path.join("parser.c");
config.scanner_path = self.get_scanner_path(config.src_path);

let mut paths_to_check = vec![parser_path.clone()];
let mut paths_to_check = vec![parser_path];

if let Some(scanner_path) = config.scanner_path.as_ref() {
paths_to_check.push(scanner_path.clone());
Expand Down Expand Up @@ -488,7 +489,9 @@ impl Loader {
}

let lock_path = if env::var("CROSS_RUNNER").is_ok() {
PathBuf::from("/tmp")
tempfile::tempdir()
.unwrap()
.path()
.join("tree-sitter")
.join("lock")
.join(format!("{}.lock", config.name))
Expand Down Expand Up @@ -1021,7 +1024,7 @@ impl Loader {
language_name: grammar_json.name.clone(),
scope: config_json.scope,
language_id,
file_types: config_json.file_types.unwrap_or(Vec::new()),
file_types: config_json.file_types.unwrap_or_default(),
content_regex: Self::regex(config_json.content_regex.as_deref()),
first_line_regex: Self::regex(config_json.first_line_regex.as_deref()),
injection_regex: Self::regex(config_json.injection_regex.as_deref()),
Expand All @@ -1048,8 +1051,11 @@ impl Loader {
.push(self.language_configurations.len());
}

self.language_configurations
.push(unsafe { mem::transmute(configuration) });
self.language_configurations.push(unsafe {
mem::transmute::<LanguageConfiguration<'_>, LanguageConfiguration<'static>>(
configuration,
)
});

if set_current_path_config
&& self.language_configuration_in_current_path.is_none()
Expand Down Expand Up @@ -1088,8 +1094,11 @@ impl Loader {
highlight_names: &self.highlight_names,
use_all_highlight_names: self.use_all_highlight_names,
};
self.language_configurations
.push(unsafe { mem::transmute(configuration) });
self.language_configurations.push(unsafe {
mem::transmute::<LanguageConfiguration<'_>, LanguageConfiguration<'static>>(
configuration,
)
});
self.languages_by_id
.push((parser_path.to_owned(), OnceCell::new(), None));
}
Expand Down Expand Up @@ -1327,8 +1336,7 @@ impl<'a> LanguageConfiguration<'a> {
.unwrap_or_else(|| ranges.last().unwrap());
error.offset = offset_within_section - range.start;
error.row = source[range.start..offset_within_section]
.chars()
.filter(|c| *c == '\n')
.matches(|c| c == '\n')
.count();
Error::from(error).context(format!("Error in query file {path:?}"))
}
Expand Down
12 changes: 6 additions & 6 deletions cli/src/generate/build_tables/build_parse_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ impl<'a> ParseTableBuilder<'a> {
}
}

reduction_info.precedence = precedence.clone();
reduction_info.precedence.clone_from(precedence);
if let Err(i) = reduction_info.symbols.binary_search(&symbol) {
reduction_info.symbols.insert(i, symbol);
}
Expand Down Expand Up @@ -604,13 +604,13 @@ impl<'a> ParseTableBuilder<'a> {
write!(&mut msg, " {}", self.symbol_name(symbol)).unwrap();
}

write!(
writeln!(
&mut msg,
" • {} …\n\n",
" • {} …\n",
self.symbol_name(&conflicting_lookahead)
)
.unwrap();
write!(&mut msg, "Possible interpretations:\n\n").unwrap();
writeln!(&mut msg, "Possible interpretations:\n").unwrap();

let mut interpretations = conflicting_items
.iter()
Expand Down Expand Up @@ -685,7 +685,7 @@ impl<'a> ParseTableBuilder<'a> {
}

let mut resolution_count = 0;
write!(&mut msg, "\nPossible resolutions:\n\n").unwrap();
writeln!(&mut msg, "\nPossible resolutions:\n").unwrap();
let mut shift_items = Vec::new();
let mut reduce_items = Vec::new();
for item in conflicting_items {
Expand Down Expand Up @@ -961,7 +961,7 @@ fn populate_following_tokens(
for entry in result.iter_mut() {
entry.insert(*extra);
}
result[extra.index] = all_tokens.clone();
result[extra.index].clone_from(&all_tokens);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion cli/src/generate/build_tables/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl<'a> ParseItem<'a> {

/// Create an item like this one, but advanced by one step.
#[must_use]
pub const fn successor(&self) -> ParseItem<'a> {
pub const fn successor(&self) -> Self {
ParseItem {
variable_index: self.variable_index,
production: self.production,
Expand Down
23 changes: 11 additions & 12 deletions cli/src/generate/parse_grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,18 @@ fn parse_rule(json: RuleJSON) -> Rule {
RuleJSON::PATTERN { value, flags } => Rule::Pattern(
value,
flags.map_or(String::new(), |f| {
f.chars()
.filter(|c| {
if *c == 'i' {
true
} else {
// silently ignore unicode flags
if *c != 'u' && *c != 'v' {
eprintln!("Warning: unsupported flag {c}");
}
false
f.matches(|c| {
if c == 'i' {
true
} else {
// silently ignore unicode flags
if c != 'u' && c != 'v' {
eprintln!("Warning: unsupported flag {c}");
}
})
.collect()
false
}
})
.collect()
}),
),
RuleJSON::SYMBOL { name } => Rule::NamedSymbol(name),
Expand Down
2 changes: 1 addition & 1 deletion cli/src/generate/prepare_grammar/process_inlines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl InlinedProductionMapBuilder {
self.productions
.iter()
.position(|p| *p == production)
.unwrap_or({
.unwrap_or_else(|| {
self.productions.push(production);
self.productions.len() - 1
})
Expand Down
17 changes: 8 additions & 9 deletions cli/src/generate/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ macro_rules! add {
}

macro_rules! add_whitespace {
($this: tt) => {{
($this:tt) => {{
for _ in 0..$this.indent_level {
write!(&mut $this.buffer, " ").unwrap();
}
Expand All @@ -45,13 +45,13 @@ macro_rules! add_line {
}

macro_rules! indent {
($this: tt) => {
($this:tt) => {
$this.indent_level += 1;
};
}

macro_rules! dedent {
($this: tt) => {
($this:tt) => {
assert_ne!($this.indent_level, 0);
$this.indent_level -= 1;
};
Expand Down Expand Up @@ -221,9 +221,8 @@ impl Generator {
});

// Some aliases match an existing symbol in the grammar.
let alias_id;
if let Some(existing_symbol) = existing_symbol {
alias_id = self.symbol_ids[&self.symbol_map[&existing_symbol]].clone();
let alias_id = if let Some(existing_symbol) = existing_symbol {
self.symbol_ids[&self.symbol_map[&existing_symbol]].clone()
}
// Other aliases don't match any existing symbol, and need their own
// identifiers.
Expand All @@ -232,12 +231,12 @@ impl Generator {
self.unique_aliases.insert(i, alias.clone());
}

alias_id = if alias.is_named {
if alias.is_named {
format!("alias_sym_{}", self.sanitize_identifier(&alias.value))
} else {
format!("anon_alias_sym_{}", self.sanitize_identifier(&alias.value))
};
}
}
};

self.alias_ids.entry(alias.clone()).or_insert(alias_id);
}
Expand Down
3 changes: 1 addition & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,8 +870,7 @@ fn run() -> Result<()> {
let open_in_browser = !playground_options.quiet;
let grammar_path = playground_options
.grammar_path
.map(PathBuf::from)
.unwrap_or(current_dir);
.map_or(current_dir, PathBuf::from);
playground::serve(&grammar_path, open_in_browser)?;
}

Expand Down
2 changes: 1 addition & 1 deletion cli/src/playground.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use tiny_http::{Header, Response, Server};
use super::wasm;

macro_rules! optional_resource {
($name: tt, $path: tt) => {
($name:tt, $path:tt) => {
#[cfg(TREE_SITTER_EMBED_WASM_BINDING)]
fn $name(tree_sitter_dir: Option<&Path>) -> Cow<'static, [u8]> {
if let Some(tree_sitter_dir) = tree_sitter_dir {
Expand Down
6 changes: 3 additions & 3 deletions cli/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,9 @@ fn write_tests_to_buffer(
if i > 0 {
writeln!(buffer)?;
}
write!(
writeln!(
buffer,
"{}\n{name}\n{}\n{input}\n{}\n\n{}\n",
"{}\n{name}\n{}\n{input}\n{}\n\n{}",
"=".repeat(*header_delim_len),
"=".repeat(*header_delim_len),
"-".repeat(*divider_delim_len),
Expand Down Expand Up @@ -662,7 +662,7 @@ fn parse_test_content(name: String, content: &str, file_path: Option<PathBuf>) -
}
}
prev_attributes = attributes;
prev_name = test_name.unwrap_or(String::new());
prev_name = test_name.unwrap_or_default();
prev_header_len = header_delim_len;
prev_header_end = header_range.end;
}
Expand Down
2 changes: 1 addition & 1 deletion cli/src/tests/corpus_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ fn test_language_corpus(

// Perform a random series of edits and reparse.
let mut undo_stack = Vec::new();
for _ in 0..1 + rand.unsigned(*EDIT_COUNT) {
for _ in 0..=rand.unsigned(*EDIT_COUNT) {
let edit = get_random_edit(&mut rand, &input);
undo_stack.push(invert_edit(&input, &edit));
perform_edit(&mut tree, &mut input, &edit).unwrap();
Expand Down
23 changes: 11 additions & 12 deletions cli/src/tests/detect_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,25 @@ fn detect_language_by_first_line_regex() {
assert_eq!(config[0].scope.as_ref().unwrap(), "source.strace");

let file_name = strace_dir.path().join("strace.log");
std::fs::write(&file_name, "execve\nworld").unwrap();
fs::write(&file_name, "execve\nworld").unwrap();
assert_eq!(
get_lang_scope(&loader, &file_name),
Some("source.strace".into())
);

let file_name = strace_dir.path().join("strace.log");
std::fs::write(&file_name, "447845 execve\nworld").unwrap();
fs::write(&file_name, "447845 execve\nworld").unwrap();
assert_eq!(
get_lang_scope(&loader, &file_name),
Some("source.strace".into())
);

let file_name = strace_dir.path().join("strace.log");
std::fs::write(&file_name, "hello\nexecve").unwrap();
fs::write(&file_name, "hello\nexecve").unwrap();
assert!(get_lang_scope(&loader, &file_name).is_none());

let file_name = strace_dir.path().join("strace.log");
std::fs::write(&file_name, "").unwrap();
fs::write(&file_name, "").unwrap();
assert!(get_lang_scope(&loader, &file_name).is_none());

let dummy_dir = tree_sitter_dir(
Expand All @@ -76,7 +76,7 @@ fn detect_language_by_first_line_regex() {
.find_language_configurations_at_path(dummy_dir.path(), false)
.unwrap();
let file_name = dummy_dir.path().join("strace.dummy");
std::fs::write(&file_name, "execve").unwrap();
fs::write(&file_name, "execve").unwrap();
assert_eq!(
get_lang_scope(&loader, &file_name),
Some("source.dummy".into())
Expand All @@ -85,15 +85,14 @@ fn detect_language_by_first_line_regex() {

fn tree_sitter_dir(package_json: &str, name: &str) -> tempfile::TempDir {
let temp_dir = tempfile::tempdir().unwrap();
std::fs::write(temp_dir.path().join("package.json"), package_json).unwrap();
std::fs::create_dir(temp_dir.path().join("src")).unwrap();
std::fs::create_dir(temp_dir.path().join("src/tree_sitter")).unwrap();
std::fs::write(
fs::write(temp_dir.path().join("package.json"), package_json).unwrap();
fs::create_dir_all(temp_dir.path().join("src/tree_sitter")).unwrap();
fs::write(
temp_dir.path().join("src/grammar.json"),
format!(r#"{{"name":"{name}"}}"#),
)
.unwrap();
std::fs::write(
fs::write(
temp_dir.path().join("src/parser.c"),
format!(
r##"
Expand All @@ -108,15 +107,15 @@ fn tree_sitter_dir(package_json: &str, name: &str) -> tempfile::TempDir {
),
)
.unwrap();
std::fs::write(
fs::write(
temp_dir.path().join("src/tree_sitter/parser.h"),
include_str!("../../../lib/src/parser.h"),
)
.unwrap();
temp_dir
}

// if we manage to get the language scope, it means we correctly detected the file-type
// If we manage to get the language scope, it means we correctly detected the file-type
fn get_lang_scope(loader: &Loader, file_name: &Path) -> Option<String> {
loader
.language_configuration_for_file_name(file_name)
Expand Down
2 changes: 1 addition & 1 deletion cli/src/tests/helpers/edits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<'a> ReadRecorder<'a> {

pub fn strings_read(&self) -> Vec<&'a str> {
let mut result = Vec::new();
let mut last_range: Option<Range<usize>> = None;
let mut last_range = Option::<Range<usize>>::None;
for index in &self.indices_read {
if let Some(ref mut range) = &mut last_range {
if range.end == *index {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/tests/helpers/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Rand {
}

pub fn unsigned(&mut self, max: usize) -> usize {
self.0.gen_range(0..max + 1)
self.0.gen_range(0..=max)
}

pub fn words(&mut self, max_count: usize) -> Vec<u8> {
Expand Down
3 changes: 1 addition & 2 deletions cli/src/tests/highlight_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,8 +753,7 @@ fn to_token_vector<'a>(
for (i, l) in s.split('\n').enumerate() {
let l = l.trim_end_matches('\r');
if i > 0 {
lines.push(line);
line = Vec::new();
lines.push(std::mem::take(&mut line));
}
if !l.is_empty() {
line.push((l, highlights.clone()));
Expand Down
Loading

0 comments on commit abc7910

Please sign in to comment.