From 2ab4a6cacd76454b326b014125d1c8fe8be93877 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Sun, 20 Feb 2022 22:26:45 +0800 Subject: [PATCH 01/21] Add quries --- queries/bash/textobjects.scm | 14 +++ queries/bibtex/textobjects.scm | 3 + queries/c/textobjects.scm | 75 +++++++++++ queries/c_sharp/textobjects.scm | 59 +++++++++ queries/cpp/textobjects.scm | 57 +++++++++ queries/cuda/textobjects.scm | 1 + queries/dart/textobjects.scm | 104 ++++++++++++++++ queries/elixir/textobjects.scm | 55 ++++++++ queries/elm/textobjects.scm | 84 +++++++++++++ queries/fennel/textobjects.scm | 52 ++++++++ queries/fish/textobjects.scm | 16 +++ queries/glsl/textobjects.scm | 1 + queries/go/textobjects.scm | 83 +++++++++++++ queries/html/textobjects.scm | 22 ++++ queries/java/textobjects.scm | 50 ++++++++ queries/javascript/textobjects.scm | 81 ++++++++++++ queries/julia/textobjects.scm | 114 +++++++++++++++++ queries/latex/textobjects.scm | 28 +++++ queries/lua/textobjects.scm | 67 ++++++++++ queries/markdown/textobjects.scm | 10 ++ queries/php/textobjects.scm | 30 +++++ queries/python/textobjects.scm | 159 ++++++++++++++++++++++++ queries/ql/textobjects.scm | 13 ++ queries/query/textobjects.scm | 11 ++ queries/rst/textobjects.scm | 32 +++++ queries/ruby/textobjects.scm | 193 +++++++++++++++++++++++++++++ queries/rust/textobjects.scm | 137 ++++++++++++++++++++ queries/scala/textobjects.scm | 13 ++ queries/tsx/textobjects.scm | 1 + queries/typescript/textobjects.scm | 1 + queries/verilog/textobjects.scm | 19 +++ queries/vim/textobjects.scm | 20 +++ queries/vue/textobjects.scm | 6 + queries/zig/textobjects.scm | 55 ++++++++ 34 files changed, 1666 insertions(+) create mode 100644 queries/bash/textobjects.scm create mode 100644 queries/bibtex/textobjects.scm create mode 100644 queries/c/textobjects.scm create mode 100644 queries/c_sharp/textobjects.scm create mode 100644 queries/cpp/textobjects.scm create mode 100644 queries/cuda/textobjects.scm create mode 100644 queries/dart/textobjects.scm create mode 100644 queries/elixir/textobjects.scm create mode 100644 queries/elm/textobjects.scm create mode 100644 queries/fennel/textobjects.scm create mode 100644 queries/fish/textobjects.scm create mode 100644 queries/glsl/textobjects.scm create mode 100644 queries/go/textobjects.scm create mode 100644 queries/html/textobjects.scm create mode 100644 queries/java/textobjects.scm create mode 100644 queries/javascript/textobjects.scm create mode 100644 queries/julia/textobjects.scm create mode 100644 queries/latex/textobjects.scm create mode 100644 queries/lua/textobjects.scm create mode 100644 queries/markdown/textobjects.scm create mode 100644 queries/php/textobjects.scm create mode 100644 queries/python/textobjects.scm create mode 100644 queries/ql/textobjects.scm create mode 100644 queries/query/textobjects.scm create mode 100644 queries/rst/textobjects.scm create mode 100644 queries/ruby/textobjects.scm create mode 100644 queries/rust/textobjects.scm create mode 100644 queries/scala/textobjects.scm create mode 100644 queries/tsx/textobjects.scm create mode 100644 queries/typescript/textobjects.scm create mode 100644 queries/verilog/textobjects.scm create mode 100644 queries/vim/textobjects.scm create mode 100644 queries/vue/textobjects.scm create mode 100644 queries/zig/textobjects.scm diff --git a/queries/bash/textobjects.scm b/queries/bash/textobjects.scm new file mode 100644 index 0000000..160bb18 --- /dev/null +++ b/queries/bash/textobjects.scm @@ -0,0 +1,14 @@ +(function_definition + (_) @function.inner ) @function.outer + +(case_statement) @conditional.outer + +(if_statement + (_) @conditional.inner ) @conditional.outer + +(for_statement + (_) @loop.inner ) @loop.outer +(while_statement + (_) @loop.inner ) @loop.outer + +(comment) @comment.outer diff --git a/queries/bibtex/textobjects.scm b/queries/bibtex/textobjects.scm new file mode 100644 index 0000000..536826f --- /dev/null +++ b/queries/bibtex/textobjects.scm @@ -0,0 +1,3 @@ +(entry) @block.outer + +(field) @statement.outer diff --git a/queries/c/textobjects.scm b/queries/c/textobjects.scm new file mode 100644 index 0000000..23691a5 --- /dev/null +++ b/queries/c/textobjects.scm @@ -0,0 +1,75 @@ +;; TODO: supported by official Tree-sitter if (_)* is more than one node +;; Neovim: will only match if (_) is exactly one node +;(function_definition + ;body: (compound_statement + ;("{" (_)* @function.inner "}"))?) @function.outer + +(declaration + declarator: (function_declarator)) @function.outer + +(function_definition + body: (compound_statement) @function.inner) @function.outer + +(struct_specifier + body: (_) @class.inner) @class.outer + +(enum_specifier + body: (_) @class.inner) @class.outer + +; conditional +(if_statement + consequence: (_)? @conditional.inner + alternative: (_)? @conditional.inner + ) @conditional.outer + +(if_statement + condition: (_) @conditional.inner) + +; loops +(for_statement + (_)? @loop.inner) @loop.outer +(while_statement + (_)? @loop.inner) @loop.outer +(do_statement + (_)? @loop.inner) @loop.outer + + +(compound_statement) @block.outer +(comment) @comment.outer + +(call_expression) @call.outer +(call_expression (_) @call.inner) + +; Statements + +;(expression_statement ;; this is what we actually want to capture in most cases (";" is missing) probably + ;(_) @statement.inner) ;; the other statement like node type is declaration but declaration has a ";" + +(compound_statement + (_) @statement.outer) + +(field_declaration_list + (_) @statement.outer) + +(preproc_if + (_) @statement.outer) + +(preproc_elif + (_) @statement.outer) + +(preproc_else + (_) @statement.outer) + +((parameter_list + "," @_start . (parameter_declaration) @parameter.inner) + (#make-range! "parameter.outer" @_start @parameter.inner)) +((parameter_list + . (parameter_declaration) @parameter.inner . ","? @_end) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +((argument_list + "," @_start . (_) @parameter.inner) + (#make-range! "parameter.outer" @_start @parameter.inner)) +((argument_list + . (_) @parameter.inner . ","? @_end) + (#make-range! "parameter.outer" @parameter.inner @_end)) diff --git a/queries/c_sharp/textobjects.scm b/queries/c_sharp/textobjects.scm new file mode 100644 index 0000000..310f627 --- /dev/null +++ b/queries/c_sharp/textobjects.scm @@ -0,0 +1,59 @@ +(class_declaration + body: (declaration_list) @class.inner) @class.outer + +(struct_declaration + body: (declaration_list) @class.inner) @class.outer + +(method_declaration + body: (block) ? @function.inner) @function.outer + +(lambda_expression + body: (_) @function.inner) @function.outer + +;; loops +(for_statement + body: (_) @loop.inner) @loop.outer + +(for_each_statement + body: (_) @loop.inner) @loop.outer + +(do_statement + (block) @loop.inner) @loop.outer + +(while_statement + (block) @loop.inner) @loop.outer + +;; conditionals +(if_statement + consequence: (_)? @conditional.inner + alternative: (_)? @conditional.inner) @conditional.outer + +(switch_statement + body: (switch_body) @conditional.inner) @conditional.outer + +;; calls +(invocation_expression + (argument_list) @call.inner) @call.outer + +;; blocks +(_ (block) @block.inner) @block.outer + +;; parameters +((parameter_list + "," @_start . (parameter) @parameter.inner) + (#make-range! "parameter.outer" @_start @parameter.inner)) + +((parameter_list + . (parameter) @parameter.inner . ","? @_end) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +((argument_list + "," @_start . (argument) @parameter.inner) + (#make-range! "parameter.outer" @_start @parameter.inner)) + +((argument_list + . (argument) @parameter.inner . ","? @_end) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +;; comments +(comment) @comment.outer diff --git a/queries/cpp/textobjects.scm b/queries/cpp/textobjects.scm new file mode 100644 index 0000000..1b44ff0 --- /dev/null +++ b/queries/cpp/textobjects.scm @@ -0,0 +1,57 @@ +; inherits: c +(class_specifier + body: (_) @class.inner) @class.outer + +(field_declaration + type: (enum_specifier) + default_value: (initializer_list) @class.inner) @class.outer + +(for_range_loop + (_)? @loop.inner) @loop.outer + +(template_declaration + (function_definition) @function.outer) @function.outer.start + +(template_declaration + (struct_specifier) @class.outer) @class.outer.start + +(template_declaration + (class_specifier) @class.outer) @class.outer.start + +((lambda_capture_specifier + "," @_start . (_) @parameter.inner) + (#make-range! "parameter.outer" @_start @parameter.inner)) +((lambda_capture_specifier + . (_) @parameter.inner . ","? @_end) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +((template_argument_list + "," @_start . (_) @parameter.inner) + (#make-range! "parameter.outer" @_start @parameter.inner)) +((template_argument_list + . (_) @parameter.inner . ","? @_end) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +((template_parameter_list + "," @_start . (_) @parameter.inner) + (#make-range! "parameter.outer" @_start @parameter.inner)) +((template_parameter_list + . (_) @parameter.inner . ","? @_end) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +((parameter_list + "," @_start . (optional_parameter_declaration) @parameter.inner) + (#make-range! "parameter.outer" @_start @parameter.inner)) +((parameter_list + . (optional_parameter_declaration) @parameter.inner . ","? @_end) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +((initializer_list + "," @_start . (_) @parameter.inner @_end) + (#make-range! "parameter.outer" @_start @parameter.inner)) +((initializer_list + . (_) @parameter.inner . ","? @_end) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +(new_expression + (argument_list) @call.inner) @call.outer diff --git a/queries/cuda/textobjects.scm b/queries/cuda/textobjects.scm new file mode 100644 index 0000000..b617fdc --- /dev/null +++ b/queries/cuda/textobjects.scm @@ -0,0 +1 @@ +; inherits: cpp diff --git a/queries/dart/textobjects.scm b/queries/dart/textobjects.scm new file mode 100644 index 0000000..31b5e85 --- /dev/null +++ b/queries/dart/textobjects.scm @@ -0,0 +1,104 @@ +; class +(( + [(marker_annotation)? (annotation)?] @class.outer.start . + (class_definition + body: (class_body) @_end @class.inner) @_start + ) + (#make-range! "class.outer" @_start @_end)) +(mixin_declaration (class_body) @class.inner) @class.outer +(enum_declaration + body: (enum_body) @class.inner) @class.outer +(extension_declaration + body: (extension_body) @class.inner) @class.outer + +; function/method +(( + [(marker_annotation)? (annotation)?] @function.outer.start . + [(method_signature) (function_signature)] @_start . + (function_body) @_end @function.inner + ) + (#make-range! "function.outer" @_start @_end)) +(type_alias (function_type)? @function.inner) @function.outer + +; parameter +[ + (formal_parameter) + (normal_parameter_type) + (type_parameter) +] @parameter.inner +( +"," @_start . [ + (formal_parameter) + (normal_parameter_type) + (type_parameter) + ] @_par + (#make-range! "parameter.outer" @_start @_par)) +( + [ + (formal_parameter) + (normal_parameter_type) + (type_parameter) + ] @_par . "," @_end + (#make-range! "parameter.outer" @_par @_end)) + +;; TODO: (_)* not supported yet -> for now this works correctly only with simple arguments +((arguments + . (_) @parameter.inner . ","? @_end) + (#make-range! "parameter.outer" @parameter.inner @_end)) +((arguments + "," @_start . (_) @parameter.inner) + (#make-range! "parameter.outer" @_start @parameter.inner)) + +; call +(expression_statement + (selector) @call.inner) @call.outer + +; block +(block) @block.outer + +; conditional +(if_statement + [ + condition: (_) + consequence: (_) + alternative: (_)? + ] @conditional.inner) @conditional.outer +(switch_statement + body: (switch_block) @conditional.inner) @conditional.outer +(conditional_expression + [ + consequence: (_) + alternative: (_) + ] @conditional.inner) @conditional.outer + +; loop +(for_statement + body: (block) @loop.inner) @loop.outer +(while_statement + body: (block) @loop.inner) @loop.outer +(do_statement + body: (block) @loop.inner) @loop.outer + +; comment +[ + (comment) + (documentation_comment) +] @comment.outer + +; statement +[ + (break_statement) + (do_statement) + (expression_statement) + (for_statement) + (if_statement) + (return_statement) + (switch_statement) + (while_statement) + (assert_statement) + (labeled_statement) + (yield_statement) + (yield_each_statement) + (continue_statement) + (try_statement) +] @statement.outer diff --git a/queries/elixir/textobjects.scm b/queries/elixir/textobjects.scm new file mode 100644 index 0000000..28af54b --- /dev/null +++ b/queries/elixir/textobjects.scm @@ -0,0 +1,55 @@ +; Block Objects +([ + (do_block "do" . (_) @_do (_) @_end . "end") + (do_block "do" . ((_) @_do) @_end . "end") +] (#make-range! "block.inner" @_do @_end)) @block.outer + +; Class Objects (Modules, Protocols) +(call + target: ((identifier) @_identifier (#any-of? @_identifier + "defmodule" + "defprotocol" + "defimpl" + )) + (arguments (alias)) + [ + (do_block "do" . (_) @_do (_) @_end . "end") + (do_block "do" . ((_) @_do) @_end . "end") + ] + (#make-range! "class.inner" @_do @_end) +) @class.outer + +; Function, Parameter, and Call Objects +(call + target: ((identifier) @_identifier (#any-of? @_identifier + "def" + "defmacro" + "defmacrop" + "defn" + "defnp" + "defp" + )) + (arguments (call [ + (arguments (_) @parameter.inner . "," @_delimiter) + (arguments ((_) @parameter.inner) @_delimiter .) + ] (#make-range! "parameter.outer" @parameter.inner @_delimiter))) + [ + (do_block "do" . (_) @_do (_) @_end . "end") + (do_block "do" . ((_) @_do) @_end . "end") + ] + (#make-range! "function.inner" @_do @_end) +) @function.outer + +; Comment Objects +(comment) @comment.outer + +; Documentation Objects +(unary_operator + operator: "@" + operand: (call target: ((identifier) @_identifier (#any-of? @_identifier + "moduledoc" + "typedoc" + "shortdoc" + "doc" + ))) +) @comment.outer diff --git a/queries/elm/textobjects.scm b/queries/elm/textobjects.scm new file mode 100644 index 0000000..9609f67 --- /dev/null +++ b/queries/elm/textobjects.scm @@ -0,0 +1,84 @@ +;; Functions +; top level function with type annotation and doc comment +( + (module_declaration) + (block_comment) @function.outer.start + . + (type_annotation) + . + (value_declaration + body: (_)? @function.inner) @function.outer +) + +; top level function with type annotation +( + (module_declaration) + (type_annotation) @function.outer.start + . + (value_declaration + body: (_)? @function.inner) @function.outer +) + +; top level function without type annotation +( + (module_declaration) + (value_declaration + body: (_)? @function.inner) @function.outer +) + +;; Comments +[ + (block_comment) + (line_comment) +] @comment.outer + +; Conditionals +(if_else_expr + exprList: (_) + exprList: (_) @conditional.inner) @conditional.outer + +(case_of_expr + branch: (case_of_branch) @conditional.inner) @conditional.outer + +;; Parameters +; type annotations +((type_expression + (arrow) @_start . + (type_ref) @parameter.inner + ) + (#make-range! "parameter.outer" @_start @parameter.inner)) + +((type_expression + . + (type_ref) @parameter.inner + . (arrow)? @_end + ) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +; list items +((list_expr + "," @_start . + exprList: (_) @parameter.inner + ) + (#make-range! "parameter.outer" @_start @parameter.inner)) + +((list_expr + . + exprList: (_) @parameter.inner + . ","? @_end + ) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +; tuple items +((tuple_expr + "," @_start . + expr: (_) @parameter.inner + ) + (#make-range! "parameter.outer" @_start @parameter.inner)) + +((tuple_expr + . + expr: (_) @parameter.inner + . ","? @_end + ) + (#make-range! "parameter.outer" @parameter.inner @_end)) diff --git a/queries/fennel/textobjects.scm b/queries/fennel/textobjects.scm new file mode 100644 index 0000000..3e12681 --- /dev/null +++ b/queries/fennel/textobjects.scm @@ -0,0 +1,52 @@ + +; https://fennel-lang.org/reference + +(comment) @comment.outer + +(_ . "(" ")" .) @statement.outer + +; functions +((fn . name: (_)? . (parameters) . docstring: (_)? . (_) @_start . (_)* . (_)? @_end .) + (#make-range! "function.inner" @_start @_end)) @function.outer + +((lambda . name: (_)? . (parameters) . docstring: (_)? . (_) @_start . (_)* . (_)? @_end .) + (#make-range! "function.inner" @_start @_end)) @function.outer + +(hashfn ["#" "hashfn"] @function.outer.start (_) @function.inner) @function.outer + +; parameters +(parameters (_) @parameter.inner) +(parameters (_) @parameter.outer) + +; call +((list . [(multi_symbol) (symbol)] @call.inner) @call.outer + (#not-any-of? @call.inner "if" "do" "while" "for" "let" "when")) + + +; conditionals +((list . ((symbol) @_if (#any-of? @_if "if" "when")) . (_) . + (_) @_start . + (_)* . + (_)? @_end .) + (#make-range! "conditional.inner" @_start @_end)) @conditional.outer + + +; loops +((for . (for_clause) . + (_) @_start . + (_)* . + (_)? @_end .) + (#make-range! "loop.inner" @_start @_end)) @loop.outer + +((each . (each_clause) . + (_) @_start . + (_)* . + (_)? @_end .) + (#make-range! "loop.inner" @_start @_end)) @loop.outer + +((list . ((symbol) @_while (#eq? @_while "while")) . (_) . + (_) @_start . + (_)* . + (_)? @_end .) + (#make-range! "loop.inner" @_start @_end)) @loop.outer + diff --git a/queries/fish/textobjects.scm b/queries/fish/textobjects.scm new file mode 100644 index 0000000..1c0bf3f --- /dev/null +++ b/queries/fish/textobjects.scm @@ -0,0 +1,16 @@ +(function_definition) @function.outer + +(for_statement + (_) @loop.inner) @loop.outer + +(while_statement + condition: (command) + (command) @loop.inner) @loop.outer + +(if_statement + (command) @conditional.inner) @conditional.outer + +(switch_statement + (_) @conditional.inner) @conditional.outer + +(comment) @comment.outer diff --git a/queries/glsl/textobjects.scm b/queries/glsl/textobjects.scm new file mode 100644 index 0000000..a5a5208 --- /dev/null +++ b/queries/glsl/textobjects.scm @@ -0,0 +1 @@ +; inherits: c diff --git a/queries/go/textobjects.scm b/queries/go/textobjects.scm new file mode 100644 index 0000000..ad86fa1 --- /dev/null +++ b/queries/go/textobjects.scm @@ -0,0 +1,83 @@ +;; function textobject +(function_declaration + body: (block)? @function.inner) @function.outer + +;; function literals +(func_literal + (_)? @function.inner) @function.outer + +;; method as function textobject +(method_declaration + body: (block)? @function.inner) @function.outer + +;; struct and interface declaration as class textobject? +(type_declaration + (type_spec (type_identifier) (struct_type (field_declaration_list (_)?) @class.inner))) @class.outer + +(type_declaration + (type_spec (type_identifier) (interface_type (method_spec_list (_)?) @class.inner))) @class.outer + +;; struct literals as class textobject +(composite_literal + (type_identifier)? + (struct_type (_))? + (literal_value (_)) @class.inner) @class.outer + +;; conditionals +(if_statement + alternative: (_ (_) @conditional.inner)?) @conditional.outer + +(if_statement + consequence: (block)? @conditional.inner) + +(if_statement + condition: (_) @conditional.inner) + +;; loops +(for_statement + body: (block)? @loop.inner) @loop.outer + +;; blocks +(_ (block) @block.inner) @block.outer + +;; statements +(block (_) @statement.outer) + +;; comments +(comment) @comment.outer + +;; calls +(call_expression (_)? @call.inner) @call.outer + +;; parameters +(parameter_list + "," @_start . + (parameter_declaration) @parameter.inner + (#make-range! "parameter.outer" @_start @parameter.inner)) +(parameter_list + . (parameter_declaration) @parameter.inner + . ","? @_end + (#make-range! "parameter.outer" @parameter.inner @_end)) + +(parameter_declaration + (identifier) + (identifier) @parameter.inner) + +(parameter_declaration + (identifier) @parameter.inner + (identifier)) + +(parameter_list + "," @_start . + (variadic_parameter_declaration) @parameter.inner + (#make-range! "parameter.outer" @_start @parameter.inner)) + +;; arguments +(argument_list + "," @_start . + (_) @parameter.inner + (#make-range! "parameter.outer" @_start @parameter.inner)) +(argument_list + . (_) @parameter.inner + . ","? @_end + (#make-range! "parameter.outer" @parameter.inner @_end)) diff --git a/queries/html/textobjects.scm b/queries/html/textobjects.scm new file mode 100644 index 0000000..0e6b00b --- /dev/null +++ b/queries/html/textobjects.scm @@ -0,0 +1,22 @@ +(element) @function.outer + +(element (start_tag) . (_) @function.inner . (end_tag)) + +((element (start_tag) . (_) @_start (_) @_end . (end_tag)) + (#make-range! "function.inner" @_start @_end)) + +(script_element) @function.outer +(script_element (start_tag) . (_) @function.inner . (end_tag)) + +(style_element) @function.outer +(style_element (start_tag) . (_) @function.inner . (end_tag)) + +((element (start_tag (tag_name) @_tag)) @class.outer + (#match? @_tag "^(html|section|h[0-9]|header|title|head|body)$")) + +((element (start_tag (tag_name) @_tag) . (_) @class.inner . (end_tag)) + (#match? @_tag "^(html|section|h[0-9]|header|title|head|body)$")) + +((element (start_tag (tag_name) @_tag) . (_) @_start (_) @_end . (end_tag)) + (#match? @_tag "^(html|section|h[0-9]|header|title|head|body)$") + (#make-range! "class.inner" @_start @_end)) diff --git a/queries/java/textobjects.scm b/queries/java/textobjects.scm new file mode 100644 index 0000000..bcda707 --- /dev/null +++ b/queries/java/textobjects.scm @@ -0,0 +1,50 @@ +(class_declaration + body: (class_body) @class.inner) @class.outer + +(method_declaration + body: (_) @function.inner) @function.outer + +(constructor_declaration) @function.outer +(constructor_body) @function.inner + +(for_statement + body: (_)? @loop.inner) @loop.outer + +(enhanced_for_statement + body: (_)? @loop.inner) @loop.outer + +(while_statement + body: (_)? @loop.inner) @loop.outer + +(do_statement + body: (_)? @loop.inner) @loop.outer + +(if_statement + condition: (_ (parenthesized_expression) @conditional.inner) @conditional.outer) + +(if_statement + consequence: (_)? @conditional.inner + alternative: (_)? @conditional.inner + ) @conditional.outer + +(switch_expression + body: (_)? @conditional.inner) @conditional.outer + +;; blocks +(block) @block.outer + + +(method_invocation) @call.outer +(method_invocation (argument_list) @call.inner) + +;; parameters +(formal_parameters + "," @_start . + (formal_parameter) @parameter.inner + (#make-range! "parameter.outer" @_start @parameter.inner)) +(formal_parameters + . (formal_parameter) @parameter.inner + . ","? @_end + (#make-range! "parameter.outer" @_start @parameter.inner)) + +(comment) @comment.outer diff --git a/queries/javascript/textobjects.scm b/queries/javascript/textobjects.scm new file mode 100644 index 0000000..aff02a9 --- /dev/null +++ b/queries/javascript/textobjects.scm @@ -0,0 +1,81 @@ +; inherits: (jsx) +(function_declaration + body: (statement_block) @function.inner) @function.outer + +(function + body: (statement_block) @function.inner) @function.outer + +(export_statement + (function_declaration) @function.outer) @function.outer.start + +(arrow_function + body: (_) @function.inner) @function.outer + +(method_definition + body: (statement_block) @function.inner) @function.outer + +(class_declaration + body: (class_body) @class.inner) @class.outer + +(export_statement + (class_declaration) @class.outer) @class.outer.start + +(for_in_statement + body: (_)? @loop.inner) @loop.outer + +(while_statement + body: (_)? @loop.inner) @loop.outer + +(do_statement + body: (_)? @loop.inner) @loop.outer + +(if_statement + consequence: (_)? @conditional.inner + alternative: (_)? @conditional.inner) @conditional.outer + +(switch_statement + body: (_)? @conditional.inner) @conditional.outer + +(call_expression) @call.outer +(call_expression (arguments) @call.inner) + +;; blocks +(_ (statement_block) @block.inner) @block.outer + +;; parameters +; function ({ x }) ... +; function ([ x ]) ... +; function (v = default_value) +(formal_parameters + "," @_start . + (_) @parameter.inner + (#make-range! "parameter.outer" @_start @parameter.inner)) +(formal_parameters + . (_) @parameter.inner + . ","? @_end + (#make-range! "parameter.outer" @parameter.inner @_end)) + +; If the array/object pattern is the first parameter, treat its elements as the argument list +(formal_parameters + . (_ + [(object_pattern "," @_start . (_) @parameter.inner) + (array_pattern "," @_start . (_) @parameter.inner)] + ) + (#make-range! "parameter.outer" @_start @parameter.inner)) +(formal_parameters + . (_ + [(object_pattern . (_) @parameter.inner . ","? @_end) + (array_pattern . (_) @parameter.inner . ","? @_end)] + ) + (#make-range! "parameter.outer" @parameter.inner @_end)) + + +;; arguments +(arguments + "," @_start . + (_) @parameter.inner + (#make-range! "parameter.outer" @_start @parameter.inner)) +(arguments + . (_) @parameter.inner + . ","? @_end + (#make-range! "parameter.outer" @parameter.inner @_end)) diff --git a/queries/julia/textobjects.scm b/queries/julia/textobjects.scm new file mode 100644 index 0000000..1d822f5 --- /dev/null +++ b/queries/julia/textobjects.scm @@ -0,0 +1,114 @@ +; Blocks +((compound_expression + . (_)? @_start + (_) @_end .) +(#make-range! "block.inner" @_start @_end)) @block.outer +((let_statement + . (_)? @_start + (_) @_end .) +(#make-range! "block.inner" @_start @_end)) @block.outer + +; Calls +(call_expression + (argument_list) @call.inner) @call.outer + +; Objects (class) +((struct_definition + name: (_) + . (_)? @_start + (_) @_end . + "end" +)(#make-range! "class.inner" @_start @_end)) @class.outer + +((struct_definition + name: (_) type_parameters: (_) + . (_)? @_start + (_) @_end . + "end" +)(#make-range! "class.inner" @_start @_end)) @class.outer + +; Comments +[(comment) (triple_string)]@comment.outer + +; Conditionals +((if_statement condition: (_) + . (_)? @_start + . + (_) @_end . + ["end" (elseif_clause) (else_clause)]) +(#make-range! "conditional.inner" @_start @_end)) @conditional.outer +((elseif_clause condition: (_) + . (_)? @_start + (_) @_end .) +(#make-range! "conditional.inner" @_start @_end)) +((else_clause + . (_)? @_start + (_) @_end .) +(#make-range! "conditional.inner" @_start @_end)) + +; Functions +(assignment_expression + (call_expression (_)) + (_) @function.inner) @function.outer + +(function_expression + [ (identifier) (parameter_list) ] + "->" + (_) @function.inner) @function.outer + +((macro_definition + name: (_) parameters: (_) + . (_)? @_start + (_) @_end . + "end" +)(#make-range! "function.inner" @_start @_end)) @function.outer + +((function_definition + name: (_) parameters: (_) + . (_)? @_start + (_) @_end . + "end" +)(#make-range! "function.inner" @_start @_end)) @function.outer + +; Loops +(for_statement + . (_)? @_start + (_) @_end . + (#make-range! "loop.inner" @_start @_end) + "end") @loop.outer +(while_statement + . (_)? @_start + (_) @_end . + (#make-range! "loop.inner" @_start @_end) + "end") @loop.outer + +; Parameters +((subscript_expression + "," @_start . + (_) @parameter.inner) + (#make-range! "parameter.outer" @_start @parameter.inner)) + +((subscript_expression + . (_) @parameter.inner + . ","? @_end) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +((argument_list + "," @_start . + (_) @parameter.inner) +(#make-range! "parameter.outer" @_start @parameter.inner)) + +((argument_list + (_) @parameter.inner + . "," @_end) +(#make-range! "parameter.outer" @parameter.inner @_end)) + +((parameter_list + "," @_start . + (_) @parameter.inner) +(#make-range! "parameter.outer" @_start @parameter.inner)) + +((parameter_list + (_) @parameter.inner + . [","] @_end) +(#make-range! "parameter.outer" @parameter.inner @_end)) diff --git a/queries/latex/textobjects.scm b/queries/latex/textobjects.scm new file mode 100644 index 0000000..886def0 --- /dev/null +++ b/queries/latex/textobjects.scm @@ -0,0 +1,28 @@ +((environment + . (_) + . (_) @_start + (_) @_end . (_) . +) @block.outer +(#make-range! "block.inner" @_start @_end)) + +((environment + (begin + name: (word) @_frame) + . (_) @_start + (_) @_end . (_) .) @frame.outer + (#eq? @_frame "frame") + (#make-range! "frame.inner" @_start @_end)) + +[ + (generic_command) +] @statement.outer + +[ + (chapter) + (part) + (section) + (subsection) + (subsubsection) + (paragraph) + (subparagraph) +] @class.outer diff --git a/queries/lua/textobjects.scm b/queries/lua/textobjects.scm new file mode 100644 index 0000000..ca08bd0 --- /dev/null +++ b/queries/lua/textobjects.scm @@ -0,0 +1,67 @@ +(function) @function.outer +(local_function) @function.outer +(function_definition) @function.outer + +(for_in_statement) @loop.outer +(for_statement) @loop.outer +(while_statement) @loop.outer +(repeat_statement) @loop.outer + +(if_statement) @conditional.outer + +(function_call + (arguments) @call.inner) +(function_call) @call.outer + +(arguments + "," @_start . + (_) @parameter.inner + (#make-range! "parameter.outer" @_start @parameter.inner)) +(arguments + . (_) @parameter.inner + . ","? @_end + (#make-range! "parameter.outer" @parameter.inner @_end)) + +(parameters + "," @_start . + (_) @parameter.inner + (#make-range! "parameter.outer" @_start @parameter.inner)) +(parameters + . (_) @parameter.inner + . ","? @_end + (#make-range! "parameter.outer" @parameter.inner @_end)) + +(arguments + . (table + "," @_start . + (_) @parameter.inner + (#make-range! "parameter.outer" @_start @parameter.inner)) + . ) +(arguments + . (table + . (_) @parameter.inner + . ","? @_end + (#make-range! "parameter.outer" @parameter.inner @_end)) + . ) + +(comment) @comment.outer + +((function + . (function_name) . (parameters) . (_) @_start + (_) @_end .) + (#make-range! "function.inner" @_start @_end)) +((local_function + . (identifier) . (parameters) . (_) @_start + (_) @_end .) + (#make-range! "function.inner" @_start @_end)) +((function_definition + . (parameters) . (_) @_start + (_) @_end .) + (#make-range! "function.inner" @_start @_end)) + +((function + . (function_name) . (parameters) . (_) @function.inner .)) +((local_function + . (identifier) . (parameters) . (_) @function.inner .)) +((function_definition + . (parameters) . (_) @function.inner .)) diff --git a/queries/markdown/textobjects.scm b/queries/markdown/textobjects.scm new file mode 100644 index 0000000..aa6a416 --- /dev/null +++ b/queries/markdown/textobjects.scm @@ -0,0 +1,10 @@ +(atx_heading (heading_content) @class.inner) @class.outer +(setext_heading (heading_content) @class.inner) @class.outer +(thematic_break) @class.outer + +(fenced_code_block (code_fence_content) @block.inner) @block.outer + +[ + (paragraph) + (list) +] @block.outer diff --git a/queries/php/textobjects.scm b/queries/php/textobjects.scm new file mode 100644 index 0000000..a14034d --- /dev/null +++ b/queries/php/textobjects.scm @@ -0,0 +1,30 @@ +(function_definition + body: (compound_statement) @function.inner) @function.outer + +(method_declaration + body: (compound_statement) @function.inner) @function.outer + +(class_declaration + body: (declaration_list) @class.inner) @class.outer + +(foreach_statement + body: (_)? @loop.inner) @loop.outer + +(while_statement + body: (_)? @loop.inner) @loop.outer + +(do_statement + body: (_)? @loop.inner) @loop.outer + +(switch_statement + body: (_)? @conditional.inner) @conditional.outer + +;;blocks +(_ (switch_block) @block.inner) @block.outer + +;; parameters +(formal_parameters + (simple_parameter) @parameter.inner) + +(arguments + (argument) @parameter.inner) diff --git a/queries/python/textobjects.scm b/queries/python/textobjects.scm new file mode 100644 index 0000000..8aaaca4 --- /dev/null +++ b/queries/python/textobjects.scm @@ -0,0 +1,159 @@ +(function_definition + body: (block)? @function.inner) @function.outer + +(decorated_definition + (function_definition) @function.outer) @function.outer.start + +(class_definition + body: (block)? @class.inner) @class.outer + +(decorated_definition + (class_definition) @class.outer) @class.outer.start + +(while_statement + body: (block)? @loop.inner) @loop.outer + +(for_statement + body: (block)? @loop.inner) @loop.outer + +(if_statement + alternative: (_ (_) @conditional.inner)?) @conditional.outer + +(if_statement + consequence: (block)? @conditional.inner) + +(if_statement + condition: (_) @conditional.inner) + +(_ (block) @block.inner) @block.outer +(comment) @comment.outer + +(block (_) @statement.outer) + +(call) @call.outer +(call (_) @call.inner) + +;; Parameters + +((parameters + "," @_start . + [ + (identifier) + (tuple) + (typed_parameter) + (default_parameter) + (typed_default_parameter) + (dictionary_splat_pattern) + (list_splat_pattern) + ] @parameter.inner + ) + (#make-range! "parameter.outer" @_start @parameter.inner)) + +((parameters + . [ + (identifier) + (tuple) + (typed_parameter) + (default_parameter) + (typed_default_parameter) + (dictionary_splat_pattern) + (list_splat_pattern) + ] @parameter.inner + . ","? @_end + ) + (#make-range! "parameter.outer" @parameter.inner @_end) +) + +((lambda_parameters + "," @_start . + [ + (identifier) + (tuple) + (typed_parameter) + (default_parameter) + (typed_default_parameter) + (dictionary_splat_pattern) + (list_splat_pattern) + ] @parameter.inner + ) + (#make-range! "parameter.outer" @_start @parameter.inner)) + +((lambda_parameters + . [ + (identifier) + (tuple) + (typed_parameter) + (default_parameter) + (typed_default_parameter) + (dictionary_splat_pattern) + (list_splat_pattern) + ] @parameter.inner + . ","? @_end + ) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +((tuple + "," @_start . + (_) @parameter.inner + ) + (#make-range! "parameter.outer" @_start @parameter.inner) +) + +((tuple + "(" . + (_) @parameter.inner + . ","? @_end + ) + (#make-range! "parameter.outer" @parameter.inner @_end) +) + +((list + "," @_start . + (_) @parameter.inner + ) + (#make-range! "parameter.outer" @_start @parameter.inner) +) + +((list + . (_) @parameter.inner + . ","? @_end + ) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +((dictionary + . (pair) @parameter.inner + . ","? @_end + ) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +((dictionary + "," @_start . + (pair) @parameter.inner + ) + (#make-range! "parameter.outer" @_start @parameter.inner)) + +((argument_list + . (_) @parameter.inner + . ","? @_end + ) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +((argument_list + "," @_start . + (_) @parameter.inner + ) + (#make-range! "parameter.outer" @_start @parameter.inner)) + +((subscript + "[" . (_) @parameter.inner + . ","? @_end + ) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +((subscript + "," @_start . + (_) @parameter.inner + ) + (#make-range! "parameter.outer" @_start @parameter.inner)) + +; TODO: exclude comments using the future negate syntax from tree-sitter diff --git a/queries/ql/textobjects.scm b/queries/ql/textobjects.scm new file mode 100644 index 0000000..3e8a64d --- /dev/null +++ b/queries/ql/textobjects.scm @@ -0,0 +1,13 @@ +;; class textobject +(dataclass (typeExpr) (_) @class.inner) @class.outer + +;; function textobject +(charpred (className) (_) @function.inner) @function.outer +(memberPredicate (body) @function.inner) @function.outer +(classlessPredicate (body) @function.inner) @function.outer + +;; scope name textobject +(dataclass name: (className) @scopename.inner) +(classlessPredicate name: (predicateName) @scopename.inner) +(memberPredicate name: (predicateName) @scopename.inner) +(charpred (className) @scopename.inner) diff --git a/queries/query/textobjects.scm b/queries/query/textobjects.scm new file mode 100644 index 0000000..faf8c18 --- /dev/null +++ b/queries/query/textobjects.scm @@ -0,0 +1,11 @@ +(comment) @comment.outer + +[ + (named_node) + (grouping) + (list) +] @block.outer + +(program [(named_node) (grouping) (list)] @statement.outer) + +(parameters (_) @parameter.inner) diff --git a/queries/rst/textobjects.scm b/queries/rst/textobjects.scm new file mode 100644 index 0000000..80df9c5 --- /dev/null +++ b/queries/rst/textobjects.scm @@ -0,0 +1,32 @@ +(directive + body: (body) @function.inner) @function.outer + +(section (title) @class.inner) @class.outer + +(transition) @class.outer + +[ + (bullet_list) + (enumerated_list) + (definition_list) + (field_list) + + (literal_block) + (line_block) + (block_quote) + (doctest_block) +] @block.outer + +(footnote + body: (body) @block.inner) @block.outer + +(citation + body: (body) @block.inner) @block.outer + +(target + link: (link) @block.inner) @block.outer + +(substitution_definition + body: (directive) @block.inner) @block.outer + +(comment) @comment.outer diff --git a/queries/ruby/textobjects.scm b/queries/ruby/textobjects.scm new file mode 100644 index 0000000..bd1385d --- /dev/null +++ b/queries/ruby/textobjects.scm @@ -0,0 +1,193 @@ +;; @functions +( + [ + (method + . name: (identifier) + !parameters + . (_) @_start (_) @_end .) + + (method + . name: (identifier) + . parameters: (method_parameters) + . (_) @_start (_) @_end .) + + (singleton_method + . object: (_) + . name: (identifier) + !parameters + . (_) @_start (_) @_end .) + + (singleton_method + . object: (_) + . name: (identifier) + . parameters: (method_parameters) + . (_) @_start (_) @_end .) + ] @function.outer + (#make-range! "function.inner" @_start @_end)) + +[ + (method + . name: (identifier) + !parameters + . (_) @function.inner .) + + (method + . name: (identifier) + . parameters: (method_parameters) + . (_) @function.inner .) + + (singleton_method + . object: (_) + . name: (identifier) + !parameters + . (_) @function.inner .) + + (singleton_method + . object: (_) + . name: (identifier) + . parameters: (method_parameters) + . (_) @function.inner .) +] @function.outer + +;; @blocks +( + [ + (call + block: (_ + !parameters + . (_) @_start (_) @_end .) + ) + + (call + block: (_ + . parameters: (block_parameters) + . (_) @_start (_) @_end .) + ) + ] + (#make-range! "block.inner" @_start @_end)) + +[ + (call + block: (_ + !parameters + . (_) @block.inner .)) + + (call + block: (_ + . parameters: (block_parameters) + . (_) @block.inner .)) +] + +[ + (do_block) + (block) +] @block.outer + +((lambda + body: (_ + . (_) @_start (_) @_end .) + ) @block.outer + (#make-range! "block.inner" @_start @_end)) + +(lambda + body: (_ + . (_) @block.inner .) @block.outer) + +;; calls +(call + method: (_) @call.inner ) @call.outer + +;; classes +( + [ + (class + . name: (_) + . superclass: (_) + . (_) @_start (_) @_end .) + + (class + . name: (_) + !superclass + . (_) @_start (_) @_end .) + + (module + . name: (_) + . (_) @_start (_) @_end .) + + (singleton_class + . value: (_) + . (_) @_start (_) @_end .) + ] @class.outer + (#make-range! "class.inner" @_start @_end)) + +[ + ;; match against classes with and without parrents + (class + . name: (_) + . superclass: (_) + . (_) @class.inner .) + + (class + . name: (_) + !superclass + . (_) @class.inner .) + + (module + . name: (_) + . (_) @class.inner .) + + (singleton_class + . value: (_) + . (_) @class.inner .) +] @class.outer + +;; comments +(comment) @comment.outer + +;; conditionals +[ + (if + consequence: (_) @conditional.inner + alternative: (_) @conditional.inner) + + (if_modifier + condition: (_) @conditional.inner) + + (until_modifier + condition: (_) @conditional.inner) + + (unless + consequence: (_) @conditional.inner + alternative: (_) @conditional.inner) + + (case + value: (_) + (_) @conditional.inner) +] @conditional.outer + +;; loops +[ + (while + body: (_) @loop.inner) + + (while_modifier + condition: (_) @loop.inner) + + (until + body: (_) @loop.inner) + + (until_modifier + condition: (_) @loop.inner) + + (for + body: (_) @loop.inner) +] @loop.outer + +;; parameters +[ + (block_parameters (_) @parameter.inner) + + (method_parameters (_) @parameter.inner) + + (lambda_parameters (_) @parameter.inner) +] @parameter.outer diff --git a/queries/rust/textobjects.scm b/queries/rust/textobjects.scm new file mode 100644 index 0000000..da3c62d --- /dev/null +++ b/queries/rust/textobjects.scm @@ -0,0 +1,137 @@ +;; functions +(function_item + (_) @function.inner) @function.outer + +;; quantifies as class(es) +(struct_item + (_) @class.inner) @class.outer + +(enum_item + (_) @class.inner) @class.outer + +(union_item + (_) @class.inner) @class.outer + +(trait_item + (_) @class.inner) @class.outer + +(impl_item + (_) @class.inner) @class.outer + +(mod_item + (_) @class.inner) @class.outer + +;; conditionals +(if_expression + alternative: (_ (_) @conditional.inner)? + ) @conditional.outer + +(if_expression + alternative: (else_clause (block) @conditional.inner)) + +(if_expression + condition: (_) @conditional.inner) + +(if_expression + consequence: (block) @conditional.inner) + +(match_arm + (_)) @conditional.inner + +(match_expression) @conditional.outer + +(if_let_expression + consequence: (block)? + @conditional.inner) @conditional.outer + +(if_let_expression + alternative: (else_clause (block) @conditional.inner)) + +(if_let_expression + pattern: (_) @conditional.inner) + +;; loops +(loop_expression + (_)? @loop.inner) @loop.outer + +(while_expression + (_)? @loop.inner) @loop.outer + +(while_let_expression + (_)? @loop.inner) @loop.outer + +(for_expression + body: (block)? @loop.inner) @loop.outer + +;; blocks +(_ (block) @block.inner) @block.outer +(unsafe_block (_)? @block.inner) @block.outer + +;; calls +(call_expression (_)? @call.inner) @call.outer + +;; statements +(block (_) @statement.outer) + +;; comments +(line_comment) @comment.outer +(block_comment) @comment.outer + +;; parameter + +((parameters + "," @_start . (parameter) @parameter.inner) + (#make-range! "parameter.outer" @_start @parameter.inner)) +((parameters + . (parameter) @parameter.inner . ","? @_end) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +((type_parameters + "," @_start . (_) @parameter.inner) + (#make-range! "parameter.outer" @_start @parameter.inner)) +((type_parameters + . (_) @parameter.inner . ","? @_end) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +((tuple_pattern + "," @_start . (identifier) @parameter.inner) + (#make-range! "parameter.outer" @_start @parameter.inner)) +((tuple_pattern + . (identifier) @parameter.inner . ","? @_end) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +((tuple_struct_pattern + "," @_start . (identifier) @parameter.inner) + (#make-range! "parameter.outer" @_start @parameter.inner)) +((tuple_struct_pattern + . (identifier) @parameter.inner . ","? @_end) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +((closure_parameters + "," @_start . (_) @parameter.inner) + (#make-range! "parameter.outer" @_start @parameter.inner)) +((closure_parameters + . (_) @parameter.inner . ","? @_end) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +((arguments + "," @_start . (_) @parameter.inner) + (#make-range! "parameter.outer" @_start @parameter.inner)) +((arguments + . (_) @parameter.inner . ","? @_end) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +((type_arguments + "," @_start . (_) @parameter.inner) + (#make-range! "parameter.outer" @_start @parameter.inner)) +((type_arguments + . (_) @parameter.inner . ","? @_end) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +((meta_arguments + "," @_start . (_) @parameter.inner) + (#make-range! "parameter.outer" @_start @parameter.inner)) +((meta_arguments + . (_) @parameter.inner . ","? @_end) + (#make-range! "parameter.outer" @parameter.inner @_end)) + diff --git a/queries/scala/textobjects.scm b/queries/scala/textobjects.scm new file mode 100644 index 0000000..db08856 --- /dev/null +++ b/queries/scala/textobjects.scm @@ -0,0 +1,13 @@ +(class_definition + body: (template_body)? @class.inner) @class.outer + +(object_definition + body: (template_body)? @class.inner) @class.outer + +(function_definition + body: (block) @function.inner) @function.outer + +(parameter + name: (identifier) @parameter.inner) @parameter.outer + +(comment) @comment.outer diff --git a/queries/tsx/textobjects.scm b/queries/tsx/textobjects.scm new file mode 100644 index 0000000..0739123 --- /dev/null +++ b/queries/tsx/textobjects.scm @@ -0,0 +1 @@ +; inherits: typescript,jsx diff --git a/queries/typescript/textobjects.scm b/queries/typescript/textobjects.scm new file mode 100644 index 0000000..ff0ddfa --- /dev/null +++ b/queries/typescript/textobjects.scm @@ -0,0 +1 @@ +; inherits: javascript diff --git a/queries/verilog/textobjects.scm b/queries/verilog/textobjects.scm new file mode 100644 index 0000000..462e319 --- /dev/null +++ b/queries/verilog/textobjects.scm @@ -0,0 +1,19 @@ +[ + (loop_generate_construct) + (loop_statement) +] @loop.outer + +[ + (conditional_statement) + (case_item) +] @conditional.outer + +(comment) @comment.outer + +(function_declaration) @function.outer + +(always_construct) @block.outer + +[ + (module_declaration) +] @class.outer diff --git a/queries/vim/textobjects.scm b/queries/vim/textobjects.scm new file mode 100644 index 0000000..3956809 --- /dev/null +++ b/queries/vim/textobjects.scm @@ -0,0 +1,20 @@ +(comment) @comment.outer + +(function_definition (body) @function.inner) @function.outer + +(parameters (identifier) @parameter.inner) + +((parameters "," @_start . (identifier) @_end) + (#make-range! "parameter.outer" @_start @_end)) + +((parameters . (identifier) @_start . "," @_end) + (#make-range! "parameter.outer" @_start @_end)) + +(if_statement (body) @conditional.inner) @conditional.outer +(for_loop (body) @loop.inner) @loop.outer +(while_loop (body) @loop.inner) @loop.outer + +(call_expression) @call.outer + +(_ (body) @block.inner) @block.outer +(body (_) @statement.outer) diff --git a/queries/vue/textobjects.scm b/queries/vue/textobjects.scm new file mode 100644 index 0000000..68456b7 --- /dev/null +++ b/queries/vue/textobjects.scm @@ -0,0 +1,6 @@ +(element) @function.outer +[ + (attribute) + (directive_attribute) +] @call.outer +(attribute_value) @parameter.outer diff --git a/queries/zig/textobjects.scm b/queries/zig/textobjects.scm new file mode 100644 index 0000000..964bf51 --- /dev/null +++ b/queries/zig/textobjects.scm @@ -0,0 +1,55 @@ +;; "Classes" +(VarDecl + (_ (_ (ContainerDecl) @class.inner))) @class.outer + +;; functions +(_ + (FnProto) + (Block) @function.inner) @function.outer + +;; loops +(_ + (ForPrefix) + (_) @loop.inner) @loop.outer + +(_ + (WhilePrefix) + (_) @loop.inner) @loop.outer + +;; blocks +(_ (Block) @block.inner) @block.outer + +;; statements +(Statement) @statement.outer + +;; parameters +((ParamDeclList + "," @_start . (ParamDecl) @parameter.inner) + (#make-range! "parameter.outer" @_start @parameter.inner)) +((ParamDeclList + . (ParamDecl) @parameter.inner . ","? @_end) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +;; arguments +((FnCallArguments + "," @_start . (_) @parameter.inner) + (#make-range! "parameter.outer" @_start @parameter.inner)) +((FnCallArguments + . (_) @parameter.inner . ","? @_end) + (#make-range! "parameter.outer" @parameter.inner @_end)) + +;; comments +(doc_comment) @comment.outer +(line_comment) @comment.outer + +;; conditionals +(_ + (IfPrefix) + (_) @conditional.inner) @conditional.outer + +((SwitchExpr + "{" @_start "}" @_end) + (#make-range! "conditional.inner" @_start @_end)) @conditional.outer + +;; calls +(_ (FnCallArguments) @call.inner) @call.outer From d041f299e852602201634d8d5a0e901cc0a07694 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Mon, 21 Feb 2022 01:32:00 +0800 Subject: [PATCH 02/21] Update core --- Cask | 1 - queries/{c_sharp => c-sharp}/textobjects.scm | 0 ts-fold-indicators.el | 10 +- ts-fold-parsers.el | 218 ------------ ts-fold-summary.el | 2 +- ts-fold-util.el | 2 +- ts-fold.el | 328 ++++++++++++------- 7 files changed, 225 insertions(+), 336 deletions(-) rename queries/{c_sharp => c-sharp}/textobjects.scm (100%) delete mode 100644 ts-fold-parsers.el diff --git a/Cask b/Cask index 46c78ac..34a1acf 100644 --- a/Cask +++ b/Cask @@ -5,7 +5,6 @@ (files "ts-fold.el" - "ts-fold-parsers.el" "ts-fold-summary.el" "ts-fold-indicators.el" "ts-fold-util.el") diff --git a/queries/c_sharp/textobjects.scm b/queries/c-sharp/textobjects.scm similarity index 100% rename from queries/c_sharp/textobjects.scm rename to queries/c-sharp/textobjects.scm diff --git a/ts-fold-indicators.el b/ts-fold-indicators.el index 27bcbb9..aaa2d7c 100644 --- a/ts-fold-indicators.el +++ b/ts-fold-indicators.el @@ -1,6 +1,6 @@ ;;; ts-fold-indicators.el --- Display indicators for folding range -*- lexical-binding: t; -*- -;; Copyright (C) 2021 Shen, Jen-Chieh +;; Copyright (C) 2021-2022 Shen, Jen-Chieh ;; Created date 2021-10-04 20:03:12 ;; This file is NOT part of GNU Emacs. @@ -93,6 +93,10 @@ "........" "........" "........" "........" "........" "........" "........" "........" "........" "........") +(defun ts-fold--after-command (&rest _) + "Function call after interactive commands." + (ts-fold-indicators-refresh)) + ;; ;; (@* "Entry" ) ;; @@ -108,12 +112,16 @@ "Enable `ts-fold-indicators' mode." (if (ts-fold-mode 1) ; Enable `ts-fold-mode' automatically (progn + (dolist (cmd ts-fold-interactive-commands) + (advice-add cmd :after #'ts-fold--after-command)) (add-hook 'tree-sitter-after-change-functions #'ts-fold-indicators-refresh nil t) (add-hook 'after-save-hook #'ts-fold-indicators-refresh nil t)) (ts-fold-indicators-mode -1))) (defun ts-fold-indicators--disable () "Disable `ts-fold-indicators' mode." + (dolist (cmd ts-fold-interactive-commands) + (advice-remove cmd #'ts-fold--after-command)) (remove-hook 'tree-sitter-after-change-functions #'ts-fold-indicators-refresh t) (remove-hook 'after-save-hook #'ts-fold-indicators-refresh t) (ts-fold-indicators--remove-overlays)) diff --git a/ts-fold-parsers.el b/ts-fold-parsers.el deleted file mode 100644 index bd3c0e7..0000000 --- a/ts-fold-parsers.el +++ /dev/null @@ -1,218 +0,0 @@ -;;; ts-fold-parsers.el --- Adapter layer to Tree-Sitter -*- lexical-binding: t; -*- - -;; Copyright (C) 2021 Shen, Jen-Chieh -;; Created date 2021-10-04 17:45:48 - -;; This file is NOT part of GNU Emacs. - -;; This program is free software; you can redistribute it and/or modify -;; it under the terms of the GNU General Public License as published by -;; the Free Software Foundation, either version 3 of the License, or -;; (at your option) any later version. - -;; This program is distributed in the hope that it will be useful, -;; but WITHOUT ANY WARRANTY; without even the implied warranty of -;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -;; GNU General Public License for more details. - -;; You should have received a copy of the GNU General Public License -;; along with this program. If not, see . - -;;; Commentary: -;; -;; Adapter layer to Tree-Sitter -;; -;; This isn't a real parser implementation, but records down the rule -;; in order to let the Tree-Sitter to parse things correctly. Think of -;; the rule sets! -;; - -;;; Code: - -;; -;; (@* "Externals" ) -;; - -(declare-function ts-fold-range-seq "ts-fold.el") -(declare-function ts-fold-range-line-comment "ts-fold.el") -(declare-function ts-fold-range-block-comment "ts-fold.el") -(declare-function ts-fold-range-c-like-comment "ts-fold.el") - -(declare-function ts-fold-range-c-preproc-ifdef "ts-fold.el") -(declare-function ts-fold-range-c-preproc-if "ts-fold.el") -(declare-function ts-fold-range-c-preproc-elif "ts-fold.el") -(declare-function ts-fold-range-c-preproc-else "ts-fold.el") -(declare-function ts-fold-range-html "ts-fold.el") -(declare-function ts-fold-range-python "ts-fold.el") -(declare-function ts-fold-range-ruby "ts-fold.el") -(declare-function ts-fold-range-rust-macro "ts-fold.el") - -;; -;; (@* "Parsers" ) -;; - -(defun ts-fold-parsers-agda () - "Rule sets for Agda." - '(())) - -(defun ts-fold-parsers-bash () - "Rule sets for Bash." - '((compound_statement . ts-fold-range-seq) - (expansion . ts-fold-range-seq) - (comment - . (lambda (node offset) - (ts-fold-range-line-comment node offset "#"))))) - -(defun ts-fold-parsers-c () - "Rule sets for C." - '((compound_statement . ts-fold-range-seq) - (declaration_list . ts-fold-range-seq) - (enumerator_list . ts-fold-range-seq) - (field_declaration_list . ts-fold-range-seq) - (preproc_if . ts-fold-range-c-preproc-if) - (preproc_ifdef . ts-fold-range-c-preproc-ifdef) - (preproc_elif . ts-fold-range-c-preproc-elif) - (preproc_else . ts-fold-range-c-preproc-else) - (comment . ts-fold-range-c-like-comment))) - -(defun ts-fold-parsers-c++ () - "Rule sets for C++." - (append (ts-fold-parsers-c))) - -(defun ts-fold-parsers-csharp () - "Rule sets for C#." - '((block . ts-fold-range-seq) - (accessor_list . ts-fold-range-seq) - (enum_member_declaration_list . ts-fold-range-seq) - (declaration_list . ts-fold-range-seq) - (switch_body . ts-fold-range-seq) - (anonymous_object_creation_expression . ts-fold-range-seq) - (initializer_expression . ts-fold-range-seq) - ;;(if_directive . ts-fold-range-seq) - ;;(else_directive . ts-fold-range-seq) - ;;(elif_directive . ts-fold-range-seq) - ;;(endif_directive . ts-fold-range-seq) - ;;(region_directive . ts-fold-range-seq) - ;;(endregion_directive . ts-fold-range-seq) - (comment . ts-fold-range-c-like-comment))) - -(defun ts-fold-parsers-css () - "Rule sets for CSS." - '((keyframe_block_list . ts-fold-range-seq) - (block . ts-fold-range-seq) - (comment . ts-fold-range-c-like-comment))) - -(defun ts-fold-parsers-go () - "Rule sets for Go." - '((block . ts-fold-range-seq) - (comment . ts-fold-range-seq))) - -(defun ts-fold-parsers-html () - "Rule sets for HTML." - '((element . ts-fold-range-html) - (comment . (ts-fold-range-seq 1 -1)))) - -(defun ts-fold-parsers-java () - "Rule sets for Java." - '((switch_block . ts-fold-range-seq) - (block . ts-fold-range-seq) - (element_value_array_initializer . ts-fold-range-seq) - (module_body . ts-fold-range-seq) - (enum_body . ts-fold-range-seq) - (class_body . ts-fold-range-seq) - (constructor_body . ts-fold-range-seq) - (annotation_type_body . ts-fold-range-seq) - (interface_body . ts-fold-range-seq) - (array_initializer . ts-fold-range-seq) - (comment . (ts-fold-range-seq 1 -1)))) - -(defun ts-fold-parsers-javascript () - "Rule sets for JavaScript." - '((export_clause . ts-fold-range-seq) - (statement_block . ts-fold-range-seq) - (comment . ts-fold-range-c-like-comment))) - -(defun ts-fold-parsers-json () - "Rule sets for JSON." - '((object . ts-fold-range-seq) - (array . ts-fold-range-seq))) - -(defun ts-fold-parsers-nix () - "Rule sets for Nix." - '((attrset . ts-fold-range-seq) - (interpolation . ts-fold-range-seq) - (list . ts-fold-range-seq))) - -(defun ts-fold-parsers-php () - "Rule sets for PHP." - '((namespace_use_group . ts-fold-range-seq) - (declaration_list . ts-fold-range-seq) - (use_list . ts-fold-range-seq) - (switch_block . ts-fold-range-seq) - (compound_statement . ts-fold-range-seq) - (comment - . (lambda (node offset) - (if (string-prefix-p "#" (tsc-node-text node)) - (ts-fold-range-line-comment node offset "#") - (ts-fold-range-c-like-comment node offset)))))) - -(defun ts-fold-parsers-python () - "Rule sets for Python." - '((function_definition . ts-fold-range-python) - (class_definition . ts-fold-range-python) - (list . ts-fold-range-seq) - (comment - . (lambda (node offset) - (ts-fold-range-line-comment node offset "#"))))) - -(defun ts-fold-parsers-r () - "Rule sets for R." - '((brace_list . ts-fold-range-seq))) - -(defun ts-fold-parsers-ruby () - "Rule sets for Ruby." - '((class . ts-fold-range-ruby) - (method . ts-fold-range-ruby) - (array . ts-fold-range-seq) - (comment - . (lambda (node offset) - (ts-fold-range-line-comment node offset "#"))))) - -(defun ts-fold-parsers-rust () - "Rule sets for Rust." - '((declaration_list . ts-fold-range-seq) - (enum_variant_list . ts-fold-range-seq) - (field_declaration_list . ts-fold-range-seq) - (use_list . ts-fold-range-seq) - (field_initializer_list . ts-fold-range-seq) - (match_block . ts-fold-range-seq) - (macro_definition . (ts-fold-range-rust-macro 1 -1)) - (block . ts-fold-range-seq) - (line_comment . (lambda (node offset) - (ts-fold-range-line-comment node offset "///"))) - (block_comment . ts-fold-range-block-comment))) - -(defun ts-fold-parsers-scala () - "Rule sets for Scala." - '((import_selectors . ts-fold-range-seq) - (template_body . ts-fold-range-seq) - (block . ts-fold-range-seq) - (comment . ts-fold-range-c-like-comment))) - -(defun ts-fold-parsers-swift () - "Rule sets for Swift." - '((switch_statement . ts-fold-range-seq) - (function_declaration . ts-fold-range-seq) - (enum_declaration . ts-fold-range-seq) - (struct_declaration . ts-fold-range-seq) - (class_declaration . ts-fold-range-seq) - (protocol_declaration . ts-fold-range-seq) - (extension_declaration . ts-fold-range-seq) - (comment . ts-fold-range-c-like-comment))) - -(defun ts-fold-parsers-typescript () - "Rule sets for TypeScript." - (append (ts-fold-parsers-javascript))) - -(provide 'ts-fold-parsers) -;;; ts-fold-parsers.el ends here diff --git a/ts-fold-summary.el b/ts-fold-summary.el index 628d4c9..9fd179b 100644 --- a/ts-fold-summary.el +++ b/ts-fold-summary.el @@ -1,6 +1,6 @@ ;;; ts-fold-summary.el --- Extract summary from fold region -*- lexical-binding: t; -*- -;; Copyright (C) 2021 Shen, Jen-Chieh +;; Copyright (C) 2021-2022 Shen, Jen-Chieh ;; Created date 2021-10-04 16:59:22 ;; This file is NOT part of GNU Emacs. diff --git a/ts-fold-util.el b/ts-fold-util.el index a05b2dc..6b2ee0e 100644 --- a/ts-fold-util.el +++ b/ts-fold-util.el @@ -1,6 +1,6 @@ ;;; ts-fold-util.el --- Utility module -*- lexical-binding: t; -*- -;; Copyright (C) 2021 Shen, Jen-Chieh +;; Copyright (C) 2021-2022 Shen, Jen-Chieh ;; Created date 2021-10-04 20:19:42 ;; This file is NOT part of GNU Emacs. diff --git a/ts-fold.el b/ts-fold.el index 7ccf1ab..375349e 100644 --- a/ts-fold.el +++ b/ts-fold.el @@ -1,7 +1,7 @@ ;;; ts-fold.el --- Code folding using tree-sitter -*- lexical-binding: t; -*- ;; Copyright (C) 2021 Junyi Hou -;; Copyright (C) 2021 Shen, Jen-Chieh +;; Copyright (C) 2021-2022 Shen, Jen-Chieh ;; Created date 2021-08-11 14:12:37 @@ -38,6 +38,7 @@ ;;; Code: +(require 'cl-lib) (require 'seq) (require 'subr-x) @@ -45,7 +46,6 @@ (require 'tree-sitter) (require 'ts-fold-util) -(require 'ts-fold-parsers) (require 'ts-fold-summary) ;; @@ -54,58 +54,69 @@ (defgroup ts-fold nil "Code folding using tree-sitter." - :group 'tree-sitter - :prefix "ts-fold-") - -(defvar ts-fold-foldable-node-alist nil - "Collect a list of foldable node from variable `ts-fold-range-alist'. - -The alist is in form of (major-mode . (foldable-node-type)).") - -(defcustom ts-fold-range-alist - `((agda-mode . ,(ts-fold-parsers-agda)) - (sh-mode . ,(ts-fold-parsers-bash)) - (c-mode . ,(ts-fold-parsers-c)) - (c++-mode . ,(ts-fold-parsers-c++)) - (csharp-mode . ,(ts-fold-parsers-csharp)) - (css-mode . ,(ts-fold-parsers-css)) - (ess-r-mode . ,(ts-fold-parsers-r)) - (go-mode . ,(ts-fold-parsers-go)) - (html-mode . ,(ts-fold-parsers-html)) - (java-mode . ,(ts-fold-parsers-java)) - (javascript-mode . ,(ts-fold-parsers-javascript)) - (js-mode . ,(ts-fold-parsers-javascript)) - (js2-mode . ,(ts-fold-parsers-javascript)) - (js3-mode . ,(ts-fold-parsers-javascript)) - (json-mode . ,(ts-fold-parsers-json)) - (jsonc-mode . ,(ts-fold-parsers-json)) - (nix-mode . ,(ts-fold-parsers-nix)) - (php-mode . ,(ts-fold-parsers-php)) - (python-mode . ,(ts-fold-parsers-python)) - (rjsx-mode . ,(ts-fold-parsers-javascript)) - (ruby-mode . ,(ts-fold-parsers-ruby)) - (rust-mode . ,(ts-fold-parsers-rust)) - (rustic-mode . ,(ts-fold-parsers-rust)) - (scala-mode . ,(ts-fold-parsers-scala)) - (swift-mode . ,(ts-fold-parsers-swift)) - (typescript-mode . ,(ts-fold-parsers-typescript))) - "An alist of (major-mode . (foldable-node-type . function)). - -FUNCTION is used to determine where the beginning and end for FOLDABLE-NODE-TYPE -in MAJOR-MODE. It should take a single argument (the syntax node with type -FOLDABLE-NODE-TYPE) and return the buffer positions of the beginning and end of -the fold in a cons cell. See `ts-fold-range-python' for an example." - :type '(alist :key-type symbol - :value-type (alist :key-type symbol :value-type function)) - :set (lambda (symbol value) - (set-default symbol value) - (setq ts-fold-foldable-node-alist - (let (alist) - (dolist (item ts-fold-range-alist) - (let ((mode (car item)) nodes) - (dolist (rule (cdr item)) (push (car rule) nodes)) - (push (cons mode nodes) alist))) - alist))) + :prefix "ts-fold-" + :group 'tree-sitter) + +(defconst ts-fold--dir (file-name-directory (locate-library "ts-fold.el")) + "The directory where the library `ts-fold' is located.") + +(defconst ts-fold--queries-dir + (file-name-as-directory (concat ts-fold--dir "queries")) + "The directory where the `ts-fold' queries is located.") + +(defcustom ts-fold-major-mode-language-alist nil + "Alist that maps major modes to tree-sitter language names." + :type '(alist :key-type symbol :value-type string) + :group 'ts-fold) + +(pcase-dolist + (`(,major-mode . ,lang-symbol) + (reverse + '((sh-mode . "bash") + (shell-script-mode . "bash") + (c-mode . "c") + (csharp-mode . "c-sharp") + (c++-mode . "cpp") + (go-mode . "go") + (html-mode . "html") + (java-mode . "java") + (javascript-mode . "javascript") + (js-mode . "javascript") + (js2-mode . "javascript") + (js3-mode . "javascript") + (julia-mode . "julia") + (php-mode . "php") + (python-mode . "python") + (rjsx-mode . "javascript") + (ruby-mode . "ruby") + (rust-mode . "rust") + (rustic-mode . "rust") + (typescript-mode . "typescript")))) + (setf (map-elt ts-fold-major-mode-language-alist major-mode) lang-symbol)) + +(defcustom ts-fold-groups-alist + '((sh-mode . ()) + (shell-script-mode . ()) + (c-mode . ()) + (csharp-mode . ("class .inner" "function .inner" "loop .inner" "conditional .inner" "block.inner")) + (c++-mode . ()) + (go-mode . ()) + (html-mode . ()) + (java-mode . ()) + (javascript-mode . ()) + (js-mode . ()) + (js2-mode . ()) + (js3-mode . ()) + (julia-mode . ()) + (php-mode . ()) + (python-mode . ()) + (rjsx-mode . ()) + (ruby-mode . ()) + (rust-mode . ()) + (rustic-mode . ()) + (typescript-mode . ())) + "" + :type 'list :group 'ts-fold) (defcustom ts-fold-mode-hook nil @@ -147,7 +158,7 @@ the fold in a cons cell. See `ts-fold-range-python' for an example." (when (bound-and-true-p evil-fold-list) (add-to-list 'evil-fold-list '((ts-fold-mode) - :toggle ts-fold-toggle + :toggle ts-fold-toggle :open ts-fold-open :close ts-fold-close :open-rec ts-fold-open-recursively @@ -173,35 +184,133 @@ the fold in a cons cell. See `ts-fold-range-python' for an example." (define-global-minor-mode global-ts-fold-mode ts-fold-mode (lambda () (ts-fold-mode 1))) +;; +;; (@* "Queries" ) +;; + +(defun ts-fold--get-query (language top-level) + "Get tree sitter query for LANGUAGE. +TOP-LEVEL is used to mention if we should load optional inherits." + (with-temp-buffer + (when-let* ((filename (concat ts-fold--queries-dir + ;;language "/fold.scm")) ; TODO: replace this? + language "/textobjects.scm")) + ((file-exists-p filename))) + (insert-file-contents filename) + (goto-char (point-min)) + (when-let* ((first-line (thing-at-point 'line t)) + (first-line-matches + (save-match-data + (when (string-match "^; *inherits *:? *\\([a-z_,()]+\\) *$" + first-line) + (match-string 1 first-line))))) + (insert + (string-join + (mapcar (lambda (x) + (if (string-prefix-p "(" x) + (when top-level + (ts-fold--get-query (substring x 1 -1) + nil)) + (ts-fold--get-query x nil))) + (split-string first-line-matches ",")) + "\n"))) + (buffer-string)))) + ;; ;; (@* "Core" ) ;; -(defun ts-fold--foldable-node-at-pos (&optional pos) - "Return the smallest foldable node at POS. If POS is nil, use `point'. - -Raise `user-error' if no foldable node is found. - -This function is borrowed from `tree-sitter-node-at-point'." - (let* ((pos (or pos (point))) - (foldable-types (alist-get major-mode ts-fold-foldable-node-alist)) - (root (tsc-root-node tree-sitter-tree)) - (node (tsc-get-descendant-for-position-range root pos pos))) - (let ((current node) result) - (while current - (if (memq (tsc-node-type current) foldable-types) - (setq result current - current nil) - (setq current (tsc-get-parent current)))) - (or result (user-error "No foldable node found at POS"))))) - -(defun ts-fold--get-fold-range (node) - "Return the beginning (as buffer position) of fold for NODE." - (when-let* ((fold-alist (alist-get major-mode ts-fold-range-alist)) - (item (alist-get (tsc-node-type node) fold-alist))) - (cond ((functionp item) (funcall item node (cons 0 0))) - ((listp item) (funcall (nth 0 item) node (cons (nth 1 item) (nth 2 item)))) - (t (user-error "Current node is not found in `ts-fold-range-alist' in %s" major-mode))))) +(defun ts-fold--nodes-before (nodes) + "NODES which contain the current after them." + (cl-remove-if-not (lambda (x) + (< (byte-to-position (cdr (tsc-node-byte-range x))) (point))) + nodes)) + +(defun ts-fold--nodes-within (nodes) + "NODES which contain the current point inside them ordered inside out." + (let ((byte-pos (position-bytes (point)))) + (sort (cl-remove-if-not (lambda (x) + (and (<= (car (tsc-node-byte-range x)) byte-pos) + (< byte-pos (cdr (tsc-node-byte-range x))))) + nodes) + (lambda (x y) + (< (+ (abs (- byte-pos + (car (tsc-node-byte-range x)))) + (abs (- byte-pos + (cdr (tsc-node-byte-range x))))) + (+ (abs (- byte-pos + (car (tsc-node-byte-range y)))) + (abs (- byte-pos + (cdr (tsc-node-byte-range y)))))))))) + +(defun ts-fold--nodes-after (nodes) + "NODES which contain the current point before them ordered top to bottom." + (cl-remove-if-not (lambda (x) + (> (byte-to-position (car (tsc-node-byte-range x))) (point))) + nodes)) + +(defun ts-fold--get-nodes (group query) + "Get a list of viable nodes based on `GROUP' value. +They will be order with captures with point inside them first then the +ones that follow. If a `QUERY' alist is provided, we make use of that +instead of the builtin query set." + (let* ((lang-name (alist-get major-mode ts-fold-major-mode-language-alist)) + (debugging-query (if query (alist-get major-mode query) + (ts-fold--get-query lang-name t))) + (root-node (tsc-root-node tree-sitter-tree)) + (query (tsc-make-query tree-sitter-language debugging-query)) + (captures (tsc-query-captures query root-node #'tsc--buffer-substring-no-properties)) + (filtered-captures (cl-remove-if-not (lambda (x) + (member (car x) group)) + captures)) + (nodes (seq-map #'cdr filtered-captures))) + (cl-remove-duplicates + nodes + :test (lambda (x y) + (and (= (car (tsc-node-byte-range x)) (car (tsc-node-byte-range y))) + (= (cdr (tsc-node-byte-range x)) (cdr (tsc-node-byte-range y)))))))) + +(defun ts-fold--get-within-and-after (group count query) + "Given a `GROUP' `QUERY' find `COUNT' number of nodes within in and after current point." + (let* ((nodes (ts-fold--get-nodes group query)) + (nodes-within (ts-fold--nodes-within nodes)) + (nodes-after (ts-fold--nodes-after nodes)) + (filtered-nodes (append nodes-within nodes-after))) + (when (> (length filtered-nodes) 0) + (cl-subseq filtered-nodes 0 count)))) + +(defun ts-fold--range (count ts-group &optional query) + "Get the range of the closeset item of type `TS-GROUP'. +`COUNT' is supported even thought it does not actually make sense in +most cases as if we do 3-in-func the selections will not be continues, +but we can only provide the start and end as of now which is what we +are doing. If a `QUERY' alist is provided, we make use of that +instead of the builtin query set." + (when-let ((nodes (ts-fold--get-within-and-after ts-group count query))) + (let ((range-min (apply #'min + (seq-map (lambda (x) + (car (tsc-node-byte-range x))) + nodes))) + (range-max (apply #'max + (seq-map (lambda (x) + (cdr (tsc-node-byte-range x))) + nodes)))) + ;; Have to compute min and max like this as we might have nested functions + ;; We have to use `cl-callf byte-to-position` ot the positioning might be off for unicode chars + (cons (cl-callf byte-to-position range-min) (cl-callf byte-to-position range-max))))) + +(defun ts-fold--get-fold-range () + "Return fold range." + (if-let* ((pt (point)) + (group (alist-get major-mode ts-fold-groups-alist)) + (groups (if (eq (type-of group) 'string) + (list group) + group)) + (interned-groups (mapcar #'intern groups)) + (range (ts-fold--range 1 interned-groups))) + (when (and (<= (car range) pt) (<= pt (cdr range))) + range) + (message "[INFO] No region found, `%s`" group))) ;; ;; (@* "Overlays" ) @@ -223,18 +332,15 @@ This function is borrowed from `tree-sitter-node-at-point'." "Open overlay OV during `isearch' session." (delete-overlay ov)) -(defun ts-fold-overlay-at (node) +(defun ts-fold-overlay-at (range) "Return the ts-fold overlay at NODE if NODE is foldable and folded. Return nil otherwise." - (when-let* ((foldable-types (alist-get major-mode ts-fold-foldable-node-alist)) - ((memq (tsc-node-type node) foldable-types)) - (range (ts-fold--get-fold-range node))) - (thread-last (overlays-in (car range) (cdr range)) - (seq-filter (lambda (ov) - (and (eq (overlay-get ov 'invisible) 'ts-fold) - (= (overlay-start ov) (car range)) - (= (overlay-end ov) (cdr range))))) - car))) + (thread-last (overlays-in (car range) (cdr range)) + (seq-filter (lambda (ov) + (and (eq (overlay-get ov 'invisible) 'ts-fold) + (= (overlay-start ov) (car range)) + (= (overlay-end ov) (cdr range))))) + car)) ;; ;; (@* "Commands" ) @@ -243,23 +349,30 @@ Return nil otherwise." (defmacro ts-fold--ensure-ts (&rest body) "Run BODY only if `tree-sitter-mode` is enabled." (declare (indent 0)) - `(if (bound-and-true-p tree-sitter-mode) - (progn ,@body) + `(if (bound-and-true-p tree-sitter-mode) ,@body (user-error "Ignored, tree-sitter-mode is not enable in the current buffer"))) +(defconst ts-fold-interactive-commands + '(ts-fold-close + ts-fold-open + ts-fold-open-recursively + ts-fold-close-all + ts-fold-open-all + ts-fold-toggle) + "List of interactive commands.") + ;;;###autoload -(defun ts-fold-close (&optional node) +(defun ts-fold-close () "Fold the syntax node at `point` if it is foldable. Foldable nodes are defined in `ts-fold-foldable-node-alist' for the current `major-mode'. If no foldable NODE is found in point, do nothing." (interactive) (ts-fold--ensure-ts - (let ((node (or node (ts-fold--foldable-node-at-pos)))) + (when-let ((range (ts-fold--get-fold-range))) ;; make sure I do not create multiple overlays for the same fold - (when-let* ((ov (ts-fold-overlay-at node))) - (delete-overlay ov)) - (ts-fold--create-overlay (ts-fold--get-fold-range node))))) + (when-let* ((ov (ts-fold-overlay-at range))) (delete-overlay ov)) + (ts-fold--create-overlay range)))) ;;;###autoload (defun ts-fold-open () @@ -267,8 +380,8 @@ current `major-mode'. If no foldable NODE is found in point, do nothing." If the current node is not folded or not foldable, do nothing." (interactive) (ts-fold--ensure-ts - (when-let* ((node (ts-fold--foldable-node-at-pos)) - (ov (ts-fold-overlay-at node))) + (when-let* ((range (ts-fold--get-fold-range)) + (ov (ts-fold-overlay-at range))) (delete-overlay ov)))) ;;;###autoload @@ -313,24 +426,11 @@ If the current node is not folded or not foldable, do nothing." If the current syntax node is not foldable, do nothing." (interactive) (ts-fold--ensure-ts - (if-let* ((node (ts-fold--foldable-node-at-pos (point))) - (ov (ts-fold-overlay-at node))) + (if-let* ((range (ts-fold--get-fold-range)) + (ov (ts-fold-overlay-at range))) (progn (delete-overlay ov) t) (ts-fold-close)))) -(defun ts-fold--after-command (&rest _) - "Function call after interactive commands." - (ts-fold-indicators-refresh)) - -(let ((commands '(ts-fold-close - ts-fold-open - ts-fold-open-recursively - ts-fold-close-all - ts-fold-open-all - ts-fold-toggle))) - (dolist (command commands) - (advice-add command :after #'ts-fold--after-command))) - ;; ;; (@* "Rule Helpers" ) ;; From 0c348b45626c21a52cfa5ab556cb1e32934394f4 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Mon, 21 Feb 2022 02:04:20 +0800 Subject: [PATCH 03/21] Rename quries --- queries/bash/{textobjects.scm => fold.scm} | 0 queries/bibtex/{textobjects.scm => fold.scm} | 0 queries/c-sharp/{textobjects.scm => fold.scm} | 0 queries/c/{textobjects.scm => fold.scm} | 0 queries/cpp/{textobjects.scm => fold.scm} | 0 queries/cuda/{textobjects.scm => fold.scm} | 0 queries/dart/{textobjects.scm => fold.scm} | 0 queries/elixir/{textobjects.scm => fold.scm} | 0 queries/elm/{textobjects.scm => fold.scm} | 0 queries/fennel/{textobjects.scm => fold.scm} | 0 queries/fish/{textobjects.scm => fold.scm} | 0 queries/glsl/{textobjects.scm => fold.scm} | 0 queries/go/{textobjects.scm => fold.scm} | 0 queries/html/{textobjects.scm => fold.scm} | 0 queries/java/{textobjects.scm => fold.scm} | 0 .../javascript/{textobjects.scm => fold.scm} | 0 queries/julia/{textobjects.scm => fold.scm} | 0 queries/latex/{textobjects.scm => fold.scm} | 0 queries/lua/{textobjects.scm => fold.scm} | 0 .../markdown/{textobjects.scm => fold.scm} | 0 queries/php/{textobjects.scm => fold.scm} | 0 queries/python/{textobjects.scm => fold.scm} | 0 queries/ql/{textobjects.scm => fold.scm} | 0 queries/query/{textobjects.scm => fold.scm} | 0 queries/rst/{textobjects.scm => fold.scm} | 0 queries/ruby/{textobjects.scm => fold.scm} | 0 queries/rust/{textobjects.scm => fold.scm} | 0 queries/scala/{textobjects.scm => fold.scm} | 0 queries/tsx/{textobjects.scm => fold.scm} | 0 .../typescript/{textobjects.scm => fold.scm} | 0 queries/verilog/{textobjects.scm => fold.scm} | 0 queries/vim/{textobjects.scm => fold.scm} | 0 queries/vue/{textobjects.scm => fold.scm} | 0 queries/zig/{textobjects.scm => fold.scm} | 0 ts-fold-indicators.el | 10 ++-- ts-fold.el | 48 ++++++++----------- 36 files changed, 25 insertions(+), 33 deletions(-) rename queries/bash/{textobjects.scm => fold.scm} (100%) rename queries/bibtex/{textobjects.scm => fold.scm} (100%) rename queries/c-sharp/{textobjects.scm => fold.scm} (100%) rename queries/c/{textobjects.scm => fold.scm} (100%) rename queries/cpp/{textobjects.scm => fold.scm} (100%) rename queries/cuda/{textobjects.scm => fold.scm} (100%) rename queries/dart/{textobjects.scm => fold.scm} (100%) rename queries/elixir/{textobjects.scm => fold.scm} (100%) rename queries/elm/{textobjects.scm => fold.scm} (100%) rename queries/fennel/{textobjects.scm => fold.scm} (100%) rename queries/fish/{textobjects.scm => fold.scm} (100%) rename queries/glsl/{textobjects.scm => fold.scm} (100%) rename queries/go/{textobjects.scm => fold.scm} (100%) rename queries/html/{textobjects.scm => fold.scm} (100%) rename queries/java/{textobjects.scm => fold.scm} (100%) rename queries/javascript/{textobjects.scm => fold.scm} (100%) rename queries/julia/{textobjects.scm => fold.scm} (100%) rename queries/latex/{textobjects.scm => fold.scm} (100%) rename queries/lua/{textobjects.scm => fold.scm} (100%) rename queries/markdown/{textobjects.scm => fold.scm} (100%) rename queries/php/{textobjects.scm => fold.scm} (100%) rename queries/python/{textobjects.scm => fold.scm} (100%) rename queries/ql/{textobjects.scm => fold.scm} (100%) rename queries/query/{textobjects.scm => fold.scm} (100%) rename queries/rst/{textobjects.scm => fold.scm} (100%) rename queries/ruby/{textobjects.scm => fold.scm} (100%) rename queries/rust/{textobjects.scm => fold.scm} (100%) rename queries/scala/{textobjects.scm => fold.scm} (100%) rename queries/tsx/{textobjects.scm => fold.scm} (100%) rename queries/typescript/{textobjects.scm => fold.scm} (100%) rename queries/verilog/{textobjects.scm => fold.scm} (100%) rename queries/vim/{textobjects.scm => fold.scm} (100%) rename queries/vue/{textobjects.scm => fold.scm} (100%) rename queries/zig/{textobjects.scm => fold.scm} (100%) diff --git a/queries/bash/textobjects.scm b/queries/bash/fold.scm similarity index 100% rename from queries/bash/textobjects.scm rename to queries/bash/fold.scm diff --git a/queries/bibtex/textobjects.scm b/queries/bibtex/fold.scm similarity index 100% rename from queries/bibtex/textobjects.scm rename to queries/bibtex/fold.scm diff --git a/queries/c-sharp/textobjects.scm b/queries/c-sharp/fold.scm similarity index 100% rename from queries/c-sharp/textobjects.scm rename to queries/c-sharp/fold.scm diff --git a/queries/c/textobjects.scm b/queries/c/fold.scm similarity index 100% rename from queries/c/textobjects.scm rename to queries/c/fold.scm diff --git a/queries/cpp/textobjects.scm b/queries/cpp/fold.scm similarity index 100% rename from queries/cpp/textobjects.scm rename to queries/cpp/fold.scm diff --git a/queries/cuda/textobjects.scm b/queries/cuda/fold.scm similarity index 100% rename from queries/cuda/textobjects.scm rename to queries/cuda/fold.scm diff --git a/queries/dart/textobjects.scm b/queries/dart/fold.scm similarity index 100% rename from queries/dart/textobjects.scm rename to queries/dart/fold.scm diff --git a/queries/elixir/textobjects.scm b/queries/elixir/fold.scm similarity index 100% rename from queries/elixir/textobjects.scm rename to queries/elixir/fold.scm diff --git a/queries/elm/textobjects.scm b/queries/elm/fold.scm similarity index 100% rename from queries/elm/textobjects.scm rename to queries/elm/fold.scm diff --git a/queries/fennel/textobjects.scm b/queries/fennel/fold.scm similarity index 100% rename from queries/fennel/textobjects.scm rename to queries/fennel/fold.scm diff --git a/queries/fish/textobjects.scm b/queries/fish/fold.scm similarity index 100% rename from queries/fish/textobjects.scm rename to queries/fish/fold.scm diff --git a/queries/glsl/textobjects.scm b/queries/glsl/fold.scm similarity index 100% rename from queries/glsl/textobjects.scm rename to queries/glsl/fold.scm diff --git a/queries/go/textobjects.scm b/queries/go/fold.scm similarity index 100% rename from queries/go/textobjects.scm rename to queries/go/fold.scm diff --git a/queries/html/textobjects.scm b/queries/html/fold.scm similarity index 100% rename from queries/html/textobjects.scm rename to queries/html/fold.scm diff --git a/queries/java/textobjects.scm b/queries/java/fold.scm similarity index 100% rename from queries/java/textobjects.scm rename to queries/java/fold.scm diff --git a/queries/javascript/textobjects.scm b/queries/javascript/fold.scm similarity index 100% rename from queries/javascript/textobjects.scm rename to queries/javascript/fold.scm diff --git a/queries/julia/textobjects.scm b/queries/julia/fold.scm similarity index 100% rename from queries/julia/textobjects.scm rename to queries/julia/fold.scm diff --git a/queries/latex/textobjects.scm b/queries/latex/fold.scm similarity index 100% rename from queries/latex/textobjects.scm rename to queries/latex/fold.scm diff --git a/queries/lua/textobjects.scm b/queries/lua/fold.scm similarity index 100% rename from queries/lua/textobjects.scm rename to queries/lua/fold.scm diff --git a/queries/markdown/textobjects.scm b/queries/markdown/fold.scm similarity index 100% rename from queries/markdown/textobjects.scm rename to queries/markdown/fold.scm diff --git a/queries/php/textobjects.scm b/queries/php/fold.scm similarity index 100% rename from queries/php/textobjects.scm rename to queries/php/fold.scm diff --git a/queries/python/textobjects.scm b/queries/python/fold.scm similarity index 100% rename from queries/python/textobjects.scm rename to queries/python/fold.scm diff --git a/queries/ql/textobjects.scm b/queries/ql/fold.scm similarity index 100% rename from queries/ql/textobjects.scm rename to queries/ql/fold.scm diff --git a/queries/query/textobjects.scm b/queries/query/fold.scm similarity index 100% rename from queries/query/textobjects.scm rename to queries/query/fold.scm diff --git a/queries/rst/textobjects.scm b/queries/rst/fold.scm similarity index 100% rename from queries/rst/textobjects.scm rename to queries/rst/fold.scm diff --git a/queries/ruby/textobjects.scm b/queries/ruby/fold.scm similarity index 100% rename from queries/ruby/textobjects.scm rename to queries/ruby/fold.scm diff --git a/queries/rust/textobjects.scm b/queries/rust/fold.scm similarity index 100% rename from queries/rust/textobjects.scm rename to queries/rust/fold.scm diff --git a/queries/scala/textobjects.scm b/queries/scala/fold.scm similarity index 100% rename from queries/scala/textobjects.scm rename to queries/scala/fold.scm diff --git a/queries/tsx/textobjects.scm b/queries/tsx/fold.scm similarity index 100% rename from queries/tsx/textobjects.scm rename to queries/tsx/fold.scm diff --git a/queries/typescript/textobjects.scm b/queries/typescript/fold.scm similarity index 100% rename from queries/typescript/textobjects.scm rename to queries/typescript/fold.scm diff --git a/queries/verilog/textobjects.scm b/queries/verilog/fold.scm similarity index 100% rename from queries/verilog/textobjects.scm rename to queries/verilog/fold.scm diff --git a/queries/vim/textobjects.scm b/queries/vim/fold.scm similarity index 100% rename from queries/vim/textobjects.scm rename to queries/vim/fold.scm diff --git a/queries/vue/textobjects.scm b/queries/vue/fold.scm similarity index 100% rename from queries/vue/textobjects.scm rename to queries/vue/fold.scm diff --git a/queries/zig/textobjects.scm b/queries/zig/fold.scm similarity index 100% rename from queries/zig/textobjects.scm rename to queries/zig/fold.scm diff --git a/ts-fold-indicators.el b/ts-fold-indicators.el index aaa2d7c..f6de85b 100644 --- a/ts-fold-indicators.el +++ b/ts-fold-indicators.el @@ -261,10 +261,12 @@ head (first line) of the region." (defun ts-fold-indicators--create (node) "Create indicators using NODE." - (when-let* ((range (ts-fold--get-fold-range node)) - (beg (car range)) (end (cdr range))) - (let ((folded (ts-fold-overlay-at node))) - (ts-fold-indicators--create-overlays beg end folded)))) + ;; TODO: + ;; (when-let* ((range (ts-fold--get-fold-range node)) + ;; (beg (car range)) (end (cdr range))) + ;; (let ((folded (ts-fold-overlay-at node))) + ;; (ts-fold-indicators--create-overlays beg end folded))) + ) ;;;###autoload (defun ts-fold-indicators-refresh (&rest _) diff --git a/ts-fold.el b/ts-fold.el index 375349e..d5ded47 100644 --- a/ts-fold.el +++ b/ts-fold.el @@ -98,7 +98,7 @@ '((sh-mode . ()) (shell-script-mode . ()) (c-mode . ()) - (csharp-mode . ("class .inner" "function .inner" "loop .inner" "conditional .inner" "block.inner")) + (csharp-mode . ("class.inner" "function .inner" "loop .inner" "conditional .inner" "block.inner")) (c++-mode . ()) (go-mode . ()) (html-mode . ()) @@ -192,9 +192,7 @@ "Get tree sitter query for LANGUAGE. TOP-LEVEL is used to mention if we should load optional inherits." (with-temp-buffer - (when-let* ((filename (concat ts-fold--queries-dir - ;;language "/fold.scm")) ; TODO: replace this? - language "/textobjects.scm")) + (when-let* ((filename (concat ts-fold--queries-dir language "/fold.scm")) ((file-exists-p filename))) (insert-file-contents filename) (goto-char (point-min)) @@ -301,16 +299,12 @@ instead of the builtin query set." (defun ts-fold--get-fold-range () "Return fold range." - (if-let* ((pt (point)) - (group (alist-get major-mode ts-fold-groups-alist)) - (groups (if (eq (type-of group) 'string) - (list group) - group)) - (interned-groups (mapcar #'intern groups)) - (range (ts-fold--range 1 interned-groups))) - (when (and (<= (car range) pt) (<= pt (cdr range))) - range) - (message "[INFO] No region found, `%s`" group))) + (when-let* ((pt (point)) + (groups (alist-get major-mode ts-fold-groups-alist)) + (interned-groups (mapcar #'intern groups)) + (range (ts-fold--range 1 interned-groups))) + (when (and (<= (car range) pt) (<= pt (cdr range))) + (cons (1+ (car range)) (1- (cdr range)))))) ;; ;; (@* "Overlays" ) @@ -318,15 +312,14 @@ instead of the builtin query set." (defun ts-fold--create-overlay (range) "Create invisible overlay in RANGE." - (when range - (let* ((beg (car range)) (end (cdr range)) (ov (make-overlay beg end))) - (overlay-put ov 'creator 'ts-fold) - (overlay-put ov 'invisible 'ts-fold) - (overlay-put ov 'display (or (and ts-fold-summary-show - (ts-fold-summary--get (buffer-substring beg end))) - ts-fold-replacement)) - (overlay-put ov 'face 'ts-fold-replacement-face) - (overlay-put ov 'isearch-open-invisible #'ts-fold--isearch-open)))) + (let* ((beg (car range)) (end (cdr range)) (ov (make-overlay beg end))) + (overlay-put ov 'creator 'ts-fold) + (overlay-put ov 'invisible 'ts-fold) + (overlay-put ov 'display (or (and ts-fold-summary-show + (ts-fold-summary--get (buffer-substring beg end))) + ts-fold-replacement)) + (overlay-put ov 'face 'ts-fold-replacement-face) + (overlay-put ov 'isearch-open-invisible #'ts-fold--isearch-open))) (defun ts-fold--isearch-open (ov) "Open overlay OV during `isearch' session." @@ -349,7 +342,7 @@ Return nil otherwise." (defmacro ts-fold--ensure-ts (&rest body) "Run BODY only if `tree-sitter-mode` is enabled." (declare (indent 0)) - `(if (bound-and-true-p tree-sitter-mode) ,@body + `(if (bound-and-true-p tree-sitter-mode) (progn ,@body) (user-error "Ignored, tree-sitter-mode is not enable in the current buffer"))) (defconst ts-fold-interactive-commands @@ -363,15 +356,12 @@ Return nil otherwise." ;;;###autoload (defun ts-fold-close () - "Fold the syntax node at `point` if it is foldable. - -Foldable nodes are defined in `ts-fold-foldable-node-alist' for the -current `major-mode'. If no foldable NODE is found in point, do nothing." + "Fold the syntax node at `point` if it is foldable." (interactive) (ts-fold--ensure-ts (when-let ((range (ts-fold--get-fold-range))) ;; make sure I do not create multiple overlays for the same fold - (when-let* ((ov (ts-fold-overlay-at range))) (delete-overlay ov)) + (when-let ((ov (ts-fold-overlay-at range))) (delete-overlay ov)) (ts-fold--create-overlay range)))) ;;;###autoload From 87fecfa4369c00c7bbff091ce95d4a907ac2098d Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Mon, 21 Feb 2022 02:05:31 +0800 Subject: [PATCH 04/21] Remove parser helpers --- ts-fold.el | 151 ----------------------------------------------------- 1 file changed, 151 deletions(-) diff --git a/ts-fold.el b/ts-fold.el index d5ded47..41b8efb 100644 --- a/ts-fold.el +++ b/ts-fold.el @@ -421,156 +421,5 @@ If the current syntax node is not foldable, do nothing." (progn (delete-overlay ov) t) (ts-fold-close)))) -;; -;; (@* "Rule Helpers" ) -;; - -(defun ts-fold--next-prev-node (node next) - "Return previous/next sibling node starting from NODE. - -If NEXT is non-nil, return next sibling. Otherwirse, return previouse sibling." - (if next (tsc-get-next-sibling node) (tsc-get-prev-sibling node))) - -(defun ts-fold--continuous-node-prefix (node prefix next) - "Iterate through node starting from NODE and compare node-text to PREFIX; -then return the last iterated node. - -Argument NEXT is a boolean type. If non-nil iterate forward; otherwise iterate -in backward direction." - (let ((iter-node node) (last-node node) - (last-line (car (tsc-node-start-point node))) line text break - (line-range 1) (last-line-range 1) max-line-range) - (while (and iter-node (not break)) - (setq text (tsc-node-text iter-node) - line (car (tsc-node-start-point iter-node)) - line-range (1+ (s-count-matches "\n" text)) - max-line-range (max line-range last-line-range)) - (if (and (ts-fold-util--in-range-p line (- last-line max-line-range) (+ last-line max-line-range)) - (string-prefix-p prefix text)) - (setq last-node iter-node last-line line - last-line-range (1+ (s-count-matches "\n" text))) - (setq break t)) - (setq iter-node (ts-fold--next-prev-node iter-node next))) - last-node)) - -(defun ts-fold-range-seq (node offset) - "Return the fold range in sequence starting from NODE. - -Argument OFFSET can be used to tweak the final beginning and end position." - (let ((beg (1+ (tsc-node-start-position node))) - (end (1- (tsc-node-end-position node)))) - (ts-fold-util--cons-add (cons beg end) offset))) - -(defun ts-fold-range-line-comment (node offset prefix) - "Define fold range for line comment. - -For arguments NODE and OFFSET, see function `ts-fold-range-seq' for -more information. - -Argument PREFIX is the comment prefix in string." - (when-let* ((first-node (ts-fold--continuous-node-prefix node prefix nil)) - (last-node (ts-fold--continuous-node-prefix node prefix t)) - (prefix-len (length prefix)) - (beg (+ (tsc-node-start-position first-node) prefix-len)) - (end (tsc-node-end-position last-node))) - (ts-fold-util--cons-add (cons beg end) offset))) - -(defun ts-fold-range-block-comment (node offset) - "Define fold range for block comment. - -For arguments NODE and OFFSET, see function `ts-fold-range-seq' for -more information." - (ts-fold-range-seq node (ts-fold-util--cons-add '(1 . -1) offset))) - -(defun ts-fold-range-c-like-comment (node offset) - "Define fold range for C-like comemnt." - (let ((text (tsc-node-text node))) - (if (and (string-match-p "\n" text) (string-prefix-p "/*" text)) - (ts-fold-range-block-comment node offset) - (if (string-prefix-p "///" text) - (ts-fold-range-line-comment node offset "///") - (ts-fold-range-line-comment node offset "//"))))) - -;; -;; (@* "Languages" ) -;; - -(defun ts-fold-range-c-preproc-if (node offset) - "Define fold range for `if' preprocessor." - (let* ((named-node (tsc-get-child-by-field node :condition)) - (else (tsc-get-child-by-field node :alternative)) - (beg (tsc-node-end-position named-node)) - (end (1- (tsc-node-start-position else)))) - (ts-fold-util--cons-add (cons beg end) offset))) - -(defun ts-fold-range-c-preproc-ifdef (node offset) - "Define fold range for `ifdef' and `ifndef' preprocessor." - (when-let* ((named-node (tsc-get-child-by-field node :name)) - (else (tsc-get-child-by-field node :alternative)) - (beg (tsc-node-end-position named-node)) - (end (1- (tsc-node-start-position else)))) - (ts-fold-util--cons-add (cons beg end) offset))) - -(defun ts-fold-range-c-preproc-elif (node offset) - "Define fold range for `elif' preprocessor." - (when-let* ((named-node (tsc-get-child-by-field node :condition)) - (else (tsc-get-child-by-field node :alternative)) - (beg (tsc-node-end-position named-node)) - (end (1- (tsc-node-start-position else)))) - (ts-fold-util--cons-add (cons beg end) offset))) - -(defun ts-fold-range-c-preproc-else (node offset) - "Define fold range for `else' preprocessor." - (when-let* ((target "#else") - (len (length target)) - (beg (+ (tsc-node-start-position node) len)) - (end (tsc-node-end-position node))) - (ts-fold-util--cons-add (cons beg end) offset))) - -(defun ts-fold-range-html (node offset) - "Define fold range for tag in HTML." - (let* ((beg (tsc-node-end-position (tsc-get-nth-child node 0))) - (end-node (tsc-get-nth-child node (1- (tsc-count-children node)))) - (end (tsc-node-start-position end-node))) - (ts-fold-util--cons-add (cons beg end) offset))) - -(defun ts-fold-range-python (node offset) - "Define fold range for `function_definition' and `class_definition'. - -For arguments NODE and OFFSET, see function `ts-fold-range-seq' for -more information." - (when-let* ((named-node (or (tsc-get-child-by-field node :superclasses) - (tsc-get-child-by-field node :return_type) - (tsc-get-child-by-field node :parameters) - (tsc-get-child-by-field node :name))) - ;; the colon is an anonymous node after return_type or parameters node - (beg (tsc-node-end-position (tsc-get-next-sibling named-node))) - (end (tsc-node-end-position node))) - (ts-fold-util--cons-add (cons beg end) offset))) - -(defun ts-fold-range-ruby (node offset) - "Define fold range for `method' and `class' in Ruby. - -For arguments NODE and OFFSET, see function `ts-fold-range-seq' for -more information." - (when-let* ((named-node (or (tsc-get-child-by-field node :superclass) - (tsc-get-child-by-field node :parameters) - (tsc-get-child-by-field node :name))) - (beg (tsc-node-end-position named-node)) - (end (tsc-node-end-position node))) - (ts-fold-util--cons-add (cons beg end) offset))) - -(defun ts-fold-range-rust-macro (node offset) - "Return the fold range for `macro_definition' NODE in Rust. - -For arguments NODE and OFFSET, see function `ts-fold-range-seq' for -more information." - (when-let* ((children (tsc-count-children node)) - (last_bracket (tsc-get-nth-child node (- children 1))) - (first_bracket (tsc-get-nth-child node 2)) - (beg (tsc-node-start-position first_bracket)) - (end (1+ (tsc-node-start-position last_bracket)))) - (ts-fold-util--cons-add (cons beg end) offset))) - (provide 'ts-fold) ;;; ts-fold.el ends here From d5c79ec25816901553c4d60a9ecd6b6cde3a5b87 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Mon, 21 Feb 2022 02:23:22 +0800 Subject: [PATCH 05/21] Update lang list --- ts-fold.el | 48 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/ts-fold.el b/ts-fold.el index 41b8efb..fae20b4 100644 --- a/ts-fold.el +++ b/ts-fold.el @@ -95,27 +95,23 @@ (setf (map-elt ts-fold-major-mode-language-alist major-mode) lang-symbol)) (defcustom ts-fold-groups-alist - '((sh-mode . ()) - (shell-script-mode . ()) - (c-mode . ()) - (csharp-mode . ("class.inner" "function .inner" "loop .inner" "conditional .inner" "block.inner")) - (c++-mode . ()) - (go-mode . ()) - (html-mode . ()) - (java-mode . ()) - (javascript-mode . ()) - (js-mode . ()) - (js2-mode . ()) - (js3-mode . ()) - (julia-mode . ()) - (php-mode . ()) - (python-mode . ()) - (rjsx-mode . ()) - (ruby-mode . ()) - (rust-mode . ()) - (rustic-mode . ()) - (typescript-mode . ())) - "" + '((bash . ("function.inner" "conditional.inner" "loop.inner" "comment.outer")) + (c . ("function.inner" "class.inner" "conditional.inner" "loop.inner" + "block.outer" "comment.outer" "statement.inner")) + (c-sharp . ("class.inner" "function.inner" "loop.inner" "conditional.inner" + "block.inner")) + (cpp . ()) + (go . ()) + (html . ()) + (java . ()) + (javascript . ()) + (julia . ()) + (php . ()) + (python . ()) + (ruby . ()) + (rust . ()) + (typescript . ())) + "Alist of language symbol and groups." :type 'list :group 'ts-fold) @@ -300,7 +296,8 @@ instead of the builtin query set." (defun ts-fold--get-fold-range () "Return fold range." (when-let* ((pt (point)) - (groups (alist-get major-mode ts-fold-groups-alist)) + (lang-name (alist-get major-mode ts-fold-major-mode-language-alist)) + (groups (alist-get (intern lang-name) ts-fold-groups-alist)) (interned-groups (mapcar #'intern groups)) (range (ts-fold--range 1 interned-groups))) (when (and (<= (car range) pt) (<= pt (cdr range))) @@ -379,9 +376,9 @@ If the current node is not folded or not foldable, do nothing." "Open recursively folded syntax NODE that are contained in the node at point." (interactive) (ts-fold--ensure-ts - (when-let* ((node (ts-fold--foldable-node-at-pos)) - (beg (tsc-node-start-position node)) - (end (tsc-node-end-position node))) + (when-let* ((range (ts-fold--get-fold-range)) + (beg (car range)) + (end (cdr range))) (thread-last (overlays-in beg end) (seq-filter (lambda (ov) (eq (overlay-get ov 'invisible) 'ts-fold))) (mapc #'delete-overlay))))) @@ -391,6 +388,7 @@ If the current node is not folded or not foldable, do nothing." "Fold all foldable syntax nodes in the buffer." (interactive) (ts-fold--ensure-ts + ;; TODO: .. (let* ((node (tsc-root-node tree-sitter-tree)) (patterns (seq-mapcat (lambda (type) `(,(list type) @name)) (alist-get major-mode ts-fold-foldable-node-alist) From 4f65197e09c079fb219ed9e07a632e0b4ec7f7aa Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Mon, 21 Feb 2022 02:42:15 +0800 Subject: [PATCH 06/21] Update --- ts-fold.el | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ts-fold.el b/ts-fold.el index fae20b4..7e84638 100644 --- a/ts-fold.el +++ b/ts-fold.el @@ -99,8 +99,9 @@ (c . ("function.inner" "class.inner" "conditional.inner" "loop.inner" "block.outer" "comment.outer" "statement.inner")) (c-sharp . ("class.inner" "function.inner" "loop.inner" "conditional.inner" - "block.inner")) - (cpp . ()) + "block.inner" "comment.outer")) + (cpp . ("function.inner" "class.inner" "conditional.inner" "loop.inner" + "block.outer" "comment.outer" "statement.inner")) (go . ()) (html . ()) (java . ()) From 624f5dc7cf53e74032e0d3d78d1c49f550eb80bc Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Sat, 26 Feb 2022 00:22:22 +0800 Subject: [PATCH 07/21] Revert to use node --- ts-fold.el | 83 +++++++++++++++++++++++++++--------------------------- 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/ts-fold.el b/ts-fold.el index 7e84638..da5140e 100644 --- a/ts-fold.el +++ b/ts-fold.el @@ -215,6 +215,13 @@ TOP-LEVEL is used to mention if we should load optional inherits." ;; (@* "Core" ) ;; +(defun ts-fold--get-groups () + "" + (let* ((lang-name (alist-get major-mode ts-fold-major-mode-language-alist)) + (groups (alist-get (intern lang-name) ts-fold-groups-alist)) + (interned-groups (mapcar #'intern groups))) + interned-groups)) + (defun ts-fold--nodes-before (nodes) "NODES which contain the current after them." (cl-remove-if-not (lambda (x) @@ -274,35 +281,29 @@ instead of the builtin query set." (when (> (length filtered-nodes) 0) (cl-subseq filtered-nodes 0 count)))) -(defun ts-fold--range (count ts-group &optional query) - "Get the range of the closeset item of type `TS-GROUP'. -`COUNT' is supported even thought it does not actually make sense in -most cases as if we do 3-in-func the selections will not be continues, -but we can only provide the start and end as of now which is what we -are doing. If a `QUERY' alist is provided, we make use of that -instead of the builtin query set." - (when-let ((nodes (ts-fold--get-within-and-after ts-group count query))) - (let ((range-min (apply #'min - (seq-map (lambda (x) - (car (tsc-node-byte-range x))) - nodes))) - (range-max (apply #'max - (seq-map (lambda (x) - (cdr (tsc-node-byte-range x))) - nodes)))) - ;; Have to compute min and max like this as we might have nested functions - ;; We have to use `cl-callf byte-to-position` ot the positioning might be off for unicode chars - (cons (cl-callf byte-to-position range-min) (cl-callf byte-to-position range-max))))) - -(defun ts-fold--get-fold-range () +(defun ts-fold--current-node () + "Return the current foldable node." + (ts-fold--get-within-and-after (ts-fold--get-groups) 1 nil)) + +(defun ts-fold--range (nodes) + "" + (let* ((range-min (apply #'min + (seq-map (lambda (x) + (car (tsc-node-byte-range x))) + nodes))) + (range-max (apply #'max + (seq-map (lambda (x) + (cdr (tsc-node-byte-range x))) + nodes))) + (min (cl-callf byte-to-position range-min)) + (max (cl-callf byte-to-position range-max))) + ;; Have to compute min and max like this as we might have nested functions + ;; We have to use `cl-callf byte-to-position` ot the positioning might be off for unicode chars + (cons (1+ min) (1- max)))) + +(defun ts-fold--get-fold-range (node) "Return fold range." - (when-let* ((pt (point)) - (lang-name (alist-get major-mode ts-fold-major-mode-language-alist)) - (groups (alist-get (intern lang-name) ts-fold-groups-alist)) - (interned-groups (mapcar #'intern groups)) - (range (ts-fold--range 1 interned-groups))) - (when (and (<= (car range) pt) (<= pt (cdr range))) - (cons (1+ (car range)) (1- (cdr range)))))) + (ts-fold--range (if (listp node) node (list node)))) ;; ;; (@* "Overlays" ) @@ -353,11 +354,15 @@ Return nil otherwise." "List of interactive commands.") ;;;###autoload -(defun ts-fold-close () - "Fold the syntax node at `point` if it is foldable." +(defun ts-fold-close (&optional node) + "Fold the syntax node at `point` if it is foldable. + +Foldable nodes are defined in `ts-fold-groups-alist' for the current +`major-mode'. If no foldable NODE is found in point, do nothing." (interactive) (ts-fold--ensure-ts - (when-let ((range (ts-fold--get-fold-range))) + (when-let ((node (or node (ts-fold--current-node))) + (range (ts-fold--get-fold-range node))) ;; make sure I do not create multiple overlays for the same fold (when-let ((ov (ts-fold-overlay-at range))) (delete-overlay ov)) (ts-fold--create-overlay range)))) @@ -368,7 +373,8 @@ Return nil otherwise." If the current node is not folded or not foldable, do nothing." (interactive) (ts-fold--ensure-ts - (when-let* ((range (ts-fold--get-fold-range)) + (when-let* ((node (ts-fold--current-node)) + (range (ts-fold--get-fold-range node)) (ov (ts-fold-overlay-at range))) (delete-overlay ov)))) @@ -377,7 +383,8 @@ If the current node is not folded or not foldable, do nothing." "Open recursively folded syntax NODE that are contained in the node at point." (interactive) (ts-fold--ensure-ts - (when-let* ((range (ts-fold--get-fold-range)) + (when-let* ((node (ts-fold--current-node)) + (range (ts-fold--get-fold-range node)) (beg (car range)) (end (cdr range))) (thread-last (overlays-in beg end) @@ -389,15 +396,9 @@ If the current node is not folded or not foldable, do nothing." "Fold all foldable syntax nodes in the buffer." (interactive) (ts-fold--ensure-ts - ;; TODO: .. - (let* ((node (tsc-root-node tree-sitter-tree)) - (patterns (seq-mapcat (lambda (type) `(,(list type) @name)) - (alist-get major-mode ts-fold-foldable-node-alist) - 'vector)) - (query (tsc-make-query tree-sitter-language patterns)) - (nodes-to-fold (tsc-query-captures query node #'ignore))) + (let* ((groups (ts-fold--get-groups)) + (nodes-to-fold (ts-fold--get-nodes groups nil))) (thread-last nodes-to-fold - (mapcar #'cdr) (mapc #'ts-fold-close))))) ;;;###autoload From 978e1614e078ad2f1dbcfa4b5cfcc6f047151925 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Sat, 26 Feb 2022 00:22:29 +0800 Subject: [PATCH 08/21] Line endings --- queries/c-sharp/fold.scm | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/queries/c-sharp/fold.scm b/queries/c-sharp/fold.scm index 310f627..a467b3b 100644 --- a/queries/c-sharp/fold.scm +++ b/queries/c-sharp/fold.scm @@ -1,4 +1,4 @@ -(class_declaration +(class_declaration body: (declaration_list) @class.inner) @class.outer (struct_declaration @@ -7,7 +7,7 @@ (method_declaration body: (block) ? @function.inner) @function.outer -(lambda_expression +(lambda_expression body: (_) @function.inner) @function.outer ;; loops @@ -32,7 +32,7 @@ body: (switch_body) @conditional.inner) @conditional.outer ;; calls -(invocation_expression +(invocation_expression (argument_list) @call.inner) @call.outer ;; blocks @@ -41,19 +41,19 @@ ;; parameters ((parameter_list "," @_start . (parameter) @parameter.inner) - (#make-range! "parameter.outer" @_start @parameter.inner)) + (#make-range! "parameter.outer" @_start @parameter.inner)) ((parameter_list . (parameter) @parameter.inner . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) + (#make-range! "parameter.outer" @parameter.inner @_end)) ((argument_list "," @_start . (argument) @parameter.inner) - (#make-range! "parameter.outer" @_start @parameter.inner)) + (#make-range! "parameter.outer" @_start @parameter.inner)) ((argument_list . (argument) @parameter.inner . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) + (#make-range! "parameter.outer" @parameter.inner @_end)) ;; comments (comment) @comment.outer From 67ba0f572ab3c4b11c3a626a37283db658096b6f Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Sat, 26 Feb 2022 01:14:18 +0800 Subject: [PATCH 09/21] Fix indicators --- ts-fold-indicators.el | 23 +++++++---------------- ts-fold.el | 33 +++++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/ts-fold-indicators.el b/ts-fold-indicators.el index f6de85b..0ffea51 100644 --- a/ts-fold-indicators.el +++ b/ts-fold-indicators.el @@ -230,8 +230,8 @@ head (first line) of the region." (defun ts-fold-indicators--get-end-fringe () "Return end fringe bitmap according to variable `ts-fold-indicators-fringe'." (cl-case ts-fold-indicators-fringe - (left-fringe 'ts-fold-indicators-fr-end-left) - (right-fringe 'ts-fold-indicators-fr-end-right) + (`left-fringe 'ts-fold-indicators-fr-end-left) + (`right-fringe 'ts-fold-indicators-fr-end-right) (t (user-error "Invalid indicators fringe type: %s" ts-fold-indicators-fringe)))) (defun ts-fold-indicators--update-overlays (ov-lst folded) @@ -261,28 +261,19 @@ head (first line) of the region." (defun ts-fold-indicators--create (node) "Create indicators using NODE." - ;; TODO: - ;; (when-let* ((range (ts-fold--get-fold-range node)) - ;; (beg (car range)) (end (cdr range))) - ;; (let ((folded (ts-fold-overlay-at node))) - ;; (ts-fold-indicators--create-overlays beg end folded))) - ) + (when-let* ((range (ts-fold--get-fold-range node)) + (beg (car range)) (end (cdr range))) + (ts-fold-indicators--create-overlays beg end (ts-fold-overlay-at range)))) ;;;###autoload (defun ts-fold-indicators-refresh (&rest _) "Refresh indicators for all folding range." (when ts-fold-indicators-mode (ts-fold--ensure-ts - (when-let* ((node (tsc-root-node tree-sitter-tree)) - (patterns (seq-mapcat (lambda (type) `(,(list type) @name)) - (alist-get major-mode ts-fold-foldable-node-alist) - 'vector)) - (query (ignore-errors - (tsc-make-query tree-sitter-language patterns))) - (nodes-to-fold (tsc-query-captures query node #'ignore))) + (when-let* ((groups (ts-fold--get-groups)) + (nodes-to-fold (ts-fold--get-nodes groups nil))) (ts-fold-indicators--remove-overlays) (thread-last nodes-to-fold - (mapcar #'cdr) (mapc #'ts-fold-indicators--create)))))) (defun ts-fold-indicators--remove-overlays () diff --git a/ts-fold.el b/ts-fold.el index da5140e..a7541fa 100644 --- a/ts-fold.el +++ b/ts-fold.el @@ -10,7 +10,7 @@ ;; Description: Code folding using tree-sitter ;; Keyword: folding tree-sitter ;; Version: 0.1.0 -;; Package-Requires: ((emacs "26.1") (tree-sitter "0.15.1") (s "1.9.0") (fringe-helper "1.0.1")) +;; Package-Requires: ((emacs "26.1") (tree-sitter "0.15.1") (s "1.9.0") (fringe-helper "1.0.1") (ht "2.0")) ;; URL: https://github.com/jcs090218/ts-fold ;; This file is NOT part of GNU Emacs. @@ -42,6 +42,7 @@ (require 'seq) (require 'subr-x) +(require 'ht) (require 's) (require 'tree-sitter) @@ -94,6 +95,7 @@ (typescript-mode . "typescript")))) (setf (map-elt ts-fold-major-mode-language-alist major-mode) lang-symbol)) +;; TODO: We need to upgrade our queries. (defcustom ts-fold-groups-alist '((bash . ("function.inner" "conditional.inner" "loop.inner" "comment.outer")) (c . ("function.inner" "class.inner" "conditional.inner" "loop.inner" @@ -136,6 +138,12 @@ "Face used to display fringe contents." :group 'ts-fold) +(defvar ts-fold--query-cache (ht-create) + "Hashmap of `lang-symbol' and it's query file string.") + +(defvar ts-fold-debug nil + "Set to non-nil to enable debug mode.") + ;; ;; (@* "Externals" ) ;; @@ -216,7 +224,7 @@ TOP-LEVEL is used to mention if we should load optional inherits." ;; (defun ts-fold--get-groups () - "" + "Return the current groups from `ts-fold-groups-alist'." (let* ((lang-name (alist-get major-mode ts-fold-major-mode-language-alist)) (groups (alist-get (intern lang-name) ts-fold-groups-alist)) (interned-groups (mapcar #'intern groups))) @@ -257,8 +265,9 @@ They will be order with captures with point inside them first then the ones that follow. If a `QUERY' alist is provided, we make use of that instead of the builtin query set." (let* ((lang-name (alist-get major-mode ts-fold-major-mode-language-alist)) - (debugging-query (if query (alist-get major-mode query) - (ts-fold--get-query lang-name t))) + (debugging-query (or (ht-get ts-fold--query-cache lang-name) + (if query (alist-get major-mode query) + (ts-fold--get-query lang-name t)))) (root-node (tsc-root-node tree-sitter-tree)) (query (tsc-make-query tree-sitter-language debugging-query)) (captures (tsc-query-captures query root-node #'tsc--buffer-substring-no-properties)) @@ -266,6 +275,8 @@ instead of the builtin query set." (member (car x) group)) captures)) (nodes (seq-map #'cdr filtered-captures))) + (ht-set ts-fold--query-cache lang-name debugging-query) + (when ts-fold-debug (ts-fold-clear-cache)) (cl-remove-duplicates nodes :test (lambda (x y) @@ -286,7 +297,7 @@ instead of the builtin query set." (ts-fold--get-within-and-after (ts-fold--get-groups) 1 nil)) (defun ts-fold--range (nodes) - "" + "Return the range from NODES." (let* ((range-min (apply #'min (seq-map (lambda (x) (car (tsc-node-byte-range x))) @@ -361,8 +372,8 @@ Foldable nodes are defined in `ts-fold-groups-alist' for the current `major-mode'. If no foldable NODE is found in point, do nothing." (interactive) (ts-fold--ensure-ts - (when-let ((node (or node (ts-fold--current-node))) - (range (ts-fold--get-fold-range node))) + (when-let* ((node (or node (ts-fold--current-node))) + (range (ts-fold--get-fold-range node))) ;; make sure I do not create multiple overlays for the same fold (when-let ((ov (ts-fold-overlay-at range))) (delete-overlay ov)) (ts-fold--create-overlay range)))) @@ -416,10 +427,16 @@ If the current node is not folded or not foldable, do nothing." If the current syntax node is not foldable, do nothing." (interactive) (ts-fold--ensure-ts - (if-let* ((range (ts-fold--get-fold-range)) + (if-let* ((node (ts-fold--current-node)) + (range (ts-fold--get-fold-range node)) (ov (ts-fold-overlay-at range))) (progn (delete-overlay ov) t) (ts-fold-close)))) +(defun ts-fold-clear-cache () + "Clean up the query cache." + (interactive) + (ht-clear ts-fold--query-cache)) + (provide 'ts-fold) ;;; ts-fold.el ends here From 9d5f0930d2aa11be9e8fb7661b6ac48fc7bb1b8d Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Sat, 26 Feb 2022 01:18:13 +0800 Subject: [PATCH 10/21] Update changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 268deaa..767106a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. +## 0.2.0 +> Released N/A + +* Use queries instead (#5) + ## 0.1.0 > Released Oct 18, 2021 From 0af4c8f3b3c912536add7f44d532a05e59f51c66 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Sat, 26 Feb 2022 01:18:19 +0800 Subject: [PATCH 11/21] Bump for next release --- ts-fold.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ts-fold.el b/ts-fold.el index a7541fa..b0152f6 100644 --- a/ts-fold.el +++ b/ts-fold.el @@ -9,7 +9,7 @@ ;; Shen, Jen-Chieh ;; Description: Code folding using tree-sitter ;; Keyword: folding tree-sitter -;; Version: 0.1.0 +;; Version: 0.2.0 ;; Package-Requires: ((emacs "26.1") (tree-sitter "0.15.1") (s "1.9.0") (fringe-helper "1.0.1") (ht "2.0")) ;; URL: https://github.com/jcs090218/ts-fold From a763a9b89dad8cf7ac5efea61da96cf979585c2f Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Sat, 26 Feb 2022 01:18:56 +0800 Subject: [PATCH 12/21] Fix docstr --- ts-fold.el | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ts-fold.el b/ts-fold.el index b0152f6..db0ef1a 100644 --- a/ts-fold.el +++ b/ts-fold.el @@ -284,7 +284,8 @@ instead of the builtin query set." (= (cdr (tsc-node-byte-range x)) (cdr (tsc-node-byte-range y)))))))) (defun ts-fold--get-within-and-after (group count query) - "Given a `GROUP' `QUERY' find `COUNT' number of nodes within in and after current point." + "Given a `GROUP' `QUERY' find `COUNT' number of nodes within in and after +current point." (let* ((nodes (ts-fold--get-nodes group query)) (nodes-within (ts-fold--nodes-within nodes)) (nodes-after (ts-fold--nodes-after nodes)) From fe571b93e10c5a5b91d3b6e5e6e949e790376db5 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Sat, 26 Feb 2022 01:23:25 +0800 Subject: [PATCH 13/21] Test python --- ts-fold.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ts-fold.el b/ts-fold.el index db0ef1a..539f632 100644 --- a/ts-fold.el +++ b/ts-fold.el @@ -110,7 +110,7 @@ (javascript . ()) (julia . ()) (php . ()) - (python . ()) + (python . ("class.outer" "function.outer" "loop.outer" "block.outer")) (ruby . ()) (rust . ()) (typescript . ())) From a2dfebfcb2aa70d3a06066129275a6fd59f29753 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Sat, 26 Feb 2022 02:16:58 +0800 Subject: [PATCH 14/21] Rename util --- ts-fold-indicators.el | 4 ++-- ts-fold-summary.el | 8 ++++---- ts-fold-util.el | 28 ++++++++++++++++------------ 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/ts-fold-indicators.el b/ts-fold-indicators.el index 0ffea51..e0a712c 100644 --- a/ts-fold-indicators.el +++ b/ts-fold-indicators.el @@ -152,8 +152,8 @@ (mouse-set-point event) (beginning-of-line) (setq cur-ln (line-number-at-pos (point))) - (setq ovs (append (ts-fold-util--overlays-in 'type 'ts-fold-indicators-fr-plus) - (ts-fold-util--overlays-in 'type 'ts-fold-indicators-fr-minus-tail))) + (setq ovs (append (ts-fold--overlays-in 'type 'ts-fold-indicators-fr-plus) + (ts-fold--overlays-in 'type 'ts-fold-indicators-fr-minus-tail))) (when ovs (setq ov (cl-some (lambda (ov) (= cur-ln (line-number-at-pos (overlay-start ov)))) diff --git a/ts-fold-summary.el b/ts-fold-summary.el index 9fd179b..fc46305 100644 --- a/ts-fold-summary.el +++ b/ts-fold-summary.el @@ -93,7 +93,7 @@ type of content by checking the word boundary's existence." (defun ts-fold-summary--generic (doc-str sym) "Generic DOC-STR extraction using SYM." - (when (ts-fold-util--doc-faces-p doc-str) + (when (ts-fold--doc-faces-p doc-str) (ts-fold-summary--doc-extract doc-str sym))) (defun ts-fold-summary-batch (doc-str) @@ -138,9 +138,9 @@ type of content by checking the word boundary's existence." (defun ts-fold-summary-c-macro (doc-str) "Parse C macro summary from DOC-STR." - (when (ts-fold-util--is-face doc-str - '(font-lock-preprocessor-face - preproc-font-lock-preprocessor-background)) + (when (ts-fold--is-face doc-str + '(font-lock-preprocessor-face + preproc-font-lock-preprocessor-background)) (ts-fold-summary--doc-extract doc-str ""))) (defun ts-fold-summary-c (doc-str) diff --git a/ts-fold-util.el b/ts-fold-util.el index 6b2ee0e..fb2103b 100644 --- a/ts-fold-util.el +++ b/ts-fold-util.el @@ -29,7 +29,7 @@ ;; (@* "Cons" ) ;; -(defun ts-fold-util--cons-add (c1 c2) +(defun ts-fold--cons-add (c1 c2) "Addition for two cons C1 and C2." (cons (+ (car c1) (car c2)) (+ (cdr c1) (cdr c2)))) @@ -37,7 +37,7 @@ ;; (@* "Overlay" ) ;; -(defun ts-fold-util--overlays-in (prop name &optional beg end) +(defun ts-fold--overlays-in (prop name &optional beg end) "Return overlays with PROP of NAME, from region BEG to END." (unless beg (setq beg (point-min))) (unless end (setq end (point-max))) (let ((lst '()) (ovs (overlays-in beg end))) @@ -50,7 +50,7 @@ ;; (@* "Face" ) ;; -(defvar ts-fold-util--doc-faces +(defvar ts-fold--doc-faces '(font-lock-doc-face font-lock-comment-face font-lock-comment-delimiter-face @@ -59,30 +59,34 @@ hl-todo) "List of face that apply for document string.") -(defun ts-fold-util--get-face (obj trim) +(defun ts-fold--get-face (obj trim) "Return face name from OBJ. If argument TRIM is non-nil, trim the OBJ." (get-text-property 0 'face (if trim (string-trim obj) obj))) -(defun ts-fold-util--is-face (obj lst-face &optional trim) +(defun ts-fold--is-face (obj lst-face &optional trim) "Return non-nil if OBJ's face is define inside list LST-FACE. -Optional argument TRIM, see function `ts-fold-util--get-face'." +Optional argument TRIM, see function `ts-fold--get-face'." (unless (listp lst-face) (setq lst-face (list lst-face))) - (let ((faces (ts-fold-util--get-face obj trim))) + (let ((faces (ts-fold--get-face obj trim))) (cond ((listp faces) (cl-some (lambda (face) (memq face lst-face)) faces)) (t (memq faces lst-face))))) -(defun ts-fold-util--doc-faces-p (obj &optional trim) - "Return non-nil if face at OBJ is within `ts-fold-util--doc-faces' list. -Optional argument TRIM, see function `ts-fold-util--get-face'." - (ts-fold-util--is-face obj ts-fold-util--doc-faces trim)) +(defun ts-fold--doc-faces-p (obj &optional trim) + "Return non-nil if face at OBJ is within `ts-fold--doc-faces' list. +Optional argument TRIM, see function `ts-fold--get-face'." + (ts-fold--is-face obj ts-fold--doc-faces trim)) ;; ;; (@* "Math" ) ;; -(defun ts-fold-util--in-range-p (in-val in-min in-max) +(defun ts-folde--with-in-range (range) + "Return non-nil if point is inside the range." + (and (<= (car range) (point)) (<= (point) (cdr range)))) + +(defun ts-fold--in-range-p (in-val in-min in-max) "Check to see if IN-VAL is between IN-MIN and IN-MAX." (and (<= in-min in-val) (<= in-val in-max))) From 759dafc7fe4256fae60884414298257884349f25 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Sat, 26 Feb 2022 03:04:57 +0800 Subject: [PATCH 15/21] Rename folds --- ts-fold.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ts-fold.el b/ts-fold.el index 539f632..3723ab6 100644 --- a/ts-fold.el +++ b/ts-fold.el @@ -197,7 +197,7 @@ "Get tree sitter query for LANGUAGE. TOP-LEVEL is used to mention if we should load optional inherits." (with-temp-buffer - (when-let* ((filename (concat ts-fold--queries-dir language "/fold.scm")) + (when-let* ((filename (concat ts-fold--queries-dir language "/folds.scm")) ((file-exists-p filename))) (insert-file-contents filename) (goto-char (point-min)) From f6efbc6b85ebb9d012fd7324b8cee40cbb41666e Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Sat, 26 Feb 2022 03:09:12 +0800 Subject: [PATCH 16/21] Update folds scm --- queries/bash/fold.scm | 14 -- queries/bash/folds.scm | 8 ++ queries/beancount/folds.scm | 4 + queries/bibtex/fold.scm | 3 - queries/bibtex/folds.scm | 3 + queries/c-sharp/fold.scm | 59 -------- queries/c-sharp/folds.scm | 18 +++ queries/c/fold.scm | 75 ----------- queries/c/folds.scm | 19 +++ queries/clojure/folds.scm | 1 + queries/cmake/folds.scm | 7 + queries/commonlisp/folds.scm | 1 + queries/cpp/fold.scm | 57 -------- queries/cpp/folds.scm | 14 ++ queries/css/folds.scm | 3 + queries/cuda/{fold.scm => folds.scm} | 0 queries/d/folds.scm | 1 + queries/dart/fold.scm | 104 --------------- queries/devicetree/folds.scm | 1 + queries/ecma/folds.scm | 21 +++ queries/elixir/fold.scm | 55 -------- queries/elixir/folds.scm | 9 ++ queries/elm/fold.scm | 84 ------------ queries/fennel/fold.scm | 52 -------- queries/fish/fold.scm | 16 --- queries/fish/folds.scm | 8 ++ queries/foam/folds.scm | 7 + queries/fortran/folds.scm | 11 ++ queries/fusion/folds.scm | 6 + queries/gleam/folds.scm | 11 ++ queries/glsl/{fold.scm => folds.scm} | 0 queries/go/fold.scm | 83 ------------ queries/go/folds.scm | 13 ++ queries/godot_resource/folds.scm | 3 + queries/hcl/folds.scm | 6 + queries/heex/folds.scm | 6 + queries/hjson/folds.scm | 1 + queries/html/fold.scm | 22 --- queries/html/folds.scm | 5 + queries/java/fold.scm | 50 ------- queries/javascript/fold.scm | 81 ----------- queries/javascript/folds.scm | 1 + queries/json/folds.scm | 5 + queries/jsonc/folds.scm | 1 + queries/jsx/folds.scm | 1 + queries/julia/fold.scm | 114 ---------------- queries/julia/folds.scm | 17 +++ queries/kotlin/folds.scm | 17 +++ queries/latex/fold.scm | 28 ---- queries/latex/folds.scm | 13 ++ queries/ledger/folds.scm | 3 + queries/lua/fold.scm | 67 ---------- queries/lua/folds.scm | 10 ++ queries/markdown/fold.scm | 10 -- queries/markdown/folds.scm | 5 + queries/ninja/folds.scm | 1 + queries/nix/folds.scm | 11 ++ queries/ocaml/folds.scm | 30 +++++ queries/ocaml_interface/folds.scm | 1 + queries/pascal/folds.scm | 33 +++++ queries/perl/folds.scm | 12 ++ queries/php/fold.scm | 30 ----- queries/php/folds.scm | 6 + queries/python/fold.scm | 159 ---------------------- queries/ql/fold.scm | 13 -- queries/query/fold.scm | 11 -- queries/query/folds.scm | 6 + queries/rasi/folds.scm | 5 + queries/rst/fold.scm | 32 ----- queries/ruby/fold.scm | 193 --------------------------- queries/ruby/folds.scm | 12 ++ queries/rust/fold.scm | 137 ------------------- queries/rust/folds.scm | 25 ++++ queries/scala/fold.scm | 13 -- queries/scala/folds.scm | 15 +++ queries/sparql/folds.scm | 26 ++++ queries/supercollider/folds.scm | 7 + queries/surface/folds.scm | 6 + queries/svelte/folds.scm | 9 ++ queries/teal/folds.scm | 13 ++ queries/tlaplus/folds.scm | 5 + queries/toml/folds.scm | 5 + queries/tsx/{fold.scm => folds.scm} | 0 queries/turtle/folds.scm | 4 + queries/typescript/fold.scm | 1 - queries/typescript/folds.scm | 7 + queries/verilog/fold.scm | 19 --- queries/verilog/folds.scm | 6 + queries/vim/fold.scm | 20 --- queries/vue/fold.scm | 6 - queries/vue/folds.scm | 6 + queries/yaml/folds.scm | 3 + queries/yang/folds.scm | 3 + queries/zig/fold.scm | 55 -------- queries/zig/folds.scm | 16 +++ 95 files changed, 533 insertions(+), 1663 deletions(-) delete mode 100644 queries/bash/fold.scm create mode 100644 queries/bash/folds.scm create mode 100644 queries/beancount/folds.scm delete mode 100644 queries/bibtex/fold.scm create mode 100644 queries/bibtex/folds.scm delete mode 100644 queries/c-sharp/fold.scm create mode 100644 queries/c-sharp/folds.scm delete mode 100644 queries/c/fold.scm create mode 100644 queries/c/folds.scm create mode 100644 queries/clojure/folds.scm create mode 100644 queries/cmake/folds.scm create mode 100644 queries/commonlisp/folds.scm delete mode 100644 queries/cpp/fold.scm create mode 100644 queries/cpp/folds.scm create mode 100644 queries/css/folds.scm rename queries/cuda/{fold.scm => folds.scm} (100%) create mode 100644 queries/d/folds.scm delete mode 100644 queries/dart/fold.scm create mode 100644 queries/devicetree/folds.scm create mode 100644 queries/ecma/folds.scm delete mode 100644 queries/elixir/fold.scm create mode 100644 queries/elixir/folds.scm delete mode 100644 queries/elm/fold.scm delete mode 100644 queries/fennel/fold.scm delete mode 100644 queries/fish/fold.scm create mode 100644 queries/fish/folds.scm create mode 100644 queries/foam/folds.scm create mode 100644 queries/fortran/folds.scm create mode 100644 queries/fusion/folds.scm create mode 100644 queries/gleam/folds.scm rename queries/glsl/{fold.scm => folds.scm} (100%) delete mode 100644 queries/go/fold.scm create mode 100644 queries/go/folds.scm create mode 100644 queries/godot_resource/folds.scm create mode 100644 queries/hcl/folds.scm create mode 100644 queries/heex/folds.scm create mode 100644 queries/hjson/folds.scm delete mode 100644 queries/html/fold.scm create mode 100644 queries/html/folds.scm delete mode 100644 queries/java/fold.scm delete mode 100644 queries/javascript/fold.scm create mode 100644 queries/javascript/folds.scm create mode 100644 queries/json/folds.scm create mode 100644 queries/jsonc/folds.scm create mode 100644 queries/jsx/folds.scm delete mode 100644 queries/julia/fold.scm create mode 100644 queries/julia/folds.scm create mode 100644 queries/kotlin/folds.scm delete mode 100644 queries/latex/fold.scm create mode 100644 queries/latex/folds.scm create mode 100644 queries/ledger/folds.scm delete mode 100644 queries/lua/fold.scm create mode 100644 queries/lua/folds.scm delete mode 100644 queries/markdown/fold.scm create mode 100644 queries/markdown/folds.scm create mode 100644 queries/ninja/folds.scm create mode 100644 queries/nix/folds.scm create mode 100644 queries/ocaml/folds.scm create mode 100644 queries/ocaml_interface/folds.scm create mode 100644 queries/pascal/folds.scm create mode 100644 queries/perl/folds.scm delete mode 100644 queries/php/fold.scm create mode 100644 queries/php/folds.scm delete mode 100644 queries/python/fold.scm delete mode 100644 queries/ql/fold.scm delete mode 100644 queries/query/fold.scm create mode 100644 queries/query/folds.scm create mode 100644 queries/rasi/folds.scm delete mode 100644 queries/rst/fold.scm delete mode 100644 queries/ruby/fold.scm create mode 100644 queries/ruby/folds.scm delete mode 100644 queries/rust/fold.scm create mode 100644 queries/rust/folds.scm delete mode 100644 queries/scala/fold.scm create mode 100644 queries/scala/folds.scm create mode 100644 queries/sparql/folds.scm create mode 100644 queries/supercollider/folds.scm create mode 100644 queries/surface/folds.scm create mode 100644 queries/svelte/folds.scm create mode 100644 queries/teal/folds.scm create mode 100644 queries/tlaplus/folds.scm create mode 100644 queries/toml/folds.scm rename queries/tsx/{fold.scm => folds.scm} (100%) create mode 100644 queries/turtle/folds.scm delete mode 100644 queries/typescript/fold.scm create mode 100644 queries/typescript/folds.scm delete mode 100644 queries/verilog/fold.scm create mode 100644 queries/verilog/folds.scm delete mode 100644 queries/vim/fold.scm delete mode 100644 queries/vue/fold.scm create mode 100644 queries/vue/folds.scm create mode 100644 queries/yaml/folds.scm create mode 100644 queries/yang/folds.scm delete mode 100644 queries/zig/fold.scm create mode 100644 queries/zig/folds.scm diff --git a/queries/bash/fold.scm b/queries/bash/fold.scm deleted file mode 100644 index 160bb18..0000000 --- a/queries/bash/fold.scm +++ /dev/null @@ -1,14 +0,0 @@ -(function_definition - (_) @function.inner ) @function.outer - -(case_statement) @conditional.outer - -(if_statement - (_) @conditional.inner ) @conditional.outer - -(for_statement - (_) @loop.inner ) @loop.outer -(while_statement - (_) @loop.inner ) @loop.outer - -(comment) @comment.outer diff --git a/queries/bash/folds.scm b/queries/bash/folds.scm new file mode 100644 index 0000000..851c67e --- /dev/null +++ b/queries/bash/folds.scm @@ -0,0 +1,8 @@ +[ + (function_definition) + (if_statement) + (case_statement) + (for_statement) + (while_statement) + (c_style_for_statement) +] @fold diff --git a/queries/beancount/folds.scm b/queries/beancount/folds.scm new file mode 100644 index 0000000..b65ae01 --- /dev/null +++ b/queries/beancount/folds.scm @@ -0,0 +1,4 @@ +[ + (transaction) + (heading) +] @fold diff --git a/queries/bibtex/fold.scm b/queries/bibtex/fold.scm deleted file mode 100644 index 536826f..0000000 --- a/queries/bibtex/fold.scm +++ /dev/null @@ -1,3 +0,0 @@ -(entry) @block.outer - -(field) @statement.outer diff --git a/queries/bibtex/folds.scm b/queries/bibtex/folds.scm new file mode 100644 index 0000000..3b24d5f --- /dev/null +++ b/queries/bibtex/folds.scm @@ -0,0 +1,3 @@ +[ + (entry) +] @fold diff --git a/queries/c-sharp/fold.scm b/queries/c-sharp/fold.scm deleted file mode 100644 index a467b3b..0000000 --- a/queries/c-sharp/fold.scm +++ /dev/null @@ -1,59 +0,0 @@ -(class_declaration - body: (declaration_list) @class.inner) @class.outer - -(struct_declaration - body: (declaration_list) @class.inner) @class.outer - -(method_declaration - body: (block) ? @function.inner) @function.outer - -(lambda_expression - body: (_) @function.inner) @function.outer - -;; loops -(for_statement - body: (_) @loop.inner) @loop.outer - -(for_each_statement - body: (_) @loop.inner) @loop.outer - -(do_statement - (block) @loop.inner) @loop.outer - -(while_statement - (block) @loop.inner) @loop.outer - -;; conditionals -(if_statement - consequence: (_)? @conditional.inner - alternative: (_)? @conditional.inner) @conditional.outer - -(switch_statement - body: (switch_body) @conditional.inner) @conditional.outer - -;; calls -(invocation_expression - (argument_list) @call.inner) @call.outer - -;; blocks -(_ (block) @block.inner) @block.outer - -;; parameters -((parameter_list - "," @_start . (parameter) @parameter.inner) - (#make-range! "parameter.outer" @_start @parameter.inner)) - -((parameter_list - . (parameter) @parameter.inner . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -((argument_list - "," @_start . (argument) @parameter.inner) - (#make-range! "parameter.outer" @_start @parameter.inner)) - -((argument_list - . (argument) @parameter.inner . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -;; comments -(comment) @comment.outer diff --git a/queries/c-sharp/folds.scm b/queries/c-sharp/folds.scm new file mode 100644 index 0000000..8354c55 --- /dev/null +++ b/queries/c-sharp/folds.scm @@ -0,0 +1,18 @@ +body: [ + (declaration_list) + (switch_body) + (enum_member_declaration_list) +] @fold + +accessors: [ + (accessor_list) +] @fold + +initializer: [ + (initializer_expression) +] @fold + +(block) @fold + +;; comments +(comment) @comment.outer diff --git a/queries/c/fold.scm b/queries/c/fold.scm deleted file mode 100644 index 23691a5..0000000 --- a/queries/c/fold.scm +++ /dev/null @@ -1,75 +0,0 @@ -;; TODO: supported by official Tree-sitter if (_)* is more than one node -;; Neovim: will only match if (_) is exactly one node -;(function_definition - ;body: (compound_statement - ;("{" (_)* @function.inner "}"))?) @function.outer - -(declaration - declarator: (function_declarator)) @function.outer - -(function_definition - body: (compound_statement) @function.inner) @function.outer - -(struct_specifier - body: (_) @class.inner) @class.outer - -(enum_specifier - body: (_) @class.inner) @class.outer - -; conditional -(if_statement - consequence: (_)? @conditional.inner - alternative: (_)? @conditional.inner - ) @conditional.outer - -(if_statement - condition: (_) @conditional.inner) - -; loops -(for_statement - (_)? @loop.inner) @loop.outer -(while_statement - (_)? @loop.inner) @loop.outer -(do_statement - (_)? @loop.inner) @loop.outer - - -(compound_statement) @block.outer -(comment) @comment.outer - -(call_expression) @call.outer -(call_expression (_) @call.inner) - -; Statements - -;(expression_statement ;; this is what we actually want to capture in most cases (";" is missing) probably - ;(_) @statement.inner) ;; the other statement like node type is declaration but declaration has a ";" - -(compound_statement - (_) @statement.outer) - -(field_declaration_list - (_) @statement.outer) - -(preproc_if - (_) @statement.outer) - -(preproc_elif - (_) @statement.outer) - -(preproc_else - (_) @statement.outer) - -((parameter_list - "," @_start . (parameter_declaration) @parameter.inner) - (#make-range! "parameter.outer" @_start @parameter.inner)) -((parameter_list - . (parameter_declaration) @parameter.inner . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -((argument_list - "," @_start . (_) @parameter.inner) - (#make-range! "parameter.outer" @_start @parameter.inner)) -((argument_list - . (_) @parameter.inner . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) diff --git a/queries/c/folds.scm b/queries/c/folds.scm new file mode 100644 index 0000000..80c3039 --- /dev/null +++ b/queries/c/folds.scm @@ -0,0 +1,19 @@ +[ + (for_statement) + (if_statement) + (while_statement) + (switch_statement) + (case_statement) + (function_definition) + (struct_specifier) + (enum_specifier) + (comment) + (preproc_if) + (preproc_elif) + (preproc_else) + (preproc_ifdef) + (initializer_list) +] @fold + + (compound_statement + (compound_statement) @fold) diff --git a/queries/clojure/folds.scm b/queries/clojure/folds.scm new file mode 100644 index 0000000..d54daad --- /dev/null +++ b/queries/clojure/folds.scm @@ -0,0 +1 @@ +(source (list_lit) @fold) diff --git a/queries/cmake/folds.scm b/queries/cmake/folds.scm new file mode 100644 index 0000000..ed5cc8d --- /dev/null +++ b/queries/cmake/folds.scm @@ -0,0 +1,7 @@ +[ + (if_condition) + (foreach_loop) + (while_loop) + (function_def) + (macro_def) +] @fold diff --git a/queries/commonlisp/folds.scm b/queries/commonlisp/folds.scm new file mode 100644 index 0000000..d54daad --- /dev/null +++ b/queries/commonlisp/folds.scm @@ -0,0 +1 @@ +(source (list_lit) @fold) diff --git a/queries/cpp/fold.scm b/queries/cpp/fold.scm deleted file mode 100644 index 1b44ff0..0000000 --- a/queries/cpp/fold.scm +++ /dev/null @@ -1,57 +0,0 @@ -; inherits: c -(class_specifier - body: (_) @class.inner) @class.outer - -(field_declaration - type: (enum_specifier) - default_value: (initializer_list) @class.inner) @class.outer - -(for_range_loop - (_)? @loop.inner) @loop.outer - -(template_declaration - (function_definition) @function.outer) @function.outer.start - -(template_declaration - (struct_specifier) @class.outer) @class.outer.start - -(template_declaration - (class_specifier) @class.outer) @class.outer.start - -((lambda_capture_specifier - "," @_start . (_) @parameter.inner) - (#make-range! "parameter.outer" @_start @parameter.inner)) -((lambda_capture_specifier - . (_) @parameter.inner . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -((template_argument_list - "," @_start . (_) @parameter.inner) - (#make-range! "parameter.outer" @_start @parameter.inner)) -((template_argument_list - . (_) @parameter.inner . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -((template_parameter_list - "," @_start . (_) @parameter.inner) - (#make-range! "parameter.outer" @_start @parameter.inner)) -((template_parameter_list - . (_) @parameter.inner . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -((parameter_list - "," @_start . (optional_parameter_declaration) @parameter.inner) - (#make-range! "parameter.outer" @_start @parameter.inner)) -((parameter_list - . (optional_parameter_declaration) @parameter.inner . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -((initializer_list - "," @_start . (_) @parameter.inner @_end) - (#make-range! "parameter.outer" @_start @parameter.inner)) -((initializer_list - . (_) @parameter.inner . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -(new_expression - (argument_list) @call.inner) @call.outer diff --git a/queries/cpp/folds.scm b/queries/cpp/folds.scm new file mode 100644 index 0000000..23d623b --- /dev/null +++ b/queries/cpp/folds.scm @@ -0,0 +1,14 @@ +; inherits: c + +[ + (for_range_loop) + (class_specifier) + (field_declaration + type: (enum_specifier) + default_value: (initializer_list)) + (template_declaration) + (namespace_definition) + (try_statement) + (catch_clause) + (lambda_expression) +] @fold diff --git a/queries/css/folds.scm b/queries/css/folds.scm new file mode 100644 index 0000000..9d2995c --- /dev/null +++ b/queries/css/folds.scm @@ -0,0 +1,3 @@ +[ + (rule_set) +] @fold diff --git a/queries/cuda/fold.scm b/queries/cuda/folds.scm similarity index 100% rename from queries/cuda/fold.scm rename to queries/cuda/folds.scm diff --git a/queries/d/folds.scm b/queries/d/folds.scm new file mode 100644 index 0000000..be4dee4 --- /dev/null +++ b/queries/d/folds.scm @@ -0,0 +1 @@ +(block_statement) @fold diff --git a/queries/dart/fold.scm b/queries/dart/fold.scm deleted file mode 100644 index 31b5e85..0000000 --- a/queries/dart/fold.scm +++ /dev/null @@ -1,104 +0,0 @@ -; class -(( - [(marker_annotation)? (annotation)?] @class.outer.start . - (class_definition - body: (class_body) @_end @class.inner) @_start - ) - (#make-range! "class.outer" @_start @_end)) -(mixin_declaration (class_body) @class.inner) @class.outer -(enum_declaration - body: (enum_body) @class.inner) @class.outer -(extension_declaration - body: (extension_body) @class.inner) @class.outer - -; function/method -(( - [(marker_annotation)? (annotation)?] @function.outer.start . - [(method_signature) (function_signature)] @_start . - (function_body) @_end @function.inner - ) - (#make-range! "function.outer" @_start @_end)) -(type_alias (function_type)? @function.inner) @function.outer - -; parameter -[ - (formal_parameter) - (normal_parameter_type) - (type_parameter) -] @parameter.inner -( -"," @_start . [ - (formal_parameter) - (normal_parameter_type) - (type_parameter) - ] @_par - (#make-range! "parameter.outer" @_start @_par)) -( - [ - (formal_parameter) - (normal_parameter_type) - (type_parameter) - ] @_par . "," @_end - (#make-range! "parameter.outer" @_par @_end)) - -;; TODO: (_)* not supported yet -> for now this works correctly only with simple arguments -((arguments - . (_) @parameter.inner . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) -((arguments - "," @_start . (_) @parameter.inner) - (#make-range! "parameter.outer" @_start @parameter.inner)) - -; call -(expression_statement - (selector) @call.inner) @call.outer - -; block -(block) @block.outer - -; conditional -(if_statement - [ - condition: (_) - consequence: (_) - alternative: (_)? - ] @conditional.inner) @conditional.outer -(switch_statement - body: (switch_block) @conditional.inner) @conditional.outer -(conditional_expression - [ - consequence: (_) - alternative: (_) - ] @conditional.inner) @conditional.outer - -; loop -(for_statement - body: (block) @loop.inner) @loop.outer -(while_statement - body: (block) @loop.inner) @loop.outer -(do_statement - body: (block) @loop.inner) @loop.outer - -; comment -[ - (comment) - (documentation_comment) -] @comment.outer - -; statement -[ - (break_statement) - (do_statement) - (expression_statement) - (for_statement) - (if_statement) - (return_statement) - (switch_statement) - (while_statement) - (assert_statement) - (labeled_statement) - (yield_statement) - (yield_each_statement) - (continue_statement) - (try_statement) -] @statement.outer diff --git a/queries/devicetree/folds.scm b/queries/devicetree/folds.scm new file mode 100644 index 0000000..206c4be --- /dev/null +++ b/queries/devicetree/folds.scm @@ -0,0 +1 @@ +(node) @fold diff --git a/queries/ecma/folds.scm b/queries/ecma/folds.scm new file mode 100644 index 0000000..960b247 --- /dev/null +++ b/queries/ecma/folds.scm @@ -0,0 +1,21 @@ +[ + (for_in_statement) + (for_statement) + (while_statement) + (arrow_function) + (function) + (function_declaration) + (class_declaration) + (method_definition) + (do_statement) + (with_statement) + (switch_statement) + (switch_case) + (import_statement) + (if_statement) + (try_statement) + (catch_clause) + (object) + (generator_function) + (generator_function_declaration) +] @fold diff --git a/queries/elixir/fold.scm b/queries/elixir/fold.scm deleted file mode 100644 index 28af54b..0000000 --- a/queries/elixir/fold.scm +++ /dev/null @@ -1,55 +0,0 @@ -; Block Objects -([ - (do_block "do" . (_) @_do (_) @_end . "end") - (do_block "do" . ((_) @_do) @_end . "end") -] (#make-range! "block.inner" @_do @_end)) @block.outer - -; Class Objects (Modules, Protocols) -(call - target: ((identifier) @_identifier (#any-of? @_identifier - "defmodule" - "defprotocol" - "defimpl" - )) - (arguments (alias)) - [ - (do_block "do" . (_) @_do (_) @_end . "end") - (do_block "do" . ((_) @_do) @_end . "end") - ] - (#make-range! "class.inner" @_do @_end) -) @class.outer - -; Function, Parameter, and Call Objects -(call - target: ((identifier) @_identifier (#any-of? @_identifier - "def" - "defmacro" - "defmacrop" - "defn" - "defnp" - "defp" - )) - (arguments (call [ - (arguments (_) @parameter.inner . "," @_delimiter) - (arguments ((_) @parameter.inner) @_delimiter .) - ] (#make-range! "parameter.outer" @parameter.inner @_delimiter))) - [ - (do_block "do" . (_) @_do (_) @_end . "end") - (do_block "do" . ((_) @_do) @_end . "end") - ] - (#make-range! "function.inner" @_do @_end) -) @function.outer - -; Comment Objects -(comment) @comment.outer - -; Documentation Objects -(unary_operator - operator: "@" - operand: (call target: ((identifier) @_identifier (#any-of? @_identifier - "moduledoc" - "typedoc" - "shortdoc" - "doc" - ))) -) @comment.outer diff --git a/queries/elixir/folds.scm b/queries/elixir/folds.scm new file mode 100644 index 0000000..b99a6d9 --- /dev/null +++ b/queries/elixir/folds.scm @@ -0,0 +1,9 @@ +[ + (anonymous_function) + (arguments) + (block) + (do_block) + (list) + (map) + (tuple) +] @fold diff --git a/queries/elm/fold.scm b/queries/elm/fold.scm deleted file mode 100644 index 9609f67..0000000 --- a/queries/elm/fold.scm +++ /dev/null @@ -1,84 +0,0 @@ -;; Functions -; top level function with type annotation and doc comment -( - (module_declaration) - (block_comment) @function.outer.start - . - (type_annotation) - . - (value_declaration - body: (_)? @function.inner) @function.outer -) - -; top level function with type annotation -( - (module_declaration) - (type_annotation) @function.outer.start - . - (value_declaration - body: (_)? @function.inner) @function.outer -) - -; top level function without type annotation -( - (module_declaration) - (value_declaration - body: (_)? @function.inner) @function.outer -) - -;; Comments -[ - (block_comment) - (line_comment) -] @comment.outer - -; Conditionals -(if_else_expr - exprList: (_) - exprList: (_) @conditional.inner) @conditional.outer - -(case_of_expr - branch: (case_of_branch) @conditional.inner) @conditional.outer - -;; Parameters -; type annotations -((type_expression - (arrow) @_start . - (type_ref) @parameter.inner - ) - (#make-range! "parameter.outer" @_start @parameter.inner)) - -((type_expression - . - (type_ref) @parameter.inner - . (arrow)? @_end - ) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -; list items -((list_expr - "," @_start . - exprList: (_) @parameter.inner - ) - (#make-range! "parameter.outer" @_start @parameter.inner)) - -((list_expr - . - exprList: (_) @parameter.inner - . ","? @_end - ) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -; tuple items -((tuple_expr - "," @_start . - expr: (_) @parameter.inner - ) - (#make-range! "parameter.outer" @_start @parameter.inner)) - -((tuple_expr - . - expr: (_) @parameter.inner - . ","? @_end - ) - (#make-range! "parameter.outer" @parameter.inner @_end)) diff --git a/queries/fennel/fold.scm b/queries/fennel/fold.scm deleted file mode 100644 index 3e12681..0000000 --- a/queries/fennel/fold.scm +++ /dev/null @@ -1,52 +0,0 @@ - -; https://fennel-lang.org/reference - -(comment) @comment.outer - -(_ . "(" ")" .) @statement.outer - -; functions -((fn . name: (_)? . (parameters) . docstring: (_)? . (_) @_start . (_)* . (_)? @_end .) - (#make-range! "function.inner" @_start @_end)) @function.outer - -((lambda . name: (_)? . (parameters) . docstring: (_)? . (_) @_start . (_)* . (_)? @_end .) - (#make-range! "function.inner" @_start @_end)) @function.outer - -(hashfn ["#" "hashfn"] @function.outer.start (_) @function.inner) @function.outer - -; parameters -(parameters (_) @parameter.inner) -(parameters (_) @parameter.outer) - -; call -((list . [(multi_symbol) (symbol)] @call.inner) @call.outer - (#not-any-of? @call.inner "if" "do" "while" "for" "let" "when")) - - -; conditionals -((list . ((symbol) @_if (#any-of? @_if "if" "when")) . (_) . - (_) @_start . - (_)* . - (_)? @_end .) - (#make-range! "conditional.inner" @_start @_end)) @conditional.outer - - -; loops -((for . (for_clause) . - (_) @_start . - (_)* . - (_)? @_end .) - (#make-range! "loop.inner" @_start @_end)) @loop.outer - -((each . (each_clause) . - (_) @_start . - (_)* . - (_)? @_end .) - (#make-range! "loop.inner" @_start @_end)) @loop.outer - -((list . ((symbol) @_while (#eq? @_while "while")) . (_) . - (_) @_start . - (_)* . - (_)? @_end .) - (#make-range! "loop.inner" @_start @_end)) @loop.outer - diff --git a/queries/fish/fold.scm b/queries/fish/fold.scm deleted file mode 100644 index 1c0bf3f..0000000 --- a/queries/fish/fold.scm +++ /dev/null @@ -1,16 +0,0 @@ -(function_definition) @function.outer - -(for_statement - (_) @loop.inner) @loop.outer - -(while_statement - condition: (command) - (command) @loop.inner) @loop.outer - -(if_statement - (command) @conditional.inner) @conditional.outer - -(switch_statement - (_) @conditional.inner) @conditional.outer - -(comment) @comment.outer diff --git a/queries/fish/folds.scm b/queries/fish/folds.scm new file mode 100644 index 0000000..6075e2e --- /dev/null +++ b/queries/fish/folds.scm @@ -0,0 +1,8 @@ +[ + (function_definition) + (if_statement) + (switch_statement) + (for_statement) + (while_statement) + (begin_statement) +] @fold diff --git a/queries/foam/folds.scm b/queries/foam/folds.scm new file mode 100644 index 0000000..2e499c2 --- /dev/null +++ b/queries/foam/folds.scm @@ -0,0 +1,7 @@ +[ + (comment) + (list) + (dict_core) +] @fold + +(code (code_body)* @fold) diff --git a/queries/fortran/folds.scm b/queries/fortran/folds.scm new file mode 100644 index 0000000..a2edc64 --- /dev/null +++ b/queries/fortran/folds.scm @@ -0,0 +1,11 @@ +;; by @oponkork +[ + (if_statement) + (where_statement) + (enum_statement) + (do_loop_statement) + (derived_type_definition) + (function) + (subroutine) + (interface) +] @fold diff --git a/queries/fusion/folds.scm b/queries/fusion/folds.scm new file mode 100644 index 0000000..179fc16 --- /dev/null +++ b/queries/fusion/folds.scm @@ -0,0 +1,6 @@ +[ + (comment) + (block) + (afx_comment) + (afx_element) +] @fold diff --git a/queries/gleam/folds.scm b/queries/gleam/folds.scm new file mode 100644 index 0000000..ff05eeb --- /dev/null +++ b/queries/gleam/folds.scm @@ -0,0 +1,11 @@ +; Folds +[ + (case) + (expression_group) + (function) + (public_function) + (anonymous_function) + (type_definition) + (public_type_definition) + (public_opaque_type_definition) +] @fold diff --git a/queries/glsl/fold.scm b/queries/glsl/folds.scm similarity index 100% rename from queries/glsl/fold.scm rename to queries/glsl/folds.scm diff --git a/queries/go/fold.scm b/queries/go/fold.scm deleted file mode 100644 index ad86fa1..0000000 --- a/queries/go/fold.scm +++ /dev/null @@ -1,83 +0,0 @@ -;; function textobject -(function_declaration - body: (block)? @function.inner) @function.outer - -;; function literals -(func_literal - (_)? @function.inner) @function.outer - -;; method as function textobject -(method_declaration - body: (block)? @function.inner) @function.outer - -;; struct and interface declaration as class textobject? -(type_declaration - (type_spec (type_identifier) (struct_type (field_declaration_list (_)?) @class.inner))) @class.outer - -(type_declaration - (type_spec (type_identifier) (interface_type (method_spec_list (_)?) @class.inner))) @class.outer - -;; struct literals as class textobject -(composite_literal - (type_identifier)? - (struct_type (_))? - (literal_value (_)) @class.inner) @class.outer - -;; conditionals -(if_statement - alternative: (_ (_) @conditional.inner)?) @conditional.outer - -(if_statement - consequence: (block)? @conditional.inner) - -(if_statement - condition: (_) @conditional.inner) - -;; loops -(for_statement - body: (block)? @loop.inner) @loop.outer - -;; blocks -(_ (block) @block.inner) @block.outer - -;; statements -(block (_) @statement.outer) - -;; comments -(comment) @comment.outer - -;; calls -(call_expression (_)? @call.inner) @call.outer - -;; parameters -(parameter_list - "," @_start . - (parameter_declaration) @parameter.inner - (#make-range! "parameter.outer" @_start @parameter.inner)) -(parameter_list - . (parameter_declaration) @parameter.inner - . ","? @_end - (#make-range! "parameter.outer" @parameter.inner @_end)) - -(parameter_declaration - (identifier) - (identifier) @parameter.inner) - -(parameter_declaration - (identifier) @parameter.inner - (identifier)) - -(parameter_list - "," @_start . - (variadic_parameter_declaration) @parameter.inner - (#make-range! "parameter.outer" @_start @parameter.inner)) - -;; arguments -(argument_list - "," @_start . - (_) @parameter.inner - (#make-range! "parameter.outer" @_start @parameter.inner)) -(argument_list - . (_) @parameter.inner - . ","? @_end - (#make-range! "parameter.outer" @parameter.inner @_end)) diff --git a/queries/go/folds.scm b/queries/go/folds.scm new file mode 100644 index 0000000..c79d457 --- /dev/null +++ b/queries/go/folds.scm @@ -0,0 +1,13 @@ +[ + (const_declaration) + (expression_switch_statement) + (for_statement) + (func_literal) + (function_declaration) + (if_statement) + (import_declaration) + (method_declaration) + (type_declaration) + (var_declaration) +] @fold + diff --git a/queries/godot_resource/folds.scm b/queries/godot_resource/folds.scm new file mode 100644 index 0000000..943e062 --- /dev/null +++ b/queries/godot_resource/folds.scm @@ -0,0 +1,3 @@ +[ + (section) +] @fold diff --git a/queries/hcl/folds.scm b/queries/hcl/folds.scm new file mode 100644 index 0000000..cb20b2a --- /dev/null +++ b/queries/hcl/folds.scm @@ -0,0 +1,6 @@ +[ + (comment) + (block) + (heredoc_template) + (object) +] @fold diff --git a/queries/heex/folds.scm b/queries/heex/folds.scm new file mode 100644 index 0000000..88d4f17 --- /dev/null +++ b/queries/heex/folds.scm @@ -0,0 +1,6 @@ +; HEEx tags, components, and slots fold similar to HTML +[ + (component) + (tag) + (slot) +] @fold diff --git a/queries/hjson/folds.scm b/queries/hjson/folds.scm new file mode 100644 index 0000000..4126921 --- /dev/null +++ b/queries/hjson/folds.scm @@ -0,0 +1 @@ +; inherits: json diff --git a/queries/html/fold.scm b/queries/html/fold.scm deleted file mode 100644 index 0e6b00b..0000000 --- a/queries/html/fold.scm +++ /dev/null @@ -1,22 +0,0 @@ -(element) @function.outer - -(element (start_tag) . (_) @function.inner . (end_tag)) - -((element (start_tag) . (_) @_start (_) @_end . (end_tag)) - (#make-range! "function.inner" @_start @_end)) - -(script_element) @function.outer -(script_element (start_tag) . (_) @function.inner . (end_tag)) - -(style_element) @function.outer -(style_element (start_tag) . (_) @function.inner . (end_tag)) - -((element (start_tag (tag_name) @_tag)) @class.outer - (#match? @_tag "^(html|section|h[0-9]|header|title|head|body)$")) - -((element (start_tag (tag_name) @_tag) . (_) @class.inner . (end_tag)) - (#match? @_tag "^(html|section|h[0-9]|header|title|head|body)$")) - -((element (start_tag (tag_name) @_tag) . (_) @_start (_) @_end . (end_tag)) - (#match? @_tag "^(html|section|h[0-9]|header|title|head|body)$") - (#make-range! "class.inner" @_start @_end)) diff --git a/queries/html/folds.scm b/queries/html/folds.scm new file mode 100644 index 0000000..69b57ea --- /dev/null +++ b/queries/html/folds.scm @@ -0,0 +1,5 @@ +[ + (element) + (style_element) + (script_element) +] @fold diff --git a/queries/java/fold.scm b/queries/java/fold.scm deleted file mode 100644 index bcda707..0000000 --- a/queries/java/fold.scm +++ /dev/null @@ -1,50 +0,0 @@ -(class_declaration - body: (class_body) @class.inner) @class.outer - -(method_declaration - body: (_) @function.inner) @function.outer - -(constructor_declaration) @function.outer -(constructor_body) @function.inner - -(for_statement - body: (_)? @loop.inner) @loop.outer - -(enhanced_for_statement - body: (_)? @loop.inner) @loop.outer - -(while_statement - body: (_)? @loop.inner) @loop.outer - -(do_statement - body: (_)? @loop.inner) @loop.outer - -(if_statement - condition: (_ (parenthesized_expression) @conditional.inner) @conditional.outer) - -(if_statement - consequence: (_)? @conditional.inner - alternative: (_)? @conditional.inner - ) @conditional.outer - -(switch_expression - body: (_)? @conditional.inner) @conditional.outer - -;; blocks -(block) @block.outer - - -(method_invocation) @call.outer -(method_invocation (argument_list) @call.inner) - -;; parameters -(formal_parameters - "," @_start . - (formal_parameter) @parameter.inner - (#make-range! "parameter.outer" @_start @parameter.inner)) -(formal_parameters - . (formal_parameter) @parameter.inner - . ","? @_end - (#make-range! "parameter.outer" @_start @parameter.inner)) - -(comment) @comment.outer diff --git a/queries/javascript/fold.scm b/queries/javascript/fold.scm deleted file mode 100644 index aff02a9..0000000 --- a/queries/javascript/fold.scm +++ /dev/null @@ -1,81 +0,0 @@ -; inherits: (jsx) -(function_declaration - body: (statement_block) @function.inner) @function.outer - -(function - body: (statement_block) @function.inner) @function.outer - -(export_statement - (function_declaration) @function.outer) @function.outer.start - -(arrow_function - body: (_) @function.inner) @function.outer - -(method_definition - body: (statement_block) @function.inner) @function.outer - -(class_declaration - body: (class_body) @class.inner) @class.outer - -(export_statement - (class_declaration) @class.outer) @class.outer.start - -(for_in_statement - body: (_)? @loop.inner) @loop.outer - -(while_statement - body: (_)? @loop.inner) @loop.outer - -(do_statement - body: (_)? @loop.inner) @loop.outer - -(if_statement - consequence: (_)? @conditional.inner - alternative: (_)? @conditional.inner) @conditional.outer - -(switch_statement - body: (_)? @conditional.inner) @conditional.outer - -(call_expression) @call.outer -(call_expression (arguments) @call.inner) - -;; blocks -(_ (statement_block) @block.inner) @block.outer - -;; parameters -; function ({ x }) ... -; function ([ x ]) ... -; function (v = default_value) -(formal_parameters - "," @_start . - (_) @parameter.inner - (#make-range! "parameter.outer" @_start @parameter.inner)) -(formal_parameters - . (_) @parameter.inner - . ","? @_end - (#make-range! "parameter.outer" @parameter.inner @_end)) - -; If the array/object pattern is the first parameter, treat its elements as the argument list -(formal_parameters - . (_ - [(object_pattern "," @_start . (_) @parameter.inner) - (array_pattern "," @_start . (_) @parameter.inner)] - ) - (#make-range! "parameter.outer" @_start @parameter.inner)) -(formal_parameters - . (_ - [(object_pattern . (_) @parameter.inner . ","? @_end) - (array_pattern . (_) @parameter.inner . ","? @_end)] - ) - (#make-range! "parameter.outer" @parameter.inner @_end)) - - -;; arguments -(arguments - "," @_start . - (_) @parameter.inner - (#make-range! "parameter.outer" @_start @parameter.inner)) -(arguments - . (_) @parameter.inner - . ","? @_end - (#make-range! "parameter.outer" @parameter.inner @_end)) diff --git a/queries/javascript/folds.scm b/queries/javascript/folds.scm new file mode 100644 index 0000000..b6d9b28 --- /dev/null +++ b/queries/javascript/folds.scm @@ -0,0 +1 @@ +; inherits: ecma,jsx diff --git a/queries/json/folds.scm b/queries/json/folds.scm new file mode 100644 index 0000000..4c2b9d3 --- /dev/null +++ b/queries/json/folds.scm @@ -0,0 +1,5 @@ +[ + (pair) + (object) + (array) +] @fold diff --git a/queries/jsonc/folds.scm b/queries/jsonc/folds.scm new file mode 100644 index 0000000..4126921 --- /dev/null +++ b/queries/jsonc/folds.scm @@ -0,0 +1 @@ +; inherits: json diff --git a/queries/jsx/folds.scm b/queries/jsx/folds.scm new file mode 100644 index 0000000..93c3d9c --- /dev/null +++ b/queries/jsx/folds.scm @@ -0,0 +1 @@ +(jsx_element) @fold diff --git a/queries/julia/fold.scm b/queries/julia/fold.scm deleted file mode 100644 index 1d822f5..0000000 --- a/queries/julia/fold.scm +++ /dev/null @@ -1,114 +0,0 @@ -; Blocks -((compound_expression - . (_)? @_start - (_) @_end .) -(#make-range! "block.inner" @_start @_end)) @block.outer -((let_statement - . (_)? @_start - (_) @_end .) -(#make-range! "block.inner" @_start @_end)) @block.outer - -; Calls -(call_expression - (argument_list) @call.inner) @call.outer - -; Objects (class) -((struct_definition - name: (_) - . (_)? @_start - (_) @_end . - "end" -)(#make-range! "class.inner" @_start @_end)) @class.outer - -((struct_definition - name: (_) type_parameters: (_) - . (_)? @_start - (_) @_end . - "end" -)(#make-range! "class.inner" @_start @_end)) @class.outer - -; Comments -[(comment) (triple_string)]@comment.outer - -; Conditionals -((if_statement condition: (_) - . (_)? @_start - . - (_) @_end . - ["end" (elseif_clause) (else_clause)]) -(#make-range! "conditional.inner" @_start @_end)) @conditional.outer -((elseif_clause condition: (_) - . (_)? @_start - (_) @_end .) -(#make-range! "conditional.inner" @_start @_end)) -((else_clause - . (_)? @_start - (_) @_end .) -(#make-range! "conditional.inner" @_start @_end)) - -; Functions -(assignment_expression - (call_expression (_)) - (_) @function.inner) @function.outer - -(function_expression - [ (identifier) (parameter_list) ] - "->" - (_) @function.inner) @function.outer - -((macro_definition - name: (_) parameters: (_) - . (_)? @_start - (_) @_end . - "end" -)(#make-range! "function.inner" @_start @_end)) @function.outer - -((function_definition - name: (_) parameters: (_) - . (_)? @_start - (_) @_end . - "end" -)(#make-range! "function.inner" @_start @_end)) @function.outer - -; Loops -(for_statement - . (_)? @_start - (_) @_end . - (#make-range! "loop.inner" @_start @_end) - "end") @loop.outer -(while_statement - . (_)? @_start - (_) @_end . - (#make-range! "loop.inner" @_start @_end) - "end") @loop.outer - -; Parameters -((subscript_expression - "," @_start . - (_) @parameter.inner) - (#make-range! "parameter.outer" @_start @parameter.inner)) - -((subscript_expression - . (_) @parameter.inner - . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -((argument_list - "," @_start . - (_) @parameter.inner) -(#make-range! "parameter.outer" @_start @parameter.inner)) - -((argument_list - (_) @parameter.inner - . "," @_end) -(#make-range! "parameter.outer" @parameter.inner @_end)) - -((parameter_list - "," @_start . - (_) @parameter.inner) -(#make-range! "parameter.outer" @_start @parameter.inner)) - -((parameter_list - (_) @parameter.inner - . [","] @_end) -(#make-range! "parameter.outer" @parameter.inner @_end)) diff --git a/queries/julia/folds.scm b/queries/julia/folds.scm new file mode 100644 index 0000000..a8cfc94 --- /dev/null +++ b/queries/julia/folds.scm @@ -0,0 +1,17 @@ +[ + (module_definition) + (struct_definition) + (macro_definition) + (function_definition) + + (if_statement) + (try_statement) + (for_statement) + (while_statement) + (let_statement) + (quote_statement) + + (do_clause) + (compound_expression) +] @fold + diff --git a/queries/kotlin/folds.scm b/queries/kotlin/folds.scm new file mode 100644 index 0000000..768972b --- /dev/null +++ b/queries/kotlin/folds.scm @@ -0,0 +1,17 @@ +[ + (import_list) + + (when_expression) + (control_structure_body) + + (lambda_literal) + (function_body) + (primary_constructor) + (secondary_constructor) + (anonymous_initializer) + + (class_body) + (enum_class_body) + + (interpolated_expression) +] @fold diff --git a/queries/latex/fold.scm b/queries/latex/fold.scm deleted file mode 100644 index 886def0..0000000 --- a/queries/latex/fold.scm +++ /dev/null @@ -1,28 +0,0 @@ -((environment - . (_) - . (_) @_start - (_) @_end . (_) . -) @block.outer -(#make-range! "block.inner" @_start @_end)) - -((environment - (begin - name: (word) @_frame) - . (_) @_start - (_) @_end . (_) .) @frame.outer - (#eq? @_frame "frame") - (#make-range! "frame.inner" @_start @_end)) - -[ - (generic_command) -] @statement.outer - -[ - (chapter) - (part) - (section) - (subsection) - (subsubsection) - (paragraph) - (subparagraph) -] @class.outer diff --git a/queries/latex/folds.scm b/queries/latex/folds.scm new file mode 100644 index 0000000..6769b2e --- /dev/null +++ b/queries/latex/folds.scm @@ -0,0 +1,13 @@ +[ + (chapter) + (part) + (section) + (subsection) + (subsubsection) + (paragraph) + (subparagraph) + + (generic_environment) + (comment_environment) + (displayed_equation) +] @fold diff --git a/queries/ledger/folds.scm b/queries/ledger/folds.scm new file mode 100644 index 0000000..d807fc3 --- /dev/null +++ b/queries/ledger/folds.scm @@ -0,0 +1,3 @@ +[ + (xact) +] @fold diff --git a/queries/lua/fold.scm b/queries/lua/fold.scm deleted file mode 100644 index ca08bd0..0000000 --- a/queries/lua/fold.scm +++ /dev/null @@ -1,67 +0,0 @@ -(function) @function.outer -(local_function) @function.outer -(function_definition) @function.outer - -(for_in_statement) @loop.outer -(for_statement) @loop.outer -(while_statement) @loop.outer -(repeat_statement) @loop.outer - -(if_statement) @conditional.outer - -(function_call - (arguments) @call.inner) -(function_call) @call.outer - -(arguments - "," @_start . - (_) @parameter.inner - (#make-range! "parameter.outer" @_start @parameter.inner)) -(arguments - . (_) @parameter.inner - . ","? @_end - (#make-range! "parameter.outer" @parameter.inner @_end)) - -(parameters - "," @_start . - (_) @parameter.inner - (#make-range! "parameter.outer" @_start @parameter.inner)) -(parameters - . (_) @parameter.inner - . ","? @_end - (#make-range! "parameter.outer" @parameter.inner @_end)) - -(arguments - . (table - "," @_start . - (_) @parameter.inner - (#make-range! "parameter.outer" @_start @parameter.inner)) - . ) -(arguments - . (table - . (_) @parameter.inner - . ","? @_end - (#make-range! "parameter.outer" @parameter.inner @_end)) - . ) - -(comment) @comment.outer - -((function - . (function_name) . (parameters) . (_) @_start - (_) @_end .) - (#make-range! "function.inner" @_start @_end)) -((local_function - . (identifier) . (parameters) . (_) @_start - (_) @_end .) - (#make-range! "function.inner" @_start @_end)) -((function_definition - . (parameters) . (_) @_start - (_) @_end .) - (#make-range! "function.inner" @_start @_end)) - -((function - . (function_name) . (parameters) . (_) @function.inner .)) -((local_function - . (identifier) . (parameters) . (_) @function.inner .)) -((function_definition - . (parameters) . (_) @function.inner .)) diff --git a/queries/lua/folds.scm b/queries/lua/folds.scm new file mode 100644 index 0000000..d8f0b42 --- /dev/null +++ b/queries/lua/folds.scm @@ -0,0 +1,10 @@ +[ + (do_statement) + (while_statement) + (repeat_statement) + (if_statement) + (for_statement) + (function_declaration) + (function_definition) + (table_constructor) +] @fold diff --git a/queries/markdown/fold.scm b/queries/markdown/fold.scm deleted file mode 100644 index aa6a416..0000000 --- a/queries/markdown/fold.scm +++ /dev/null @@ -1,10 +0,0 @@ -(atx_heading (heading_content) @class.inner) @class.outer -(setext_heading (heading_content) @class.inner) @class.outer -(thematic_break) @class.outer - -(fenced_code_block (code_fence_content) @block.inner) @block.outer - -[ - (paragraph) - (list) -] @block.outer diff --git a/queries/markdown/folds.scm b/queries/markdown/folds.scm new file mode 100644 index 0000000..f6c34f7 --- /dev/null +++ b/queries/markdown/folds.scm @@ -0,0 +1,5 @@ +[ + (fenced_code_block) + (indented_code_block) + (list) +] @fold diff --git a/queries/ninja/folds.scm b/queries/ninja/folds.scm new file mode 100644 index 0000000..341a8b8 --- /dev/null +++ b/queries/ninja/folds.scm @@ -0,0 +1 @@ +(body) @fold diff --git a/queries/nix/folds.scm b/queries/nix/folds.scm new file mode 100644 index 0000000..72050aa --- /dev/null +++ b/queries/nix/folds.scm @@ -0,0 +1,11 @@ +; Nix doesn't really have blocks, so just guess what people might want folds for +[ + (if) + (with) + (let) + (function) + (attrset) + (rec_attrset) + (list) + (indented_string) +] @fold diff --git a/queries/ocaml/folds.scm b/queries/ocaml/folds.scm new file mode 100644 index 0000000..1d45dbb --- /dev/null +++ b/queries/ocaml/folds.scm @@ -0,0 +1,30 @@ +[ + (let_binding) + (external) + (type_binding) + (exception_definition) + (module_binding) + (module_type_definition) + (open_module) + (include_module) + (include_module_type) + (class_binding) + (class_type_binding) + (value_specification) + (inheritance_specification) + (instance_variable_specification) + (method_specification) + (inheritance_definition) + (instance_variable_definition) + (method_definition) + (class_initializer) + (match_case) + (attribute) + (item_attribute) + (floating_attribute) + (extension) + (item_extension) + (quoted_extension) + (quoted_item_extension) + (comment) +] @fold diff --git a/queries/ocaml_interface/folds.scm b/queries/ocaml_interface/folds.scm new file mode 100644 index 0000000..6d3dfbc --- /dev/null +++ b/queries/ocaml_interface/folds.scm @@ -0,0 +1 @@ +; inherits: ocaml diff --git a/queries/pascal/folds.scm b/queries/pascal/folds.scm new file mode 100644 index 0000000..6db2857 --- /dev/null +++ b/queries/pascal/folds.scm @@ -0,0 +1,33 @@ +[ + (interface) + (implementation) + (initialization) + (finalization) + + (if) + (ifElse) + (while) + (repeat) + (for) + (foreach) + (try) + (case) + (caseCase) + (asm) + (with) + + (declVar) + (declConst) + (declEnum) + (declProcRef) + (declExports) + (declProcRef) + (declType) + (defProc) + (declField) + (declProp) + + (comment) +] @fold + +(interface (declProc) @fold) diff --git a/queries/perl/folds.scm b/queries/perl/folds.scm new file mode 100644 index 0000000..994d569 --- /dev/null +++ b/queries/perl/folds.scm @@ -0,0 +1,12 @@ +[ + (function_definition) + (if_statement) + (unless_statement) + (while_statement) + (until_statement) + (for_statement_1) + (for_statement_2) + (foreach_statement) + (standalone_block) + (pod_statement) +] @fold diff --git a/queries/php/fold.scm b/queries/php/fold.scm deleted file mode 100644 index a14034d..0000000 --- a/queries/php/fold.scm +++ /dev/null @@ -1,30 +0,0 @@ -(function_definition - body: (compound_statement) @function.inner) @function.outer - -(method_declaration - body: (compound_statement) @function.inner) @function.outer - -(class_declaration - body: (declaration_list) @class.inner) @class.outer - -(foreach_statement - body: (_)? @loop.inner) @loop.outer - -(while_statement - body: (_)? @loop.inner) @loop.outer - -(do_statement - body: (_)? @loop.inner) @loop.outer - -(switch_statement - body: (_)? @conditional.inner) @conditional.outer - -;;blocks -(_ (switch_block) @block.inner) @block.outer - -;; parameters -(formal_parameters - (simple_parameter) @parameter.inner) - -(arguments - (argument) @parameter.inner) diff --git a/queries/php/folds.scm b/queries/php/folds.scm new file mode 100644 index 0000000..3d44782 --- /dev/null +++ b/queries/php/folds.scm @@ -0,0 +1,6 @@ +[ + (class_declaration) + (compound_statement) + (switch_statement) + (case_statement) +] @fold diff --git a/queries/python/fold.scm b/queries/python/fold.scm deleted file mode 100644 index 8aaaca4..0000000 --- a/queries/python/fold.scm +++ /dev/null @@ -1,159 +0,0 @@ -(function_definition - body: (block)? @function.inner) @function.outer - -(decorated_definition - (function_definition) @function.outer) @function.outer.start - -(class_definition - body: (block)? @class.inner) @class.outer - -(decorated_definition - (class_definition) @class.outer) @class.outer.start - -(while_statement - body: (block)? @loop.inner) @loop.outer - -(for_statement - body: (block)? @loop.inner) @loop.outer - -(if_statement - alternative: (_ (_) @conditional.inner)?) @conditional.outer - -(if_statement - consequence: (block)? @conditional.inner) - -(if_statement - condition: (_) @conditional.inner) - -(_ (block) @block.inner) @block.outer -(comment) @comment.outer - -(block (_) @statement.outer) - -(call) @call.outer -(call (_) @call.inner) - -;; Parameters - -((parameters - "," @_start . - [ - (identifier) - (tuple) - (typed_parameter) - (default_parameter) - (typed_default_parameter) - (dictionary_splat_pattern) - (list_splat_pattern) - ] @parameter.inner - ) - (#make-range! "parameter.outer" @_start @parameter.inner)) - -((parameters - . [ - (identifier) - (tuple) - (typed_parameter) - (default_parameter) - (typed_default_parameter) - (dictionary_splat_pattern) - (list_splat_pattern) - ] @parameter.inner - . ","? @_end - ) - (#make-range! "parameter.outer" @parameter.inner @_end) -) - -((lambda_parameters - "," @_start . - [ - (identifier) - (tuple) - (typed_parameter) - (default_parameter) - (typed_default_parameter) - (dictionary_splat_pattern) - (list_splat_pattern) - ] @parameter.inner - ) - (#make-range! "parameter.outer" @_start @parameter.inner)) - -((lambda_parameters - . [ - (identifier) - (tuple) - (typed_parameter) - (default_parameter) - (typed_default_parameter) - (dictionary_splat_pattern) - (list_splat_pattern) - ] @parameter.inner - . ","? @_end - ) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -((tuple - "," @_start . - (_) @parameter.inner - ) - (#make-range! "parameter.outer" @_start @parameter.inner) -) - -((tuple - "(" . - (_) @parameter.inner - . ","? @_end - ) - (#make-range! "parameter.outer" @parameter.inner @_end) -) - -((list - "," @_start . - (_) @parameter.inner - ) - (#make-range! "parameter.outer" @_start @parameter.inner) -) - -((list - . (_) @parameter.inner - . ","? @_end - ) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -((dictionary - . (pair) @parameter.inner - . ","? @_end - ) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -((dictionary - "," @_start . - (pair) @parameter.inner - ) - (#make-range! "parameter.outer" @_start @parameter.inner)) - -((argument_list - . (_) @parameter.inner - . ","? @_end - ) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -((argument_list - "," @_start . - (_) @parameter.inner - ) - (#make-range! "parameter.outer" @_start @parameter.inner)) - -((subscript - "[" . (_) @parameter.inner - . ","? @_end - ) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -((subscript - "," @_start . - (_) @parameter.inner - ) - (#make-range! "parameter.outer" @_start @parameter.inner)) - -; TODO: exclude comments using the future negate syntax from tree-sitter diff --git a/queries/ql/fold.scm b/queries/ql/fold.scm deleted file mode 100644 index 3e8a64d..0000000 --- a/queries/ql/fold.scm +++ /dev/null @@ -1,13 +0,0 @@ -;; class textobject -(dataclass (typeExpr) (_) @class.inner) @class.outer - -;; function textobject -(charpred (className) (_) @function.inner) @function.outer -(memberPredicate (body) @function.inner) @function.outer -(classlessPredicate (body) @function.inner) @function.outer - -;; scope name textobject -(dataclass name: (className) @scopename.inner) -(classlessPredicate name: (predicateName) @scopename.inner) -(memberPredicate name: (predicateName) @scopename.inner) -(charpred (className) @scopename.inner) diff --git a/queries/query/fold.scm b/queries/query/fold.scm deleted file mode 100644 index faf8c18..0000000 --- a/queries/query/fold.scm +++ /dev/null @@ -1,11 +0,0 @@ -(comment) @comment.outer - -[ - (named_node) - (grouping) - (list) -] @block.outer - -(program [(named_node) (grouping) (list)] @statement.outer) - -(parameters (_) @parameter.inner) diff --git a/queries/query/folds.scm b/queries/query/folds.scm new file mode 100644 index 0000000..47dd965 --- /dev/null +++ b/queries/query/folds.scm @@ -0,0 +1,6 @@ +[ + (named_node) + (predicate) + (grouping) + (list) +] @fold diff --git a/queries/rasi/folds.scm b/queries/rasi/folds.scm new file mode 100644 index 0000000..1557b83 --- /dev/null +++ b/queries/rasi/folds.scm @@ -0,0 +1,5 @@ +[ + (rule_set) + (list_value) + (distance_calc) + ] @fold diff --git a/queries/rst/fold.scm b/queries/rst/fold.scm deleted file mode 100644 index 80df9c5..0000000 --- a/queries/rst/fold.scm +++ /dev/null @@ -1,32 +0,0 @@ -(directive - body: (body) @function.inner) @function.outer - -(section (title) @class.inner) @class.outer - -(transition) @class.outer - -[ - (bullet_list) - (enumerated_list) - (definition_list) - (field_list) - - (literal_block) - (line_block) - (block_quote) - (doctest_block) -] @block.outer - -(footnote - body: (body) @block.inner) @block.outer - -(citation - body: (body) @block.inner) @block.outer - -(target - link: (link) @block.inner) @block.outer - -(substitution_definition - body: (directive) @block.inner) @block.outer - -(comment) @comment.outer diff --git a/queries/ruby/fold.scm b/queries/ruby/fold.scm deleted file mode 100644 index bd1385d..0000000 --- a/queries/ruby/fold.scm +++ /dev/null @@ -1,193 +0,0 @@ -;; @functions -( - [ - (method - . name: (identifier) - !parameters - . (_) @_start (_) @_end .) - - (method - . name: (identifier) - . parameters: (method_parameters) - . (_) @_start (_) @_end .) - - (singleton_method - . object: (_) - . name: (identifier) - !parameters - . (_) @_start (_) @_end .) - - (singleton_method - . object: (_) - . name: (identifier) - . parameters: (method_parameters) - . (_) @_start (_) @_end .) - ] @function.outer - (#make-range! "function.inner" @_start @_end)) - -[ - (method - . name: (identifier) - !parameters - . (_) @function.inner .) - - (method - . name: (identifier) - . parameters: (method_parameters) - . (_) @function.inner .) - - (singleton_method - . object: (_) - . name: (identifier) - !parameters - . (_) @function.inner .) - - (singleton_method - . object: (_) - . name: (identifier) - . parameters: (method_parameters) - . (_) @function.inner .) -] @function.outer - -;; @blocks -( - [ - (call - block: (_ - !parameters - . (_) @_start (_) @_end .) - ) - - (call - block: (_ - . parameters: (block_parameters) - . (_) @_start (_) @_end .) - ) - ] - (#make-range! "block.inner" @_start @_end)) - -[ - (call - block: (_ - !parameters - . (_) @block.inner .)) - - (call - block: (_ - . parameters: (block_parameters) - . (_) @block.inner .)) -] - -[ - (do_block) - (block) -] @block.outer - -((lambda - body: (_ - . (_) @_start (_) @_end .) - ) @block.outer - (#make-range! "block.inner" @_start @_end)) - -(lambda - body: (_ - . (_) @block.inner .) @block.outer) - -;; calls -(call - method: (_) @call.inner ) @call.outer - -;; classes -( - [ - (class - . name: (_) - . superclass: (_) - . (_) @_start (_) @_end .) - - (class - . name: (_) - !superclass - . (_) @_start (_) @_end .) - - (module - . name: (_) - . (_) @_start (_) @_end .) - - (singleton_class - . value: (_) - . (_) @_start (_) @_end .) - ] @class.outer - (#make-range! "class.inner" @_start @_end)) - -[ - ;; match against classes with and without parrents - (class - . name: (_) - . superclass: (_) - . (_) @class.inner .) - - (class - . name: (_) - !superclass - . (_) @class.inner .) - - (module - . name: (_) - . (_) @class.inner .) - - (singleton_class - . value: (_) - . (_) @class.inner .) -] @class.outer - -;; comments -(comment) @comment.outer - -;; conditionals -[ - (if - consequence: (_) @conditional.inner - alternative: (_) @conditional.inner) - - (if_modifier - condition: (_) @conditional.inner) - - (until_modifier - condition: (_) @conditional.inner) - - (unless - consequence: (_) @conditional.inner - alternative: (_) @conditional.inner) - - (case - value: (_) - (_) @conditional.inner) -] @conditional.outer - -;; loops -[ - (while - body: (_) @loop.inner) - - (while_modifier - condition: (_) @loop.inner) - - (until - body: (_) @loop.inner) - - (until_modifier - condition: (_) @loop.inner) - - (for - body: (_) @loop.inner) -] @loop.outer - -;; parameters -[ - (block_parameters (_) @parameter.inner) - - (method_parameters (_) @parameter.inner) - - (lambda_parameters (_) @parameter.inner) -] @parameter.outer diff --git a/queries/ruby/folds.scm b/queries/ruby/folds.scm new file mode 100644 index 0000000..3a497b3 --- /dev/null +++ b/queries/ruby/folds.scm @@ -0,0 +1,12 @@ +[ + (method) + (singleton_method) + (class) + (module) + (if) + (else) + (case) + (do_block) + (singleton_class) + (lambda) +] @fold diff --git a/queries/rust/fold.scm b/queries/rust/fold.scm deleted file mode 100644 index da3c62d..0000000 --- a/queries/rust/fold.scm +++ /dev/null @@ -1,137 +0,0 @@ -;; functions -(function_item - (_) @function.inner) @function.outer - -;; quantifies as class(es) -(struct_item - (_) @class.inner) @class.outer - -(enum_item - (_) @class.inner) @class.outer - -(union_item - (_) @class.inner) @class.outer - -(trait_item - (_) @class.inner) @class.outer - -(impl_item - (_) @class.inner) @class.outer - -(mod_item - (_) @class.inner) @class.outer - -;; conditionals -(if_expression - alternative: (_ (_) @conditional.inner)? - ) @conditional.outer - -(if_expression - alternative: (else_clause (block) @conditional.inner)) - -(if_expression - condition: (_) @conditional.inner) - -(if_expression - consequence: (block) @conditional.inner) - -(match_arm - (_)) @conditional.inner - -(match_expression) @conditional.outer - -(if_let_expression - consequence: (block)? - @conditional.inner) @conditional.outer - -(if_let_expression - alternative: (else_clause (block) @conditional.inner)) - -(if_let_expression - pattern: (_) @conditional.inner) - -;; loops -(loop_expression - (_)? @loop.inner) @loop.outer - -(while_expression - (_)? @loop.inner) @loop.outer - -(while_let_expression - (_)? @loop.inner) @loop.outer - -(for_expression - body: (block)? @loop.inner) @loop.outer - -;; blocks -(_ (block) @block.inner) @block.outer -(unsafe_block (_)? @block.inner) @block.outer - -;; calls -(call_expression (_)? @call.inner) @call.outer - -;; statements -(block (_) @statement.outer) - -;; comments -(line_comment) @comment.outer -(block_comment) @comment.outer - -;; parameter - -((parameters - "," @_start . (parameter) @parameter.inner) - (#make-range! "parameter.outer" @_start @parameter.inner)) -((parameters - . (parameter) @parameter.inner . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -((type_parameters - "," @_start . (_) @parameter.inner) - (#make-range! "parameter.outer" @_start @parameter.inner)) -((type_parameters - . (_) @parameter.inner . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -((tuple_pattern - "," @_start . (identifier) @parameter.inner) - (#make-range! "parameter.outer" @_start @parameter.inner)) -((tuple_pattern - . (identifier) @parameter.inner . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -((tuple_struct_pattern - "," @_start . (identifier) @parameter.inner) - (#make-range! "parameter.outer" @_start @parameter.inner)) -((tuple_struct_pattern - . (identifier) @parameter.inner . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -((closure_parameters - "," @_start . (_) @parameter.inner) - (#make-range! "parameter.outer" @_start @parameter.inner)) -((closure_parameters - . (_) @parameter.inner . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -((arguments - "," @_start . (_) @parameter.inner) - (#make-range! "parameter.outer" @_start @parameter.inner)) -((arguments - . (_) @parameter.inner . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -((type_arguments - "," @_start . (_) @parameter.inner) - (#make-range! "parameter.outer" @_start @parameter.inner)) -((type_arguments - . (_) @parameter.inner . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -((meta_arguments - "," @_start . (_) @parameter.inner) - (#make-range! "parameter.outer" @_start @parameter.inner)) -((meta_arguments - . (_) @parameter.inner . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) - diff --git a/queries/rust/folds.scm b/queries/rust/folds.scm new file mode 100644 index 0000000..d83351c --- /dev/null +++ b/queries/rust/folds.scm @@ -0,0 +1,25 @@ +[(mod_item) + (function_item) + (struct_item) + (trait_item) + (enum_item) + (impl_item) + (type_item) + (union_item) + + (use_declaration) + (let_declaration) + + (loop_expression) + (for_expression) + (while_expression) + (if_expression) + (if_let_expression) + (match_expression) + (call_expression) + + (macro_definition) + (macro_invocation) + + (attribute_item) +] @fold diff --git a/queries/scala/fold.scm b/queries/scala/fold.scm deleted file mode 100644 index db08856..0000000 --- a/queries/scala/fold.scm +++ /dev/null @@ -1,13 +0,0 @@ -(class_definition - body: (template_body)? @class.inner) @class.outer - -(object_definition - body: (template_body)? @class.inner) @class.outer - -(function_definition - body: (block) @function.inner) @function.outer - -(parameter - name: (identifier) @parameter.inner) @parameter.outer - -(comment) @comment.outer diff --git a/queries/scala/folds.scm b/queries/scala/folds.scm new file mode 100644 index 0000000..e748e86 --- /dev/null +++ b/queries/scala/folds.scm @@ -0,0 +1,15 @@ +(call_expression (block) @fold) + +[(class_definition) + (trait_definition) + (object_definition) + (function_definition) + (val_definition) + (import_declaration) + + (while_expression) + (do_while_expression) + (for_expression) + (try_expression) + (match_expression) +] @fold diff --git a/queries/sparql/folds.scm b/queries/sparql/folds.scm new file mode 100644 index 0000000..2b93a7b --- /dev/null +++ b/queries/sparql/folds.scm @@ -0,0 +1,26 @@ +[ + (prologue) + (select_query) + (construct_query) + (describe_query) + (ask_query) + (values_clause) + (load) + (clear) + (drop) + (add) + (move) + (copy) + (create) + (insert_data) + (delete_data) + (delete_where) + (modify) + (group_graph_pattern) + (triples_same_subject) + (where_clause) + (delete_clause) + (insert_clause) + (data_block) + (blank_node_property_list) +] @fold diff --git a/queries/supercollider/folds.scm b/queries/supercollider/folds.scm new file mode 100644 index 0000000..214247b --- /dev/null +++ b/queries/supercollider/folds.scm @@ -0,0 +1,7 @@ +[ +(function_call) +(code_block) +(function_block) +(control_structure) +] @fold + diff --git a/queries/surface/folds.scm b/queries/surface/folds.scm new file mode 100644 index 0000000..994f12e --- /dev/null +++ b/queries/surface/folds.scm @@ -0,0 +1,6 @@ +; Surface folds similar to HTML and includes blocks +[ + (tag) + (component) + (block) +] @fold diff --git a/queries/svelte/folds.scm b/queries/svelte/folds.scm new file mode 100644 index 0000000..795c32f --- /dev/null +++ b/queries/svelte/folds.scm @@ -0,0 +1,9 @@ +[ + (style_element) + (script_element) + (element) + (if_statement) + (else_statement) + (each_statement) + (await_statement) +] @fold diff --git a/queries/teal/folds.scm b/queries/teal/folds.scm new file mode 100644 index 0000000..4583057 --- /dev/null +++ b/queries/teal/folds.scm @@ -0,0 +1,13 @@ +[ +(do_statement) +(numeric_for_statement) +(generic_for_statement) +(while_statement) +(repeat_statement) +(if_statement) +(function_statement) +(record_declaration) +(enum_declaration) +(anon_function) +(table_constructor) +] @fold diff --git a/queries/tlaplus/folds.scm b/queries/tlaplus/folds.scm new file mode 100644 index 0000000..2ca0168 --- /dev/null +++ b/queries/tlaplus/folds.scm @@ -0,0 +1,5 @@ +[ + (extramodular_text) + (block_comment) + (non_terminal_proof) +] @fold diff --git a/queries/toml/folds.scm b/queries/toml/folds.scm new file mode 100644 index 0000000..a58aae4 --- /dev/null +++ b/queries/toml/folds.scm @@ -0,0 +1,5 @@ +[ + (table) + (array) + (table_array_element) +] @fold diff --git a/queries/tsx/fold.scm b/queries/tsx/folds.scm similarity index 100% rename from queries/tsx/fold.scm rename to queries/tsx/folds.scm diff --git a/queries/turtle/folds.scm b/queries/turtle/folds.scm new file mode 100644 index 0000000..863e442 --- /dev/null +++ b/queries/turtle/folds.scm @@ -0,0 +1,4 @@ +[ + (statement) + (blank_node_property_list) +] @fold diff --git a/queries/typescript/fold.scm b/queries/typescript/fold.scm deleted file mode 100644 index ff0ddfa..0000000 --- a/queries/typescript/fold.scm +++ /dev/null @@ -1 +0,0 @@ -; inherits: javascript diff --git a/queries/typescript/folds.scm b/queries/typescript/folds.scm new file mode 100644 index 0000000..dced30d --- /dev/null +++ b/queries/typescript/folds.scm @@ -0,0 +1,7 @@ +; inherits: ecma + +[ + (interface_declaration) + (internal_module) + (type_alias_declaration) +] @fold diff --git a/queries/verilog/fold.scm b/queries/verilog/fold.scm deleted file mode 100644 index 462e319..0000000 --- a/queries/verilog/fold.scm +++ /dev/null @@ -1,19 +0,0 @@ -[ - (loop_generate_construct) - (loop_statement) -] @loop.outer - -[ - (conditional_statement) - (case_item) -] @conditional.outer - -(comment) @comment.outer - -(function_declaration) @function.outer - -(always_construct) @block.outer - -[ - (module_declaration) -] @class.outer diff --git a/queries/verilog/folds.scm b/queries/verilog/folds.scm new file mode 100644 index 0000000..efc0ac6 --- /dev/null +++ b/queries/verilog/folds.scm @@ -0,0 +1,6 @@ +[ + (seq_block) + (function_body_declaration) + (task_body_declaration) + (generate_block) +] @fold diff --git a/queries/vim/fold.scm b/queries/vim/fold.scm deleted file mode 100644 index 3956809..0000000 --- a/queries/vim/fold.scm +++ /dev/null @@ -1,20 +0,0 @@ -(comment) @comment.outer - -(function_definition (body) @function.inner) @function.outer - -(parameters (identifier) @parameter.inner) - -((parameters "," @_start . (identifier) @_end) - (#make-range! "parameter.outer" @_start @_end)) - -((parameters . (identifier) @_start . "," @_end) - (#make-range! "parameter.outer" @_start @_end)) - -(if_statement (body) @conditional.inner) @conditional.outer -(for_loop (body) @loop.inner) @loop.outer -(while_loop (body) @loop.inner) @loop.outer - -(call_expression) @call.outer - -(_ (body) @block.inner) @block.outer -(body (_) @statement.outer) diff --git a/queries/vue/fold.scm b/queries/vue/fold.scm deleted file mode 100644 index 68456b7..0000000 --- a/queries/vue/fold.scm +++ /dev/null @@ -1,6 +0,0 @@ -(element) @function.outer -[ - (attribute) - (directive_attribute) -] @call.outer -(attribute_value) @parameter.outer diff --git a/queries/vue/folds.scm b/queries/vue/folds.scm new file mode 100644 index 0000000..314256e --- /dev/null +++ b/queries/vue/folds.scm @@ -0,0 +1,6 @@ +[ + (element) + (template_element) + (script_element) + (style_element) +] @fold diff --git a/queries/yaml/folds.scm b/queries/yaml/folds.scm new file mode 100644 index 0000000..f82378e --- /dev/null +++ b/queries/yaml/folds.scm @@ -0,0 +1,3 @@ +[ + (block_node) +] @fold diff --git a/queries/yang/folds.scm b/queries/yang/folds.scm new file mode 100644 index 0000000..adf0eb1 --- /dev/null +++ b/queries/yang/folds.scm @@ -0,0 +1,3 @@ +[ + (block) +] @fold diff --git a/queries/zig/fold.scm b/queries/zig/fold.scm deleted file mode 100644 index 964bf51..0000000 --- a/queries/zig/fold.scm +++ /dev/null @@ -1,55 +0,0 @@ -;; "Classes" -(VarDecl - (_ (_ (ContainerDecl) @class.inner))) @class.outer - -;; functions -(_ - (FnProto) - (Block) @function.inner) @function.outer - -;; loops -(_ - (ForPrefix) - (_) @loop.inner) @loop.outer - -(_ - (WhilePrefix) - (_) @loop.inner) @loop.outer - -;; blocks -(_ (Block) @block.inner) @block.outer - -;; statements -(Statement) @statement.outer - -;; parameters -((ParamDeclList - "," @_start . (ParamDecl) @parameter.inner) - (#make-range! "parameter.outer" @_start @parameter.inner)) -((ParamDeclList - . (ParamDecl) @parameter.inner . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -;; arguments -((FnCallArguments - "," @_start . (_) @parameter.inner) - (#make-range! "parameter.outer" @_start @parameter.inner)) -((FnCallArguments - . (_) @parameter.inner . ","? @_end) - (#make-range! "parameter.outer" @parameter.inner @_end)) - -;; comments -(doc_comment) @comment.outer -(line_comment) @comment.outer - -;; conditionals -(_ - (IfPrefix) - (_) @conditional.inner) @conditional.outer - -((SwitchExpr - "{" @_start "}" @_end) - (#make-range! "conditional.inner" @_start @_end)) @conditional.outer - -;; calls -(_ (FnCallArguments) @call.inner) @call.outer diff --git a/queries/zig/folds.scm b/queries/zig/folds.scm new file mode 100644 index 0000000..9659874 --- /dev/null +++ b/queries/zig/folds.scm @@ -0,0 +1,16 @@ +[ + (Block) + (ContainerDecl) + (SwitchExpr) + (InitList) + (AsmExpr) + (ErrorSetDecl) + (LINESTRING) + ( + [ + (IfPrefix) + (WhilePrefix) + (ForPrefix) + ] + ) +] @fold From 3a66f6286963f5a776f89b25bb100ba9e3f9bba5 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Sat, 26 Feb 2022 03:17:01 +0800 Subject: [PATCH 17/21] Remove group alsit --- queries/c-sharp/folds.scm | 2 +- ts-fold.el | 35 ++--------------------------------- 2 files changed, 3 insertions(+), 34 deletions(-) diff --git a/queries/c-sharp/folds.scm b/queries/c-sharp/folds.scm index 8354c55..21c8f61 100644 --- a/queries/c-sharp/folds.scm +++ b/queries/c-sharp/folds.scm @@ -15,4 +15,4 @@ initializer: [ (block) @fold ;; comments -(comment) @comment.outer +(comment) @comment diff --git a/ts-fold.el b/ts-fold.el index 3723ab6..d32ad49 100644 --- a/ts-fold.el +++ b/ts-fold.el @@ -95,29 +95,6 @@ (typescript-mode . "typescript")))) (setf (map-elt ts-fold-major-mode-language-alist major-mode) lang-symbol)) -;; TODO: We need to upgrade our queries. -(defcustom ts-fold-groups-alist - '((bash . ("function.inner" "conditional.inner" "loop.inner" "comment.outer")) - (c . ("function.inner" "class.inner" "conditional.inner" "loop.inner" - "block.outer" "comment.outer" "statement.inner")) - (c-sharp . ("class.inner" "function.inner" "loop.inner" "conditional.inner" - "block.inner" "comment.outer")) - (cpp . ("function.inner" "class.inner" "conditional.inner" "loop.inner" - "block.outer" "comment.outer" "statement.inner")) - (go . ()) - (html . ()) - (java . ()) - (javascript . ()) - (julia . ()) - (php . ()) - (python . ("class.outer" "function.outer" "loop.outer" "block.outer")) - (ruby . ()) - (rust . ()) - (typescript . ())) - "Alist of language symbol and groups." - :type 'list - :group 'ts-fold) - (defcustom ts-fold-mode-hook nil "Hook to run when enabling `ts-fold-mode`." :type 'hook @@ -223,13 +200,6 @@ TOP-LEVEL is used to mention if we should load optional inherits." ;; (@* "Core" ) ;; -(defun ts-fold--get-groups () - "Return the current groups from `ts-fold-groups-alist'." - (let* ((lang-name (alist-get major-mode ts-fold-major-mode-language-alist)) - (groups (alist-get (intern lang-name) ts-fold-groups-alist)) - (interned-groups (mapcar #'intern groups))) - interned-groups)) - (defun ts-fold--nodes-before (nodes) "NODES which contain the current after them." (cl-remove-if-not (lambda (x) @@ -295,7 +265,7 @@ current point." (defun ts-fold--current-node () "Return the current foldable node." - (ts-fold--get-within-and-after (ts-fold--get-groups) 1 nil)) + (ts-fold--get-within-and-after '(fold comment) 1 nil)) (defun ts-fold--range (nodes) "Return the range from NODES." @@ -408,8 +378,7 @@ If the current node is not folded or not foldable, do nothing." "Fold all foldable syntax nodes in the buffer." (interactive) (ts-fold--ensure-ts - (let* ((groups (ts-fold--get-groups)) - (nodes-to-fold (ts-fold--get-nodes groups nil))) + (let* ((nodes-to-fold (ts-fold--get-nodes '(fold comment) nil))) (thread-last nodes-to-fold (mapc #'ts-fold-close))))) From 91c2adee6e24a7bc9a74ecbe60fcf9cf2609e28c Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Sat, 26 Feb 2022 03:18:29 +0800 Subject: [PATCH 18/21] Remove group alsit --- ts-fold-indicators.el | 3 +-- ts-fold.el | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/ts-fold-indicators.el b/ts-fold-indicators.el index e0a712c..08efe1c 100644 --- a/ts-fold-indicators.el +++ b/ts-fold-indicators.el @@ -270,8 +270,7 @@ head (first line) of the region." "Refresh indicators for all folding range." (when ts-fold-indicators-mode (ts-fold--ensure-ts - (when-let* ((groups (ts-fold--get-groups)) - (nodes-to-fold (ts-fold--get-nodes groups nil))) + (when-let* ((nodes-to-fold (ts-fold--get-nodes '(fold comment) nil))) (ts-fold-indicators--remove-overlays) (thread-last nodes-to-fold (mapc #'ts-fold-indicators--create)))))) diff --git a/ts-fold.el b/ts-fold.el index d32ad49..45280b0 100644 --- a/ts-fold.el +++ b/ts-fold.el @@ -378,7 +378,7 @@ If the current node is not folded or not foldable, do nothing." "Fold all foldable syntax nodes in the buffer." (interactive) (ts-fold--ensure-ts - (let* ((nodes-to-fold (ts-fold--get-nodes '(fold comment) nil))) + (let ((nodes-to-fold (ts-fold--get-nodes '(fold comment) nil))) (thread-last nodes-to-fold (mapc #'ts-fold-close))))) From 8380fb50d4bfc7d95509b82c1670d9ec24e64ef1 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Sat, 26 Feb 2022 03:19:21 +0800 Subject: [PATCH 19/21] Use when-let instead --- ts-fold-indicators.el | 2 +- ts-fold.el | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ts-fold-indicators.el b/ts-fold-indicators.el index 08efe1c..dfd5f48 100644 --- a/ts-fold-indicators.el +++ b/ts-fold-indicators.el @@ -270,7 +270,7 @@ head (first line) of the region." "Refresh indicators for all folding range." (when ts-fold-indicators-mode (ts-fold--ensure-ts - (when-let* ((nodes-to-fold (ts-fold--get-nodes '(fold comment) nil))) + (when-let ((nodes-to-fold (ts-fold--get-nodes '(fold comment) nil))) (ts-fold-indicators--remove-overlays) (thread-last nodes-to-fold (mapc #'ts-fold-indicators--create)))))) diff --git a/ts-fold.el b/ts-fold.el index 45280b0..ea6974f 100644 --- a/ts-fold.el +++ b/ts-fold.el @@ -378,7 +378,7 @@ If the current node is not folded or not foldable, do nothing." "Fold all foldable syntax nodes in the buffer." (interactive) (ts-fold--ensure-ts - (let ((nodes-to-fold (ts-fold--get-nodes '(fold comment) nil))) + (when-let ((nodes-to-fold (ts-fold--get-nodes '(fold comment) nil))) (thread-last nodes-to-fold (mapc #'ts-fold-close))))) From 2f06803d384ce3a6496ad098ba38e8a05ba101b0 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Sat, 26 Feb 2022 03:25:37 +0800 Subject: [PATCH 20/21] Update summary --- ts-fold-summary.el | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ts-fold-summary.el b/ts-fold-summary.el index fc46305..4062676 100644 --- a/ts-fold-summary.el +++ b/ts-fold-summary.el @@ -87,9 +87,10 @@ type of content by checking the word boundary's existence." (defun ts-fold-summary--doc-extract (doc-str sym) "Default way to extract the doc summary from DOC-STR." - (let* ((lines (ts-fold-summary--extract-summary doc-str sym)) (summary (nth 0 lines))) - (when summary (setq summary (string-trim summary))) - (if (string-empty-p summary) nil summary))) + (when-let* ((lines (ts-fold-summary--extract-summary doc-str sym)) + (summary (nth 0 lines)) + (summary (string-trim summary))) + (unless (string-empty-p summary) summary))) (defun ts-fold-summary--generic (doc-str sym) "Generic DOC-STR extraction using SYM." @@ -183,15 +184,14 @@ type of content by checking the word boundary's existence." (defun ts-fold-summary--get (doc-str) "Extract summary from DOC-STR in order to display ontop of the overlay." - (let ((parser (cdr (ts-fold-summary--parser))) summary) - (when parser - (setq summary (funcall parser doc-str)) + (when-let ((parser (cdr (ts-fold-summary--parser)))) + (let ((summary (funcall parser doc-str))) (when (integerp ts-fold-summary-max-length) (setq summary (ts-fold-summary--keep-length summary))) (when summary (setq summary (ts-fold-summary--apply-format summary) - summary (propertize summary 'face 'ts-fold-replacement-face)))) - summary)) + summary (propertize summary 'face 'ts-fold-replacement-face))) + summary))) (defcustom ts-fold-summary-parsers-alist `((actionscript-mode . ts-fold-summary-javadoc) From 28451ae9bed452394ea2c9d1d83c930730a56d35 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Fri, 4 Mar 2022 19:40:58 +0800 Subject: [PATCH 21/21] Revert scm --- queries/c-sharp/folds.scm | 1 - 1 file changed, 1 deletion(-) diff --git a/queries/c-sharp/folds.scm b/queries/c-sharp/folds.scm index 21c8f61..3d5a38c 100644 --- a/queries/c-sharp/folds.scm +++ b/queries/c-sharp/folds.scm @@ -14,5 +14,4 @@ initializer: [ (block) @fold -;; comments (comment) @comment