Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[0.3.14]

- Add Zig support

[0.3.13]

- Add server version to logs on startup
Expand Down
11 changes: 11 additions & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ tree-sitter-rust = "<0.25.0"
tree-sitter-toml-ng = "<0.8.0"
tree-sitter-typescript = "0.23.2"
tree-sitter-zig = "<2"
codebook-tree-sitter-latex = "<0.7.0"
unicode-segmentation = "1.12.0"
url = "2.4.0"
walkdir = "2.3.3"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ Codebook is in active development. As better dictionaries are added, words that
| Haskell | ⚠️ |
| Java | ✅ |
| JavaScript | ✅ |
| LaTeX | ⚠️ |
| Lua | ✅ |
| Markdown | ✅ |
| PHP | ⚠️ |
Expand All @@ -140,6 +141,7 @@ Codebook is in active development. As better dictionaries are added, words that
| Rust | ✅ |
| TOML | ✅ |
| TypeScript | ✅ |
| Zig | ✅ |

✅ = Good to go.
⚠️ = Supported, but needs more testing. Help us improve!
Expand Down
1 change: 1 addition & 0 deletions crates/codebook/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ tree-sitter-haskell.workspace = true
tree-sitter-html.workspace = true
tree-sitter-java.workspace = true
tree-sitter-javascript.workspace = true
codebook-tree-sitter-latex.workspace = true
tree-sitter-lua.workspace = true
tree-sitter-php.workspace = true
tree-sitter-python.workspace = true
Expand Down
26 changes: 18 additions & 8 deletions crates/codebook/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use crate::splitter::{self};

use crate::queries::{LanguageType, get_language_setting};
use regex::Regex;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use streaming_iterator::StreamingIterator;
use tree_sitter::{Parser, Query, QueryCursor};
use unicode_segmentation::UnicodeSegmentation;

#[derive(Debug, Clone, Copy, PartialEq, Ord, Eq, PartialOrd)]
#[derive(Debug, Clone, Copy, PartialEq, Ord, Eq, PartialOrd, Hash)]
pub struct TextRange {
/// Start position in utf-8 byte offset
pub start_byte: usize,
Expand Down Expand Up @@ -195,7 +195,7 @@ fn find_locations_code(

let query = Query::new(&language, language_setting.query).unwrap();
let mut cursor = QueryCursor::new();
let mut word_locations: HashMap<String, Vec<TextRange>> = HashMap::new();
let mut word_locations: HashMap<String, HashSet<TextRange>> = HashMap::new();
let provider = text.as_bytes();
let mut matches_query = cursor.matches(&query, root_node, provider);

Expand All @@ -217,13 +217,18 @@ fn find_locations_code(
end_byte: range.end_byte + node_start_byte,
};
if let Some(existing_result) = word_locations.get_mut(&word_pos.word) {
let added = existing_result.insert(location);
#[cfg(debug_assertions)]
if existing_result.contains(&location) {
panic!("Two of the same locations found. Make a better query.")
if !added {
let word = word_pos.word.clone();
panic!(
"Two of the same locations found. Make a better query. Word: {word}, Location: {location:?}"
)
}
existing_result.push(location);
} else {
word_locations.insert(word_pos.word.clone(), vec![location]);
let mut set = HashSet::new();
set.insert(location);
word_locations.insert(word_pos.word.clone(), set);
}
}
}
Expand All @@ -235,7 +240,12 @@ fn find_locations_code(
.keys()
.map(|word| WordLocation {
word: word.clone(),
locations: word_locations.get(word).cloned().unwrap_or_default(),
locations: word_locations
.get(word)
.cloned()
.unwrap_or_default()
.into_iter()
.collect(),
})
.collect()
}
Expand Down
9 changes: 9 additions & 0 deletions crates/codebook/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub enum LanguageType {
HTML,
Java,
Javascript,
Latex,
Lua,
Php,
Python,
Expand Down Expand Up @@ -106,6 +107,13 @@ pub static LANGUAGE_SETTINGS: &[LanguageSetting] = &[
query: include_str!("queries/javascript.scm"),
extensions: &["js", "jsx"],
},
LanguageSetting {
type_: LanguageType::Latex,
ids: &["latex"],
dictionary_ids: &["latex"],
query: include_str!("queries/latex.scm"),
extensions: &["tex", "latex", "ltx"],
},
LanguageSetting {
type_: LanguageType::Typescript,
ids: &["typescript", "typescriptreact"],
Expand Down Expand Up @@ -216,6 +224,7 @@ impl LanguageSetting {
LanguageType::HTML => Some(tree_sitter_html::LANGUAGE.into()),
LanguageType::Java => Some(tree_sitter_java::LANGUAGE.into()),
LanguageType::Javascript => Some(tree_sitter_javascript::LANGUAGE.into()),
LanguageType::Latex => Some(codebook_tree_sitter_latex::language()),
LanguageType::Lua => Some(tree_sitter_lua::LANGUAGE.into()),
LanguageType::Php => Some(tree_sitter_php::LANGUAGE_PHP.into()),
LanguageType::Python => Some(tree_sitter_python::LANGUAGE.into()),
Expand Down
5 changes: 5 additions & 0 deletions crates/codebook/src/queries/latex.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
; Comments - capture LaTeX comments (lines starting with %)
(line_comment) @comment

; Text content in the document
(text) @string
5 changes: 4 additions & 1 deletion crates/codebook/tests/test_elixir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ fn test_elixir_module() {
println!("Expecting {}", expect.word);
let result = misspelled.iter().find(|r| r.word == expect.word).unwrap();
assert_eq!(result.word, expect.word);
assert_eq!(result.locations, expect.locations);
assert!(result.locations.len() == expect.locations.len());
for location in result.locations.iter() {
assert!(expect.locations.contains(location))
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion crates/codebook/tests/test_go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,11 @@ fn test_go_location() {
for e in &expected {
println!("Expecting: {e:?}");
let miss = misspelled.iter().find(|r| r.word == e.word).unwrap();
assert_eq!(miss.locations, e.locations);
// assert_eq!(miss.locations, e.locations);
assert!(miss.locations.len() == e.locations.len());
for location in &miss.locations {
assert!(e.locations.contains(location));
}
}
for result in misspelled {
assert!(!not_expected.contains(&result.word.as_str()));
Expand Down
Loading