]> git.eshelyaron.com Git - emacs.git/log
emacs.git
2 years ago; Replace error with signal in treesit.el
Yuan Fu [Fri, 4 Nov 2022 16:58:08 +0000 (09:58 -0700)]
; Replace error with signal in treesit.el

(or (treesit-parser-create language)
    (error "Cannot find a parser for %s" language))

is turned into

(treesit-parser-create language)

because treesit-parser-create never returns nil.  (We used to use
treesit-get-parser which don't create parser automatically.)

* lisp/treesit.el (treesit-buffer-root-node): Replace error with signal.

2 years ago; Remove unused tree-sitter functions
Yuan Fu [Fri, 4 Nov 2022 16:57:32 +0000 (09:57 -0700)]
; Remove unused tree-sitter functions

They didn't turn out to be very useful.

* lisp/treesit.el (treesit-set-ranges)
(treesit-get-ranges): Remove functions.

2 years agoAdd treesit-indent-error
Yuan Fu [Fri, 4 Nov 2022 16:56:38 +0000 (09:56 -0700)]
Add treesit-indent-error

* lisp/treesit.el (treesit-indent-error): New error.
(treesit--simple-indent-eval): Signal treesit-indent-error.

2 years agoRevise tree-sitter facility for multi-language buffers
Yuan Fu [Fri, 4 Nov 2022 08:31:56 +0000 (01:31 -0700)]
Revise tree-sitter facility for multi-language buffers

* doc/lispref/parsing.texi (Multiple Languages): Extend and update
manual.
* lisp/treesit.el (treesit-range-functions): Remove variable.
(treesit-range-settings): New variable.
(treesit-range-rules): New function.
(treesit--merge-ranges): New function.
(treesit-update-ranges): Use treesit-range-settings instead of
treesit-range-functions.
(treesit-font-lock-rules): Fix docstring.
(treesit-indent)
(treesit-indent-region): Only update ranges in a region.
* test/src/treesit-tests.el (treesit-range): New test.

2 years ago; * lisp/progmodes/js.el (js--fontify-template-string): Fix typo.
Yuan Fu [Fri, 4 Nov 2022 08:18:59 +0000 (01:18 -0700)]
; * lisp/progmodes/js.el (js--fontify-template-string): Fix typo.

2 years agoFix treesit-parser-set-included-ranges
Yuan Fu [Fri, 4 Nov 2022 05:14:50 +0000 (22:14 -0700)]
Fix treesit-parser-set-included-ranges

Track whether a parser has ranges set ourselves.  See comment in
Ftreesit_parser_included_ranges for detail.

* src/treesit.c (make_treesit_parser): Initialize has_range.
(Ftreesit_parser_set_included_ranges): Set has_range.  Fill docstring.
(Ftreesit_parser_included_ranges): Check has_range.
* src/treesit.h (Lisp_TS_Parser): Add has_range.

2 years ago; * src/treesit.c: Minor comment change.
Yuan Fu [Fri, 4 Nov 2022 02:39:43 +0000 (19:39 -0700)]
; * src/treesit.c: Minor comment change.

2 years ago; Tree-sitter: Don't refontify if entity contained in the region
Yuan Fu [Fri, 4 Nov 2022 02:37:55 +0000 (19:37 -0700)]
; Tree-sitter: Don't refontify if entity contained in the region

* lisp/treesit.el (treesit-font-lock-contextual-post-process): Only
refontify when NODE-START < region START.

2 years agoAdd contextual tree-sitter font-lock to ts-mode and js-mode
Yuan Fu [Thu, 3 Nov 2022 18:53:49 +0000 (11:53 -0700)]
Add contextual tree-sitter font-lock to ts-mode and js-mode

* lisp/progmodes/js.el (js--treesit-font-lock-settings)
* lisp/progmodes/ts-mode.el (ts-mode--font-lock-settings): Capture
commend and strings.  Add empty lines.

2 years agoMinor change to tree-sitter imenu function of js-mode
Yuan Fu [Thu, 3 Nov 2022 18:44:35 +0000 (11:44 -0700)]
Minor change to tree-sitter imenu function of js-mode

If someone don’t like the labels, they can set them to empty strings.

* lisp/progmodes/js.el (js--treesit-imenu-type-alist): Add space.
(js--treesit-imenu-label): Remove space.

2 years ago; Update guides in /admin/notes/tree-sitter
Yuan Fu [Thu, 3 Nov 2022 18:41:42 +0000 (11:41 -0700)]
; Update guides in /admin/notes/tree-sitter

* admin/notes/tree-sitter/html-manual/Language-Definitions.html
* admin/notes/tree-sitter/html-manual/Multiple-Languages.html
* admin/notes/tree-sitter/html-manual/Parser_002dbased-Font-Lock.html
* admin/notes/tree-sitter/html-manual/Parser_002dbased-Indentation.html
* admin/notes/tree-sitter/html-manual/Parsing-Program-Source.html
* admin/notes/tree-sitter/html-manual/Pattern-Matching.html
* admin/notes/tree-sitter/html-manual/Retrieving-Node.html
* admin/notes/tree-sitter/html-manual/Tree_002dsitter-C-API.html
* admin/notes/tree-sitter/html-manual/Using-Parser.html
* admin/notes/tree-sitter/starter-guide: Update to reflect changes
made recently.

2 years agoMore style fixes to tree-sitter code
Po Lu [Thu, 3 Nov 2022 11:14:16 +0000 (19:14 +0800)]
More style fixes to tree-sitter code

* src/treesit.c (treesit_node_uptodate_p):
* src/treesit.h: Fix coding style.

2 years agoMinor fixes to last tree-sitter change
Po Lu [Thu, 3 Nov 2022 11:13:18 +0000 (19:13 +0800)]
Minor fixes to last tree-sitter change

* src/treesit.c (treesit_find_override_name): Initialize
variable.
(Ftreesit_query_expand): Revert change to behavior.

2 years agoStylistic changes to tree-sitter code
Po Lu [Thu, 27 Oct 2022 13:30:23 +0000 (21:30 +0800)]
Stylistic changes to tree-sitter code

* src/treesit.c (treesit_find_override_name)
(treesit_load_language_push_for_each_suffix, treesit_load_language)
(Ftreesit_langauge_available_p, treesit_record_change)
(make_treesit_parser, make_treesit_node, make_treesit_query)
(Ftreesit_parser_create, Ftreesit_parser_delete, Ftreesit_parser_list)
(treesit_check_range_argument, Ftreesit_parser_included_ranges)
(Ftreesit_node_start, Ftreesit_node_end, Ftreesit_node_check)
(Ftreesit_node_child_by_field_name, Ftreesit_pattern_expand)
(Ftreesit_query_expand, treesit_predicates_for_pattern)
(treesit_predicate_capture_name_to_text, treesit_predicate_equal)
(treesit_predicate_match, treesit_eval_predicates)
(Ftreesit_query_capture, treesit_search_dfs, treesit_build_sparse_tree)
(syms_of_treesit): Use FOR_EACH_TAIL (or FOR_EACH_TAIL_SAFE where not
obviously safe), and check list heads and tails correctly; fix coding
style of various constructs, especially:

  variable =
    mumble (frob (bar), (foo () + bar ()) + (baz () + quux ()))

which should actually be

  variable
    = mumble (frob (bar), (foo () + bar ()) + (baz () + quux ()))

and

  foo =
    mumble (frob (bar), 0)
      + (foo () + bar ())
      + (baz () + quux ())

which should actually be

  foo = (mumble (frob (bar), 0)
 + (foo () + bar ())
 + (baz () + quux ()))

* src/treesit.h: Make declaration coding style consistent.

2 years agoCheck for outdated tree-sitter node when printing
Yuan Fu [Thu, 3 Nov 2022 00:47:14 +0000 (17:47 -0700)]
Check for outdated tree-sitter node when printing

* src/print.c (print_vectorlike): Check for outdated node.
* src/treesit.c (treesit_check_node): Use the new function.
(treesit_node_uptodate_p): New function.
* src/treesit.h: Declare new function.

2 years ago; * src/print.c (print_vectorlike): Fix comment for tree-sitter.
Yuan Fu [Thu, 3 Nov 2022 00:23:29 +0000 (17:23 -0700)]
; * src/print.c (print_vectorlike): Fix comment for tree-sitter.

2 years agoPass region start and end to tree-sitter fontification functions
Yuan Fu [Thu, 3 Nov 2022 00:03:03 +0000 (17:03 -0700)]
Pass region start and end to tree-sitter fontification functions

* doc/lispref/modes.texi (Parser-based Font Lock): Update manual.
* lisp/progmodes/js.el (js--fontify-template-string)
* lisp/progmodes/python.el (python--treesit-fontify-string): Update
function to only fontify within the region.
* lisp/treesit.el (treesit-font-lock-rules): Update docstring.
(treesit-font-lock-fontify-region): Pass START and END to
fontification functions.

2 years agoAdd handling of contextual entities in tree-sitter font-lock
Yuan Fu [Wed, 2 Nov 2022 23:31:25 +0000 (16:31 -0700)]
Add handling of contextual entities in tree-sitter font-lock

* lisp/progmodes/python.el: Remove function.
(python--treesit-settings): Capture contextual node.
* lisp/treesit.el (treesit--set-nonsticky):
(treesit-font-lock-contextual-post-process): New functions.
(treesit-font-lock-fontify-region): Change local variable START and
END to NODE-START and NODE-END, handle special capture name
"contextual".
* doc/lispref/modes.texi (Parser-based Font Lock): Update manual.

2 years agoChange signature of tree-sitter font-lock functions
Yuan Fu [Wed, 2 Nov 2022 18:26:38 +0000 (11:26 -0700)]
Change signature of tree-sitter font-lock functions

Change from

(START END NODE OVERRIDE &rest _)

to

(NODE OVERRIDE &rest _)

START and END aren't used frequently enough to justify.  If a
fontification function needs them, it can get them from NODE.

* doc/lispref/modes.texi (Parser-based Font Lock): Update manual.

* lisp/progmodes/js.el (js--fontify-template-string)
* lisp/progmodes/python.el (python--treesit-fontify-string)
(python--treesit-fontify-string-end): Change signature.

* lisp/treesit.el (treesit-font-lock-rules): Update docstring.
(treesit-font-lock-fontify-region): Remove START and END arguments.

2 years agoFix string fontification for tree-sitter python-mode
Yuan Fu [Tue, 1 Nov 2022 20:26:16 +0000 (13:26 -0700)]
Fix string fontification for tree-sitter python-mode

Now when user inserts a ending quote, the whole string is ganranteed
to be refontified and redisplayed.

* lisp/progmodes/python.el (python--treesit-fontify-string): Change
docstring, now it's called on the leading quote.
(python--treesit-fontify-string-end): New function.
(python--treesit-settings): Capture both leading and ending quote in a
string.

2 years agoDon't print buffer when printing tree-sitter node and parser
Yuan Fu [Tue, 1 Nov 2022 07:39:58 +0000 (00:39 -0700)]
Don't print buffer when printing tree-sitter node and parser

Additionally, print node's position in buffer.

* doc/lispref/parsing.texi (Retrieving Node): Reflect this change in
manual.
* src/print.c (print_vectorlike): Remove printing of buffer, add
printing of node position.

2 years ago; Tree-sitter font-lock debugging: print node type not language
Yuan Fu [Tue, 1 Nov 2022 07:26:15 +0000 (00:26 -0700)]
; Tree-sitter font-lock debugging: print node type not language

* lisp/treesit.el (treesit-font-lock-fontify-region): Print node type.

2 years agoAllow user to add/remove tree-sitter font-lock features
Yuan Fu [Tue, 1 Nov 2022 07:25:47 +0000 (00:25 -0700)]
Allow user to add/remove tree-sitter font-lock features

* lisp/treesit.el (treesit-font-lock-recompute-features): Add
ADD-LIST, REMOVE-LIST argument.

2 years agoQuery on the root node in tree-sitter font-lock
Yuan Fu [Mon, 31 Oct 2022 22:27:40 +0000 (15:27 -0700)]
Query on the root node in tree-sitter font-lock

Rather than querying on the smallest node that spans START to END, we
query on the root node between START to END.

* lisp/progmodes/python.el (python--treesit-fontify-string): Accept
the string rather than the quote node.
(python--treesit-settings): Capture the string rather than the quote
node.
* lisp/treesit.el (treesit-font-lock-fontify-region): Query the root
node rather than the smallest node.

2 years ago; Explain the BEG and END argument in treesit-query-capture better
Yuan Fu [Mon, 31 Oct 2022 22:11:44 +0000 (15:11 -0700)]
; Explain the BEG and END argument in treesit-query-capture better

* doc/lispref/parsing.texi (Pattern Matching): Update manual.
* src/treesit.c (Ftreesit_query_capture): Update docstring.

2 years agoUse treesit-fontify-with-override in tree-sitter functions
Yuan Fu [Mon, 31 Oct 2022 22:08:17 +0000 (15:08 -0700)]
Use treesit-fontify-with-override in tree-sitter functions

* lisp/progmodes/js.el (js--fontify-template-string)
* lisp/progmodes/python.el (python--treesit-fontify-string): Use
treesit-fontify-with-override.

2 years agoAdd an argument OVERRIDE to tree-sitter font-lock functions
Yuan Fu [Mon, 31 Oct 2022 22:03:49 +0000 (15:03 -0700)]
Add an argument OVERRIDE to tree-sitter font-lock functions

* doc/lispref/modes.texi (Parser-based Font Lock): Reflect this change
in manual.
* lisp/progmodes/js.el (js--fontify-template-string): Add _OVERRIDE
argument.
* lisp/progmodes/python.el (python--treesit-fontify-string): Add
_OVERRIDE argument.
* lisp/treesit.el (treesit-font-lock-rules): Update docstring.
(treesit-fontify-with-override): New function.
(treesit-font-lock-fontify-region): Extract out into
treesit-fontify-with-override.

2 years ago; * lisp/treesit.el (tresit--font-lock-verbose): Fix typo.
Yuan Fu [Mon, 31 Oct 2022 21:26:03 +0000 (14:26 -0700)]
; * lisp/treesit.el (tresit--font-lock-verbose): Fix typo.

2 years agoUse tree-sitter for font-lock's syntactic function (optionally)
Yuan Fu [Mon, 31 Oct 2022 06:59:56 +0000 (23:59 -0700)]
Use tree-sitter for font-lock's syntactic function (optionally)

Before this change, we rebind font-lock-fontify-region-function and
call font-lock's function in tree-sitter's function, and the order of
fontification is tree-sitter, font-lock syntax, font-lock regexp.

Now we make font-lock's syntax function customizable and replace it
with tree-sitter's function, and the order of fontification is
tree-sitter, font-lock regexp.

* doc/lispref/modes.texi (Parser-based Font Lock): Reflect the change
in manual.
* lisp/font-lock.el (font-lock-fontify-syntactically-function): New
varaible.
(font-lock-default-fontify-region): Call
font-lock-fontify-syntactically-function rather.
(font-lock-fontify-syntactically-region): Rename to
font-lock-default-fontify-syntactically
(font-lock-default-fontify-syntactically): Rename to this.

* lisp/treesit.el (treesit-font-lock-fontify-region): Don't call
font-lock functions.
(treesit-font-lock-enable): Remove this function.  It is not used even
before this change.
(treesit-major-mode-setup): Instead of binding
font-lock-fontify-region-function, now bind to
font-lock-fontify-syntactically-function.  And we can let font-lock do
it's thing.

2 years agoImprove python tree-sitter's string fontification
Yuan Fu [Mon, 31 Oct 2022 03:59:50 +0000 (20:59 -0700)]
Improve python tree-sitter's string fontification

* lisp/progmodes/python.el (python--treesit-fontify-string): Handle
not only f-strings, but also docstrings, and NODE is now the last
quote rather than the whole string.
(python--treesit-settings): Use python--treesit-fontify-string for
every occasion.

2 years agoAdd tresit--font-lock-verbose
Yuan Fu [Mon, 31 Oct 2022 03:56:21 +0000 (20:56 -0700)]
Add tresit--font-lock-verbose

* lisp/treesit.el (tresit--font-lock-verbose): New variable.
(treesit-font-lock-fontify-region): Take tresit--font-lock-verbose
into account.  Return jit-lock-bounds.

2 years agoSet jit-lock-contextually in treesit-major-mode-setup
Yuan Fu [Mon, 31 Oct 2022 03:53:44 +0000 (20:53 -0700)]
Set jit-lock-contextually in treesit-major-mode-setup

* lisp/treesit.el (treesit-major-mode-setup): Set jit-lock-contextually.

2 years agoSeparate python tree-sitter fontification into features
Yuan Fu [Sun, 30 Oct 2022 02:59:50 +0000 (19:59 -0700)]
Separate python tree-sitter fontification into features

Level 1: comment string function-name class-name
Level 2: keyword builtin constant type
Level 3: assignment decorator escape-sequence string-interpolation

* lisp/progmodes/python.el (python--treesit-settings): Separate.
(python-mode): Set features.

2 years agoFix byte-compile warning in treesit.el
Yuan Fu [Sat, 29 Oct 2022 21:50:25 +0000 (14:50 -0700)]
Fix byte-compile warning in treesit.el

Originally treesit-simple-indent-presets was

'((parent-is . (lambda (type)
                 `(lambda (n p b)
                    (equal (treesit-node-type p)
                           ,type)))))

Then I changed it to

`((parent-is . ,(byte-compile
                 (lambda (type)
                   (lambda (n p b)
                     (equal (treesit-node-type p)
                            type))))))

but that generates "function already compiled" warning when
byte-compiling treesit.el.  The latest form shouldn't have any problem
anymore.

* lisp/treesit.el (treesit-simple-indent-presets): Change byte-compile
to plain evaluation.

2 years agoNot return noindent in treesit-indent
Yuan Fu [Sat, 29 Oct 2022 21:49:31 +0000 (14:49 -0700)]
Not return noindent in treesit-indent

* lisp/treesit.el (treesit-indent): Don't return noindent.
(treesit-defun-type-regexp): Return (nil . nil) rather than nil.

2 years agoAdd treesit-node-top-level
Yuan Fu [Sat, 29 Oct 2022 21:11:52 +0000 (14:11 -0700)]
Add treesit-node-top-level

Merge treesit-node-top-level-p and treesit--find-top-level-match into
treesit-node-top-level.

* doc/lispref/parsing.texi (Retrieving Node): Add manual entry for it.
* lisp/progmodes/js.el (js--treesit-imenu-1): Replace
treesit-node-top-level-p with treesit-node-top-level.
* lisp/treesit.el (treesit-node-top-level-p)
(treesit--find-top-level-match): Remove functions.
(treesit-node-top-level): New function.
* lisp/treesit.el (treesit-beginning-of-defun)
(treesit-end-of-defun): Replace treesit--find-top-level-match with
treesit-node-top-level.

2 years agoReflect recent change in tree-sitter manual sections
Yuan Fu [Sat, 29 Oct 2022 20:51:45 +0000 (13:51 -0700)]
Reflect recent change in tree-sitter manual sections

* doc/lispref/modes.texi (Parser-based Font Lock):
treesit-font-lock-enable is replaced by treesit-major-mode-setup.
* doc/lispref/modes.texi (Parser-based Indentation): Mention
treesit-major-mode-setup,
* doc/lispref/parsing.texi (Tree-sitter major modes): Remove
treesit-mode and global-treesit-mode, explain setup guideline.

2 years agoFix parent-bol preset for tree-sitter indent
Yuan Fu [Sat, 29 Oct 2022 20:05:52 +0000 (13:05 -0700)]
Fix parent-bol preset for tree-sitter indent

* lisp/treesit.el (treesit-simple-indent-presets): Return point rather
than column.

2 years agoKeep cursor position in treesit-indent
Yuan Fu [Sat, 29 Oct 2022 18:50:15 +0000 (11:50 -0700)]
Keep cursor position in treesit-indent

* lisp/treesit.el (treesit-indent): Create a marker and jump back to
that marker after indentation.

2 years agoRemove hack in js-mode's tree-sitter indentation rules
Yuan Fu [Sat, 29 Oct 2022 16:50:46 +0000 (09:50 -0700)]
Remove hack in js-mode's tree-sitter indentation rules

* lisp/progmodes/js.el (js--treesit-backward-up-list): Remove
function.
(js--treesit-indent-rules): Replace with new rule.

2 years agoRefine handling of node at point in treesit-indent
Yuan Fu [Sat, 29 Oct 2022 16:43:50 +0000 (09:43 -0700)]
Refine handling of node at point in treesit-indent

* doc/lispref/modes.texi (Parser-based Indentation): Document the
semantic clearly.
* lisp/treesit.el (treesit-simple-indent-presets): Replace with
current-indentation.
(treesit--indent-1): Now NODE always starts at BOL, and parent is
always the smallest node above NODE that spans BOL.
(treesit-indent): Don't use save-excurtion, no one else does.

2 years agoFix treesit-indent-region
Yuan Fu [Sat, 29 Oct 2022 16:04:35 +0000 (09:04 -0700)]
Fix treesit-indent-region

* lisp/treesit.el (treesit-simple-indent-presets): Remove extra comma.
(treesit--indent-1): Return (ANCHOR . OFFSET) instead of column.
(treesit-indent): Accept (ANCHOR . OFFSET) from treesit--indent-1.
(treesit--indent-region-batch-size): Reduce to 400.
(treesit-indent-region): Put a marker on each line's ANCHOR and
compute the indentation with ANCHOR + OFFSET.  Precomputing column
gives wrong indentation when the ANHOR line doesn't have correct
indent yet.

2 years agoRemove test of treesit-language-at
Yuan Fu [Sat, 29 Oct 2022 03:17:43 +0000 (20:17 -0700)]
Remove test of treesit-language-at

Because now it just calls a user-supplied function rather than doing
any work by itself.

* test/src/treesit-tests.el (treesit-parser-supplemental): Remove the
test.

2 years agoByte-compile treesit-simple-indent-presets
Yuan Fu [Sat, 29 Oct 2022 00:56:05 +0000 (17:56 -0700)]
Byte-compile treesit-simple-indent-presets

* lisp/treesit.el (treesit-simple-indent-presets): Byte-compile these
functions.

2 years agoAdd treesit-indent-region
Yuan Fu [Sat, 29 Oct 2022 00:54:43 +0000 (17:54 -0700)]
Add treesit-indent-region

Now indenting a large buffer is reasonably fast.

* lisp/treesit.el (treesit-indent): Move meat to treesit--indent-1.
(treesit--indent-1)
(treesit-indent-region): New functions.
(treesit-major-mode-setup): Setup indent-region-function.

2 years agoMake Emacs compile without tree-sitter library
Yuan Fu [Fri, 28 Oct 2022 23:38:37 +0000 (16:38 -0700)]
Make Emacs compile without tree-sitter library

* lisp/treesit.el: Add function declaration forms.
* src/emacs.c: Always include syms_of_treesit.

2 years ago; Make ts-mode indent rules more compact (whitespace change)
Yuan Fu [Fri, 28 Oct 2022 20:36:42 +0000 (13:36 -0700)]
; Make ts-mode indent rules more compact (whitespace change)

* lisp/progmodes/ts-mode.el (ts-mode--indent-rules): fold two-line
rules into one line.

2 years agoAdd in new font lock features in js/ts-mode
Theodor Thornhill [Fri, 28 Oct 2022 19:23:19 +0000 (21:23 +0200)]
Add in new font lock features in js/ts-mode

We want to support font lock features on the syntactic level, not only
the decoration-level level.

New supported features are:
  - comment
  - constant
  - keyword
  - string
  - declaration
  - identifier
  - expression
  - property
  - pattern
  - jsx

* lisp/progmodes/js.el (js--treesit-font-lock-settings, js-mode): Add
in the new features.

* lisp/progmodes/ts-mode.el (ts-mode--indent-rules): Remove faulty '.'
anchor.

(ts-mode--font-lock-settings, ts-mode): Add in the new features.

2 years ago* configure.ac (emacs_config_features): Change to TREE_SITTER
Yuan Fu [Fri, 28 Oct 2022 18:06:56 +0000 (11:06 -0700)]
* configure.ac (emacs_config_features): Change to TREE_SITTER

Last commit on this line has a wrong changelog, the subject should be
emacs_config_features, not WINDOW_SYSTEM_OBJ.

2 years agoMinor doc fixes in treesit.el
Stefan Kangas [Fri, 28 Oct 2022 04:38:00 +0000 (06:38 +0200)]
Minor doc fixes in treesit.el

* lisp/treesit.el (treesit-font-lock-rules)
(treesit-font-lock-settings): Minor doc fixes.

2 years agoReflect tree-sitter indent rule presets change in manual
Yuan Fu [Fri, 28 Oct 2022 04:21:45 +0000 (21:21 -0700)]
Reflect tree-sitter indent rule presets change in manual

* doc/lispref/modes.texi (Parser-based Indentation): String
equivalence checks are changed to regexp matching.

2 years agoImprove tree-sitter indent rules in ts-mode and js-mode
Theodor Thornhill [Fri, 28 Oct 2022 04:05:02 +0000 (21:05 -0700)]
Improve tree-sitter indent rules in ts-mode and js-mode

* lisp/progmodes/js.el (js--treesit-indent-rules)
* lisp/progmodes/ts-mode.el (ts-mode--indent-rules): Add more rules.
(ts-mode): Create parser.

2 years agoAdd some js-mode tree-sitter indent rules
Yuan Fu [Fri, 28 Oct 2022 00:07:12 +0000 (17:07 -0700)]
Add some js-mode tree-sitter indent rules

* lisp/progmodes/js.el (js--treesit-indent-rules): Add rules and a let
form.

2 years ago; * lisp/treesit.el (treesit-simple-indent-presets): Add TODO.
Yuan Fu [Thu, 27 Oct 2022 23:50:20 +0000 (16:50 -0700)]
; * lisp/treesit.el (treesit-simple-indent-presets): Add TODO.

2 years agoRemove tree-sitter-indent's integration with cc-mode
Yuan Fu [Thu, 27 Oct 2022 23:44:33 +0000 (16:44 -0700)]
Remove tree-sitter-indent's integration with cc-mode

Also minor improvement of tree-sitter-indent error messaging.

* lisp/progmodes/js.el (js--treesit-indent-rules): Fix it so it works
with the new presets (which expects regexps).
(js--treesit-cc-indent-rules): Remove variable.
(js-mode): Go back to js--treesit-indent-rules.
* lisp/treesit.el (treesit-indent): Don't report error message when
ANCHOR or OFFSET is nil: let treesit-simple-indent display the
message.
(treesit-simple-indent): Remove cc-mode integration.  Display error
message when no rules matches.

2 years ago* configure.ac (WINDOW_SYSTEM_OBJ): Move tree-sitter placement.
Yuan Fu [Thu, 27 Oct 2022 22:54:04 +0000 (15:54 -0700)]
* configure.ac (WINDOW_SYSTEM_OBJ): Move tree-sitter placement.

2 years agoMinor copy-edits in tree-sitter docs
Stefan Kangas [Thu, 27 Oct 2022 12:01:49 +0000 (14:01 +0200)]
Minor copy-edits in tree-sitter docs

* doc/lispref/parsing.texi (Parsing Program Source): Improve wording.

2 years ago; Fix typos and formatting in tree-sitter docs
Stefan Kangas [Thu, 27 Oct 2022 11:03:17 +0000 (13:03 +0200)]
; Fix typos and formatting in tree-sitter docs

* doc/lispref/parsing.texi (Parsing Program Source): Fix typos and
formatting.

2 years ago; Update copyright years
Stefan Kangas [Thu, 27 Oct 2022 10:13:12 +0000 (12:13 +0200)]
; Update copyright years

* doc/lispref/parsing.texi:
* src/treesit.h:
* test/src/treesit-tests.el: Update copyright years.

2 years ago; Fix typos and formatting in treesit.[ch]
Stefan Kangas [Thu, 27 Oct 2022 10:01:04 +0000 (12:01 +0200)]
; Fix typos and formatting in treesit.[ch]

* src/treesit.c:
* src/treesit.h: Fix typos and formatting.

2 years ago; * src/treesit.c: Fix typos.
Stefan Kangas [Thu, 27 Oct 2022 06:22:16 +0000 (08:22 +0200)]
; * src/treesit.c: Fix typos.

2 years ago; * lisp/treesit.el: Minor copy-edits of documentation.
Stefan Kangas [Thu, 27 Oct 2022 06:02:58 +0000 (08:02 +0200)]
; * lisp/treesit.el: Minor copy-edits of documentation.

2 years agoPlug tree-sitter-simple-indent into c-offset-alist
Yuan Fu [Thu, 27 Oct 2022 01:31:43 +0000 (18:31 -0700)]
Plug tree-sitter-simple-indent into c-offset-alist

Now tree-sitter indentation can produce a cc-engine syntax symbol and
use c-offset-alist to compute the offset.  Catch: line-up functions
don't work with tree-sitter.

* lisp/progmodes/js.el (js--treesit-cc-indent-rules): New variable.
(js-mode): Use cc-indent rules by default.
* lisp/treesit.el (treesit-simple-indent-presets): Consider types as
regexp now.  New matchers: n-p-gp, field-is, top-level, catch-all.
New anchors: nth-sibling, grand-parent, and, or, not, list.
first-sibling now returns the actual first sibling rather than the
first named sibling.

2 years ago* lisp/treesit.el (treesit-node-top-level-p): New argument TYPE.
Yuan Fu [Thu, 27 Oct 2022 01:31:07 +0000 (18:31 -0700)]
* lisp/treesit.el (treesit-node-top-level-p): New argument TYPE.

2 years agoGeneralize js--treesit-imenu-top-level-p
Yuan Fu [Thu, 27 Oct 2022 01:06:07 +0000 (18:06 -0700)]
Generalize js--treesit-imenu-top-level-p

* lisp/progmodes/js.el (js--treesit-imenu-top-level-p): Move.
(js--treesit-font-lock-settings): Fix usage.
* lisp/treesit.el (treesit-node-top-level-p): Move to here.

2 years agoAdd tree-sitter navigation support to python-mode
Yuan Fu [Wed, 26 Oct 2022 22:38:40 +0000 (15:38 -0700)]
Add tree-sitter navigation support to python-mode

* lisp/progmodes/python.el (python-mode): Set
treesit-defun-type-regexp.

2 years agoAdd tree-sitter imenu support for js-mode and ts-mode
Yuan Fu [Wed, 26 Oct 2022 22:36:15 +0000 (15:36 -0700)]
Add tree-sitter imenu support for js-mode and ts-mode

js-mode's current imenu is pretty plain and incomplete, so I took the
liberty to add a bit more flair to it.

* lisp/progmodes/js.el (js--treesit-imenu-type-alist): New variable.
(js--treesit-imenu-top-level-p)
(js--treesit-imenu-label)
(js--treesit-imenu-1)
(js--treesit-imenu): New functions.

2 years agoFix tree-sitter navigation
Yuan Fu [Wed, 26 Oct 2022 22:34:02 +0000 (15:34 -0700)]
Fix tree-sitter navigation

* lisp/treesit.el (treesit-search-forward-goto): Track current
position with a local variable instead of (point).  Clean up the
condition forms.
(treesit-beginning-of-defun): Fix traverse direction.

2 years ago* src/treesit.c (treesit_search_dfs): Fix traverse algorithm.
Yuan Fu [Wed, 26 Oct 2022 05:48:30 +0000 (22:48 -0700)]
* src/treesit.c (treesit_search_dfs): Fix traverse algorithm.

2 years ago; * src/treesit.c (treesit_search_forward): Fix comment.
Yuan Fu [Wed, 26 Oct 2022 03:28:40 +0000 (20:28 -0700)]
; * src/treesit.c (treesit_search_forward): Fix comment.

2 years ago* src/treesit.c (treesit_search_forward): Fix traverses algorithm.
Yuan Fu [Wed, 26 Oct 2022 02:49:03 +0000 (19:49 -0700)]
* src/treesit.c (treesit_search_forward): Fix traverses algorithm.

2 years agoDon't disable parse cache in tree-sitter activated js-mode
Yuan Fu [Wed, 26 Oct 2022 02:46:58 +0000 (19:46 -0700)]
Don't disable parse cache in tree-sitter activated js-mode

* lisp/progmodes/js.el (js-mode): Move parse cache setup back.

2 years ago* lisp/treesit.el (treesit-simple-indent-rules): Make buffer-local.
Yuan Fu [Tue, 25 Oct 2022 21:27:23 +0000 (14:27 -0700)]
* lisp/treesit.el (treesit-simple-indent-rules): Make buffer-local.

2 years agoFix treesit-language-at
Yuan Fu [Tue, 25 Oct 2022 21:26:41 +0000 (14:26 -0700)]
Fix treesit-language-at

* lisp/treesit.el (treesit-language-at-point-function): New local
variable.
(treesit-language-at): Change to use
treesit-language-at-point-function rather than trying each parser
one-by-one.
* doc/lispref/parsing.texi (Multiple Languages): Update manual.

2 years agoNew tree-sitter toggle scheme
Yuan Fu [Tue, 25 Oct 2022 20:54:12 +0000 (13:54 -0700)]
New tree-sitter toggle scheme

This version: central variable, everything controlled by
treesit-settings.  Major mode sets up tree-sitter/non-tree-sitter
in a conditional branch, based on the setting.

* lisp/treesit.el (treesit-settings): New option.
(treesit-defun-type-regexp): Change docstring.
(treesit-mode-supported)
(treesit-required-languages)
(treesit--local-variable-backup): Remove variables.
(treesit--backup-local-variable)
(treesit-mode)
(global-treesit-mode--turn-on)
(global-treesit-mode): Remove functions.
(treesit--setting-for-mode): New function.
(treesit-ready-p): New argument MODE, changed REPORT to QUIET, and
LANGUAGEs to LANGUAGE (now it can be a single symbol or a list of
them).
(treesit-major-mode-setup): New function.  Mostly comes from
treesit-mode.

* test/src/treesit-tests.el (treesit-misc): New test.

* lisp/progmodes/python.el (python-mode): Move some setup code into
the conditional branch at the end.

* lisp/progmodes/js.el (js-json-mode)
(js-mode): Move some setup code into the conditional branch at the
end.

* lisp/progmodes/ts-mode.el: Move tree-sitter setup into the
conditional branch.

2 years ago* lisp/progmodes/ts-mode.el (ts-mode): Fix font-lock setup.
Yuan Fu [Mon, 24 Oct 2022 22:02:41 +0000 (15:02 -0700)]
* lisp/progmodes/ts-mode.el (ts-mode): Fix font-lock setup.

2 years ago* lisp/treesit.el (treesit-node-at): Remove argument LARGEST.
Yuan Fu [Mon, 24 Oct 2022 01:54:12 +0000 (18:54 -0700)]
* lisp/treesit.el (treesit-node-at): Remove argument LARGEST.

LARGEST returns the root node at BOB, finding the smallest node at
point and search upward is a better practice.

2 years ago* lisp/treesit.el (treesit-parser-range-on): New function.
Yuan Fu [Mon, 24 Oct 2022 01:51:35 +0000 (18:51 -0700)]
* lisp/treesit.el (treesit-parser-range-on): New function.

2 years agoFix treesit-beginning/end-of-defun
Yuan Fu [Mon, 24 Oct 2022 01:47:02 +0000 (18:47 -0700)]
Fix treesit-beginning/end-of-defun

Now they always move to the top-level defun (i.e., skips nested ones).

* lisp/treesit.el (treesit--find-top-level-match): New function
(treesit-beginning-of-defun): Goes to the top-level match.
(treesit-end-of-defun): Simply move to the end of current match.

2 years agoMake treesit-search-forward-goto accept a NODE argument
Yuan Fu [Mon, 24 Oct 2022 01:29:02 +0000 (18:29 -0700)]
Make treesit-search-forward-goto accept a NODE argument

With NODE argument we can do

(setq node (treesit-search-forward-goto node))

And we can choose what node to pass to it (maybe we want to pass it
the largest node at point, rather than the smallest node, and in case
of multiple parsers, we can choose which parser to use).

* doc/lispref/parsing.texi (Retrieving Node): Update manual.
* lisp/treesit.el (treesit-search-forward-goto): Accept a NODE
argument.

2 years agoMake treesit-node-at return the last leaf node at EOB
Yuan Fu [Mon, 24 Oct 2022 01:23:17 +0000 (18:23 -0700)]
Make treesit-node-at return the last leaf node at EOB

* doc/lispref/parsing.texi (Retrieving Node): Update manual.
* lisp/treesit.el (treesit-node-at): Add a (when node) form just to be
explicit, return the last leaf node if there is no node after POS.

2 years agoMake treesit-node-child and fiends accept negative index
Yuan Fu [Mon, 24 Oct 2022 01:05:10 +0000 (18:05 -0700)]
Make treesit-node-child and fiends accept negative index

* doc/lispref/parsing.texi (Retrieving Node): Update manual.
* src/treesit.c (Ftreesit_node_child)
(Ftreesit_node_field_name_for_child): Accept and process negative
index.

2 years ago; * lisp/treesit.el (treesit-range-functions): Fix typo.
Yuan Fu [Sun, 23 Oct 2022 23:45:54 +0000 (16:45 -0700)]
; * lisp/treesit.el (treesit-range-functions): Fix typo.

2 years ago* lisp/treesit.el (treesit-node-at): Add LARGEST argument.
Yuan Fu [Sun, 23 Oct 2022 23:16:27 +0000 (16:16 -0700)]
* lisp/treesit.el (treesit-node-at): Add LARGEST argument.

2 years agoChange function signiture of treesit search functions
Yuan Fu [Sun, 23 Oct 2022 21:56:17 +0000 (14:56 -0700)]
Change function signiture of treesit search functions

Justification: We want to make the SIDE argument in
treesit-search-forward-goto optional, so I changed it to START.
It makes more sense for BACKWARD to follow START so two common
case, search for end forward and beginning backwards are

(treesit-search-forward-goto node pred)
(treesit-search-forward-goto node pred t t)

Then since we swapped BACKWARD and ALL for
treesit-search-forward-goto, it's best to swap them for
treesit-search-forward and treesit-search-subtree, too.  And BACKWARD
will probably be used more frequently than ALL anyway.

* doc/lispref/parsing.texi (Retrieving Node): Resolve FIXME and update
function signitures.
* lisp/treesit.el (treesit-search-forward-goto): Change SIDE to
START, swap BACKWARD and ALL.
(treesit-beginning-of-defun)
(treesit-end-of-defun): Update use of treesit-search-forward-goto
* src/treesit.c (Ftreesit_search_subtree)
(Ftreesit_search_forward): Swap BACKWARD and ALL.

2 years ago; Resolve FIXME's in tree-sitter manual sections
Yuan Fu [Sun, 23 Oct 2022 21:23:59 +0000 (14:23 -0700)]
; Resolve FIXME's in tree-sitter manual sections

* doc/lispref/modes.texi (Parser-based Indentation):
* doc/lispref/parsing.texi (Retrieving Node): Resolve FIXME's.
* src/treesit.c: Update tree graph in docstring.

2 years ago; Minor copyedits of tree-sitter stuff in ELisp manual
Eli Zaretskii [Sun, 23 Oct 2022 08:14:18 +0000 (11:14 +0300)]
; Minor copyedits of tree-sitter stuff in ELisp manual

* doc/lispref/parsing.texi (Pattern Matching):
* doc/lispref/modes.texi (Parser-based Font Lock)
(Parser-based Indentation): Fix wording and typos.

2 years agoFix infinite loop in treesit-search-forward-goto
Yuan Fu [Sun, 23 Oct 2022 05:50:56 +0000 (22:50 -0700)]
Fix infinite loop in treesit-search-forward-goto

* lisp/treesit.el (treesit-search-forward-goto): Remove UP argument.
* src/treesit.c (treesit_traverse_child_helper): New function.
(treesit_search_forward): Remove UP_ONLY and SKIP_START argument.
Don't traverse subtree of START.  And after we've found the next
sibling/parent, go down to the first leaf node.  Also change recursion
to loop.
(Ftreesit_search_forward): Change docstring, remove UP argument.

2 years agoResolve FIXME's in tree-sitter manual sections
Yuan Fu [Sun, 23 Oct 2022 01:44:11 +0000 (18:44 -0700)]
Resolve FIXME's in tree-sitter manual sections

Pattern vs query: a query consists of many patterns.  I tightened up
the use of pattern vs query in the manual, now there shouldn't be
ambiguities.

* doc/lispref/modes.texi (Parser-based Font Lock):
* doc/lispref/parsing.texi (Language Definitions): Resolve FIXME's.

2 years agoClean up tree-sitter sections of the ELisp manual
Eli Zaretskii [Sat, 22 Oct 2022 15:48:42 +0000 (18:48 +0300)]
Clean up tree-sitter sections of the ELisp manual

* doc/lispref/parsing.texi (Parsing Program Source):
* doc/lispref/modes.texi (Font Lock Mode)
(Parser-based Font Lock): Fix wording, punctuation, and markup.
Add index entries.

* lisp/treesit.el (treesit-node-at, treesit-language-at): Rename
argument POINT to POS.

2 years ago* lisp/treesit.el (treesit-font-lock-enable): Add function back.
Yuan Fu [Sat, 22 Oct 2022 01:50:24 +0000 (18:50 -0700)]
* lisp/treesit.el (treesit-font-lock-enable): Add function back.

2 years agoRevise the toggle scheme of tree-sitter (again)
Yuan Fu [Wed, 19 Oct 2022 23:44:04 +0000 (16:44 -0700)]
Revise the toggle scheme of tree-sitter (again)

Now instead of a toggle function (major-mode-backend-function), we let
major mode set local variables like treesit-font-lock-settings,
treesit-imenu-function, then treesit-mode takes care of activating
those things (clearing font-lock-keywords, setting
imenu-create-index-function to treesit-imenu-function, etc).

js.el and python.el: I've returned js-mode and python-mode to exactly
what they were before tree-sitter change, plus lines at the end
setting up tree-sitter variables.  Sorry about all these fuss :-D

* lisp/treesit.el (treesit-mode-inhibit-message): Remove option.
(major-mode-backend-function)
(treesit-remapped-major-mode-alist): Remove variables.
(treesit-mode): Move down to the end of buffer.  Do more things.
(global-treesit-mode): Move down to the end of buffer.  Don't handle
major-mode-remap-alist anymore.
(global-treesit-mode--turn-on): Move down to the end of buffer.
(treesit-ready-p): Move down to the end of buffer.  Changed signature.
(treesit-font-lock-enable): Remove function.
(treesit-defun-type-regexp): New variable.
(treesit-beginning-of-defun)
(treesit-end-of-defun): New function.
(treesit-imenu-function): New variable.
(treesit-mode-supported)
(treesit-required-languages)
(treesit--local-variable-backup): New variables.
(treesit--backup-local-variable): New function

* lisp/progmodes/js.el (js-use-tree-sitter): Remove option.
(js--treesit-defun-type-regexp): Remove variable.  (Now set inline in
js-mode.)
(js--treesit-beginning-of-defun)
(js--treesit-end-of-defun): Remove functions.  (Now use
treesit-beginning/end-of-defun.)
(js--backend-toggle)
(js--json-backend-toggle): Remove function.
(js-mode)
(js-json-mode): Restore back to before tree-sitter changes.  Add
tree-sitter setup at the end.

* lisp/progmodes/python.el (python--backend-toggle): Remove function.
(python-mode): Restore back to before tree-sitter changes.  Add
tree-sitter setup at the end.

* lisp/progmodes/ts-mode.el (ts-mode--font-lock-settings): Use
js--fontify-template-string.
(ts-mode--fontify-template-string): Remove function (because we can
just use js--fontify-template-string).
(ts-mode--defun-type-regexp): Remove variable (now set inline in
ts-mode).
(ts-mode--beginning-of-defun)
(ts-mode--end-of-defun): Remove functions (now using
treesit-beginning/end-of-defun).
(ts-mode): Setup tree-sitter variables and then turn on treesit-mode
or move to js-mode.

2 years agoAdd a new section to tree-sitter's manual node
Yuan Fu [Tue, 18 Oct 2022 21:24:22 +0000 (14:24 -0700)]
Add a new section to tree-sitter's manual node

* doc/lispref/parsing.texi (Parsing Program Source): New section
Tree-sitter major modes.

2 years ago; * lisp/progmodes/python.el (python--backend-toggle): Use cond.
Yuan Fu [Tue, 18 Oct 2022 21:06:13 +0000 (14:06 -0700)]
; * lisp/progmodes/python.el (python--backend-toggle): Use cond.

2 years agoAdd more granular features in font-locking
Theodor Thornhill [Mon, 17 Oct 2022 10:49:19 +0000 (12:49 +0200)]
Add more granular features in font-locking

There is now support for three font-locking levels, 'minimal',
'moderate' and 'full'.  The richest experience is to be expected from
the 'full', and all levels are enabled by default.

* lisp/progmodes/js.el (js--treesit-font-lock-settings): New defvar
renamed from 'js--treesit-settings'.

(js--treesit-font-lock-settings): New defvar renamed from
'js--json-treesit-settings'.

* lisp/progmodes/ts-mode.el (ts-mode--font-lock-settings): New defvar
renamed from 'ts-mode--settings'.

2 years agoRename tree-sitter group to treesit
Yuan Fu [Tue, 18 Oct 2022 20:24:37 +0000 (13:24 -0700)]
Rename tree-sitter group to treesit

* lisp/treesit.el (tree-sitter): Rename to treesit.
(treesit-mode)
(global-treesit-mode): Use treesit group.

2 years ago* lisp/treesit.el: Add commentary.
Yuan Fu [Tue, 18 Oct 2022 20:21:55 +0000 (13:21 -0700)]
* lisp/treesit.el: Add commentary.

2 years agoInstall new toggle scheme for tree-sitter
Yuan Fu [Tue, 18 Oct 2022 20:06:25 +0000 (13:06 -0700)]
Install new toggle scheme for tree-sitter

Basically we now have treesit-mode and global-treesit-mode.  Major
modes set major-mode-backend-function which treesit-mode calls to
activate/deactivate tree-sitter.

js.el needs a bit explanation: I'm so sorry for messing up the
history, but basically now js-mode and js-json-mode should be exactly
the same as before any tree-sitter change was introduced, sans some
initialization code that are moved to js(-json)--backend-toggle.
js-mode and js-json-mode now just sets major-mode-backend-function and
initialize with js(-json)--backend-toggle.

* lisp/treesit.el (treesit-mode-inhibit-message): New option.
(treesit-can-enable-p): Remove function.
(major-mode-backend-function)
(treesit-remapped-major-mode-alist): New variables.
(treesit-mode)
(global-treesit-mode): New minor modes.
(global-treesit-mode--turn-on)
(treesit-ready-p): New functions.

* lisp/progmodes/python.el: Remove option.
(python--backend-toggle): New function.
(python-mode): Remove the if-form, all the initialization code are
moved to python--backend-toggle, python-mode now just sets
major-mode-backend-function and initialize with
python--backend-toggle.
* lisp/progmodes/js.el (js--treesit-can-enable-p)
(js--json-treesit-can-enable-p)
(js--treesit-enable)
(js--json-treesit-enable): Remove functions.
(js--backend-toggle)
(js-json--backend-toggle): New function.

* lisp/progmodes/ts-mode.el (ts-mode): Use treesit-ready-p.

2 years ago; Fix typos in treesit.c
Daniel Martín [Mon, 17 Oct 2022 23:00:13 +0000 (01:00 +0200)]
; Fix typos in treesit.c

* src/treesit.c: Fix a couple of typos.  (Bug#58600)

2 years agoChange free to xfree in treesit.el
Yuan Fu [Tue, 18 Oct 2022 00:31:23 +0000 (17:31 -0700)]
Change free to xfree in treesit.el

This should be in the last commit but I forgot.

* src/treesit.c (Ftreesit_parser_set_included_ranges): Change free to
xfree.