tree-sitter-nu
tree-sitter-nu copied to clipboard
Help with finding ERRORs in tree-sitter-nu.
Related Problem
Because tree-sitter-nu is still WIP there are various errors still in it. The code could still be highlighted even when errors occur (except for the snippest of code affected by the error), so they might go unnoticed.
To deal with this it can be helpful for people to test their nushell scripts via tree-sitter-cli. This can be done manually (running tree-sitter parse on every file and checking for errors there) or through I've script (tree-sitter-get-errors.nu) that I've written that I would post below.
Steps for helping out:
- Add the script below to your
$env.NU_LIB_DIRSdirectory - by default it is a folder called scripts in your configuration directory. - Import the script via
use tree-sitter-get-errors.nu - You can then call tree-sitter-get-errors to find TS errors in your nushell files. Example usage:
tree-sitter-get-errors file1.nu file2.nuCan take any number of files by nametree-sitter-get-errors --glob *nuCan accept globcommand_that_returns_paths | tree-sitter-get-errorsCan take stdin- Or any combination from the listed examples.
- You can pipe the output into
columnsto get a list of all files names with an error in them. (Optional) - Make an issue providing either the full text, or preferably a minimal snippet of the code that still provides an error. You can use the table output from the
tree-sitter-get-errorsscript, to view a list of all error line ranges (LINES START AT 0) in each file to find the code snippet that creates the TS error.
Related script
- tree-sitter-get-errors.nu:
# Test files for tree-sitter errors.
#
# Prints a table with the filename where an error has occured.
# The list is the line numbers of each error in the format of:
# "$STARTING_ERROR_LINE $FINAL_ERROR_LINE"
#
# **Line numbers are offset by -1**: Treesitter parse lines starting from line 0.
#
# The stdin can also be used to accept paths (they are appended to the files).
export def main [
...files: path # Files to parse for error.
--glob (-g): string # Provide a glob to add to the files.
--threads (-t): int # Number of threads to use. Defaults at all.
--no-reduce (-R) # Instead of printing in a table, provide the result in table of lists.
]: [list<path> -> table, list<path> -> list] {
# Parameter assingment with globs & stdin.
let stdin = ($in | default [])
let files = ($files ++ $stdin) | path expand |
append (if $glob != null {glob ($glob | default "")}) | uniq
# Setting threads.
let threads = $threads | default 0
# Output
if $files == [] {return ("Run command with --help to see usage.")}
$files | par-each --threads $threads {|it| _tree-sitter-get-errors-single $it} |
where {|it| ($it | describe) != list<any>} |
if $no_reduce {$in} else {$in | reduce {|it, acc| $acc | merge $it}}
}
# Parse file for TS errors.
#
# Returns table with one column being the file name.
# The list elements are the line number ranges of the error.
export def _tree-sitter-get-errors-single [file: path] {
tree-sitter parse $file | split row "\n" |
where {|it| $it =~ '^\s++\(ERROR'} |
str replace -r '^\s++\(ERROR \[(\d++)[^\[]++\[(\d++).*+$' '$1 $2' |
# Return a table with file name - relative if possible.
wrap (try {$file | path relative-to (pwd)} catch {$file})
}
Fix issue with the script not working in certian cases & add an additional flag for alternative output (-R it is better when a lot of files have errors).
Or: open the file in neovim, hit :InspectTree and then /ERROR<CR>. It opens the AST in a split window in which you can use vim motions to navigate - and as a bonus, the two windows sync positions. That means, when you jump to an ERROR, the cursor in the source file will automatically jump to the position where the error occured.
This is also an option, the reason why I recommended a script was twofold:
- First not everyone is using vim, but I assume people who will be using tree-sitter-nu would also be using nushell, so a script would apply to everyone.
- The script allows you to test your whole nu configuration and a lot of files in bulk.
In no way did I want to devalue the script you provide. I noted that for completeness.