Skip to content
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

Help with finding ERRORs in tree-sitter-nu. #91

Open
CabalCrow opened this issue May 24, 2024 · 1 comment
Open

Help with finding ERRORs in tree-sitter-nu. #91

CabalCrow opened this issue May 24, 2024 · 1 comment

Comments

@CabalCrow
Copy link
Contributor

CabalCrow commented May 24, 2024

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:

  1. Add the script below to your $env.NU_LIB_DIRS directory - by default it is a folder called scripts in your configuration directory.
  2. Import the script via use tree-sitter-get-errors.nu
  3. 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.
  4. You can pipe the output into columns to get a list of all files names with an error in them. (Optional)
  5. 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})
}
@fdncred fdncred pinned this issue May 24, 2024
@CabalCrow
Copy link
Contributor Author

CabalCrow commented May 25, 2024

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant