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_DIRS
directory - 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.nu
Can take any number of files by name -
tree-sitter-get-errors --glob *nu
Can accept glob -
command_that_returns_paths | tree-sitter-get-errors
Can take stdin - Or any combination from the listed examples.
-
- You can pipe the output into
columns
to 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-errors
script, 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})
}