tree-sitter-nu icon indicating copy to clipboard operation
tree-sitter-nu copied to clipboard

Support the spread operator

Open hedyhli opened this issue 1 year ago • 2 comments

image

Currently, the ... is parsed as a string, whereas it's a special operator that should be assigned something special, and then highlighted as an operator. If it's parsed as a expr_unary, highlights.scm can then be changed as follows:

diff --git a/queries/nu/highlights.scm b/queries/nu/highlights.scm
index c73f8f3..ce0b839 100644
--- a/queries/nu/highlights.scm
+++ b/queries/nu/highlights.scm
@@ -161,7 +161,7 @@
     "++="
 ] @operator)
 
-(expr_unary ["not" "-"] @operator)
+(expr_unary ["not" "-" "..."] @operator)
 
 (val_range [
     ".."

hedyhli avatar Jun 12 '24 01:06 hedyhli

agreed

fdncred avatar Jun 12 '24 02:06 fdncred

I'd take a look at this. I also observed a problem with an alias:

image

They all work correctly, even without the quotes, but tree-sitter-nu can't handle them. These two are probably related. If not, I'll create separate PRs

mrdgo avatar Sep 24 '24 09:09 mrdgo

So far, I couldn't get this to work in pure grammar.js. As I understand, the dots must have no whitespace before the list and we would have to detect a correctly delimited list. I fear that we need to implement this in a scanner.c. Except @blindFS can do some magic?

Edit: just as I wrote that down, an idea struck. Will fiddle, might still need someone to discuss a solution.

As soon as a bracket, brace, or paren follows the three dots, nu expects a closing one and tries to spread the resulting value. In theory, this should be doable as a unary operator.

mrdgo avatar Nov 15 '24 21:11 mrdgo

I noticed this issue the other day, my plan is

  1. create new rules for ...() ...[] ...{} ...$, correct me if something's missing.
  2. Then add those rules to allowed places, I believe ...{} is only allowed inside record.

We can group ...( for example as single terminal token, so it won't mess other patterns with leading dots like range or decimal.

blindFS avatar Nov 15 '24 23:11 blindFS

Maybe I should create a new issue for all the problems that I found so far, so that someone may come and fix it before me.

blindFS avatar Nov 15 '24 23:11 blindFS

I'd take a look at this. I also observed a problem with an alias:

image

They all work correctly, even without the quotes, but tree-sitter-nu can't handle them. These two are probably related. If not, I'll create separate PRs

@mrdgo The alias example still fails after #143, but it's another issue caused by restricted command identifier.

blindFS avatar Nov 17 '24 05:11 blindFS