diff --git a/Cargo.lock b/Cargo.lock index 198aea26..ff6ec4b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -391,6 +391,7 @@ dependencies = [ "tree-sitter-javascript", "tree-sitter-lua", "tree-sitter-md", + "tree-sitter-nix", "tree-sitter-ocaml", "tree-sitter-odin-codebook", "tree-sitter-php", @@ -2743,6 +2744,16 @@ dependencies = [ "tree-sitter-language", ] +[[package]] +name = "tree-sitter-nix" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4952a9733f3a98f6683a0ccd1035d84ab7a52f7e84eeed58548d86765ad92de3" +dependencies = [ + "cc", + "tree-sitter-language", +] + [[package]] name = "tree-sitter-ocaml" version = "0.25.0" diff --git a/Cargo.toml b/Cargo.toml index 758a8775..ec6ed40f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -67,6 +67,7 @@ tree-sitter-java = "<0.25.0" tree-sitter-javascript = "<0.26.0" tree-sitter-lua = "<0.25.0" tree-sitter-md = "<0.6.0" +tree-sitter-nix = "0.3.0" tree-sitter-ocaml = "<0.26" tree-sitter-odin-codebook = "1.4.0" tree-sitter-php = "<0.25.0" diff --git a/README.md b/README.md index c1b598a3..7344f6a2 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ Codebook uses a hierarchical configuration system with global (user-level) and p ✅ **Good to go:** C, Go, Java, JavaScript, Lua, Markdown, Odin, Plain Text, Python, Ruby, Rust, TOML, TypeScript, Zig -⚠️ **Supported, needs more testing** (help us improve!): Astro, Bash, C#, C++, CSS, Dart, Elixir, Erlang, Haskell, HTML, Just, LaTeX, OCaml, PHP, Svelte, Swift, Typst, VHDL, Vue, YAML +⚠️ **Supported, needs more testing** (help us improve!): Astro, Bash, C#, C++, CSS, Dart, Elixir, Erlang, Haskell, HTML, Just, LaTeX, Nix, OCaml, PHP, Svelte, Swift, Typst, VHDL, Vue, YAML If Codebook is not marking issues you think it should, please file a GitHub issue! diff --git a/crates/codebook/Cargo.toml b/crates/codebook/Cargo.toml index 0d70d043..2f46112f 100644 --- a/crates/codebook/Cargo.toml +++ b/crates/codebook/Cargo.toml @@ -43,6 +43,7 @@ codebook-tree-sitter-just.workspace = true codebook-tree-sitter-latex.workspace = true tree-sitter-lua.workspace = true tree-sitter-md.workspace = true +tree-sitter-nix.workspace = true tree-sitter-ocaml.workspace = true tree-sitter-odin-codebook.workspace = true tree-sitter-php.workspace = true diff --git a/crates/codebook/src/queries.rs b/crates/codebook/src/queries.rs index 12e9e32e..6f72f478 100644 --- a/crates/codebook/src/queries.rs +++ b/crates/codebook/src/queries.rs @@ -21,6 +21,7 @@ pub enum LanguageType { Latex, Lua, Markdown, + Nix, OCaml, Odin, Php, @@ -234,6 +235,13 @@ pub static LANGUAGE_SETTINGS: &[LanguageSetting] = &[ query: include_str!("queries/markdown.scm"), extensions: &["md", "markdown"], }, + LanguageSetting { + type_: LanguageType::Nix, + ids: &["nix"], + dictionary_ids: &[], + query: include_str!("queries/nix.scm"), + extensions: &["nix"], + }, LanguageSetting { type_: LanguageType::Bash, ids: &[ @@ -344,6 +352,7 @@ impl LanguageSetting { LanguageType::Latex => Some(codebook_tree_sitter_latex::LANGUAGE.into()), LanguageType::Lua => Some(tree_sitter_lua::LANGUAGE.into()), LanguageType::Markdown => Some(tree_sitter_md::LANGUAGE.into()), + LanguageType::Nix => Some(tree_sitter_nix::LANGUAGE.into()), LanguageType::OCaml => Some(tree_sitter_ocaml::LANGUAGE_OCAML.into()), LanguageType::Odin => Some(tree_sitter_odin_codebook::LANGUAGE.into()), LanguageType::Php => Some(tree_sitter_php::LANGUAGE_PHP.into()), diff --git a/crates/codebook/src/queries/nix.scm b/crates/codebook/src/queries/nix.scm new file mode 100644 index 00000000..45edd67b --- /dev/null +++ b/crates/codebook/src/queries/nix.scm @@ -0,0 +1,30 @@ +(comment) @comment + +; Strings +(string_expression) @string +(indented_string_expression (string_fragment) @string.heredoc) +[(path_expression) (hpath_expression) (spath_expression)] @string.special +(uri_expression) @string.special + +; Parameters +(function_expression + universal: (identifier) @identifier.parameter) +(formal + name: (identifier) @identifier.parameter) + +; Fields +(inherit + attrs: (inherited_attrs attr: (identifier) @identifier.field)) +(inherit_from + attrs: (inherited_attrs attr: (identifier) @identifier.field)) + +; Constants, let binds are immutable, don't fall into variables category +(binding + attrpath: (attrpath (identifier)) @identifier.constant) + +; Function +(apply_expression + function: [ + (variable_expression (identifier)) @identifier.function + (select_expression + attrpath: (attrpath attr: (identifier) @identifier.function .))]) diff --git a/crates/codebook/tests/examples/example.nix b/crates/codebook/tests/examples/example.nix new file mode 100644 index 00000000..9412aa01 --- /dev/null +++ b/crates/codebook/tests/examples/example.nix @@ -0,0 +1,29 @@ +# Commment with a typo +{ + description = "A useful dev shell for the packge"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + flakeUtils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, flakeUtils }: + flakeUtils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { inherit system; }; + verion = "0.1.0"; + in + { + devShells.defualt = pkgs.mkShell { + buildInputs = [ pkgs.cargo pkgs.rustc ]; + shellHook = '' + echo "Entring dev shell version ${verion}" + ''; + }; + packages.default = pkgs.stdenv.mkDerivation { + pname = "my-projet"; + version = verion; + src = ./.; + }; + }); +} diff --git a/crates/codebook/tests/languages/main.rs b/crates/codebook/tests/languages/main.rs index 59000370..0bb4ecfc 100644 --- a/crates/codebook/tests/languages/main.rs +++ b/crates/codebook/tests/languages/main.rs @@ -17,6 +17,7 @@ mod test_just; mod test_latex; mod test_lua; mod test_markdown; +mod test_nix; mod test_ocaml; mod test_odin; mod test_php; diff --git a/crates/codebook/tests/languages/test_nix.rs b/crates/codebook/tests/languages/test_nix.rs new file mode 100644 index 00000000..5431abd8 --- /dev/null +++ b/crates/codebook/tests/languages/test_nix.rs @@ -0,0 +1,28 @@ +use codebook::queries::LanguageType; + +use super::utils::assert_spelling_at; + +#[test] +fn test_nix_location() { + let sample_text = include_str!("../examples/example.nix"); + + assert_spelling_at( + LanguageType::Nix, + sample_text, + &[ + ("Commment", &[0]), + ("packge", &[0]), + // Flagged at the let binding declaration only; + // the `${verion}` interpolation and `version = verion;` usages are not flagged + ("verion", &[0]), + ("defualt", &[0]), + ("Entring", &[0]), + ("projet", &[0]), + ("pname", &[0]), + // Below strings are conventional / very common abbreviations in Nix, but they are not keywords, + // so they must be flagged as misspelled + ("nixpkgs", &[0, 2]), + ("pkgs", &[3]), + ], + ); +}