diff options
| author | Munif Tanjim <hello@muniftanjim.dev> | 2022-01-21 18:44:30 +0600 |
|---|---|---|
| committer | Christian Clason <christian.clason@uni-due.de> | 2022-01-21 16:40:36 +0100 |
| commit | 7a6d93ca5b95b4485dd70c1a1d813bb5a80fcc07 (patch) | |
| tree | a3ee6950f76938ccc8e85b9d78f9da92b097e1dd | |
| parent | fix(indent): c/cpp - support newline after closing brace (diff) | |
| download | nvim-treesitter-7a6d93ca5b95b4485dd70c1a1d813bb5a80fcc07.tar nvim-treesitter-7a6d93ca5b95b4485dd70c1a1d813bb5a80fcc07.tar.gz nvim-treesitter-7a6d93ca5b95b4485dd70c1a1d813bb5a80fcc07.tar.bz2 nvim-treesitter-7a6d93ca5b95b4485dd70c1a1d813bb5a80fcc07.tar.lz nvim-treesitter-7a6d93ca5b95b4485dd70c1a1d813bb5a80fcc07.tar.xz nvim-treesitter-7a6d93ca5b95b4485dd70c1a1d813bb5a80fcc07.tar.zst nvim-treesitter-7a6d93ca5b95b4485dd70c1a1d813bb5a80fcc07.zip | |
feat(indent): ecma - support common use-cases
| -rw-r--r-- | queries/ecma/indents.scm | 40 | ||||
| -rw-r--r-- | tests/indent/ecma/binary_expression.js | 4 | ||||
| -rw-r--r-- | tests/indent/ecma/callback.js | 6 | ||||
| -rw-r--r-- | tests/indent/ecma/func.js | 37 | ||||
| -rw-r--r-- | tests/indent/ecma/object.js | 5 | ||||
| -rw-r--r-- | tests/indent/ecma/ternary.js | 6 | ||||
| -rw-r--r-- | tests/indent/ecma/variable.js | 6 | ||||
| -rw-r--r-- | tests/indent/javascript_spec.lua | 59 |
8 files changed, 146 insertions, 17 deletions
diff --git a/queries/ecma/indents.scm b/queries/ecma/indents.scm index 6b8f0ff0d..6630562ea 100644 --- a/queries/ecma/indents.scm +++ b/queries/ecma/indents.scm @@ -1,33 +1,49 @@ [ - (object) - (array) (arguments) - (statement_block) - (object_pattern) + (array) + (binary_expression) (class_body) + (export_clause) + (formal_parameters) (method_definition) (named_imports) - (binary_expression) + (object) + (object_pattern) (return_statement) - (template_substitution) - (expression_statement (call_expression)) - (export_clause) - (switch_statement) + (statement_block) (switch_case) + (switch_statement) + (template_substitution) + (ternary_expression) ] @indent +(arguments (call_expression) @indent) +(binary_expression (call_expression) @indent) +(expression_statement (call_expression) @indent) +(arrow_function + body: (_) @_body + (#not-has-type? @_body statement_block) +) @indent +(assignment_expression + right: (_) @_right + (#not-has-type? @_right arrow_function function) +) @indent +(variable_declarator + value: (_) @_value + (#not-has-type? @_value arrow_function call_expression function) +) @indent +(arguments ")" @indent_end) +(object "}" @indent_end) (statement_block "}" @indent_end) [ (arguments (object)) - "(" ")" - "{" "}" - "[" "]" ] @branch +(statement_block "{" @branch) [ (comment) diff --git a/tests/indent/ecma/binary_expression.js b/tests/indent/ecma/binary_expression.js new file mode 100644 index 000000000..132fee9cf --- /dev/null +++ b/tests/indent/ecma/binary_expression.js @@ -0,0 +1,4 @@ +if_this_is_correct && + run_this_thing() + .filter() + .map() diff --git a/tests/indent/ecma/callback.js b/tests/indent/ecma/callback.js new file mode 100644 index 000000000..a4e1e533e --- /dev/null +++ b/tests/indent/ecma/callback.js @@ -0,0 +1,6 @@ +const itemById = Array.from( + new Set() +).reduce((byId, item) => { + byId[item.id] = item + return result; +}, {}) diff --git a/tests/indent/ecma/func.js b/tests/indent/ecma/func.js new file mode 100644 index 000000000..5bbfa1e31 --- /dev/null +++ b/tests/indent/ecma/func.js @@ -0,0 +1,37 @@ +const arrow_func = ( + a, + b, + c +) => { + log(a, b, c) +} + +const arrow_func_without_brace = (a, b, c) => + log( + a, + b, + c + ) + +function func_def( + a, + b, + { c = '' } +) { + log(a, b, c) +} + +func_call( + a, + (b) => b +) + +chained_func_call() + .map() + .filter() + +func_call( + chained_func_call() + .map() + .filter() +) diff --git a/tests/indent/ecma/object.js b/tests/indent/ecma/object.js new file mode 100644 index 000000000..ca09d238a --- /dev/null +++ b/tests/indent/ecma/object.js @@ -0,0 +1,5 @@ +const obj = { + a: 1, + b: "2", + ["c"]: `three` +} diff --git a/tests/indent/ecma/ternary.js b/tests/indent/ecma/ternary.js new file mode 100644 index 000000000..479e411c9 --- /dev/null +++ b/tests/indent/ecma/ternary.js @@ -0,0 +1,6 @@ +const value = + condition + ? typeof number === 'string' + ? Number(number) + : number + : null; diff --git a/tests/indent/ecma/variable.js b/tests/indent/ecma/variable.js new file mode 100644 index 000000000..80d34beea --- /dev/null +++ b/tests/indent/ecma/variable.js @@ -0,0 +1,6 @@ +let a = + if_this_is_right() && then_this() + +a = func_call() + .map() + .filter() diff --git a/tests/indent/javascript_spec.lua b/tests/indent/javascript_spec.lua index 889e59fdb..022f26f67 100644 --- a/tests/indent/javascript_spec.lua +++ b/tests/indent/javascript_spec.lua @@ -19,14 +19,30 @@ describe("indent JavaScript:", function() describe("new line:", function() for _, info in ipairs { { 1, 2 }, - { 2, 2 }, - { 3, 2 }, + { 2, 4 }, + { 3, 4 }, + } do + run:new_line("ecma/binary_expression.js", { on_line = info[1], text = "//", indent = info[2] }, info[3], info[4]) + end + + for _, info in ipairs { { 4, 2 }, + { 6, 0 }, + } do + run:new_line("ecma/callback.js", { on_line = info[1], text = "//", indent = info[2] }, info[3], info[4]) + end + + for _, info in ipairs { + { 2, 2 }, { 5, 2 }, - { 6, 2 }, { 7, 0 }, + { 12, 4 }, + { 18, 2 }, + { 19, 2 }, + { 20, 2 }, + { 25, 2 }, } do - run:new_line("ecma/try_catch.js", { on_line = info[1], text = "hello()", indent = info[2] }) + run:new_line("ecma/func.js", { on_line = info[1], text = "//", indent = info[2] }, info[3], info[4]) end for _, info in ipairs { @@ -40,7 +56,40 @@ describe("indent JavaScript:", function() { 12, 2 }, { 13, 0 }, } do - run:new_line("ecma/if_else.js", { on_line = info[1], text = "hello()", indent = info[2] }) + run:new_line("ecma/if_else.js", { on_line = info[1], text = "hello()", indent = info[2] }, info[3], info[4]) + end + + for _, info in ipairs { + { 2, 2 }, + { 5, 0 }, + } do + run:new_line("ecma/object.js", { on_line = info[1], text = "//", indent = info[2] }, info[3], info[4]) + end + + for _, info in ipairs { + { 3, 6 }, + { 4, 6 }, + } do + run:new_line("ecma/ternary.js", { on_line = info[1], text = "//", indent = info[2] }, info[3], info[4]) + end + + for _, info in ipairs { + { 1, 2 }, + { 2, 2 }, + { 3, 2 }, + { 4, 2 }, + { 5, 2 }, + { 6, 2 }, + { 7, 0 }, + } do + run:new_line("ecma/try_catch.js", { on_line = info[1], text = "hello()", indent = info[2] }, info[3], info[4]) + end + + for _, info in ipairs { + { 1, 2 }, + { 2, 0 }, + } do + run:new_line("ecma/variable.js", { on_line = info[1], text = "hello()", indent = info[2] }, info[3], info[4]) end end) end) |
