Skip to content

Commit

Permalink
feat: destructuring statement
Browse files Browse the repository at this point in the history
  • Loading branch information
novusnota committed Nov 15, 2024
1 parent 5f35fe2 commit f65b084
Show file tree
Hide file tree
Showing 15 changed files with 5,803 additions and 5,040 deletions.
7 changes: 6 additions & 1 deletion editor_queries/helix/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

(identifier) @variable

(destruct_bind
name: (identifier) @comment.line
bind: (identifier) @variable)

; variable.builtin
; ----------------

Expand Down Expand Up @@ -61,14 +65,15 @@
"/" "/="
"%" "%="
"=" "=="
"!" "!=" "!!"
"<" "<=" "<<" "<<="
">" ">=" ">>" ">>="
"&" "&="
"|" "|="
"^" "^="
"&&" "&&="
"||" "||="
"->"
"->" ".."
] @operator

; constructor
Expand Down
1 change: 1 addition & 0 deletions editor_queries/helix/indents.scm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

; {..., ...}
(instance_argument_list)
(destruct_bind_list)

; {...; ...}
(message_body)
Expand Down
3 changes: 3 additions & 0 deletions editor_queries/helix/textobjects.scm
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
(instance_argument_list
((_) @parameter.inside @parameter.movement . ","? @parameter.around) @parameter.around)

(destruct_bind_list
((_) @parameter.inside @parameter.movement . ","? @parameter.around) @parameter.around)

; single parameter

(receive_function
Expand Down
1 change: 1 addition & 0 deletions editor_queries/neovim/folds.scm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
(argument_list)
; {…, …}
(instance_argument_list)
(destruct_bind_list)
; {…; …}
(message_body)
(struct_body)
Expand Down
5 changes: 5 additions & 0 deletions editor_queries/neovim/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
; --------
(identifier) @variable

(destruct_bind
name: (identifier) @comment
bind: (identifier) @variable)

; variable.builtin
; ----------------
(self) @variable.builtin
Expand Down Expand Up @@ -69,6 +73,7 @@
"||"
"||="
"->"
".."
] @operator

; constructor
Expand Down
1 change: 1 addition & 0 deletions editor_queries/neovim/indents.scm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
(argument_list)
; {..., ...}
(instance_argument_list)
(destruct_bind_list)
; {...; ...}
(message_body)
(struct_body)
Expand Down
15 changes: 15 additions & 0 deletions editor_queries/neovim/textobjects.scm
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,21 @@
","? @_end
(#make-range! "parameter.outer" @parameter.inner @_end))

; second and following
(destruct_bind_list
"," @_start
.
(_) @parameter.inner
(#make-range! "parameter.outer" @_start @parameter.inner))

; first
(destruct_bind_list
.
(_) @parameter.inner
.
","? @_end
(#make-range! "parameter.outer" @parameter.inner @_end))

; single parameter
(receive_function
parameter: (_) @parameter.inner @parameter.outer)
Expand Down
33 changes: 33 additions & 0 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ function commaSepWithTrailing(rule) {
return optional(seq(rule, repeat(seq(",", rule)), optional(",")));
}

/**
* Creates a rule to match one or more of the rules separated by a comma with an optional trailing comma or a trailing comma and a .. ("the rest", remaining fields)
*
* @param {Rule} rule
*
* @return {ChoiceRule}
*
*/
function commaSepWithTrailingRest(rule) {
return optional(
seq(rule, repeat(seq(",", rule)), optional(seq(",", optional("..")))),
);
}

/**
* Creates a rule to match one or more of the rules separated by a comma
*
Expand Down Expand Up @@ -468,6 +482,7 @@ module.exports = grammar({
$.assignment_statement, // StatementAssign
$.augmented_assignment_statement, // StatementAugmentedAssign
$.do_until_statement, // StatementUntil
$.destruct_statement, // StatementDestruct
),

let_statement: ($) =>
Expand All @@ -479,6 +494,24 @@ module.exports = grammar({
field("value", $._expression),
),

destruct_statement: ($) =>
seq(
"let",
field("name", alias($._type_identifier, $.type_identifier)),
field("binds", $.destruct_bind_list),
"=",
field("value", $._expression),
),

destruct_bind_list: ($) =>
seq("{", commaSepWithTrailingRest($.destruct_bind), "}"),

destruct_bind: ($) =>
seq(
field("name", $.identifier),
optional(seq(":", field("bind", $.identifier))),
),

block_statement: ($) =>
prec.right(
seq(
Expand Down
8 changes: 6 additions & 2 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

(identifier) @variable

(destruct_bind
name: (identifier) @comment
bind: (identifier) @variable)

; variable.builtin
; ----------------

Expand Down Expand Up @@ -55,7 +59,7 @@
"^" "^="
"&&" "&&="
"||" "||="
"->"
"->" ".."
] @operator

; constructor
Expand Down Expand Up @@ -119,7 +123,7 @@

(storage_constant
name: (identifier) @constant)

; constant.builtin
; ----------------

Expand Down
7 changes: 7 additions & 0 deletions queries/locals.scm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
(let_statement
name: (identifier) @local.definition)

(destruct_bind
name: (identifier) @local.definition)

(destruct_bind
name: (identifier) @local.definition
bind: (identifier) @local.definition)

(parameter
name: (identifier) @local.definition)

Expand Down
159 changes: 159 additions & 0 deletions src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f65b084

Please sign in to comment.