aboutsummaryrefslogtreecommitdiffstats
path: root/admin/notes
diff options
context:
space:
mode:
authorStefan Kangas2024-07-21 21:13:20 +0200
committerStefan Kangas2024-07-21 22:21:11 +0200
commit85773ab9771d4a382c6313803d0ffb91d379e86d (patch)
treeeae673d35d7899d887902c5a1059597f3a617ac7 /admin/notes
parent2dacec1609a2858ad932fb766d2fb77e3bdeb23f (diff)
downloademacs-85773ab9771d4a382c6313803d0ffb91d379e86d.tar.gz
emacs-85773ab9771d4a382c6313803d0ffb91d379e86d.zip
Minor copyedits in tree-sitter starting guide
* admin/notes/tree-sitter/starter-guide: Minor copyedits. Reflow some paragraphs.
Diffstat (limited to 'admin/notes')
-rw-r--r--admin/notes/tree-sitter/starter-guide56
1 files changed, 25 insertions, 31 deletions
diff --git a/admin/notes/tree-sitter/starter-guide b/admin/notes/tree-sitter/starter-guide
index 1ff8ffef554..72102250bbb 100644
--- a/admin/notes/tree-sitter/starter-guide
+++ b/admin/notes/tree-sitter/starter-guide
@@ -34,10 +34,9 @@ merged) and rebuild Emacs.
34 34
35* Install language definitions 35* Install language definitions
36 36
37Tree-sitter by itself doesn’t know how to parse any particular 37Tree-sitter by itself doesn’t know how to parse any particular language.
38language. We need to install language definitions (or “grammars”) for 38We need to install language definitions (or “grammars”) for a language
39a language to be able to parse it. There are a couple of ways to get 39to be able to parse it. There are a couple of ways to get them.
40them.
41 40
42You can use this script that I put together here: 41You can use this script that I put together here:
43 42
@@ -50,7 +49,7 @@ GNU/Linux and macOS, they can be downloaded here:
50 49
51 https://github.com/casouri/tree-sitter-module/releases/tag/v2.4 50 https://github.com/casouri/tree-sitter-module/releases/tag/v2.4
52 51
53To build them yourself, run 52To build them yourself, run:
54 53
55 git clone git@github.com:casouri/tree-sitter-module.git 54 git clone git@github.com:casouri/tree-sitter-module.git
56 cd tree-sitter-module 55 cd tree-sitter-module
@@ -73,26 +72,25 @@ automatically download and compile the language grammar for you.
73 72
74* Setting up for adding major mode features 73* Setting up for adding major mode features
75 74
76Start Emacs and load tree-sitter with 75Start Emacs and load tree-sitter with:
77 76
78 (require 'treesit) 77 (require 'treesit)
79 78
80Now check if Emacs is built with tree-sitter library 79Now check if Emacs is built with tree-sitter library:
81 80
82 (treesit-available-p) 81 (treesit-available-p)
83 82
84Make sure Emacs can find the language grammar you want to use 83Make sure Emacs can find the language grammar you want to use:
85 84
86 (treesit-language-available-p 'lang) 85 (treesit-language-available-p 'lang)
87 86
88* Tree-sitter major modes 87* Tree-sitter major modes
89 88
90Tree-sitter modes should be separate major modes, so other modes 89Tree-sitter modes should be separate major modes, so other modes
91inheriting from the original mode don't break if tree-sitter is 90inheriting from the original mode don't break if tree-sitter is enabled.
92enabled. For example js2-mode inherits js-mode, we can't enable 91For example js2-mode inherits js-mode, we can't enable tree-sitter in
93tree-sitter in js-mode, lest js-mode would not setup things that 92js-mode, lest js-mode would not setup things that js2-mode expects to
94js2-mode expects to inherit from. So it's best to use separate major 93inherit from. So it's best to use separate major modes.
95modes.
96 94
97If the tree-sitter variant and the "native" variant could share some 95If the tree-sitter variant and the "native" variant could share some
98setup, you can create a "base mode", which only contains the common 96setup, you can create a "base mode", which only contains the common
@@ -119,19 +117,18 @@ you. The query function returns a list of (capture-name . node). For
119font-lock, we use face names as capture names. And the captured node 117font-lock, we use face names as capture names. And the captured node
120will be fontified in their capture name. 118will be fontified in their capture name.
121 119
122The capture name could also be a function, in which case (NODE 120The capture name could also be a function, in which case (NODE OVERRIDE
123OVERRIDE START END) is passed to the function for fontification. START 121START END) is passed to the function for fontification. START and END
124and END are the start and end of the region to be fontified. The 122are the start and end of the region to be fontified. The function
125function should only fontify within that region. The function should 123should only fontify within that region. The function should also allow
126also allow more optional arguments with (&rest _), for future 124more optional arguments with (&rest _), for future extensibility. For
127extensibility. For OVERRIDE check out the docstring of 125OVERRIDE check out the docstring of treesit-font-lock-rules.
128treesit-font-lock-rules.
129 126
130** Query syntax 127** Query syntax
131 128
132There are two types of nodes, named, like (identifier), 129There are two types of nodes, named, like (identifier),
133(function_definition), and anonymous, like "return", "def", "(", 130(function_definition), and anonymous, like "return", "def", "(",
134"}". Parent-child relationship is expressed as 131"}". Parent-child relationship is expressed as:
135 132
136 (parent (child) (child) (child (grand_child))) 133 (parent (child) (child) (child (grand_child)))
137 134
@@ -155,8 +152,7 @@ The query above captures both parent and child.
155 152
156 ["return" "continue" "break"] @keyword 153 ["return" "continue" "break"] @keyword
157 154
158The query above captures all the keywords with capture name 155The query above captures all the keywords with capture name "keyword".
159"keyword".
160 156
161These are the common syntax, see all of them in the manual 157These are the common syntax, see all of them in the manual
162("Parsing Program Source" section). 158("Parsing Program Source" section).
@@ -168,7 +164,7 @@ open any python source file, type M-x treesit-explore-mode RET. Now
168you should see the parse-tree in a separate window, automatically 164you should see the parse-tree in a separate window, automatically
169updated as you select text or edit the buffer. Besides this, you can 165updated as you select text or edit the buffer. Besides this, you can
170consult the grammar of the language definition. For example, Python’s 166consult the grammar of the language definition. For example, Python’s
171grammar file is at 167grammar file is at:
172 168
173 https://github.com/tree-sitter/tree-sitter-python/blob/master/grammar.js 169 https://github.com/tree-sitter/tree-sitter-python/blob/master/grammar.js
174 170
@@ -262,7 +258,7 @@ Concretely, something like this:
262 258
263* Indent 259* Indent
264 260
265Indent works like this: We have a bunch of rules that look like 261Indent works like this: We have a bunch of rules that look like:
266 262
267 (MATCHER ANCHOR OFFSET) 263 (MATCHER ANCHOR OFFSET)
268 264
@@ -354,9 +350,8 @@ Set ‘treesit-simple-imenu-settings’ and call
354 350
355* Navigation 351* Navigation
356 352
357Set ‘treesit-defun-type-regexp’ and call 353Set ‘treesit-defun-type-regexp’ and call ‘treesit-major-mode-setup’.
358‘treesit-major-mode-setup’. You can additionally set 354You can additionally set ‘treesit-defun-name-function’.
359‘treesit-defun-name-function’.
360 355
361* Which-func 356* Which-func
362 357
@@ -404,13 +399,12 @@ BTW ‘treesit-node-string’ does different things.
404* Manual 399* Manual
405 400
406I suggest you read the manual section for tree-sitter in Info. The 401I suggest you read the manual section for tree-sitter in Info. The
407section is Parsing Program Source. Typing 402section is Parsing Program Source. Typing:
408 403
409 C-h i d m elisp RET g Parsing Program Source RET 404 C-h i d m elisp RET g Parsing Program Source RET
410 405
411will bring you to that section. You don’t need to read through every 406will bring you to that section. You don’t need to read through every
412sentence, just read the text paragraphs and glance over function 407sentence, just read the text paragraphs and glance over function names.
413names.
414 408
415* Appendix 1 409* Appendix 1
416 410