Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
;;; treesit.el --- tree-sitter utilities -*- lexical-binding: t -*-
2022-10-27 08:02:58 +02:00
;; Copyright (C) 2021-2022 Free Software Foundation, Inc.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-12-25 10:22:40 -08:00
;; Maintainer: 付禹安 (Yuan Fu) <casouri@gmail.com>
;; Keywords: treesit, tree-sitter, languages
;; Package: emacs
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
;; This file is part of GNU Emacs.
;; GNU Emacs 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.
;; GNU Emacs 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
2022-10-27 08:02:58 +02:00
;; This file is the Lisp counterpart of treesit.c. Together they
2022-10-18 13:21:55 -07:00
;; provide tree-sitter integration for Emacs. This file contains
;; convenient functions that are more idiomatic and flexible than the
;; exposed C API of tree-sitter. It also contains frameworks for
2022-10-27 08:02:58 +02:00
;; integrating tree-sitter with font-lock, indentation, activating and
;; deactivating tree-sitter, debugging tree-sitter, etc.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
;;; Code:
( eval-when-compile ( require 'cl-lib ) )
2022-09-30 17:20:34 -07:00
( eval-when-compile ( require 'subr-x ) ) ; For `string-join'.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( require 'cl-seq )
( require 'font-lock )
2022-10-28 16:38:37 -07:00
;;; Function declarations
( declare-function treesit-language-available-p " treesit.c " )
( declare-function treesit-language-version " treesit.c " )
( declare-function treesit-parser-p " treesit.c " )
( declare-function treesit-node-p " treesit.c " )
( declare-function treesit-compiled-query-p " treesit.c " )
( declare-function treesit-query-p " treesit.c " )
( declare-function treesit-query-language " treesit.c " )
( declare-function treesit-node-parser " treesit.c " )
( declare-function treesit-parser-create " treesit.c " )
( declare-function treesit-parser-delete " treesit.c " )
( declare-function treesit-parser-list " treesit.c " )
( declare-function treesit-parser-buffer " treesit.c " )
( declare-function treesit-parser-language " treesit.c " )
( declare-function treesit-parser-root-node " treesit.c " )
( declare-function treesit-parser-set-included-ranges " treesit.c " )
( declare-function treesit-parser-included-ranges " treesit.c " )
2022-11-21 17:24:42 +02:00
( declare-function treesit-parser-add-notifier " treesit.c " )
2022-10-28 16:38:37 -07:00
( declare-function treesit-node-type " treesit.c " )
( declare-function treesit-node-start " treesit.c " )
( declare-function treesit-node-end " treesit.c " )
( declare-function treesit-node-string " treesit.c " )
( declare-function treesit-node-parent " treesit.c " )
( declare-function treesit-node-child " treesit.c " )
( declare-function treesit-node-check " treesit.c " )
( declare-function treesit-node-field-name-for-child " treesit.c " )
( declare-function treesit-node-child-count " treesit.c " )
( declare-function treesit-node-child-by-field-name " treesit.c " )
( declare-function treesit-node-next-sibling " treesit.c " )
( declare-function treesit-node-prev-sibling " treesit.c " )
( declare-function treesit-node-first-child-for-pos " treesit.c " )
( declare-function treesit-node-descendant-for-range " treesit.c " )
( declare-function treesit-node-eq " treesit.c " )
( declare-function treesit-pattern-expand " treesit.c " )
( declare-function treesit-query-expand " treesit.c " )
( declare-function treesit-query-compile " treesit.c " )
( declare-function treesit-query-capture " treesit.c " )
( declare-function treesit-search-subtree " treesit.c " )
( declare-function treesit-search-forward " treesit.c " )
( declare-function treesit-induce-sparse-tree " treesit.c " )
( declare-function treesit-available-p " treesit.c " )
2022-10-19 16:44:04 -07:00
;;; Custom options
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-10-18 13:24:37 -07:00
;; Tree-sitter always appear as treesit in symbols.
( defgroup treesit nil
" Incremental parser.
It is used to enhance major mode features like font-lock,
indent, imenu, etc. "
2022-10-09 20:54:19 -07:00
:group 'tools
:version " 29.1 " )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-11-21 22:32:51 -08:00
( defcustom treesit-max-buffer-size
( let ( ( mb ( * 1024 1024 ) ) )
;; 40MB for 64-bit systems, 15 for 32-bit.
2022-11-22 20:22:41 +02:00
( if ( or ( < most-positive-fixnum ( * 2.0 1024 mb ) )
;; 32-bit system with wide ints.
( string-match-p " --with-wide-int " system-configuration-options ) )
( * 15 mb )
( * 40 mb ) ) )
" Maximum buffer size (in bytes) for enabling tree-sitter parsing.
A typical tree-sitter parser needs 10 times as much memory as the
buffer it parses. Also, the tree-sitter library has a hard limit
of max unsigned 32-bit value for byte offsets into buffer text. "
2022-10-09 20:54:19 -07:00
:type 'integer
:version " 29.1 " )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
;;; Parser API supplement
( defun treesit-parse-string ( string language )
" Parse STRING using a parser for LANGUAGE.
Return the root node of the syntax tree. "
( with-temp-buffer
( insert string )
( treesit-parser-root-node
2022-06-16 01:11:09 -07:00
( treesit-parser-create language ) ) ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-10-25 14:26:41 -07:00
( defvar-local treesit-language-at-point-function nil
" A function that returns the language at point.
This is used by ` treesit-language-at ', which is used by various
functions to determine which parser to use at point.
2022-10-27 08:02:58 +02:00
The function is called with one argument, the position of point. " )
2022-10-25 14:26:41 -07:00
2022-10-27 08:02:58 +02:00
( defun treesit-language-at ( position )
" Return the language at POSITION.
2022-11-06 11:59:27 +02:00
This function assumes that parser ranges are up-to-date. It
returns the return value of ` treesit-language-at-point-function '
if it 's non-nil, otherwise it returns the language of the first
parser in ` treesit-parser-list ', or nil if there is no parser. "
2022-11-05 16:55:17 -07:00
( if treesit-language-at-point-function
( funcall treesit-language-at-point-function position )
( when-let ( ( parser ( car ( treesit-parser-list ) ) ) )
( treesit-parser-language parser ) ) ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
;;; Node API supplement
2022-12-24 14:48:50 -08:00
( define-error 'treesit-no-parser " No available parser for this buffer "
'treesit-error )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( defun treesit-node-buffer ( node )
2022-10-27 08:02:58 +02:00
" Return the buffer in which NODE belongs. "
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( treesit-parser-buffer
( treesit-node-parser node ) ) )
( defun treesit-node-language ( node )
" Return the language symbol that NODE's parser uses. "
( treesit-parser-language
( treesit-node-parser node ) ) )
2022-11-11 20:04:38 -08:00
( defun treesit-node-at ( pos &optional parser-or-lang named )
" Return the leaf node at position POS.
2022-05-13 13:38:21 -07:00
2022-11-11 20:04:38 -08:00
A leaf node is a node that doesn 't have any child nodes.
2022-10-22 18:48:42 +03:00
2022-11-12 10:57:56 +02:00
The returned node 's span covers POS: the node 's beginning is before
or at POS, and the node 's end is at or after POS.
2022-11-11 20:04:38 -08:00
If no leaf node 's span covers POS ( e.g., POS is on whitespace
between two leaf nodes ) , return the first leaf node after POS.
If there is no leaf node after POS, return the first leaf node
before POS.
Return nil if no leaf node can be returned. If NAMED is non-nil,
only look for named nodes.
2022-05-13 13:38:21 -07:00
2022-12-24 15:31:03 -08:00
If PARSER-OR-LANG is a parser, use that parser ; if PARSER-OR-LANG
is a language, find the first parser for that language in the
current buffer, or create one if none exists ; If PARSER-OR-LANG
2022-12-25 09:29:47 +02:00
is nil , try to guess the language at POS using ` treesit-language-at '. "
2022-10-23 18:23:17 -07:00
( let* ( ( root ( if ( treesit-parser-p parser-or-lang )
( treesit-parser-root-node parser-or-lang )
2022-12-24 15:31:03 -08:00
( treesit-buffer-root-node
( or parser-or-lang ( treesit-language-at pos ) ) ) ) )
2022-10-23 18:23:17 -07:00
( node root )
2022-11-11 20:04:38 -08:00
( node-before root )
( pos-1 ( max ( 1- pos ) ( point-min ) ) )
2022-10-23 18:23:17 -07:00
next )
( when node
;; This is very fast so no need for C implementation.
( while ( setq next ( treesit-node-first-child-for-pos
node pos named ) )
( setq node next ) )
2022-11-11 20:04:38 -08:00
;; If POS is at the end of buffer, after all the text, we will
;; end up with NODE = root node. Instead of returning nil,
;; return the last leaf node in the tree for convenience.
2022-10-23 18:23:17 -07:00
( if ( treesit-node-eq node root )
2022-11-11 20:04:38 -08:00
( progn
2022-10-23 18:23:17 -07:00
( while ( setq next ( treesit-node-child node -1 named ) )
( setq node next ) )
node )
2022-11-11 20:04:38 -08:00
;; Normal case, where we found a node.
( if ( <= ( treesit-node-start node ) pos )
node
;; So the node we found is completely after POS, try to find
;; a node whose end equals to POS.
( while ( setq next ( treesit-node-first-child-for-pos
node-before pos-1 named ) )
( setq node-before next ) )
( if ( eq ( treesit-node-end node-before ) pos )
node-before
node ) ) ) ) ) )
2022-05-13 13:38:21 -07:00
( defun treesit-node-on ( beg end &optional parser-or-lang named )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
" Return the smallest node covering BEG to END.
2022-05-13 13:38:21 -07:00
BEWARE! Calling this function on an empty line that is not
2022-10-27 08:02:58 +02:00
inside any top-level construct ( function definition, etc. ) most
2022-05-13 13:38:21 -07:00
probably will give you the root node, because the root node is
the smallest node that covers that empty line. You probably want
to use ` treesit-node-at ' instead.
2022-10-27 08:02:58 +02:00
Return nil if none was found. If NAMED is non-nil, only look for
named node.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-12-24 15:31:03 -08:00
If PARSER-OR-LANG is a parser, use that parser ; if PARSER-OR-LANG
is a language, find the first parser for that language in the
current buffer, or create one if none exists ; If PARSER-OR-LANG
2022-12-25 09:29:47 +02:00
is nil , try to guess the language at BEG using ` treesit-language-at '. "
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( let ( ( root ( if ( treesit-parser-p parser-or-lang )
( treesit-parser-root-node parser-or-lang )
2022-12-24 15:31:03 -08:00
( treesit-buffer-root-node
( or parser-or-lang ( treesit-language-at beg ) ) ) ) ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( treesit-node-descendant-for-range root beg ( or end beg ) named ) ) )
2022-12-27 15:07:03 -08:00
( defun treesit-node-top-level ( node &optional pred include-node )
2022-10-29 14:11:52 -07:00
" Return the top-level equivalent of NODE.
2022-12-27 15:07:03 -08:00
2022-10-29 14:11:52 -07:00
Specifically, return the highest parent of NODE that has the same
type as it. If no such parent exists, return nil.
2022-12-27 15:07:03 -08:00
If PRED is non-nil, match each parent 's type with PRED as a
regexp, rather than using NODE 's type. PRED can also be a
function that takes the node as an argument, and return
non-nil/nil for match/no match.
If INCLUDE-NODE is non-nil, return NODE if it satisfies PRED. "
( let ( ( pred ( or pred ( treesit-node-type node ) ) )
2022-10-29 14:11:52 -07:00
( result nil ) )
2022-12-27 15:07:03 -08:00
( cl-loop for cursor = ( if include-node node
( treesit-node-parent node ) )
2022-10-29 14:11:52 -07:00
then ( treesit-node-parent cursor )
while cursor
2022-12-27 15:07:03 -08:00
if ( if ( stringp pred )
( string-match-p pred ( treesit-node-type cursor ) )
( funcall pred cursor ) )
2022-10-29 14:11:52 -07:00
do ( setq result cursor ) )
result ) )
2022-10-26 18:06:07 -07:00
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( defun treesit-buffer-root-node ( &optional language )
" Return the root node of the current buffer.
2022-10-27 08:02:58 +02:00
2022-12-24 15:31:03 -08:00
Use the first parser in the parser list if LANGUAGE is omitted.
If LANGUAGE is non-nil, use the first parser for LANGUAGE in the
parser list, or create one if none exists. "
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( if-let ( ( parser
2022-12-24 14:48:50 -08:00
( if language
( treesit-parser-create language )
( or ( car ( treesit-parser-list ) )
( signal 'treesit-no-parser ( list ( current-buffer ) ) ) ) ) ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( treesit-parser-root-node parser ) ) )
( defun treesit-filter-child ( node pred &optional named )
2022-10-27 08:02:58 +02:00
" Return children of NODE that satisfies predicate PRED.
PRED is a function that takes one argument, the child node.
If optional argument NAMED is non-nil, only search for named
node. "
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( let ( ( child ( treesit-node-child node 0 named ) )
result )
( while child
( when ( funcall pred child )
( push child result ) )
( setq child ( treesit-node-next-sibling child named ) ) )
( reverse result ) ) )
( defun treesit-node-text ( node &optional no-property )
" Return the buffer (or string) content corresponding to NODE.
2022-10-27 08:02:58 +02:00
If optional argument NO-PROPERTY is non-nil, remove text
properties. "
2022-09-24 19:24:06 -07:00
( when node
( with-current-buffer ( treesit-node-buffer node )
( if no-property
( buffer-substring-no-properties
( treesit-node-start node )
( treesit-node-end node ) )
( buffer-substring
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( treesit-node-start node )
2022-09-24 19:24:06 -07:00
( treesit-node-end node ) ) ) ) ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-12-27 15:07:03 -08:00
( defun treesit-parent-until ( node pred &optional include-node )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
" Return the closest parent of NODE that satisfies PRED.
2022-12-27 15:07:03 -08:00
2022-10-27 08:02:58 +02:00
Return nil if none was found. PRED should be a function that
2022-12-27 15:07:03 -08:00
takes one argument, the parent node, and return non-nil/nil for
match/no match.
If INCLUDE-NODE is non-nil, return NODE if it satisfies PRED. "
( let ( ( node ( if include-node node
( treesit-node-parent node ) ) ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( while ( and node ( not ( funcall pred node ) ) )
( setq node ( treesit-node-parent node ) ) )
node ) )
( defun treesit-parent-while ( node pred )
" Return the furthest parent of NODE that satisfies PRED.
2022-10-27 08:02:58 +02:00
Return nil if none was found. PRED should be a function that
takes one argument, the parent node. "
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( let ( ( last nil ) )
( while ( and node ( funcall pred node ) )
( setq last node
node ( treesit-node-parent node ) ) )
last ) )
( defun treesit-node-children ( node &optional named )
" Return a list of NODE's children.
If NAMED is non-nil, collect named child only. "
( mapcar ( lambda ( idx )
( treesit-node-child node idx named ) )
( number-sequence
0 ( 1- ( treesit-node-child-count node named ) ) ) ) )
( defun treesit-node-index ( node &optional named )
" Return the index of NODE in its parent.
If NAMED is non-nil, count named child only. "
( let ( ( count 0 ) )
( while ( setq node ( treesit-node-prev-sibling node named ) )
( cl-incf count ) )
count ) )
( defun treesit-node-field-name ( node )
" Return the field name of NODE as a child of its parent. "
( when-let ( ( parent ( treesit-node-parent node ) )
( idx ( treesit-node-index node ) ) )
( treesit-node-field-name-for-child parent idx ) ) )
;;; Query API supplement
( defun treesit-query-string ( string query language )
" Query STRING with QUERY in LANGUAGE.
See ` treesit-query-capture ' for QUERY. "
( with-temp-buffer
( insert string )
2022-06-16 01:11:09 -07:00
( let ( ( parser ( treesit-parser-create language ) ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( treesit-query-capture
( treesit-parser-root-node parser )
query ) ) ) )
2022-11-08 20:43:24 -08:00
( defun treesit-query-range ( node query &optional beg end )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
" Query the current buffer and return ranges of captured nodes.
2022-11-08 20:43:24 -08:00
QUERY, NODE, BEG, END are the same as in
` treesit-query-capture '. This function returns a list
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
of ( START . END ) , where START and END specifics the range of each
captured node. Capture names don 't matter. "
( cl-loop for capture
2022-11-08 20:43:24 -08:00
in ( treesit-query-capture node query beg end )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
for node = ( cdr capture )
collect ( cons ( treesit-node-start node )
( treesit-node-end node ) ) ) )
;;; Range API supplement
2022-11-04 01:31:56 -07:00
( defvar-local treesit-range-settings nil
" A list of range settings.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-11-05 10:49:46 +02:00
Each element of the list is of the form ( QUERY LANGUAGE ) .
2022-11-04 01:31:56 -07:00
When updating the range of each parser in the buffer,
2022-11-05 10:49:46 +02:00
` treesit-update-ranges ' queries each QUERY, and sets LANGUAGE 's
2022-11-04 01:31:56 -07:00
range to the range spanned by captured nodes. QUERY must be a
compiled query.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-11-04 01:31:56 -07:00
QUERY can also be a function, in which case it is called with 2
2022-11-05 10:49:46 +02:00
arguments, START and END. It should ensure parsers ' ranges are
correct in the region between START and END.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-11-05 16:52:38 -07:00
The exact form of each setting is considered internal and subject
2022-11-04 01:31:56 -07:00
to change. Use ` treesit-range-rules ' to set this variable. " )
2022-11-05 16:52:38 -07:00
( defun treesit-range-rules ( &rest query-specs )
2022-11-04 01:31:56 -07:00
" Produce settings for `treesit-range-settings' .
2022-11-08 10:39:53 -08:00
QUERY-SPECS are a series of QUERY-SPECs, where each QUERY-SPEC is
2022-11-21 15:39:43 +01:00
a QUERY preceded by zero or more pairs of :KEYWORD and VALUE,
2022-11-08 10:39:53 -08:00
like this:
2022-11-04 01:31:56 -07:00
2022-11-05 16:52:38 -07:00
:KEYWORD VALUE... QUERY
Each QUERY is a tree-sitter query in either the string,
s-expression or compiled form.
2022-11-08 10:39:53 -08:00
For each QUERY, :KEYWORD and VALUE pairs add meta information to
it. For example,
2022-11-04 01:31:56 -07:00
( treesit-range-rules
:embed \\= 'javascript
:host \\= 'html
\\= ' ( ( script_element ( raw_text ) @cap ) ) )
2022-11-05 16:52:38 -07:00
The ` :embed ' keyword specifies the embedded language, and the
` :host ' keyword specifies the host language. They are used in
this way: Emacs queries QUERY in the host language 's parser,
computes the ranges spanned by the captured nodes, and applies
these ranges to parsers for the embedded language.
2022-11-04 01:31:56 -07:00
QUERY can also be a function that takes two arguments, START and
2022-11-06 11:59:27 +02:00
END. If QUERY is a function, it doesn 't need the :KEYWORD VALUE
pair preceding it. This function should set the ranges for
2022-11-05 16:52:38 -07:00
parsers in the current buffer in the region between START and
END. It is OK for this function to set ranges in a larger region
that encompasses the region between START and END. "
2022-11-04 01:31:56 -07:00
( let ( host embed result )
2022-11-05 16:52:38 -07:00
( while query-specs
( pcase ( pop query-specs )
( :host ( let ( ( host-lang ( pop query-specs ) ) )
2022-11-04 01:31:56 -07:00
( unless ( symbolp host-lang )
( signal 'treesit-error ( list " Value of :host option should be a symbol " host-lang ) ) )
( setq host host-lang ) ) )
2022-11-05 16:52:38 -07:00
( :embed ( let ( ( embed-lang ( pop query-specs ) ) )
2022-11-04 01:31:56 -07:00
( unless ( symbolp embed-lang )
( signal 'treesit-error ( list " Value of :embed option should be a symbol " embed-lang ) ) )
( setq embed embed-lang ) ) )
( query ( if ( functionp query )
( push ( list query nil nil ) result )
( when ( null embed )
( signal 'treesit-error ( list " Value of :embed option cannot be omitted " ) ) )
( when ( null host )
( signal 'treesit-error ( list " Value of :host option cannot be omitted " ) ) )
( push ( list ( treesit-query-compile host query )
embed host )
result ) )
( setq host nil embed nil ) ) ) )
( nreverse result ) ) )
( defun treesit--merge-ranges ( old-ranges new-ranges start end )
2022-11-05 10:49:46 +02:00
" Merge OLD-RANGES and NEW-RANGES, discarding ranges between START and END.
2022-11-22 04:40:49 +01:00
OLD-RANGES and NEW-RANGES are lists of cons of the form ( BEG . END ) .
When merging the two ranges, if a range in OLD-RANGES intersects with
2022-11-04 01:31:56 -07:00
another range in NEW-RANGES, discard the one in OLD-RANGES and
keep the one in NEW-RANGES. Also discard any range in OLD-RANGES
that intersects the region marked by START and END.
2022-11-05 10:49:46 +02:00
Return the merged list of ranges. "
2022-11-04 01:31:56 -07:00
( let ( ( result nil ) )
( while ( and old-ranges new-ranges )
( let ( ( new-beg ( caar new-ranges ) )
( new-end ( cdar new-ranges ) )
( old-beg ( caar old-ranges ) )
( old-end ( cdar old-ranges ) ) )
( cond
;; Old range intersects with START-END, discard.
( ( and ( < start old-end )
( < old-beg end ) )
( setq old-ranges ( cdr old-ranges ) ) )
;; New range and old range don't intersect, new comes
;; before, push new.
( ( <= new-end old-beg )
( push ( car new-ranges ) result )
( setq new-ranges ( cdr new-ranges ) ) )
;; New range and old range don't intersect, old comes
;; before, push old.
( ( <= old-end new-beg )
( push ( car old-ranges ) result )
( setq old-ranges ( cdr old-ranges ) ) )
( t ;; New and old range intersect, discard old.
( setq old-ranges ( cdr old-ranges ) ) ) ) ) )
( let ( ( left-over ( or new-ranges old-ranges ) ) )
( dolist ( range left-over )
( push range result ) ) )
( nreverse result ) ) )
2022-11-22 12:01:53 -08:00
( defun treesit--clip-ranges ( ranges start end )
" Clip RANGES in between START and END.
RANGES is a list of ranges of the form ( BEG . END ) . Ranges
outside of the region between START and END are thrown away, and
those inside are kept. "
( cl-loop for range in ranges
if ( <= start ( car range ) ( cdr range ) end )
collect range ) )
2022-11-04 01:31:56 -07:00
( defun treesit-update-ranges ( &optional beg end )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
" Update the ranges for each language in the current buffer.
2022-11-05 10:49:46 +02:00
If BEG and END are non-nil, only update parser ranges in that
2022-11-04 01:31:56 -07:00
region. "
;; When updating ranges, we want to avoid querying the whole buffer
;; which could be slow in very large buffers. Instead, we only
;; query for nodes that intersect with the region between BEG and
2022-11-05 10:49:46 +02:00
;; END. Also, we only update the ranges intersecting BEG and END;
2022-11-04 01:31:56 -07:00
;; outside of that region we inherit old ranges.
( dolist ( setting treesit-range-settings )
( let ( ( query ( nth 0 setting ) )
( language ( nth 1 setting ) )
( beg ( or beg ( point-min ) ) )
( end ( or end ( point-max ) ) ) )
( if ( functionp query ) ( funcall query beg end )
( let* ( ( host-lang ( treesit-query-language query ) )
( parser ( treesit-parser-create language ) )
( old-ranges ( treesit-parser-included-ranges parser ) )
( new-ranges ( treesit-query-range
host-lang query beg end ) )
2022-11-22 12:01:53 -08:00
( set-ranges ( treesit--clip-ranges
( treesit--merge-ranges
old-ranges new-ranges beg end )
( point-min ) ( point-max ) ) ) )
2022-11-04 10:03:17 -07:00
( dolist ( parser ( treesit-parser-list ) )
( when ( eq ( treesit-parser-language parser )
language )
( treesit-parser-set-included-ranges
parser set-ranges ) ) ) ) ) ) ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-10-23 18:51:35 -07:00
( defun treesit-parser-range-on ( parser beg &optional end )
" Check if PARSER's range covers the portion between BEG and END.
If it does, return the range covering that portion in the form
of ( RANGE-BEG . RANGE-END ) , if not, return nil. If nil or
omitted, default END to BEG. "
( let ( ( ranges ( treesit-parser-included-ranges parser ) )
( end ( or end beg ) ) )
( if ( null ranges )
( cons ( point-min ) ( point-max ) )
( cl-loop for rng in ranges
if ( <= ( car rng ) beg end ( cdr rng ) )
return rng
finally return nil ) ) ) )
2022-10-31 15:03:49 -07:00
;;; Fontification
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-10-04 13:28:46 -07:00
( define-error 'treesit-font-lock-error
" Generic tree-sitter font-lock error "
'treesit-error )
2022-11-26 14:54:55 -08:00
( defvar-local treesit-font-lock-level 3
2022-11-27 10:06:02 +02:00
" Decoration level to be used by tree-sitter fontifications.
2022-11-26 14:54:55 -08:00
Major modes categorize their fontification features into levels,
2022-11-27 10:06:02 +02:00
from 1 which is the absolute minimum, to 4 that yields the maximum
fontifications.
2022-11-26 14:54:55 -08:00
Level 1 usually contains only comments and definitions.
Level 2 usually adds keywords, strings, constants, types, etc.
Level 3 usually represents a full-blown fontification, including
assignment, constants, numbers, properties, etc.
2022-11-27 10:06:02 +02:00
Level 4 adds everything else that can be fontified: delimiters,
2022-11-26 14:54:55 -08:00
operators, brackets, all functions and variables, etc.
In addition to the decoration level, individual features can be
2022-11-27 10:06:02 +02:00
turned on/off by calling ` treesit-font-lock-recompute-features '.
Changing the decoration level requires calling
2022-11-26 14:54:55 -08:00
` treesit-font-lock-recompute-features ' to have an effect. " )
2022-11-21 11:26:46 -08:00
( defvar-local treesit--font-lock-query-expand-range ( cons 0 0 )
" The amount to expand the start and end of the region when fontifying.
This should be a cons cell ( START . END ) . When fontifying a
buffer, Emacs will move the start of the query range backward by
START amount, and the end of the query range by END amount. Both
START and END should be positive integers or 0. This doesn 't
2022-12-05 18:37:47 -08:00
affect the fontified range. " )
2022-11-21 11:26:46 -08:00
2022-10-13 14:44:42 -07:00
( defvar-local treesit-font-lock-feature-list nil
" A list of lists of feature symbols.
2022-11-05 10:49:46 +02:00
Use ` treesit-font-lock-recompute-features ' and
2022-11-26 14:54:55 -08:00
` treesit-font-lock-level ' to configure enabled features.
2022-11-01 00:25:47 -07:00
2022-10-13 14:44:42 -07:00
Each sublist represents a decoration level.
2022-11-26 14:54:55 -08:00
` treesit-font-lock-level ' controls which levels are activated.
2022-10-13 14:44:42 -07:00
2022-11-22 04:40:49 +01:00
Inside each sublist are feature symbols, which correspond to the
2022-10-13 14:44:42 -07:00
:feature value of a query defined in ` treesit-font-lock-rules '.
Removing a feature symbol from this list disables the
corresponding query during font-lock.
2022-10-27 08:02:58 +02:00
Common feature names ( for general programming languages ) include
2022-11-16 21:31:04 -08:00
definition, type, assignment, builtin, constant, keyword,
string-interpolation, comment, doc, string, operator, property,
preprocessor, escape-sequence, key ( in key-value pairs ) . Major
modes are free to subdivide or extend on these common features.
2022-11-22 04:40:49 +01:00
See the manual for more explanations on some of the features.
2022-10-13 14:44:42 -07:00
For changes to this variable to take effect, run
` treesit-font-lock-recompute-features '. " )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( defvar-local treesit-font-lock-settings nil
" A list of SETTINGs for treesit-based fontification.
2022-11-05 16:52:38 -07:00
The exact format of each SETTING is considered internal. Use
` treesit-font-lock-rules ' to set this variable.
2022-09-07 11:46:07 -07:00
2022-10-28 06:38:00 +02:00
Each SETTING has the form:
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-10-13 14:44:42 -07:00
( QUERY ENABLE FEATURE OVERRIDE )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-10-13 14:44:42 -07:00
QUERY must be a compiled query. See Info node ` ( elisp ) Pattern
Matching ' for how to write a query and compile it.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-10-13 14:44:42 -07:00
For SETTING to be activated for font-lock, ENABLE must be t. To
disable this SETTING, set ENABLE to nil.
2022-10-27 08:02:58 +02:00
FEATURE is the \"feature name\" of the query. Users can control
2022-11-26 14:54:55 -08:00
which features are enabled with ` treesit-font-lock-level ' and
` treesit-font-lock-feature-list '.
2022-10-04 13:28:46 -07:00
OVERRIDE is the override flag for this query. Its value can be
t , nil , append, prepend, keep. See more in
2022-10-13 14:44:42 -07:00
` treesit-font-lock-rules '. " )
2022-09-07 11:46:07 -07:00
2022-11-05 16:52:38 -07:00
( defun treesit-font-lock-rules ( &rest query-specs )
2022-09-07 11:46:07 -07:00
" Return a value suitable for `treesit-font-lock-settings' .
2022-11-08 10:39:53 -08:00
QUERY-SPECS is a series of QUERY-SPECs. Each QUERY-SPEC is a
QUERY preceded by multiple pairs of :KEYWORD and VALUE:
2022-11-05 16:52:38 -07:00
:KEYWORD VALUE... QUERY
QUERY is a tree-sitter query in either the string, s-expression
or compiled form. For each query, captured nodes are highlighted
2022-11-04 01:31:56 -07:00
with the capture name as its face.
2022-09-07 11:46:07 -07:00
2022-11-21 15:39:43 +01:00
:KEYWORD and VALUE pairs preceding a QUERY add meta information
2022-11-08 10:39:53 -08:00
to QUERY. For example,
2022-09-07 11:46:07 -07:00
( treesit-font-lock-rules
2022-09-24 19:39:10 -07:00
:language \\= 'javascript
2022-10-04 13:28:46 -07:00
:override t
2022-10-13 14:44:42 -07:00
:feature\\= 'constant
2022-09-24 19:39:10 -07:00
\\= ' ( ( true ) @font-lock-constant-face
2022-09-07 11:46:07 -07:00
( false ) @font-lock-constant-face )
2022-09-24 19:39:10 -07:00
:language \\= 'html
2022-10-13 14:44:42 -07:00
:feature \\= 'script
2022-09-07 11:46:07 -07:00
\" ( script_element ) @font-lock-builtin-face\" )
2022-11-22 04:40:49 +01:00
For each QUERY, a :language keyword and a :feature keyword are
2022-11-10 12:45:14 +01:00
required. Each query 's :feature is a symbol summarizing what the
query fontifies. It is used to allow users to enable/disable
2022-11-23 05:43:19 +01:00
certain features. See ` treesit-font-lock-feature-list ' for more.
2022-10-13 14:44:42 -07:00
Other keywords include:
2022-10-04 13:28:46 -07:00
2022-10-28 06:38:00 +02:00
KEYWORD VALUE DESCRIPTION
:override nil If the region already has a face,
discard the new face.
t Always apply the new face.
` append ' Append the new face to existing ones.
` prepend ' Prepend the new face to existing ones.
` keep ' Fill-in regions without an existing face.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
Capture names in QUERY should be face names like
` font-lock-keyword-face '. The captured node will be fontified
2022-11-02 17:03:03 -07:00
with that face.
Capture names can also be function names, in which case the
2022-11-05 10:49:46 +02:00
function will be called with the following argument list:
2022-11-02 17:03:03 -07:00
( NODE OVERRIDE START END &rest _ )
where NODE is the tree-sitter node object, OVERRIDE is the
2022-11-05 10:49:46 +02:00
override option of that rule, and START and END specify the region
2022-11-02 17:03:03 -07:00
to be fontified. This function should accept more arguments as
2022-11-05 10:49:46 +02:00
optional arguments for future extensibility, and it shouldn 't
fontify text outside the region given by START and END.
2022-11-02 11:26:38 -07:00
If a capture name is both a face and a function, the face takes
2022-10-31 15:03:49 -07:00
priority. If a capture name is not a face name nor a function
2022-11-05 16:52:38 -07:00
name, it is ignored. "
2022-10-28 16:38:37 -07:00
;; Other tree-sitter function don't tend to be called unless
;; tree-sitter is enabled, which means tree-sitter must be compiled.
;; But this function is usually call in `defvar' which runs
;; regardless whether tree-sitter is enabled. So we need this
;; guard.
( when ( treesit-available-p )
( let ( ;; Tracks the current :language/:override/:toggle/:level value
;; that following queries will apply to.
current-language current-override
current-feature
;; The list this function returns.
( result nil ) )
2022-11-05 16:52:38 -07:00
( while query-specs
( let ( ( token ( pop query-specs ) ) )
2022-10-28 16:38:37 -07:00
( pcase token
;; (1) Process keywords.
( :language
2022-11-05 16:52:38 -07:00
( let ( ( lang ( pop query-specs ) ) )
2022-10-28 16:38:37 -07:00
( when ( or ( not ( symbolp lang ) ) ( null lang ) )
( signal 'treesit-font-lock-error
` ( " Value of :language should be a symbol "
, lang ) ) )
( setq current-language lang ) ) )
( :override
2022-11-05 16:52:38 -07:00
( let ( ( flag ( pop query-specs ) ) )
2022-10-28 16:38:37 -07:00
( when ( not ( memq flag ' ( t nil append prepend keep ) ) )
( signal 'treesit-font-lock-error
` ( " Value of :override should be one of t, nil, append, prepend, keep "
, flag ) )
( signal 'wrong-type-argument
` ( ( or t nil append prepend keep )
, flag ) ) )
( setq current-override flag ) ) )
( :feature
2022-11-05 16:52:38 -07:00
( let ( ( var ( pop query-specs ) ) )
2022-10-28 16:38:37 -07:00
( when ( or ( not ( symbolp var ) )
( memq var ' ( t nil ) ) )
( signal 'treesit-font-lock-error
` ( " Value of :feature should be a symbol "
, var ) ) )
( setq current-feature var ) ) )
;; (2) Process query.
( ( pred treesit-query-p )
( when ( null current-language )
2022-10-07 01:21:09 -07:00
( signal 'treesit-font-lock-error
2022-10-28 16:38:37 -07:00
` ( " Language unspecified, use :language keyword to specify a language for this query " , token ) ) )
( when ( null current-feature )
2022-10-07 01:21:09 -07:00
( signal 'treesit-font-lock-error
2022-10-28 16:38:37 -07:00
` ( " Feature unspecified, use :feature keyword to specify the feature name for this query " , token ) ) )
( if ( treesit-compiled-query-p token )
( push ` ( , current-language token ) result )
( push ` ( , ( treesit-query-compile current-language token )
t
, current-feature
, current-override )
result ) )
;; Clears any configurations set for this query.
( setq current-language nil
current-override nil
current-feature nil ) )
( _ ( signal 'treesit-font-lock-error
` ( " Unexpected value " , token ) ) ) ) ) )
( nreverse result ) ) ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-10-30 20:56:21 -07:00
;; `font-lock-fontify-region-function' has the LOUDLY argument, but
;; `jit-lock-functions' doesn't pass that argument. So even if we set
;; `font-lock-verbose' to t, if jit-lock is enabled (and it's almost
;; always is), we don't get debug messages. So we add our own.
2022-10-31 14:26:03 -07:00
( defvar treesit--font-lock-verbose nil
2022-10-30 20:56:21 -07:00
" If non-nil, print debug messages when fontifying. " )
2022-11-01 00:25:47 -07:00
( defun treesit-font-lock-recompute-features ( &optional add-list remove-list )
2022-11-21 23:06:43 -08:00
" Enable/disable font-lock features.
2022-11-01 00:25:47 -07:00
2022-11-21 23:06:43 -08:00
Enable each feature in ADD-LIST, disable each feature in
REMOVE-LIST.
If both ADD-LIST and REMOVE-LIST are omitted, recompute each
feature according to ` treesit-font-lock-feature-list ' and
2022-11-27 10:06:02 +02:00
` treesit-font-lock-level '. If the value of ` treesit-font-lock-level ',
is N, then the features in the first N sublists of
` treesit-font-lock-feature-list ' are enabled, and the rest of
the features are disabled.
2022-11-01 00:25:47 -07:00
2022-11-27 10:06:02 +02:00
ADD-LIST and REMOVE-LIST are lists of feature symbols. The
2022-11-05 10:49:46 +02:00
same feature symbol cannot appear in both lists ; the function
2022-11-21 23:06:43 -08:00
signals the ` treesit-font-lock-error ' error if that happens. "
2022-11-01 00:25:47 -07:00
( when-let ( ( intersection ( cl-intersection add-list remove-list ) ) )
( signal 'treesit-font-lock-error
( list " ADD-LIST and REMOVE-LIST contain the same feature "
intersection ) ) )
2022-11-26 14:54:55 -08:00
( let* ( ( level treesit-font-lock-level )
2022-11-01 00:25:47 -07:00
( base-features ( cl-loop
for idx = 0 then ( 1+ idx )
for features in treesit-font-lock-feature-list
if ( or ( eq level t )
( >= level ( 1+ idx ) ) )
append features ) )
( features ( cl-set-difference ( cl-union base-features add-list )
2022-11-21 23:06:43 -08:00
remove-list ) )
;; If additive non-nil, we are configuring on top of the
;; existing configuration, if nil, we are resetting
;; everything according to `treesit-font-lock-feature-list'.
( additive ( or add-list remove-list ) ) )
2022-10-13 14:44:42 -07:00
( cl-loop for idx = 0 then ( 1+ idx )
for setting in treesit-font-lock-settings
for feature = ( nth 2 setting )
2022-11-21 23:06:43 -08:00
for current-value = ( nth 1 setting )
2022-10-13 14:44:42 -07:00
;; Set the ENABLE flag for the setting.
do ( setf ( nth 1 ( nth idx treesit-font-lock-settings ) )
2022-11-21 23:06:43 -08:00
( cond
( ( not additive )
( if ( memq feature features ) t nil ) )
( ( memq feature add-list ) t )
( ( memq feature remove-list ) nil )
( t current-value ) ) ) ) ) )
2022-10-13 14:44:42 -07:00
2022-12-04 00:22:28 -08:00
( defun treesit-fontify-with-override
( start end face override &optional bound-start bound-end )
2022-10-31 15:03:49 -07:00
" Apply FACE to the region between START and END.
OVERRIDE can be nil , t , ` append ', ` prepend ', or ` keep '.
2022-12-04 00:22:28 -08:00
See ` treesit-font-lock-rules ' for their semantic.
If BOUND-START and BOUND-END are non-nil, only fontify the region
in between them. "
( when ( or ( null bound-start ) ( null bound-end )
( and bound-start bound-end
( <= bound-start end )
( >= bound-end start ) ) )
( when ( and bound-start bound-end )
( setq start ( max bound-start start )
end ( min bound-end end ) ) )
( pcase override
( 'nil ( unless ( text-property-not-all start end 'face nil )
( put-text-property start end 'face face ) ) )
( 't ( put-text-property start end 'face face ) )
( 'append ( font-lock-append-text-property
start end 'face face ) )
( 'prepend ( font-lock-prepend-text-property
start end 'face face ) )
( 'keep ( font-lock-fillin-text-property
2022-10-31 15:03:49 -07:00
start end 'face face ) )
2022-12-04 00:22:28 -08:00
( _ ( signal 'treesit-font-lock-error
( list
" Unrecognized value of :override option "
override ) ) ) ) ) )
2022-10-31 15:03:49 -07:00
2022-11-02 16:31:25 -07:00
( defun treesit--set-nonsticky ( start end sym &optional remove )
" Set `rear-nonsticky' property between START and END.
2022-11-21 15:39:43 +01:00
Set the property to a list containing SYM. If there is already a
2022-11-02 16:31:25 -07:00
list, add SYM to that list. If REMOVE is non-nil, remove SYM
instead. "
( let* ( ( prop ( get-text-property start 'rear-nonsticky ) )
( new-prop
( pcase prop
( ( pred listp ) ; PROP is a list or nil.
( if remove
( remove sym prop )
;; We should make sure PORP doesn't contain SYM, but
;; whatever.
( cons sym prop ) ) )
;; PROP is t.
( _ ( if remove
nil
( list sym ) ) ) ) ) )
( if ( null new-prop )
( remove-text-properties start end ' ( rear-nonsticky nil ) )
( put-text-property start end 'rear-nonsticky new-prop ) ) ) )
2022-11-20 16:37:19 -08:00
( defun treesit--children-covering-range ( node start end )
" Return a list of children of NODE covering a range.
The range is between START and END. "
2022-11-22 12:01:19 -08:00
( if-let* ( ( child ( treesit-node-first-child-for-pos node start ) )
( result ( list child ) ) )
( progn
( while ( and child ( < ( treesit-node-end child ) end )
( setq child ( treesit-node-next-sibling child ) ) )
( push child result ) )
( nreverse result ) )
( list node ) ) )
2022-11-20 16:37:19 -08:00
2022-12-07 16:36:07 -08:00
( defun treesit--children-covering-range-recurse
( node start end threshold &optional limit )
2022-11-20 16:37:19 -08:00
" Return a list of children of NODE covering a range.
2022-12-07 16:36:07 -08:00
2022-11-20 16:37:19 -08:00
Recursively go down the parse tree and collect children, until
all nodes in the returned list are smaller than THRESHOLD. The
2022-12-07 16:36:07 -08:00
range is between START and END.
LIMIT is the recursion limit, which defaults to 100. "
2022-11-20 16:37:19 -08:00
( let* ( ( child ( treesit-node-first-child-for-pos node start ) )
2022-12-07 16:36:07 -08:00
( limit ( or limit 100 ) )
2022-11-20 16:37:19 -08:00
result )
2022-12-07 16:36:07 -08:00
;; If LIMIT is exceeded, we are probably seeing the erroneously
;; tall tree, in that case, just give up.
( while ( and ( > limit 0 ) child ( <= ( treesit-node-start child ) end ) )
2022-11-20 16:37:19 -08:00
;; If child still too large, recurse down. Otherwise collect
;; child.
( if ( > ( - ( treesit-node-end child )
( treesit-node-start child ) )
threshold )
( dolist ( r ( treesit--children-covering-range-recurse
2022-12-07 16:36:07 -08:00
child start end threshold ( 1- limit ) ) )
2022-11-20 16:37:19 -08:00
( push r result ) )
( push child result ) )
( setq child ( treesit-node-next-sibling child ) ) )
;; If NODE has no child, keep NODE.
2022-12-24 18:45:36 -08:00
( or result ( list node ) ) ) )
2022-11-20 16:37:19 -08:00
2022-11-20 20:29:53 -08:00
( defsubst treesit--node-length ( node )
" Return the length of the text of NODE. "
( - ( treesit-node-end node ) ( treesit-node-start node ) ) )
2022-11-21 10:52:55 -08:00
( defvar-local treesit--font-lock-fast-mode nil
2022-11-22 04:40:49 +01:00
" If this variable is t, change the way we query so it's faster.
2022-11-21 10:52:55 -08:00
This is not a general optimization and should be RARELY needed!
See comments in ` treesit-font-lock-fontify-region ' for more
detail. " )
2022-11-02 16:31:25 -07:00
;; Some details worth explaining:
;;
;; 1. When we apply face to a node, we clip the face into the
;; currently fontifying region, this way we don't overwrite faces
;; applied by regexp-based font-lock. The clipped part will be
;; fontified fine when Emacs fontifies the region containing it.
;;
2022-11-23 19:46:38 -08:00
;; 2. If you insert an ending quote into a buffer, jit-lock only wants
;; to fontify that single quote, and (treesit-node-on start end) will
;; give you that quote node. We want to capture the string and apply
;; string face to it, but querying on the quote node will not give us
;; the string node. So we don't use treesit-node-on: using the root
;; node with a restricted range is very fast anyway (even in large
;; files of size ~10MB). Plus, querying the result of
;; `treesit-node-on' could still miss patterns even if we use some
;; heuristic to enlarge the node (how much to enlarge? to which
;; extent?), it's much safer to just use the root node.
;;
;; Sometimes the source file has some errors that cause tree-sitter to
;; parse it into a enormously tall tree (10k levels tall). In that
;; case querying the root node is very slow. So we try to get
;; top-level nodes and query them. This ensures that querying is fast
;; everywhere else, except for the problematic region.
;;
2022-12-07 16:02:53 -08:00
;; Some other time the source file has a top-level node that contains
;; a huge number of children (say, 10k children), querying that node
;; is also very slow, so instead of getting the top-level node, we
;; recursively go down the tree to find nodes that cover the region
;; but are reasonably small.
;;
2022-11-23 19:46:38 -08:00
;; 3. It is possible to capture a node that's completely outside the
;; region between START and END: as long as the whole pattern
;; intersects the region, all the captured nodes in that pattern are
;; returned. If the node is outside of that region, (max node-start
;; start) and friends return bad values, so we filter them out.
;; However, we don't filter these nodes out if a function will process
;; the node, because could (and often do) fontify the relatives of the
;; captured node, not just the node itself. If we took out those
;; nodes author of those functions would be very confused.
2022-11-22 04:40:49 +01:00
( defun treesit-font-lock-fontify-region ( start end &optional loudly )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
" Fontify the region between START and END.
2022-10-27 08:02:58 +02:00
If LOUDLY is non-nil, display some debugging information. "
2022-10-31 14:26:03 -07:00
( when ( or loudly treesit--font-lock-verbose )
2022-10-30 20:56:21 -07:00
( message " Fontifying region: %s-%s " start end ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( treesit-update-ranges start end )
( font-lock-unfontify-region start end )
( dolist ( setting treesit-font-lock-settings )
2022-10-13 14:44:42 -07:00
( let* ( ( query ( nth 0 setting ) )
( enable ( nth 1 setting ) )
( override ( nth 3 setting ) )
( language ( treesit-query-language query ) ) )
2022-11-21 10:52:55 -08:00
( when-let ( ( nodes ( list ( treesit-buffer-root-node language ) ) )
2022-10-13 14:44:42 -07:00
;; Only activate if ENABLE flag is t.
( activate ( eq t enable ) ) )
2022-10-07 01:21:09 -07:00
( ignore activate )
2022-11-20 17:15:42 -08:00
2022-11-21 10:52:55 -08:00
;; If we run into problematic files, use the "fast mode" to
2022-11-23 19:46:38 -08:00
;; try to recover. See comment #2 above for more explanation.
2022-11-21 10:52:55 -08:00
( when treesit--font-lock-fast-mode
2022-12-07 16:02:53 -08:00
( setq nodes ( treesit--children-covering-range-recurse
( car nodes ) start end ( * 4 jit-lock-chunk-size ) ) ) )
2022-11-21 10:52:55 -08:00
2022-11-23 19:46:38 -08:00
;; Query each node.
2022-11-21 10:52:55 -08:00
( dolist ( sub-node nodes )
2022-11-21 11:26:46 -08:00
( let* ( ( delta-start ( car treesit--font-lock-query-expand-range ) )
( delta-end ( cdr treesit--font-lock-query-expand-range ) )
( start-time ( current-time ) )
( captures ( treesit-query-capture
sub-node query
( max ( - start delta-start ) ( point-min ) )
( min ( + end delta-end ) ( point-max ) ) ) )
2022-11-22 19:57:44 -08:00
( end-time ( current-time ) ) )
2022-11-21 10:52:55 -08:00
;; If for any query the query time is strangely long,
;; switch to fast mode (see comments above).
( when ( > ( time-to-seconds ( time-subtract end-time start-time ) )
0.01 )
( setq-local treesit--font-lock-fast-mode t ) )
2022-11-23 19:46:38 -08:00
;; For each captured node, fontify that node.
2022-11-20 17:15:42 -08:00
( with-silent-modifications
( dolist ( capture captures )
( let* ( ( face ( car capture ) )
( node ( cdr capture ) )
( node-start ( treesit-node-start node ) )
( node-end ( treesit-node-end node ) ) )
2022-11-23 19:46:38 -08:00
;; If node is not in the region, take them out. See
;; comment #3 above for more detail.
( if ( and ( facep face )
( or ( >= start node-end ) ( >= node-start end ) ) )
2022-11-23 15:20:19 -08:00
( when ( or loudly treesit--font-lock-verbose )
( message " Captured node %s(%s-%s) but it is outside of fontifing region " node node-start node-end ) )
2022-11-20 17:15:42 -08:00
( cond
( ( facep face )
( treesit-fontify-with-override
( max node-start start ) ( min node-end end )
face override ) )
( ( functionp face )
( funcall face node override start end ) ) )
;; Don't raise an error if FACE is neither a face nor
;; a function. This is to allow intermediate capture
;; names used for #match and #eq.
( when ( or loudly treesit--font-lock-verbose )
( message " Fontifying text from %d to %d, Face: %s, Node: %s "
( max node-start start ) ( min node-end end )
face ( treesit-node-type node ) ) ) ) ) ) ) ) ) ) ) )
2022-10-30 20:56:21 -07:00
` ( jit-lock-bounds , start . , end ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-11-15 02:30:50 -08:00
( defun treesit--font-lock-notifier ( ranges parser )
2022-11-22 04:40:49 +01:00
" Ensures updated parts of the parse-tree are refontified.
2022-11-15 02:30:50 -08:00
RANGES is a list of ( BEG . END ) ranges, PARSER is the tree-sitter
parser notifying of the change. "
( with-current-buffer ( treesit-parser-buffer parser )
( dolist ( range ranges )
2022-11-19 00:41:19 -08:00
( when treesit--font-lock-verbose
2022-11-21 15:39:43 +01:00
( message " Notifier received range: %s-%s "
2022-11-19 00:41:19 -08:00
( car range ) ( cdr range ) ) )
2022-11-15 02:30:50 -08:00
( put-text-property ( car range ) ( cdr range ) 'fontified nil ) ) ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
;;; Indent
2022-11-04 09:56:38 -07:00
( define-error 'treesit-indent-error
" Generic tree-sitter indentation error "
'treesit-error )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( defvar treesit--indent-verbose nil
" If non-nil, log progress when indenting. " )
2022-10-25 14:27:23 -07:00
( defvar-local treesit-simple-indent-rules nil
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
" A list of indent rule settings.
Each indent rule setting should be ( LANGUAGE . RULES ) ,
where LANGUAGE is a language symbol, and RULES is a list of
( MATCHER ANCHOR OFFSET ) .
MATCHER determines whether this rule applies, ANCHOR and OFFSET
together determines which column to indent to.
A MATCHER is a function that takes three arguments ( NODE PARENT
BOL ) . BOL is the point where we are indenting: the beginning of
line content, the position of the first non-whitespace character.
NODE is the largest ( highest-in-tree ) node starting at that
point. PARENT is the parent of NODE.
If MATCHER returns non-nil, meaning the rule matches, Emacs then
uses ANCHOR to find an anchor, it should be a function that takes
the same argument ( NODE PARENT BOL ) and returns a point.
2022-11-05 18:32:01 -07:00
Finally Emacs computes the column of that point returned by
ANCHOR and adds OFFSET to it, and indents to that column. OFFSET
can be an integer or a variable whose value is an integer.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
For MATCHER and ANCHOR, Emacs provides some convenient presets.
See ` treesit-simple-indent-presets '. " )
( defvar treesit-simple-indent-presets
2022-10-29 14:50:25 -07:00
( list ( cons 'match
( lambda
( &optional node-type parent-type node-field
node-index-min node-index-max )
( lambda ( node parent &rest _ )
( and ( or ( null node-type )
( string-match-p
node-type ( or ( treesit-node-type node ) " " ) ) )
( or ( null parent-type )
( string-match-p
parent-type ( treesit-node-type parent ) ) )
( or ( null node-field )
( string-match-p
node-field
( or ( treesit-node-field-name node ) " " ) ) )
( or ( null node-index-min )
2022-11-19 15:25:06 -08:00
( >= ( treesit-node-index node )
2022-10-29 14:50:25 -07:00
node-index-min ) )
( or ( null node-index-max )
2022-11-19 15:25:06 -08:00
( <= ( treesit-node-index node )
2022-10-29 14:50:25 -07:00
node-index-max ) ) ) ) ) )
( cons 'n-p-gp
( lambda ( node-t parent-t grand-parent-t )
( lambda ( node parent &rest _ )
( and ( or ( null node-t )
( string-match-p
node-t ( or ( treesit-node-type node ) " " ) ) )
( or ( null parent-t )
( string-match-p
parent-t ( treesit-node-type parent ) ) )
( or ( null grand-parent-t )
( string-match-p
grand-parent-t
( treesit-node-type
( treesit-node-parent parent ) ) ) ) ) ) ) )
( cons 'no-node ( lambda ( node &rest _ ) ( null node ) ) )
( cons 'parent-is ( lambda ( type )
( lambda ( _n parent &rest _ )
2022-10-28 17:56:05 -07:00
( string-match-p
2022-10-29 14:50:25 -07:00
type ( treesit-node-type parent ) ) ) ) )
( cons 'node-is ( lambda ( type )
( lambda ( node &rest _ )
( string-match-p
type ( or ( treesit-node-type node ) " " ) ) ) ) )
( cons 'field-is ( lambda ( name )
( lambda ( node &rest _ )
( string-match-p
name ( or ( treesit-node-field-name node ) " " ) ) ) ) )
2022-11-25 14:24:27 -08:00
( cons 'comment-end ( lambda ( _node _parent bol &rest _ )
( save-excursion
( goto-char bol )
2022-11-27 14:15:57 -08:00
( looking-at-p comment-end-skip ) ) ) )
2022-10-29 14:50:25 -07:00
;; TODO: Document.
( cons 'catch-all ( lambda ( &rest _ ) t ) )
( cons 'query ( lambda ( pattern )
( lambda ( node parent &rest _ )
( cl-loop for capture
in ( treesit-query-capture
parent pattern )
if ( treesit-node-eq node ( cdr capture ) )
return t
finally return nil ) ) ) )
( cons 'first-sibling ( lambda ( _n parent &rest _ )
( treesit-node-start
( treesit-node-child parent 0 ) ) ) )
;; TODO: Document.
( cons 'nth-sibling ( lambda ( n &optional named )
( lambda ( _n parent &rest _ )
( treesit-node-start
( treesit-node-child parent n named ) ) ) ) )
( cons 'parent ( lambda ( _n parent &rest _ )
( treesit-node-start parent ) ) )
2022-11-19 17:59:14 -08:00
( cons 'comment-start
( lambda ( _n parent &rest _ )
( save-excursion
( goto-char ( treesit-node-start parent ) )
2022-11-27 14:15:57 -08:00
( re-search-forward comment-start-skip )
( skip-syntax-backward " - " )
2022-11-19 17:59:14 -08:00
( point ) ) ) )
2022-12-23 17:12:32 -08:00
( cons 'prev-adaptive-prefix
( lambda ( _n parent &rest _ )
( save-excursion
( re-search-backward
( rx ( not ( or " " " \t " " \n " ) ) ) nil t )
( beginning-of-line )
( and ( >= ( point ) ( treesit-node-start parent ) )
;; `adaptive-fill-regexp' will not match "/*",
;; so we need to also try `comment-start-skip'.
( or ( and adaptive-fill-regexp
( looking-at adaptive-fill-regexp )
( > ( - ( match-end 0 ) ( match-beginning 0 ) ) 0 )
( match-end 0 ) )
( and comment-start-skip
( looking-at comment-start-skip )
( match-end 0 ) ) ) ) ) ) )
2022-10-29 14:50:25 -07:00
;; TODO: Document.
( cons 'grand-parent
( lambda ( _n parent &rest _ )
( treesit-node-start ( treesit-node-parent parent ) ) ) )
( cons 'parent-bol ( lambda ( _n parent &rest _ )
( save-excursion
( goto-char ( treesit-node-start parent ) )
( back-to-indentation )
( point ) ) ) )
( cons 'prev-sibling ( lambda ( node &rest _ )
( treesit-node-start
( treesit-node-prev-sibling node ) ) ) )
( cons 'no-indent ( lambda ( _n _p bol &rest _ ) bol ) )
( cons 'prev-line ( lambda ( _n _p bol &rest _ )
( save-excursion
( goto-char bol )
( forward-line -1 )
( skip-chars-forward " \t " ) ) ) )
2022-11-07 02:00:04 -08:00
( cons 'point-min ( lambda ( &rest _ ) ( point-min ) ) )
2022-10-29 14:50:25 -07:00
;; TODO: Document.
( cons 'and ( lambda ( &rest fns )
( lambda ( node parent bol &rest _ )
( cl-reduce ( lambda ( a b ) ( and a b ) )
( mapcar ( lambda ( fn )
( funcall fn node parent bol ) )
fns ) ) ) ) )
( cons 'or ( lambda ( &rest fns )
( lambda ( node parent bol &rest _ )
( cl-reduce ( lambda ( a b ) ( or a b ) )
( mapcar ( lambda ( fn )
( funcall fn node parent bol ) )
fns ) ) ) ) )
( cons 'not ( lambda ( fn )
( lambda ( node parent bol &rest _ )
( debug )
( not ( funcall fn node parent bol ) ) ) ) )
( cons 'list ( lambda ( &rest fns )
( lambda ( node parent bol &rest _ )
( mapcar ( lambda ( fn )
( funcall fn node parent bol ) )
fns ) ) ) ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
" A list of presets.
These presets that can be used as MATHER and ANCHOR in
2022-11-07 02:27:59 -08:00
` treesit-simple-indent-rules '. MACHTERs and ANCHORs are
2022-11-22 04:40:49 +01:00
functions that take 3 arguments: NODE, PARENT and BOL.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
MATCHER:
\(match NODE-TYPE PARENT-TYPE NODE-FIELD NODE-INDEX-MIN NODE-INDEX-MAX )
2022-11-07 02:27:59 -08:00
NODE-TYPE checks for NODE 's type, PARENT-TYPE checks for
2022-11-22 04:40:49 +01:00
PARENT 's type, NODE-FIELD checks for the field name of NODE
in PARENT, NODE-INDEX-MIN and NODE-INDEX-MAX check for
2022-11-07 02:27:59 -08:00
NODE 's index in PARENT. Therefore, to match the first child
where PARENT is \"argument_list\", use
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( match nil \"argument_list\" nil nil 0 0 ) .
2022-10-27 16:50:20 -07:00
NODE-TYPE, PARENT-TYPE, and NODE-FIELD are regexps.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
no-node
2022-11-07 02:27:59 -08:00
Matches the case where NODE is nil , i.e., there is no node
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
that starts at point. This is the case when indenting an
empty line.
\(parent-is TYPE )
2022-11-07 02:27:59 -08:00
Check that PARENT 's type matches regexp TYPE.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
\(node-is TYPE )
2022-11-07 02:27:59 -08:00
Checks that NODE 's type matches regexp TYPE.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-12-18 14:11:54 -08:00
\(n-p-gp NODE-TYPE PARENT-TYPE GRANDPARENT-TYPE )
2022-12-19 17:54:12 -08:00
Checks for NODE 's, its parent 's, and its grandparent 's type.
2022-12-18 14:11:54 -08:00
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
\(query QUERY )
2022-11-07 02:27:59 -08:00
Queries PARENT with QUERY, and checks if NODE is
captured ( by any capture name ) .
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-11-19 17:59:14 -08:00
comment-end
Matches if text after point matches ` treesit-comment-end '.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
ANCHOR:
first-sibling
2022-11-07 02:27:59 -08:00
Returns the start of the first child of PARENT.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
parent
2022-11-07 02:27:59 -08:00
Returns the start of PARENT.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
parent-bol
2022-11-07 02:27:59 -08:00
Returns the beginning of non-space characters on the line where
PARENT is on.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
prev-sibling
2022-11-07 02:27:59 -08:00
Returns the start of NODE 's previous sibling.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
no-indent
2022-11-07 02:27:59 -08:00
Returns the start of NODE.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
prev-line
2022-11-22 04:40:49 +01:00
Returns the first non-whitespace character on the previous line.
2022-11-07 02:00:04 -08:00
point-min
2022-11-19 17:59:14 -08:00
Returns the beginning of buffer, which is always at column 0.
comment-start
2022-12-18 14:11:54 -08:00
Goes to the position that ` comment-start-skip ' would return,
skips whitespace backwards, and returns the resulting
2022-12-23 17:12:32 -08:00
position. Assumes PARENT is a comment node.
prev-adaptive-prefix
Goes to the beginning of previous non-empty line, and tries
to match ` adaptive-fill-regexp '. If it matches, return the
end of the match, otherwise return nil. This is useful for a
` indent-relative '-like indent behavior for block comments. " )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-10-26 18:31:43 -07:00
( defun treesit--simple-indent-eval ( exp )
" Evaluate EXP.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-11-05 10:49:46 +02:00
If EXP is an application and the function is a key in
2022-10-26 18:31:43 -07:00
` treesit-simple-indent-presets ', use the corresponding value as
the function. "
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
;; We don't want to match uncompiled lambdas, so make sure this cons
;; is not a function. We could move the condition functionp
;; forward, but better be explicit.
2022-10-26 18:31:43 -07:00
( cond ( ( and ( consp exp ) ( not ( functionp exp ) ) )
( apply ( treesit--simple-indent-eval ( car exp ) )
( mapcar #' treesit--simple-indent-eval
( cdr exp ) ) ) )
;; Presets override functions, so this condition comes before
;; `functionp'.
( ( alist-get exp treesit-simple-indent-presets )
( alist-get exp treesit-simple-indent-presets ) )
( ( functionp exp ) exp )
( ( symbolp exp )
( if ( null exp )
exp
;; Matchers only return lambdas, anchors only return
;; integer, so we should never see a variable.
2022-11-04 09:56:38 -07:00
( signal 'treesit-indent-error
2022-11-05 10:49:46 +02:00
( list " Couldn't find the preset corresponding to expression "
exp ) ) ) )
2022-10-26 18:31:43 -07:00
( t exp ) ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
;; This variable might seem unnecessary: why split
;; `treesit-indent' and `treesit-simple-indent' into two
;; functions? We add this variable in between because later we might
;; add more powerful indentation engines, and that new engine can
;; probably share `treesit-indent'. It is also useful, suggested
;; by Stefan M, to have a function that figures out how much to indent
;; but doesn't actually performs the indentation, because we might
;; want to know where will a node indent to if we put it at some other
;; location, and use that information to calculate the actual
;; indentation. And `treesit-simple-indent' is that function. I
;; forgot the example Stefan gave, but it makes a lot of sense.
( defvar treesit-indent-function #' treesit-simple-indent
" Function used by `treesit-indent' to do some of the work.
This function is called with
( NODE PARENT BOL &rest _ )
and returns
( ANCHOR . OFFSET ) .
BOL is the position of the beginning of the line ; NODE is the
\"largest\" node that starts at BOL ; PARENT is its parent; ANCHOR
is a point ( not a node ) , and OFFSET is a number. Emacs finds the
column of ANCHOR and adds OFFSET to it as the final indentation
of the current line. " )
2022-10-28 17:54:43 -07:00
( defun treesit--indent-1 ( )
" Indent the current line.
2022-10-29 09:04:35 -07:00
Return ( ANCHOR . OFFSET ) . This function is used by
` treesit-indent ' and ` treesit-indent-region '. "
2022-10-28 17:54:43 -07:00
;; Basically holds the common part between the two indent function.
( let* ( ( bol ( save-excursion
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( forward-line 0 )
( skip-chars-forward " \t " )
( point ) ) )
( smallest-node
2022-10-25 14:26:41 -07:00
( cond ( ( null ( treesit-parser-list ) ) nil )
( ( eq 1 ( length ( treesit-parser-list ) ) )
2022-11-11 21:37:29 -08:00
( treesit-node-at bol ) )
2022-10-25 14:26:41 -07:00
( ( treesit-language-at ( point ) )
( treesit-node-at bol ( treesit-language-at ( point ) ) ) )
( t ( treesit-node-at bol ) ) ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( node ( treesit-parent-while
smallest-node
( lambda ( node )
( eq bol ( treesit-node-start node ) ) ) ) ) )
2022-10-29 09:04:35 -07:00
( let*
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( ( parser ( if smallest-node
( treesit-node-parser smallest-node )
nil ) )
;; NODE would be nil if BOL is on a whitespace. In that case
;; we set PARENT to the "node at point", which would
;; encompass the whitespace.
( parent ( cond ( ( and node parser )
( treesit-node-parent node ) )
2022-10-29 09:43:50 -07:00
( t ( treesit-node-on bol bol ) ) ) ) )
2022-10-29 09:04:35 -07:00
( funcall treesit-indent-function node parent bol ) ) ) )
2022-10-28 17:54:43 -07:00
( defun treesit-indent ( )
" Indent according to the result of `treesit-indent-function' . "
2022-11-04 01:31:56 -07:00
( treesit-update-ranges ( line-beginning-position )
( line-end-position ) )
2022-10-29 14:49:31 -07:00
;; We don't return 'noindent even if no rules match, because
;; `indent-for-tab-command' tries to indent itself when we return
;; 'noindent, which leads to wrong indentation at times.
2022-10-29 09:43:50 -07:00
( pcase-let* ( ( ` ( , anchor . , offset ) ( treesit--indent-1 ) ) )
2022-10-29 14:49:31 -07:00
( when ( and anchor offset )
( let ( ( col ( + ( save-excursion
( goto-char anchor )
( current-column ) )
offset ) )
( delta ( - ( point-max ) ( point ) ) ) )
( indent-line-to col )
;; Now point is at the end of indentation. If we started
;; from within the line, go back to where we started.
( when ( > ( - ( point-max ) delta ) ( point ) )
( goto-char ( - ( point-max ) delta ) ) ) ) ) ) )
2022-10-29 09:04:35 -07:00
2022-11-05 17:09:51 -07:00
;; Batch size can't be too large, because we put markers on each
;; ANCHOR, so a batch size of 400 lines means 400 markers.
2022-10-29 09:04:35 -07:00
( defvar treesit--indent-region-batch-size 400
2022-10-28 17:54:43 -07:00
" How many lines of indent value do we precompute.
In ` treesit-indent-region ' we indent in batches: precompute
indent for each line, apply them in one go, let parser reparse,
2022-11-22 04:40:49 +01:00
and do it again. This way the parser doesn 't need to unnecessarily
2022-10-28 17:54:43 -07:00
reparse after indenting every single line. " )
( defun treesit-indent-region ( beg end )
" Indent the region between BEG and END.
Similar to ` treesit-indent ', but indent a region instead. "
2022-11-04 01:31:56 -07:00
( treesit-update-ranges beg end )
2022-11-05 17:09:51 -07:00
;; We indent `treesit--indent-region-batch-size' lines at a time, to
;; reduce the number of times the parser needs to re-parse. In each
;; batch, we go through each line and calculate the anchor and
;; offset as usual, but instead of modifying the buffer, we save
;; these information in a vector. Once we've collected ANCHOR and
;; OFFSET for each line in the batch, we go through each line again
;; and apply the changes. Now that buffer is modified, we need to
;; reparse the buffer before continuing to indent the next batch.
2022-10-29 09:04:35 -07:00
( let* ( ( meta-len 2 )
( vector-len ( * meta-len treesit--indent-region-batch-size ) )
;; This vector saves the indent meta for each line in the
2022-11-05 17:09:51 -07:00
;; batch. It is a vector [ANCHOR OFFSET ANCHOR OFFSET...].
;; ANCHOR is a marker on the anchor position, and OFFSET is
;; an integer. ANCHOR and OFFSET are either both nil, or
;; both valid.
2022-10-29 09:04:35 -07:00
( meta-vec ( make-vector vector-len 0 ) )
( lines-left-to-move 0 )
( end ( copy-marker end t ) )
( idx 0 )
( starting-pos 0 )
( announce-progress ( > ( - end beg ) 80000 ) ) )
2022-10-28 17:54:43 -07:00
( save-excursion
( goto-char beg )
2022-10-29 09:04:35 -07:00
;; First pass. Go through each line and compute the
;; indentation.
2022-10-28 17:54:43 -07:00
( while ( and ( eq lines-left-to-move 0 ) ( < ( point ) end ) )
2022-10-29 09:04:35 -07:00
( setq idx 0
2022-10-28 17:54:43 -07:00
starting-pos ( point ) )
( while ( and ( eq lines-left-to-move 0 )
( < idx treesit--indent-region-batch-size )
( < ( point ) end ) )
2022-11-05 17:09:51 -07:00
( if ( looking-at ( rx ( * whitespace ) eol ) t )
;; Unlike in `indent-line' where we sometimes pre-indent
;; an empty line, We don't indent empty lines in
;; `indent-region'. Set ANCHOR and OFFSET to nil.
( setf ( aref meta-vec ( * idx meta-len ) ) nil
( aref meta-vec ( + 1 ( * idx meta-len ) ) ) nil )
( pcase-let* ( ( ` ( , anchor . , offset ) ( treesit--indent-1 ) )
( marker ( aref meta-vec ( * idx meta-len ) ) ) )
;; Set ANCHOR.
( when anchor
( if ( markerp marker )
( move-marker marker anchor )
( setf ( aref meta-vec ( * idx meta-len ) )
( copy-marker anchor t ) ) ) )
;; SET OFFSET.
( setf ( aref meta-vec ( + 1 ( * idx meta-len ) ) ) offset ) ) )
2022-10-28 17:54:43 -07:00
( cl-incf idx )
( setq lines-left-to-move ( forward-line 1 ) ) )
;; Now IDX = last valid IDX + 1.
( goto-char starting-pos )
2022-10-29 09:04:35 -07:00
;; Second pass, go to each line and apply the indentation.
( dotimes ( jdx idx )
( let ( ( anchor ( aref meta-vec ( * jdx meta-len ) ) )
( offset ( aref meta-vec ( + 1 ( * jdx meta-len ) ) ) ) )
( when offset
( let ( ( col ( save-excursion
( goto-char anchor )
( + offset ( current-column ) ) ) ) )
( indent-line-to col ) ) ) )
( forward-line 1 ) )
2022-10-28 17:54:43 -07:00
( when announce-progress
( message " Indenting region...%s%% "
2022-10-29 09:04:35 -07:00
( / ( * ( - ( point ) beg ) 100 ) ( - end beg ) ) ) ) )
;; Delete markers.
( dotimes ( idx treesit--indent-region-batch-size )
( let ( ( marker ( aref meta-vec ( * idx meta-len ) ) ) )
( when ( markerp marker )
( move-marker marker nil ) ) ) )
( move-marker end nil ) ) ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( defun treesit-simple-indent ( node parent bol )
" Calculate indentation according to `treesit-simple-indent-rules' .
BOL is the position of the first non-whitespace character on the
current line. NODE is the largest node that starts at BOL,
PARENT is NODE 's parent.
Return ( ANCHOR . OFFSET ) where ANCHOR is a node, OFFSET is the
indentation offset, meaning indent to align with ANCHOR and add
OFFSET. "
( if ( null parent )
2022-10-29 14:49:31 -07:00
( progn ( when treesit--indent-verbose
( message " PARENT is nil, not indenting " ) )
( cons nil nil ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( let* ( ( language ( treesit-node-language parent ) )
( rules ( alist-get language
treesit-simple-indent-rules ) ) )
( cl-loop for rule in rules
for pred = ( nth 0 rule )
for anchor = ( nth 1 rule )
for offset = ( nth 2 rule )
2022-10-26 18:31:43 -07:00
if ( treesit--simple-indent-eval
( list pred node parent bol ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
do ( when treesit--indent-verbose
( message " Matched rule: %S " rule ) )
and
2022-10-26 18:31:43 -07:00
return
( let ( ( anchor-pos
( treesit--simple-indent-eval
( list anchor node parent bol ) ) ) )
2022-11-05 18:32:01 -07:00
( cons anchor-pos ( if ( symbolp offset )
( symbol-value offset )
offset ) ) )
2022-10-27 16:44:33 -07:00
finally return
( progn ( when treesit--indent-verbose
( message " No matched rule " ) )
( cons nil nil ) ) ) ) ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( defun treesit-check-indent ( mode )
" Check current buffer's indentation against a major mode MODE.
Pop up a diff buffer showing the difference. Correct
indentation ( target ) is in green, current indentation is in red. "
( interactive " CTarget major mode: " )
( let ( ( source-buf ( current-buffer ) ) )
( with-temp-buffer
( insert-buffer-substring source-buf )
( funcall mode )
( indent-region ( point-min ) ( point-max ) )
( diff-buffers source-buf ( current-buffer ) ) ) ) )
2022-11-07 01:59:21 -08:00
( defun treesit--indent-rules-optimize ( rules )
" Optimize simple indent RULES.
RULES should be a value suitable for
` treesit-simple-indent-rules '. Return the optimized version of
RULES. "
2022-11-22 04:40:49 +01:00
;; Right now this function just compiles queries. It doesn't
2022-11-07 01:59:21 -08:00
;; byte-compile matchers and anchors because it doesn't make much
;; difference.
( cl-loop for setting in rules
for lang = ( car setting )
for indent-rules = ( cdr setting )
collect
( cl-labels
;; Optimize a matcher or anchor.
( ( optimize-func ( func )
( pcase func
( ` ( query , qry )
( list 'query ( treesit-query-compile lang qry ) ) )
( _ func ) ) )
;; Optimize a rule (MATCHER ANCHOR OFFSET).
( optimize-rule ( rule )
( let ( ( matcher ( nth 0 rule ) )
( anchor ( nth 1 rule ) )
( offset ( nth 2 rule ) ) )
( list ( optimize-func matcher )
( optimize-func anchor )
offset ) ) ) )
( cons lang ( mapcar #' optimize-rule indent-rules ) ) ) ) )
2022-05-13 16:39:29 -07:00
;;; Search
2022-05-13 14:00:56 -07:00
2022-09-24 19:35:11 -07:00
( defun treesit-search-forward-goto
2022-10-23 18:29:02 -07:00
( node predicate &optional start backward all )
2022-10-23 14:56:17 -07:00
" Search forward for a node and move to its end position.
2022-09-24 19:35:11 -07:00
2022-10-27 08:02:58 +02:00
Stop at the first node after NODE that matches PREDICATE.
2022-09-24 20:42:03 -07:00
PREDICATE can be either a regexp that matches against each node 's
2022-10-01 20:29:35 -07:00
type case-insensitively, or a function that takes a node and
returns nil/non-nil for match/no match.
2022-09-24 19:35:11 -07:00
If a node matches, move to that node and return the node,
2022-10-23 14:56:17 -07:00
otherwise return nil. If START is non-nil, stop at the
beginning rather than the end of a node.
2022-09-24 19:35:11 -07:00
2022-10-23 18:29:02 -07:00
This function guarantees that the matched node it returns makes
progress in terms of buffer position: the start/end position of
2022-11-23 12:07:07 -08:00
the returned node is always STRICTLY greater/less than that of
NODE.
2022-10-23 18:29:02 -07:00
2022-10-23 14:56:17 -07:00
BACKWARD and ALL are the same as in ` treesit-search-forward '. "
2022-10-26 15:34:02 -07:00
( when-let* ( ( start-pos ( if start
( treesit-node-start node )
( treesit-node-end node ) ) )
( current-pos start-pos ) )
2022-10-23 14:56:17 -07:00
;; When searching forward and stopping at beginnings, or search
;; backward stopping at ends, it is possible to "roll back" in
;; position. Take three nodes N1, N2, N3 as an example, if we
;; start at N3, search for forward for beginning, and N1 matches,
2022-10-23 18:29:02 -07:00
;; we would stop at beg of N1, which is backwards! So we skip N1
;; and keep going.
2022-10-23 14:56:17 -07:00
;;
;; |<--------N1------->|
;; |<--N2-->| |<--N3-->|
2022-09-24 20:42:03 -07:00
( while ( and node ( if backward
2022-10-26 15:34:02 -07:00
( >= current-pos start-pos )
( <= current-pos start-pos ) ) )
2022-09-24 20:42:03 -07:00
( setq node ( treesit-search-forward
2022-10-23 14:56:17 -07:00
node predicate backward all ) )
2022-10-26 15:34:02 -07:00
( setq current-pos ( if start
( treesit-node-start node )
( treesit-node-end node ) ) ) )
( cond
;; When there is a match and match made progress, go to the
;; result position.
( ( and node
( if backward
( < current-pos ( point ) )
( > current-pos ( point ) ) ) )
( goto-char current-pos ) ) )
2022-09-24 19:35:11 -07:00
node ) )
2022-05-13 14:00:56 -07:00
2022-12-24 22:08:17 -08:00
;;; Navigation, defun, things
2022-12-24 23:46:19 -08:00
;;
;; Emacs lets you define "things" by a regexp that matches the type of
;; a node, and here are some functions that lets you find the "things"
;; at/around point, navigate backward/forward a "thing", etc.
;;
;; The most obvious "thing" is a defun, and there are thin wrappers
;; around thing functions for defun for convenience.
;;
;; We have more command-like functions like:
;; - treesit-beginning-of-thing/defun
;; - treesit-end-of-thing/defun
;; - treesit-thing/defun-at-point
;;
;; And more generic functions like:
;; - treesit--things-around
;; - treesit--top-level-thing
;; - treesit--navigate-thing
;;
;; There are also some defun-specific functions, like
;; treesit-defun-name, treesit-add-log-current-defun.
;;
;; TODO: I'm not entirely sure how would this go, so I only documented
;; the "defun" functions and didn't document any "thing" functions.
;; We should also document `treesit-block-type-regexp' and support it
;; in major modes if we can meaningfully intergrate hideshow: I tried
;; and failed, we need SomeOne that understands hideshow to look at
;; it. (BTW, hideshow should use its own
;; `treesit-hideshow-block-type-regexp'.)
2022-10-19 16:44:04 -07:00
( defvar-local treesit-defun-type-regexp nil
" A regexp that matches the node type of defun nodes.
For example, \" ( function|class ) _definition\".
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
Sometimes not all nodes matched by the regexp are valid defuns.
In that case, set this variable to a cons cell of the
2022-12-26 01:39:02 -08:00
form ( REGEXP . PRED ) , where PRED is a function that takes a
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
node ( the matched node ) and returns t if node is valid, or nil
for invalid node.
2022-10-25 13:54:12 -07:00
This is used by ` treesit-beginning-of-defun ' and friends. " )
2022-10-19 16:44:04 -07:00
2022-12-24 22:08:17 -08:00
( defvar-local treesit-block-type-regexp nil
" Like `treesit-defun-type-regexp' , but for blocks. " )
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
( defvar-local treesit-defun-tactic 'nested
" Determines how does Emacs treat nested defuns.
2022-12-13 16:01:13 +02:00
If the value is ` top-level ', Emacs only moves across top-level
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
defuns, if the value is ` nested ', Emacs recognizes nested defuns. " )
( defvar-local treesit-defun-skipper #' treesit-default-defun-skipper
" A function called after tree-sitter navigation moved a step.
2022-12-13 15:59:29 -08:00
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
It is called with no arguments. By default, this function tries
to move to the beginning of a line, either by moving to the empty
2022-12-13 15:59:29 -08:00
newline after a defun, or the beginning of a defun.
If the value is nil , no skipping is performed. " )
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
2022-12-24 16:33:35 -08:00
( defvar-local treesit-defun-name-function nil
2022-12-25 09:29:47 +02:00
" A function that is called with a node and returns its defun name or nil.
If the node is a defun node, return the defun name, e.g., the
2022-12-24 16:33:35 -08:00
function name of a function. If the node is not a defun node, or
the defun node doesn 't have a name, or the node is nil , return
nil. " )
( defvar-local treesit-add-log-defun-delimiter " . "
" The delimiter used to connect several defun names.
This is used in ` treesit-add-log-current-defun '. " )
2022-12-24 20:17:08 -08:00
( defsubst treesit--thing-unpack-pattern ( pattern )
" Unpack PATTERN in the shape of `treesit-defun-type-regexp' .
Basically,
( unpack REGEXP ) = ( REGEXP . nil )
( unpack ( REGEXP . PRED ) ) = ( REGEXP . PRED ) "
( if ( consp pattern )
pattern
( cons pattern nil ) ) )
2022-12-24 22:08:17 -08:00
( defun treesit-beginning-of-thing ( pattern &optional arg )
" Like `beginning-of-defun' , but generalized into things.
PATTERN is like ` treesit-defun-type-regexp ', ARG
is the same as in ` beginning-of-defun '.
Return non-nil if successfully moved, nil otherwise. "
( pcase-let* ( ( arg ( or arg 1 ) )
( ` ( , regexp . , pred ) ( treesit--thing-unpack-pattern
pattern ) )
( dest ( treesit--navigate-thing
( point ) ( - arg ) 'beg regexp pred ) ) )
( when dest
( goto-char dest ) ) ) )
( defun treesit-end-of-thing ( pattern &optional arg )
" Like `end-of-defun' , but generalized into things.
PATTERN is like ` treesit-defun-type-regexp ', ARG is the same as
in ` end-of-defun '.
Return non-nil if successfully moved, nil otherwise. "
( pcase-let* ( ( arg ( or arg 1 ) )
( ` ( , regexp . , pred ) ( treesit--thing-unpack-pattern
pattern ) )
( dest ( treesit--navigate-thing
( point ) arg 'end regexp pred ) ) )
( when dest
( goto-char dest ) ) ) )
2022-10-19 16:44:04 -07:00
( defun treesit-beginning-of-defun ( &optional arg )
2022-12-15 17:44:07 -08:00
" Move backward to the beginning of a defun.
With argument ARG, do it that many times. Negative ARG means
move forward to the ARGth following beginning of defun.
If search is successful, return t , otherwise return nil.
This is a tree-sitter equivalent of ` beginning-of-defun '.
Behavior of this function depends on ` treesit-defun-type-regexp '
and ` treesit-defun-skipper '. "
( interactive " ^p " )
2022-12-24 22:08:17 -08:00
( when ( treesit-beginning-of-thing treesit-defun-type-regexp arg )
( when treesit-defun-skipper
( funcall treesit-defun-skipper ) )
t ) )
2022-12-15 17:44:07 -08:00
( defun treesit-end-of-defun ( &optional arg _ )
" Move forward to next end of defun.
With argument ARG, do it that many times.
Negative argument -N means move back to Nth preceding end of defun.
This is a tree-sitter equivalent of ` end-of-defun '. Behavior of
this function depends on ` treesit-defun-type-regexp ' and
` treesit-defun-skipper '. "
( interactive " ^p \n d " )
2022-12-24 22:08:17 -08:00
( when ( treesit-end-of-thing treesit-defun-type-regexp arg )
( when treesit-defun-skipper
( funcall treesit-defun-skipper ) ) ) )
2022-10-19 16:44:04 -07:00
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
( defun treesit-default-defun-skipper ( )
" Skips spaces after navigating a defun.
2022-12-13 16:01:13 +02:00
This function tries to move to the beginning of a line, either by
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
moving to the empty newline after a defun, or to the beginning of
the current line if the beginning of the defun is indented. "
( cond ( ( and ( looking-at ( rx ( * ( or " " " \\ t " ) ) " \n " ) )
( not ( looking-at ( rx bol ) ) ) )
( goto-char ( match-end 0 ) ) )
( ( save-excursion
( skip-chars-backward " \t " )
( eq ( point ) ( line-beginning-position ) ) )
( goto-char ( line-beginning-position ) ) ) ) )
;; prev-sibling:
;; 1. end-of-node before pos
;; 2. highest such node
;;
;; next-sibling:
;; 1. beg-of-node after pos
;; 2. highest such node
;;
;; parent:
;; 1. node covers pos
;; 2. smallest such node
2022-12-24 20:17:08 -08:00
( defun treesit--things-around ( pos regexp &optional pred )
" Return the previous, next, and parent thing around POS.
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
Return a list of ( PREV NEXT PARENT ) , where PREV and NEXT are
2022-12-24 20:17:08 -08:00
previous and next sibling things around POS, and PARENT is the
parent thing surrounding POS. All of three could be nil if no
sound things exists.
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
2022-12-24 20:17:08 -08:00
REGEXP and PRED are the same as in ` treesit-thing-at-point '. "
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
( let* ( ( node ( treesit-node-at pos ) )
2022-12-27 17:02:03 -08:00
( result ( list nil nil nil ) ) )
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
;; 1. Find previous and next sibling defuns.
( cl-loop
for idx from 0 to 1
for backward in ' ( t nil )
2022-12-27 17:02:03 -08:00
;; Make sure we go in the right direction, and the defun we find
;; doesn't cover POS.
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
for pos-pred in ( list ( lambda ( n ) ( <= ( treesit-node-end n ) pos ) )
( lambda ( n ) ( >= ( treesit-node-start n ) pos ) ) )
2022-12-27 17:02:03 -08:00
;; We repeatedly find next defun candidate with
;; `treesit-search-forward', and check if it is a valid defun,
;; until the node we find covers POS, meaning we've gone through
;; every possible sibling defuns. But there is a catch:
;; `treesit-search-forward' searches bottom-up, so for each
;; candidate we need to go up the tree and find the top-most
;; valid sibling, this defun will be at the same level as POS.
;; Don't use `treesit-search-forward-goto', it skips nodes in
;; order to enforce progress.
when node
do ( let ( ( cursor node )
( iter-pred ( lambda ( node )
( and ( string-match-p
regexp ( treesit-node-type node ) )
( or ( null pred ) ( funcall pred node ) )
( funcall pos-pred node ) ) ) ) )
;; Find the node just before/after POS to start searching.
( save-excursion
( while ( and cursor ( not ( funcall pos-pred cursor ) ) )
( setq cursor ( treesit-search-forward-goto
cursor " " backward backward t ) ) ) )
;; Keep searching until we run out of candidates.
( while ( and cursor
( funcall pos-pred cursor )
( null ( nth idx result ) ) )
( setf ( nth idx result )
( treesit-node-top-level cursor iter-pred t ) )
( setq cursor ( treesit-search-forward
cursor regexp backward backward ) ) ) ) )
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
;; 2. Find the parent defun.
2022-12-27 17:02:03 -08:00
( let ( ( cursor ( or ( nth 0 result ) ( nth 1 result ) node ) )
( iter-pred ( lambda ( node )
( and ( string-match-p
regexp ( treesit-node-type node ) )
( or ( null pred ) ( funcall pred node ) )
( not ( treesit-node-eq node ( nth 0 result ) ) )
( not ( treesit-node-eq node ( nth 1 result ) ) )
( < ( treesit-node-start node )
pos
( treesit-node-end node ) ) ) ) ) )
( setf ( nth 2 result )
( treesit-parent-until cursor iter-pred ) ) )
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
result ) )
2022-12-24 20:17:08 -08:00
( defun treesit--top-level-thing ( node regexp &optional pred )
" Return the top-level parent thing of NODE.
REGEXP and PRED are the same as in ` treesit-thing-at-point '. "
2022-12-27 17:02:03 -08:00
( treesit-node-top-level
node ( lambda ( node )
( and ( string-match-p regexp ( treesit-node-type node ) )
( or ( null pred ) ( funcall pred node ) ) ) )
t ) )
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
2022-12-13 15:59:29 -08:00
;; The basic idea for nested defun navigation is that we first try to
;; move across sibling defuns in the same level, if no more siblings
;; exist, we move to parents's beg/end, rinse and repeat. We never
;; move into a defun, only outwards.
;;
;; Let me describe roughly what does this function do: there are four
;; possible operations: prev-beg, next-end, prev-end, next-beg, and
;; each of (prev-sibling next-sibling and parent) could exist or not
;; exist. So there are 4 times 8 = 32 situations.
;;
;; I'll only describe the situation when we go backward (prev-beg &
;; prev-end), and consider only prev-sibling & parent. Deriving the
;; reverse situations is left as an exercise for the reader.
;;
;; prev-beg (easy case):
;; 1. prev-sibling or parent exists
;; -> go the prev-sibling/parent's beg
;;
;; prev-end (tricky):
;; 1. prev-sibling exists
;; -> If you think about it, we are already at prev-sibling's end!
;; So we need to go one step further, either to
;; prev-prev-sibling's end, or parent's prev-sibling's end, etc.
;; 2. prev-sibling is nil but parent exists
;; -> Obviously we don't want to go to parent's end, instead, we
;; want to go to parent's prev-sibling's end. Again, we recurse
;; in the function to do that.
2022-12-24 20:17:08 -08:00
( defun treesit--navigate-thing ( pos arg side regexp &optional pred recursing )
" Navigate thing ARG steps from POS.
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
If ARG is positive, move forward that many steps, if negative,
move backward. If SIDE is ` beg ', stop at the beginning of a
2022-12-24 20:17:08 -08:00
thing, if SIDE is ` end ', stop at the end.
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
2022-12-12 08:50:21 +01:00
This function doesn 't actually move point, it just returns the
2022-12-24 20:17:08 -08:00
position it would move to. If there aren 't enough things to move
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
across, return nil.
2022-12-24 20:17:08 -08:00
REGEXP and PRED are the same as in ` treesit-thing-at-point '.
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
RECURSING is an internal parameter, if non-nil, it means this
function is called recursively. "
( pcase-let*
( ( counter ( abs arg ) )
;; Move POS to the beg/end of NODE. If NODE is nil, terminate.
;; Return the position we moved to.
( advance ( lambda ( node )
( let ( ( dest ( pcase side
( 'beg ( treesit-node-start node ) )
( 'end ( treesit-node-end node ) ) ) ) )
( if ( null dest )
( throw 'term nil )
dest ) ) ) ) )
( catch 'term
( while ( > counter 0 )
( pcase-let
( ( ` ( , prev , next , parent )
2022-12-24 20:17:08 -08:00
( treesit--things-around pos regexp pred ) ) )
2022-12-13 15:59:29 -08:00
;; When PARENT is nil, nested and top-level are the same, if
;; there is a PARENT, make PARENT to be the top-level parent
;; and pretend there is no nested PREV and NEXT.
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
( when ( and ( eq treesit-defun-tactic 'top-level )
parent )
2022-12-24 20:17:08 -08:00
( setq parent ( treesit--top-level-thing
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
parent regexp pred )
prev nil
next nil ) )
;; Move...
( if ( > arg 0 )
;; ...forward.
( if ( and ( eq side 'beg )
;; Should we skip the defun (recurse)?
( cond ( next ( not recursing ) ) ; [1] (see below)
( parent t ) ; [2]
( t nil ) ) )
;; Special case: go to next beg-of-defun. Set POS
2022-12-13 15:59:29 -08:00
;; to the end of next-sib/parent defun, and run one
;; more step. If there is a next-sib defun, we only
;; need to recurse once, so we don't need to recurse
;; if we are already recursing [1]. If there is no
;; next-sib but a parent, keep stepping out
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
;; (recursing) until we got out of the parents until
;; (1) there is a next sibling defun, or (2) no more
;; parents [2].
2022-12-24 20:17:08 -08:00
( setq pos ( or ( treesit--navigate-thing
2022-12-13 15:59:29 -08:00
( treesit-node-end ( or next parent ) )
2022-12-24 20:17:08 -08:00
1 'beg regexp pred t )
2022-12-13 15:59:29 -08:00
( throw 'term nil ) ) )
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
;; Normal case.
( setq pos ( funcall advance ( or next parent ) ) ) )
;; ...backward.
( if ( and ( eq side 'end )
( cond ( prev ( not recursing ) )
( parent t )
( t nil ) ) )
;; Special case: go to prev end-of-defun.
2022-12-24 20:17:08 -08:00
( setq pos ( or ( treesit--navigate-thing
2022-12-13 15:59:29 -08:00
( treesit-node-start ( or prev parent ) )
2022-12-24 20:17:08 -08:00
-1 'end regexp pred t )
2022-12-13 15:59:29 -08:00
( throw 'term nil ) ) )
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
;; Normal case.
( setq pos ( funcall advance ( or prev parent ) ) ) ) )
;; A successful step! Decrement counter.
( cl-decf counter ) ) ) )
;; Counter equal to 0 means we successfully stepped ARG steps.
2022-12-13 15:59:29 -08:00
( if ( eq counter 0 ) pos nil ) ) )
Add improved tree-sitter navigation
This new set of functions (and tests) should eliminate
defun-navigation bugs and limitations we currently have. This commit
doesn't change any existing bahavior: treesit-beginning/end-of-defun
and friends are unchanged. The plan is to later switch gear and
replace the current functions with the new ones introduced in this
change.
This is a relatively big change, but I've setup a comprehensive test,
and it should fix current bugs, so I think it's ok to put it on the
release branch.
The gist of the new navigation is to use treesit--defuns-around to
find the previous sibling defun, next sibling defun, and the parent
defun, then use this information to move to previous/next
beginning/end of defun in treesit--navigate-defun.
I also added comprehensive testing that tests all four possible
operations (prev-beg, next-beg, prev-end, next-end) starting at all
possible positions (between two sibling defuns, inside a sibling
defun, etc).
* lisp/treesit.el (treesit-defun-type-regexp): Expand definition to
allow (REGEXP . FILTER). Old functions don't support this, but it
should be fine since we are soon replacing them.
(treesit-defun-tactic)
(treesit-defun-skipper): New variables.
(treesit-default-defun-skipper)
(treesit--defuns-around)
(treesit--top-level-defun)
(treesit--navigate-defun): New functions.
* test/src/treesit-tests.el (treesit--ert-insert-and-parse-marker)
(treesit--ert-collect-positions)
(treesit--ert-test-defun-navigation): New helper functions.
(treesit--ert-defun-navigation-python-program)
(treesit--ert-defun-navigation-js-program)
(treesit--ert-defun-navigation-bash-program)
(treesit--ert-defun-navigation-nested-master): New variables.
(treesit-defun-navigation-nested-1)
(treesit-defun-navigation-nested-2)
(treesit-defun-navigation-nested-3)
(treesit-defun-navigation-top-level): New tests.
2022-12-12 20:25:53 -08:00
2022-12-20 21:22:30 -08:00
;; TODO: In corporate into thing-at-point.
2022-12-24 22:08:17 -08:00
( defun treesit-thing-at-point ( pattern tactic )
2022-12-24 20:17:08 -08:00
" Return the thing node at point or nil if none is found.
2022-12-24 22:08:17 -08:00
\"Thing\" is defined by PATTERN, which can be either a string
REGEXP or a cons cell ( REGEXP . PRED ) : if a node 's type matches
REGEXP, it is a thing. The \"thing\" could be further restricted
by PRED: if non-nil, PRED should be a function that takes a node
and returns t if the node is a \"thing\", and nil if not.
2022-12-24 20:17:08 -08:00
Return the top-level defun if TACTIC is ` top-level ', return the
immediate parent thing if TACTIC is ` nested '. "
2022-12-24 22:08:17 -08:00
( pcase-let* ( ( ` ( , regexp . , pred )
( treesit--thing-unpack-pattern pattern ) )
( ` ( , _ , next , parent )
2022-12-24 20:17:08 -08:00
( treesit--things-around ( point ) regexp pred ) )
;; If point is at the beginning of a thing, we
;; prioritize that thing over the parent in nested
;; mode.
( node ( or ( and ( eq ( treesit-node-start next ) ( point ) )
next )
parent ) ) )
( if ( eq tactic 'top-level )
( treesit--top-level-thing node regexp pred )
node ) ) )
2022-12-20 21:22:30 -08:00
( defun treesit-defun-at-point ( )
2022-12-24 16:40:00 -08:00
" Return the defun node at point or nil if none is found.
2022-12-20 21:22:30 -08:00
Respects ` treesit-defun-tactic ' : return the top-level defun if it
is ` top-level ', return the immediate parent defun if it is
2022-12-24 16:40:00 -08:00
` nested '.
Return nil if ` treesit-defun-type-regexp ' is not set. "
( when treesit-defun-type-regexp
2022-12-24 22:08:17 -08:00
( treesit-thing-at-point
treesit-defun-type-regexp treesit-defun-tactic ) ) )
2022-12-24 16:40:00 -08:00
2022-12-24 16:33:35 -08:00
( defun treesit-defun-name ( node )
" Return the defun name of NODE.
Return nil if there is no name, or if NODE is not a defun node,
or if NODE is nil.
If ` treesit-defun-name-function ' is nil , always return nil. "
( when treesit-defun-name-function
( funcall treesit-defun-name-function node ) ) )
( defun treesit-add-log-current-defun ( )
" Return the name of the defun at point.
Used for ` add-log-current-defun-function '.
The delimiter between nested defun names is controlled by
` treesit-add-log-defun-delimiter '. "
( let ( ( node ( treesit-defun-at-point ) )
( name nil ) )
( while node
( when-let ( ( new-name ( treesit-defun-name node ) ) )
( if name
( setq name ( concat new-name
treesit-add-log-defun-delimiter
name ) )
( setq name new-name ) ) )
( setq node ( treesit-node-parent node ) ) )
name ) )
2022-12-20 21:22:30 -08:00
2022-12-27 20:37:29 -08:00
;;; Imenu
( defvar treesit-simple-imenu-settings nil
" Settings that configure `treesit-simple-imenu' .
It should be a list of ( CATEGORY REGEXP PRED NAME-FN ) .
CATEGORY is the name of a category, like \"Function\", \"Class\",
etc. REGEXP should be a regexp matching the type of nodes that
belong to CATEGORY. PRED should be either nil or a function
that takes a node an the argument. It should return non-nil if
the node is a valid node for CATEGORY, or nil if not.
CATEGORY could also be nil. In that case the entries matched by
REGEXP and PRED are not grouped under CATEGORY.
NAME-FN should be either nil or a function that takes a defun
node and returns the name of that defun node. If NAME-FN is nil ,
` treesit-defun-name ' is used.
` treesit-major-mode-setup ' automatically sets up Imenu if this
variable is non-nil. " )
( defun treesit--simple-imenu-1 ( node pred name-fn )
" Given a sparse tree, create an Imenu index.
NODE is a node in the tree returned by
` treesit-induce-sparse-tree ' ( not a tree-sitter node, its car is
a tree-sitter node ) . Walk that tree and return an Imenu index.
2022-12-28 17:37:46 +01:00
Return a list of entries where each ENTRY has the form:
2022-12-27 20:37:29 -08:00
ENTRY := ( NAME . MARKER )
| ( NAME . ( ( \" \" . MARKER )
ENTRY
. . . )
PRED and NAME-FN are the same as described in
` treesit-simple-imenu-settings '. NAME-FN computes NAME in an
ENTRY. MARKER marks the start of each tree-sitter node. "
( let* ( ( ts-node ( car node ) )
( children ( cdr node ) )
( subtrees ( mapcan ( lambda ( node )
( treesit--simple-imenu-1 node pred name-fn ) )
children ) )
;; The root of the tree could have a nil ts-node.
( name ( when ts-node
( or ( if name-fn
( funcall name-fn ts-node )
( treesit-defun-name ts-node ) )
" Anonymous " ) ) )
( marker ( when ts-node
( set-marker ( make-marker )
( treesit-node-start ts-node ) ) ) ) )
( cond
;; The tree-sitter node in the root node of the tree returned by
;; `treesit-induce-sparse-tree' is often nil.
( ( null ts-node )
subtrees )
;; This tree-sitter node is not a valid entry, skip it.
( ( and pred ( not ( funcall pred ts-node ) ) )
subtrees )
;; Non-leaf node, return a (list of) subgroup.
( subtrees
` ( ( , name
, ( cons " " marker )
,@ subtrees ) ) )
;; Leaf node, return a (list of) plain index entry.
( t ( list ( cons name marker ) ) ) ) ) )
( defun treesit-simple-imenu ( )
" Return an Imenu index for the current buffer. "
( let ( ( root ( treesit-buffer-root-node ) ) )
( mapcan ( lambda ( setting )
( pcase-let ( ( ` ( , category , regexp , pred , name-fn )
setting ) )
( when-let* ( ( tree ( treesit-induce-sparse-tree
root regexp ) )
( index ( treesit--simple-imenu-1
tree pred name-fn ) ) )
( if category
( list ( cons category index ) )
index ) ) ) )
treesit-simple-imenu-settings ) ) )
2022-10-19 16:44:04 -07:00
;;; Activating tree-sitter
2022-11-19 18:53:53 -08:00
( defun treesit-ready-p ( language &optional quiet )
2022-11-05 10:49:46 +02:00
" Check whether tree-sitter is ready to be used for MODE and LANGUAGE.
LANGUAGE is the language symbol to check for availability.
It can also be a list of language symbols.
2022-10-25 13:54:12 -07:00
2022-11-19 18:53:53 -08:00
If tree-sitter is not ready, emit a warning and return nil. If
2022-11-20 10:30:38 +02:00
the user has chosen to activate tree-sitter for LANGUAGE and
2022-11-19 18:53:53 -08:00
tree-sitter is ready, return non-nil. If QUIET is t , don 't emit
2022-11-22 04:40:49 +01:00
a warning in either case ; if quiet is `message', display a message
instead of emitting a warning. "
2022-10-25 13:54:12 -07:00
( let ( ( language-list ( if ( consp language )
language
( list language ) ) )
msg )
2022-10-19 16:44:04 -07:00
;; Check for each condition and set MSG.
2022-11-19 18:53:53 -08:00
( catch 'term
( when ( not ( treesit-available-p ) )
( setq msg " tree-sitter library is not compiled with Emacs " )
( throw 'term nil ) )
2022-11-22 17:29:26 -08:00
( when ( > ( position-bytes ( max ( point-min ) ( 1- ( point-max ) ) ) )
treesit-max-buffer-size )
2022-11-19 18:53:53 -08:00
( setq msg " buffer larger than `treesit-max-buffer-size' " )
( throw 'term nil ) )
( dolist ( lang language-list )
( pcase-let ( ( ` ( , available . , err )
( treesit-language-available-p lang t ) ) )
( when ( not available )
( setq msg ( format " language definition for %s is unavailable (%s): %s "
lang ( nth 0 err )
( string-join
( mapcar ( lambda ( x ) ( format " %s " x ) )
( cdr err ) )
" " ) ) )
( throw 'term nil ) ) ) ) )
;; Decide if all conditions met and whether emit a warning.
( if ( not msg )
t
( setq msg ( concat " Cannot activate tree-sitter, because " msg ) )
( pcase quiet
( 'nil ( display-warning 'treesit msg ) )
( 'message ( message " %s " msg ) ) )
nil ) ) )
2022-10-25 13:54:12 -07:00
( defun treesit-major-mode-setup ( )
2022-10-19 16:44:04 -07:00
" Activate tree-sitter to power major-mode features.
2022-10-25 13:54:12 -07:00
If ` treesit-font-lock-settings ' is non-nil, setup fontification and
enable ` font-lock-mode '.
If ` treesit-simple-indent-rules ' is non-nil, setup indentation.
If ` treesit-defun-type-regexp ' is non-nil, setup
2022-11-15 02:30:50 -08:00
` beginning/end-of-defun ' functions.
2022-12-27 20:37:29 -08:00
If ` treesit-defun-name-function ' is non-nil, setup
` add-log-current-defun '.
If ` treesit-simple-imenu-settings ' is non-nil, setup Imenu.
2022-11-15 02:30:50 -08:00
Make sure necessary parsers are created for the current buffer
before calling this function. "
2022-10-25 13:54:12 -07:00
;; Font-lock.
( when treesit-font-lock-settings
;; `font-lock-mode' wouldn't setup properly if
;; `font-lock-defaults' is nil, see `font-lock-specified-p'.
2022-10-30 23:59:56 -07:00
( setq-local font-lock-defaults
' ( nil nil nil nil
( font-lock-fontify-syntactically-function
. treesit-font-lock-fontify-region ) ) )
2022-10-25 13:54:12 -07:00
( font-lock-mode 1 )
2022-11-15 02:30:50 -08:00
( treesit-font-lock-recompute-features )
( dolist ( parser ( treesit-parser-list ) )
( treesit-parser-add-notifier
parser #' treesit--font-lock-notifier ) ) )
2022-10-25 13:54:12 -07:00
;; Indent.
( when treesit-simple-indent-rules
2022-11-07 01:59:21 -08:00
( setq-local treesit-simple-indent-rules
( treesit--indent-rules-optimize
treesit-simple-indent-rules ) )
2022-10-28 17:54:43 -07:00
( setq-local indent-line-function #' treesit-indent )
( setq-local indent-region-function #' treesit-indent-region ) )
2022-10-25 13:54:12 -07:00
;; Navigation.
( when treesit-defun-type-regexp
2022-12-15 17:44:07 -08:00
( keymap-set ( current-local-map ) " <remap> <beginning-of-defun> "
#' treesit-beginning-of-defun )
( keymap-set ( current-local-map ) " <remap> <end-of-defun> "
2022-12-22 00:41:58 -08:00
#' treesit-end-of-defun )
;; `end-of-defun' will not work completely correctly in nested
;; defuns due to its implementation. However, many lisp programs
;; use `beginning/end-of-defun', so we should still set
;; `beginning/end-of-defun-function' so they still mostly work.
;; This is also what `cc-mode' does: rebind user commands and set
;; the variables. In future we should update `end-of-defun' to
;; work with nested defuns.
( setq-local beginning-of-defun-function #' treesit-beginning-of-defun )
2022-12-24 16:33:35 -08:00
( setq-local end-of-defun-function #' treesit-end-of-defun ) )
;; Defun name.
( when treesit-defun-name-function
( setq-local add-log-current-defun-function
2022-12-27 20:37:29 -08:00
#' treesit-add-log-current-defun ) )
;; Imenu.
( when treesit-simple-imenu-settings
( setq-local imenu-create-index-function
#' treesit-simple-imenu ) ) )
2022-10-19 16:44:04 -07:00
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
;;; Debugging
( defvar-local treesit--inspect-name nil
2022-10-27 08:02:58 +02:00
" Used by `treesit-inspect-mode' to show node name in mode-line. " )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( defun treesit-inspect-node-at-point ( &optional arg )
" Show information of the node at point.
If called interactively, show in echo area, otherwise set
` treesit--inspect-name ' ( which will appear in the mode-line
if ` treesit-inspect-mode ' is enabled ) . Uses the first parser
2022-10-27 08:02:58 +02:00
in ` treesit-parser-list '. "
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( interactive " p " )
;; NODE-LIST contains all the node that starts at point.
( let* ( ( node-list
( cl-loop for node = ( treesit-node-at ( point ) )
then ( treesit-node-parent node )
while node
if ( eq ( treesit-node-start node )
( point ) )
collect node ) )
( largest-node ( car ( last node-list ) ) )
( parent ( treesit-node-parent largest-node ) )
2022-12-15 02:41:20 +01:00
;; node-list-ascending contains all the node bottom-up, then
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
;; the parent.
2022-12-15 02:41:20 +01:00
( node-list-ascending
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( if ( null largest-node )
;; If there are no nodes that start at point, just show
;; the node at point and its parent.
( list ( treesit-node-at ( point ) )
( treesit-node-parent
( treesit-node-at ( point ) ) ) )
( append node-list ( list parent ) ) ) )
( name " " ) )
;; We draw nodes like (parent field-name: (node)) recursively,
;; so it could be (node1 field-name: (node2 field-name: (node3))).
2022-12-15 02:41:20 +01:00
( dolist ( node node-list-ascending )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( setq
name
( concat
( if ( treesit-node-field-name node )
( format " %s: " ( treesit-node-field-name node ) )
" " )
( if ( treesit-node-check node 'named ) " ( " " \" " )
2022-11-04 10:50:55 -07:00
( propertize ( or ( treesit-node-type node ) " N/A " )
'face
( if ( treesit-node-eq node largest-node )
'bold nil ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
name
( if ( treesit-node-check node 'named ) " ) " " \" " ) ) ) )
( setq treesit--inspect-name name )
( force-mode-line-update )
( when arg
( if node-list
( message " %s " treesit--inspect-name )
( message " No node at point " ) ) ) ) )
( define-minor-mode treesit-inspect-mode
2022-11-05 10:49:46 +02:00
" Minor mode that displays in the mode-line the node which starts at point.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-11-05 10:49:46 +02:00
When this mode is enabled, the mode-line displays
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-11-05 10:49:46 +02:00
PARENT FIELD-NAME: ( NODE FIELD-NAME: ( CHILD ( . . . ) ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-11-05 16:52:38 -07:00
where NODE, CHILD, etc, are nodes which begin at point. PARENT
is the parent of NODE. NODE is displayed in bold typeface.
FIELD-NAMEs are field names of NODE and CHILD, etc ( see Info
node ` ( elisp ) Language Definitions ', heading \"Field names\" ) .
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
If no node starts at point, i.e., point is in the middle of a
2022-11-05 10:49:46 +02:00
node, then the mode line displays the earliest node that spans point,
and its immediate parent.
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-11-05 10:49:46 +02:00
This minor mode doesn 't create parsers on its own. It uses the first
parser in ` treesit-parser-list '. "
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
:lighter nil
( if treesit-inspect-mode
( progn
( add-hook 'post-command-hook
#' treesit-inspect-node-at-point 0 t )
( add-to-list 'mode-line-misc-info
' ( :eval treesit--inspect-name ) ) )
( remove-hook 'post-command-hook
#' treesit-inspect-node-at-point t )
( setq mode-line-misc-info
( remove ' ( :eval treesit--inspect-name )
mode-line-misc-info ) ) ) )
2022-06-14 14:30:39 -07:00
( defun treesit-query-validate ( language query )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
" Check if QUERY is valid for LANGUAGE.
2022-10-27 08:02:58 +02:00
If QUERY is invalid, display the query in a popup buffer, jump
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
to the offending pattern and highlight the pattern. "
2022-06-14 14:30:39 -07:00
( cl-assert ( or ( consp query ) ( stringp query ) ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( let ( ( buf ( get-buffer-create " *tree-sitter check query* " ) ) )
( with-temp-buffer
2022-06-16 01:11:09 -07:00
( treesit-parser-create language )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( condition-case err
2022-09-30 17:20:34 -07:00
( progn ( treesit-query-capture language query )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( message " QUERY is valid " ) )
( treesit-query-error
( with-current-buffer buf
( let* ( ( data ( cdr err ) )
( message ( nth 0 data ) )
( start ( nth 1 data ) ) )
( erase-buffer )
2022-09-20 18:02:16 -07:00
( insert ( treesit-query-expand query ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( goto-char start )
( search-forward " " nil t )
( put-text-property start ( point ) 'face 'error )
( message " %s " ( buffer-substring start ( point ) ) )
( goto-char ( point-min ) )
( insert ( format " %s: %d \n " message start ) )
( forward-char start ) ) )
2022-12-17 20:23:02 +01:00
( pop-to-buffer buf )
( view-mode ) ) ) ) ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
2022-11-15 20:29:07 -08:00
;;; Explorer
2022-12-11 14:43:58 -08:00
( defface treesit-explorer-anonymous-node
( let ( ( display t )
( atts ' ( :inherit shadow ) ) )
` ( ( , display . , atts ) ) )
" Face for anonymous nodes in tree-sitter explorer. " )
( defface treesit-explorer-field-name
( let ( ( display t )
( atts nil ) )
` ( ( , display . , atts ) ) )
" Face for field names in tree-sitter explorer. " )
2022-11-15 20:29:07 -08:00
( defvar-local treesit--explorer-buffer nil
" Buffer used to display the syntax tree. " )
( defvar-local treesit--explorer-source-buffer nil
" Source buffer corresponding to the playground buffer. " )
( defvar-local treesit--explorer-language nil
" The language used in the playground. " )
( defvar-local treesit--explorer-refresh-timer nil
" Timer for refreshing the syntax tree buffer. " )
( defvar-local treesit--explorer-highlight-overlay nil
" Overlay used to highlight in syntax tree and source buffer. " )
( defvar-local treesit--explorer-last-node nil
" Last top-level node used to generate syntax tree. " )
( defvar treesit-explore-mode )
( defun treesit--explorer--nodes-to-highlight ( language )
" Return nodes for LANGUAGE covered in region.
2022-11-21 22:33:10 -08:00
This function tries to return the largest node possible. If the
region covers exactly one node, that node is returned ( in a
list ) . If the region covers more than one node, two nodes are
returned: the very first one in the region and the very last one
in the region. "
2022-11-15 20:29:07 -08:00
( let* ( ( beg ( region-beginning ) )
( end ( region-end ) )
( node ( treesit-node-on beg end language ) )
( node ( or ( treesit-parent-while
node
( lambda ( n )
( <= beg ( treesit-node-start n )
( treesit-node-end n ) end ) ) )
node ) ) )
;; If NODE is completely contained in the region, return NODE,
;; otherwise return its children that are in the region.
( if ( <= beg ( treesit-node-start node )
( treesit-node-end node ) end )
( list node )
( list ( treesit-node-at beg )
( treesit-search-forward
( treesit-node-at end )
( lambda ( n )
( <= ( treesit-node-end n ) end ) )
t t ) ) ) ) )
( defun treesit--explorer-refresh ( )
" Update the syntax tree buffer. "
( when ( and treesit-explore-mode
( buffer-live-p treesit--explorer-buffer ) )
( let* ( ( root ( treesit-node-on
( window-start ) ( window-end ) treesit--explorer-language ) )
;; Only highlight the current top-level construct.
;; Highlighting the whole buffer is slow and unnecessary.
( top-level ( treesit-node-first-child-for-pos
root ( if ( eolp )
( max ( point-min ) ( 1- ( point ) ) )
( point ) )
t ) )
;; Only highlight node when region is active, if we
;; highlight node at point the syntax tree is too jumpy.
( nodes-hl
( when ( region-active-p )
( treesit--explorer--nodes-to-highlight
treesit--explorer-language ) ) )
;; If we didn't edit the buffer nor change the top-level
;; node, don't redraw the whole syntax tree.
( highlight-only ( treesit-node-eq
top-level treesit--explorer-last-node ) )
( source-buffer ( current-buffer ) ) )
( setq-local treesit--explorer-last-node top-level )
( with-current-buffer treesit--explorer-buffer
( let ( ( inhibit-read-only t ) )
( setq-local treesit--explorer-source-buffer source-buffer )
;; Redraw the syntax tree or just rehighlight the focused
;; node.
( when ( and top-level ( not highlight-only ) )
( erase-buffer )
( treesit--explorer-draw-node top-level ) )
( when-let ( ( pos ( treesit--explorer-highlight-node nodes-hl ) )
( window ( get-buffer-window
treesit--explorer-buffer ) ) )
( if highlight-only
( goto-char pos )
;; If HIGHLIGHT-ONLY is nil, we erased the buffer and
;; re-inserted text, scroll down from the very top until
;; we can see the highlighted node.
( goto-char ( point-min ) )
( while ( and ( null ( pos-visible-in-window-p pos window ) )
( = ( forward-line 4 ) 0 ) )
( set-window-start window ( point ) ) ) )
( set-window-point window pos ) ) ) ) ) ) )
( defun treesit--explorer-post-command ( &rest _ )
" Post-command function that runs in the source buffer. "
( when treesit-explore-mode
( when treesit--explorer-highlight-overlay
( delete-overlay treesit--explorer-highlight-overlay ) )
( when treesit--explorer-refresh-timer
( cancel-timer treesit--explorer-refresh-timer ) )
( setq-local treesit--explorer-refresh-timer
( run-with-timer 0.1 nil #' treesit--explorer-refresh ) ) ) )
( defun treesit--explorer-jump ( button )
" Mark the original text corresponding to BUTTON. "
( interactive )
( when ( and ( derived-mode-p 'treesit--explorer-tree-mode )
( buffer-live-p treesit--explorer-source-buffer ) )
( with-current-buffer treesit--explorer-source-buffer
( let ( ( start ( button-get button 'node-start ) )
( end ( button-get button 'node-end ) ) )
( when treesit--explorer-highlight-overlay
( delete-overlay treesit--explorer-highlight-overlay ) )
( setq-local treesit--explorer-highlight-overlay
( make-overlay start end nil t nil ) )
( overlay-put treesit--explorer-highlight-overlay
'face 'highlight ) ) ) ) )
( defun treesit--explorer-highlight-node ( nodes )
" Highlight nodes in NODES in the syntax tree buffer.
Return the start of the syntax tree text corresponding to NODE. "
( when treesit--explorer-highlight-overlay
( delete-overlay treesit--explorer-highlight-overlay ) )
( let ( ( start-node ( car nodes ) )
( end-node ( car ( last nodes ) ) )
start end )
( when ( and start-node end-node )
( cl-loop for ov in ( overlays-in ( point-min ) ( point-max ) )
while ( or ( null start ) ( null end ) )
if ( treesit-node-eq start-node
( overlay-get ov 'treesit-node ) )
do ( setq start ( overlay-start ov ) )
if ( treesit-node-eq end-node ( overlay-get ov 'treesit-node ) )
do ( setq end ( overlay-end ov ) ) )
( when ( and start end )
( setq-local treesit--explorer-highlight-overlay
( make-overlay start end ) )
( overlay-put treesit--explorer-highlight-overlay
'face 'highlight )
start ) ) ) )
( defun treesit--explorer-draw-node ( node )
" Draw the syntax tree of NODE.
2022-11-21 22:33:10 -08:00
When this function is called, point should be at the position
where the node should start. When this function returns, it
leaves point at the end of the last line of NODE. "
2022-11-15 20:29:07 -08:00
( let* ( ( type ( treesit-node-type node ) )
( field-name ( treesit-node-field-name node ) )
( children ( treesit-node-children node ) )
( named ( treesit-node-check node 'named ) )
;; Column number of the start of the field-name, aka start of
;; the whole node.
( before-field-column ( current-column ) )
;; Column number after the field-name.
after-field-column
;; Column number after the type.
after-type-column
;; Are all children suitable for inline?
( all-children-inline
( eq 0 ( apply #' + ( mapcar #' treesit-node-child-count children ) ) ) )
;; If the child is the first child, we can inline, if the
;; previous child is suitable for inline, this child can
;; inline, if the previous child is not suitable for inline,
;; this child cannot inline.
( can-inline t )
;; The beg and end of this node.
beg end )
( when treesit--explorer-highlight-overlay
( delete-overlay treesit--explorer-highlight-overlay ) )
( setq beg ( point ) )
;; Draw field name. If all children are suitable for inline, we
;; draw everything in one line, other wise draw field name and the
;; rest of the node in two lines.
( when field-name
2022-12-11 14:43:58 -08:00
( insert ( propertize ( concat field-name " : " )
'face 'treesit-explorer-field-name ) )
2022-11-15 20:29:07 -08:00
( when ( and children ( not all-children-inline ) )
( insert " \n " )
( indent-to-column ( 1+ before-field-column ) ) ) )
( setq after-field-column ( current-column ) )
;; Draw type.
( if named
( progn
( insert " ( " )
( insert-text-button
type 'action #' treesit--explorer-jump
'follow-link t
'node-start ( treesit-node-start node )
'node-end ( treesit-node-end node ) ) )
( pcase type
( " \n " ( insert " \\ n " ) )
( " \t " ( insert " \\ t " ) )
( " " ( insert " SPC " ) )
( _ ( insert type ) ) ) )
( setq after-type-column ( current-column ) )
;; Draw children.
( dolist ( child children )
;; If a child doesn't have children, it is suitable for inline.
( let ( ( draw-inline ( eq 0 ( treesit-node-child-count child ) ) )
( children-indent ( 1+ after-field-column ) ) )
( while
;; This form returns t if it wants to run another
;; iteration, returns nil if it wants to stop.
( if ( and draw-inline can-inline )
;; Draw children on the same line.
( let ( ( inline-beg ( point ) ) )
( insert " " )
( treesit--explorer-draw-node child )
;; If we exceeds window width, draw on the next line.
( if ( < ( current-column ) ( window-width ) )
nil
( delete-region inline-beg ( point ) )
( setq draw-inline nil
children-indent ( 1+ after-type-column ) )
t ) )
;; Draw children on the new line.
( insert " \n " )
( indent-to-column children-indent )
( treesit--explorer-draw-node child )
nil ) )
( setq can-inline draw-inline ) ) )
;; Done drawing children, draw the ending paren.
( when named ( insert " ) " ) )
( setq end ( point ) )
;; Associate the text with NODE, so we can later find a piece of
;; text by a node.
( let ( ( ov ( make-overlay beg end ) ) )
( overlay-put ov 'treesit-node node )
( overlay-put ov 'evaporate t )
( when ( not named )
2022-12-11 14:43:58 -08:00
( overlay-put ov 'face 'treesit-explorer-anonymous-node ) ) ) ) )
2022-11-15 20:29:07 -08:00
( define-derived-mode treesit--explorer-tree-mode special-mode
" TS Explorer "
" Mode for displaying syntax trees for `treesit-explore-mode' . "
nil )
( define-minor-mode treesit-explore-mode
" Enable exploring the current buffer's syntax tree.
Pops up a window showing the syntax tree of the source in the
current buffer in real time. The corresponding node enclosing
the text in the active region is highlighted in the explorer
window. "
:lighter " TSplay "
( if treesit-explore-mode
( progn
( unless ( buffer-live-p treesit--explorer-buffer )
( setq-local treesit--explorer-buffer
( get-buffer-create
2022-12-11 14:43:58 -08:00
( format " *tree-sitter explorer for %s* "
2022-11-15 20:29:07 -08:00
( buffer-name ) ) ) )
( setq-local treesit--explorer-language
( intern ( completing-read
" Language: "
( mapcar #' treesit-parser-language
( treesit-parser-list ) ) ) ) )
( with-current-buffer treesit--explorer-buffer
( treesit--explorer-tree-mode ) ) )
( display-buffer treesit--explorer-buffer
( cons nil ' ( ( inhibit-same-window . t ) ) ) )
( treesit--explorer-refresh )
( add-hook 'post-command-hook
#' treesit--explorer-post-command 0 t )
( setq-local treesit--explorer-last-node nil ) )
( remove-hook 'post-command-hook
#' treesit--explorer-post-command t )
( kill-buffer treesit--explorer-buffer ) ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
;;; Etc
( declare-function find-library-name " find-func.el " )
2022-10-10 20:20:19 +02:00
( defun treesit--check-manual-coverage ( )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
" Print tree-sitter functions missing from the manual in message buffer. "
( interactive )
( require 'find-func )
( let ( ( functions-in-source
( with-temp-buffer
2022-10-10 20:20:19 +02:00
( insert-file-contents ( find-library-name " treesit " ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( cl-remove-if
( lambda ( name ) ( string-match " treesit-- " name ) )
( cl-sort
( save-excursion
( goto-char ( point-min ) )
( cl-loop while ( re-search-forward
" ^(defun \\ ([^ ]+ \\ ) " nil t )
collect ( match-string-no-properties 1 ) ) )
#' string< ) ) ) )
( functions-in-manual
( with-temp-buffer
( insert-file-contents ( expand-file-name
" doc/lispref/parsing.texi "
source-directory ) )
( insert-file-contents ( expand-file-name
" doc/lispref/modes.texi "
source-directory ) )
( cl-sort
( save-excursion
( goto-char ( point-min ) )
( cl-loop while ( re-search-forward
" ^@defun \\ ([^ ]+ \\ ) " nil t )
collect ( match-string-no-properties 1 ) ) )
#' string< ) ) ) )
( message " Missing: %s "
( string-join
( cl-remove-if
( lambda ( name ) ( member name functions-in-manual ) )
functions-in-source )
" \n " ) ) ) )
2022-11-08 20:44:54 -08:00
;;; Shortdocs
( defun treesit--generate-shortdoc-examples ( )
" Generate examples for shortdoc. "
( with-temp-buffer
( let ( node parent )
( insert " int c = 0; " )
( print ( treesit-parser-create 'c ) )
( print ( treesit-parser-list ) )
( goto-char ( point-min ) )
( print ( setq node ( treesit-node-at ( point ) ) ) )
( print ( setq parent ( treesit-node-parent node ) ) )
( print ( treesit-node-children parent ) )
( print ( treesit-node-next-sibling node ) )
( print ( treesit-node-child-by-field-name parent " declarator " ) )
nil ) ) )
( define-short-documentation-group treesit
" Parsers "
( treesit-parser-create
:no-eval ( treesit-parser-create )
:eg-result-string " #<treesit-parser for c> " )
( treesit-parser-delete
:no-value ( treesit-parser-delete parser ) )
( treesit-parser-list
:no-eval ( treesit-parser-list )
:eg-result-string " (#<treesit-parser for c>) " )
( treesit-parser-buffer
:no-eval ( treesit-parser-buffer parser )
:eg-result-string " #<buffer xdisp.c> " )
( treesit-parser-language
:no-eval ( treesit-parser-language parser )
:eg-result c )
2022-11-15 21:30:13 -08:00
( treesit-parser-add-notifier )
( treesit-parser-remove-notifier )
( treesit-parser-notifiers
:no-eval ( treesit-parser-notifiers parser )
:eg-result ( function1 function2 function3 ) )
2022-11-08 20:44:54 -08:00
" Parser ranges "
( treesit-parser-set-included-ranges
:no-value ( treesit-parser-set-included-ranges parser ' ( ( 1 . 4 ) ( 5 . 8 ) ) ) )
( treesit-parser-included-ranges
:no-eval ( treesit-parser-included-ranges parser )
:eg-result ' ( ( 1 . 4 ) ( 5 . 8 ) ) )
( treesit-query-range
:no-eval ( treesit-query-range node ' ( ( script_element ) @cap ) )
:eg-result-string ' ( ( 1 . 4 ) ( 5 . 8 ) ) )
" Retrieving a node "
( treesit-node-at
:no-eval ( treesit-node-at ( point ) )
:eg-result-string " #<treesit-node (identifier) in 179-180> " )
( treesit-node-on
:no-eval ( treesit-node-on 18 28 )
:eg-result-string " #<treesit-node (compound_statement) in 143-290> " )
( treesit-buffer-root-node
:no-eval ( treesit-buffer-root-node )
:eg-result-string " #<treesit-node (translation_unit) in 1-4830> " )
( treesit-parser-root-node
:no-eval ( treesit-parser-root-node parser )
:eg-result-string " #<treesit-node (translation_unit) in 1-4830> " )
" Retrieving a node from another node "
( treesit-node-parent
:no-eval ( treesit-node-parent node )
:eg-result-string " #<treesit-node (declaration) in 1-11> " )
( treesit-node-child
:no-eval ( treesit-node-child node 0 )
:eg-result-string " #<treesit-node (primitive_type) in 1-4> " )
( treesit-node-children
:no-eval ( treesit-node-children node )
:eg-result-string " (#<treesit-node (primitive_type) in 1-4> #<treesit-node (init_declarator) in 5-10> #<treesit-node \" ; \" in 10-11>) " )
( treesit-node-next-sibling
:no-eval ( treesit-node-next-sibling node )
:eg-result-string " #<treesit-node (init_declarator) in 5-10> " )
( treesit-node-prev-sibling
:no-eval ( treesit-node-prev-sibling node )
:eg-result-string " #<treesit-node (primitive_type) in 1-4> " )
( treesit-node-child-by-field-name
:no-eval ( treesit-node-child-by-field-name node " declarator " )
:eg-result-string " #<treesit-node (init_declarator) in 5-10> " )
( treesit-first-child-for-pos
:no-eval ( treesit-first-child-for-pos node 1 )
:eg-result-string " #<treesit-node (primitive_type) in 1-4> " )
( treesit-node-descendant-for-range
:no-eval ( treesit-node-descendant-for-range node 2 3 )
:eg-result-string " #<treesit-node (primitive_type) in 1-4> " )
" Searching for node "
( treesit-search-subtree
:no-eval ( treesit-search-subtree node " function_definition " )
:eg-result-string " #<treesit-node (function_definition) in 57-146> " )
( treesit-search-forward
:no-eval ( treesit-search-forward node " function_definition " )
:eg-result-string " #<treesit-node (function_definition) in 57-146> " )
( treesit-search-forward-goto
:no-eval ( treesit-search-forward-goto node " function_definition " )
:eg-result-string " #<treesit-node (function_definition) in 57-146> " )
( treesit-induce-sparse-tree
:no-eval ( treesit-induce-sparse-tree node " function_definition " )
:eg-result-string " (nil (#<treesit-node (function_definition) in 57-146>) (#<treesit-node (function_definition) in 259-296>) (#<treesit-node (function_definition) in 303-659>)) " )
( treesit-filter-child
:no-eval ( treesit-filter-child node ( lambda ( n ) ( equal ( treesit-node-type ) " identifier " ) ) )
:eg-result-string " (#<treesit-node (identifier) in 195-196>) " )
( treesit-parent-until
:no-eval ( treesit-parent-until node ( lambda ( p ) ( eq ( treesit-node-start p ) ( point ) ) ) )
:eg-result-string " #<treesit-node (declaration) in 1-11> " )
( treesit-parent-while
:no-eval ( treesit-parent-while node ( lambda ( p ) ( eq ( treesit-node-start p ) ( point ) ) ) )
:eg-result-string " #<treesit-node (declaration) in 1-11> " )
( treesit-node-top-level
:no-eval ( treesit-node-top-level node )
:eg-result-string " #<treesit-node (declaration) in 1-11> " )
" Retrieving node information "
( treesit-node-text
:no-eval ( treesit-node-text node )
:eg-result " int " )
( treesit-node-start
:no-eval ( treesit-node-start node )
:eg-result 1 )
( treesit-node-end
:no-eval ( treesit-node-end node )
:eg-result 10 )
( treesit-node-type
:no-eval ( treesit-node-type node )
:eg-result " function_definition " )
( treesit-node-field-name
:no-eval ( treesit-node-field-name node )
:eg-result " body " )
( treesit-node-parser
:no-eval ( treesit-node-parser node )
:eg-result-string " #<treesit-parser for c> " )
( treesit-node-language
:no-eval ( treesit-node-language node )
:eg-result c )
( treesit-node-buffer
:no-eval ( treesit-node-buffer node )
:eg-result-string " #<buffer xdisp.c> " )
( treesit-node-index
:no-eval ( treesit-node-index node )
:eg-result 0 )
( treesit-node-string
:no-eval ( treesit-node-string node )
:eg-result-string " (init_declarator declarator: (identifier) value: (number_literal)) " )
( treesit-node-check
:no-eval ( treesit-node-check node 'named )
:eg-result t )
( treesit-field-name-for-child
:no-eval ( treesit-field-name-for-child node )
:eg-result " body " )
( treesit-child-count
:no-eval ( treesit-child-count node )
:eg-result 3 )
" Pattern matching "
( treesit-query-capture
:no-eval ( treesit-query-capture node ' ( ( identifier ) @id " return " @ret ) )
:eg-result-string " ((id . #<treesit-node (identifier) in 195-196>) (ret . #<treesit-node " return " in 338-344>)) " )
( treesit-query-compile
:no-eval ( treesit-query-compile 'c ' ( ( identifier ) @id " return " @ret ) )
:eg-result-string " #<treesit-compiled-query> " )
( treesit-query-language
:no-eval ( treesit-query-language compiled-query )
:eg-result c )
( treesit-query-expand
:eval ( treesit-query-expand ' ( ( identifier ) @id " return " @ret ) ) )
( treesit-pattern-expand
:eval ( treesit-pattern-expand :anchor )
:eval ( treesit-pattern-expand ' ( identifier ) )
:eval ( treesit-pattern-expand :equal ) )
" Parsing a string "
( treesit-parse-string
:no-eval ( treesit-parse-string " int c = 0; " 'c )
:eg-result-string " #<treesit-node (translation_unit) in 1-11> " )
( treesit-query-string
:no-eval ( treesit-query-string " int c = 0; " ' ( ( identifier ) @id ) 'c )
:eg-result-string " ((id . #<treesit-node (identifier) in 5-6>)) " ) )
Add tree-sitter intergration
* configure.ac (HAVE_TREE_SITTER, TREE_SITTER_OBJ): New variables.
(DYNAMIC_LIB_SUFFIX): new variable, I copied code from MODULES_SUFFIX
so the diff looks this way.
* doc/lispref/elisp.texi (Top): Add tree-sitter manual.
* doc/lispref/modes.texi (Font Lock Mode): mention tree-sitter.
(Parser-based Font Lock): New section.
(Auto-Indentation): Mention tree-sitter.
(Parser-based Indentation): New section.
* doc/lispref/parsing.texi (Parsing Program Source): New chapter.
* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Add
treesit-parser and treesit-node type.
* lisp/treesit.el: New file.
* src/Makefile.in (TREE_SITTER_LIBS, TREE_SITTER_FLAGS,
TREE_SITTER_OBJ): New variables.
* src/alloc.c:
(cleanup_vector): Add cleanup code for treesit-parser and
treesit-node.
* src/casefiddle.c (casify_region): Notify tree-sitter parser of
buffer change.
* src/data.c (Ftype_of): Add treesit-parser and treesit-node type
(Qtreesit_parser, Qtreesit_node): New symbol.
* src/emacs.c (main): Add symbols in treesit.c.
* src/eval.c (define_error): Move the function to here.
* src/insdel.c (insert_1_both, insert_from_string_1, insert_from_gap,
insert_from_buffer_1, replace_range, del_range_2): Notify tree-sitter
parser of buffer change.
* src/json.c (define_error): Move this function out.
* src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add treesit-parser and
treesit-node.
* src/lread.c (Vdynamic_library_suffixes): New variable.
* src/print.c (print_vectorlike): Add code for printing
treesit-parser and treesit-node.
* src/treesit.c: New file.
* src/treesit.h: New file.
* test/src/treesit-tests.el: New file.
2022-03-12 22:10:06 -08:00
( provide 'treesit )
;;; treesit.el ends here