Skip to content

Folding by custom sexp query #54

@tlkahn

Description

@tlkahn

I'm considering adding a feature that allows custom folding through a tree-sitter sexp query (validated by the tree-siter-query-builder). With some initial attempts, I couldn't get it working. Would anyone be able to take a look and see if it's possible to implement this feature? (or at least let me know what goes wrong with my approach?)

My steps:

  • Appending custom queries by vconcat with range-patterns. Please see my code here
  • In Emacs init file, you add the following snippets:
(setq ts-fold-pattern-alist
      '((python-mode . (((argument_list) @name (\#match? @name "\"[^\\n]*\\n+[^\\n]*\""))))))
  • Update the tsc--stringify-patterns function of tsc.el to:
(defun tsc--stringify-patterns (patterns)
  "Convert PATTERNS into a query string that can be passed to `tsc--make-query'."
  (cond
   ((stringp patterns) patterns)
   ((sequencep patterns)
    ;; XXX: This is hacky.
    (thread-last (mapconcat (lambda (p) (format "%S" p)) patterns "\n")
      (replace-regexp-in-string (regexp-quote "\\?") "?")
      (replace-regexp-in-string (regexp-quote "\\.") ".")
      (replace-regexp-in-string (regexp-quote "\\n") "\n")
      (replace-regexp-in-string (regexp-quote "\\#") "#")))
   (t (error "Invalid patterns"))))

Outcome:

  • Parsing seems to work. If I add (message (format patterns)) before this (line)[https://github.com/emacs-tree-sitter/elisp-tree-sitter/blob/master/core/tsc.el#L242], I will get [(function_definition) @name (class_definition) @name (list) @name (dictionary) @name (comment) @name ((argument_list) @name (#match? @name "[^\n]*\n+[^\n]*")) @name]
  • But I got this error: Error: (tsc-query-invalid-node-type "" (11 . 1) 103). It seems tsc is returning invalid nodes, which confused me. The sexp query above works well with tree-siter-query-builder.

Any thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions