Repository navigation
feat(lang): Nix language support (outline, semantic indexing, ast-grep) #1370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -139,6 +139,21 @@ pub fn detect_language_specific_mistake(pattern: &str, lang: &AstGrepLang) -> Op | |
| ); | ||
| } | ||
| } | ||
| AstGrepLang::Nix => { | ||
| // An attrset body (`a = 1; b = 2;` / `a = 1; inherit b;`) is several | ||
| // sibling expressions with no single AST node to bind, so the | ||
| // contextual fallback refuses it. Scope the pattern to one member. | ||
| if nix_pattern_has_multiple_bindings(src) { | ||
| return Some( | ||
| "Hint: A nix pattern must contain one binding or one inherit clause \ | ||
| (e.g. \"$NAME = $VALUE;\" or \"inherit $NAME;\"). An attrset body is \ | ||
| several expressions with no single AST node to match, so it will not \ | ||
| parse — search for one binding at a time, or match the enclosing \ | ||
| attrset expression." | ||
| .to_string(), | ||
| ); | ||
| } | ||
| } | ||
| _ => {} | ||
| } | ||
|
|
||
|
|
@@ -166,6 +181,101 @@ fn has_regex_escape(src: &str) -> bool { | |
| false | ||
| } | ||
|
|
||
| /// A Nix attrset body carries several sibling members (`a = 1; b = 2;` or | ||
| /// `a = 1; inherit b;`), so the contextual fallback has no single node to bind | ||
| /// and refuses the pattern. The shape reads as a top-level `;` with a further | ||
| /// binding-shaped member after it. String contents are skipped so a `;` inside | ||
| /// `"x;y"` is not mistaken for a binding terminator. | ||
| fn nix_pattern_has_multiple_bindings(src: &str) -> bool { | ||
| let bytes = src.as_bytes(); | ||
| let mut depth: i32 = 0; | ||
| let mut in_string = false; | ||
| let mut in_indented_string = false; | ||
| let mut escaped = false; | ||
| let mut i = 0; | ||
| while i < bytes.len() { | ||
| let b = bytes[i]; | ||
| if in_indented_string { | ||
| // `''` closes an indented string only when not immediately | ||
| // followed by `'` (the `'''` literal apostrophe) or `$` (the | ||
| // `''${` literal interpolation marker; interpolation itself is | ||
| // `${`, which never closes). | ||
| if b == b'\'' && bytes.get(i + 1) == Some(&b'\'') { | ||
| match bytes.get(i + 2) { | ||
| Some(&b'\'') | Some(&b'$') => i += 3, | ||
| _ => { | ||
| in_indented_string = false; | ||
| i += 2; | ||
| } | ||
| } | ||
| continue; | ||
| } | ||
| i += 1; | ||
| continue; | ||
| } | ||
| if in_string { | ||
| if escaped { | ||
| escaped = false; | ||
| } else if b == b'\\' { | ||
| escaped = true; | ||
| } else if b == b'"' { | ||
| in_string = false; | ||
| } | ||
| i += 1; | ||
| continue; | ||
| } | ||
| match b { | ||
| // Consume both opening quotes so escape detection stays anchored | ||
| // at delimiter boundaries (`''''` is an empty indented string, | ||
| // not an opening followed by an escape). | ||
| b'\'' if bytes.get(i + 1) == Some(&b'\'') => { | ||
| in_indented_string = true; | ||
| i += 2; | ||
| continue; | ||
| } | ||
| b'"' => in_string = true, | ||
| b'{' | b'[' | b'(' => depth += 1, | ||
| b'}' | b']' | b')' => depth = (depth - 1).max(0), | ||
| b';' if depth == 0 => { | ||
| // The next member can hide behind whitespace and `#` line | ||
| // comments, or open as a quoted attribute name (`"a" = 1;`). | ||
| let mut j = i + 1; | ||
| while j < bytes.len() { | ||
| let c = bytes[j]; | ||
| if c.is_ascii_whitespace() { | ||
| j += 1; | ||
| } else if c == b'#' { | ||
| while j < bytes.len() && bytes[j] != b'\n' { | ||
| j += 1; | ||
| } | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| if let Some(&c) = bytes.get(j) { | ||
| // A top-level `let …; in …` expression is one member: the | ||
| // `in` that terminates the let is not a sibling binding. | ||
| if c == b'i' | ||
| && bytes.get(j + 1) == Some(&b'n') | ||
| && !matches!(bytes.get(j + 2), | ||
| Some(w) if w.is_ascii_alphanumeric() || *w == b'_') | ||
| { | ||
| i = j + 2; | ||
| continue; | ||
| } | ||
| if c.is_ascii_alphabetic() || c == b'_' || c == b'$' || c == b'"' || c == b'\'' | ||
| { | ||
| return true; | ||
|
Comment on lines
+255
to
+268
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
A valid pattern such as Check that the pattern is an attrset-body fragment before giving this advice. |
||
| } | ||
| } | ||
| } | ||
| _ => {} | ||
| } | ||
| i += 1; | ||
| } | ||
| false | ||
| } | ||
|
|
||
| /// Recognize compact regex ranges, not subtraction inside an AST subscript. | ||
| /// A receiver followed by a terminal index is valid code even when the index | ||
| /// resembles a range; only unmistakable character-class shapes warrant advice. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.