diff options
| author | Stefan Kangas | 2024-07-21 21:13:20 +0200 |
|---|---|---|
| committer | Stefan Kangas | 2024-07-21 22:21:11 +0200 |
| commit | 85773ab9771d4a382c6313803d0ffb91d379e86d (patch) | |
| tree | eae673d35d7899d887902c5a1059597f3a617ac7 /admin/notes | |
| parent | 2dacec1609a2858ad932fb766d2fb77e3bdeb23f (diff) | |
| download | emacs-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-guide | 56 |
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 | ||
| 37 | Tree-sitter by itself doesn’t know how to parse any particular | 37 | Tree-sitter by itself doesn’t know how to parse any particular language. |
| 38 | language. We need to install language definitions (or “grammars”) for | 38 | We need to install language definitions (or “grammars”) for a language |
| 39 | a language to be able to parse it. There are a couple of ways to get | 39 | to be able to parse it. There are a couple of ways to get them. |
| 40 | them. | ||
| 41 | 40 | ||
| 42 | You can use this script that I put together here: | 41 | You 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 | ||
| 53 | To build them yourself, run | 52 | To 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 | ||
| 76 | Start Emacs and load tree-sitter with | 75 | Start Emacs and load tree-sitter with: |
| 77 | 76 | ||
| 78 | (require 'treesit) | 77 | (require 'treesit) |
| 79 | 78 | ||
| 80 | Now check if Emacs is built with tree-sitter library | 79 | Now check if Emacs is built with tree-sitter library: |
| 81 | 80 | ||
| 82 | (treesit-available-p) | 81 | (treesit-available-p) |
| 83 | 82 | ||
| 84 | Make sure Emacs can find the language grammar you want to use | 83 | Make 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 | ||
| 90 | Tree-sitter modes should be separate major modes, so other modes | 89 | Tree-sitter modes should be separate major modes, so other modes |
| 91 | inheriting from the original mode don't break if tree-sitter is | 90 | inheriting from the original mode don't break if tree-sitter is enabled. |
| 92 | enabled. For example js2-mode inherits js-mode, we can't enable | 91 | For example js2-mode inherits js-mode, we can't enable tree-sitter in |
| 93 | tree-sitter in js-mode, lest js-mode would not setup things that | 92 | js-mode, lest js-mode would not setup things that js2-mode expects to |
| 94 | js2-mode expects to inherit from. So it's best to use separate major | 93 | inherit from. So it's best to use separate major modes. |
| 95 | modes. | ||
| 96 | 94 | ||
| 97 | If the tree-sitter variant and the "native" variant could share some | 95 | If the tree-sitter variant and the "native" variant could share some |
| 98 | setup, you can create a "base mode", which only contains the common | 96 | setup, 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 | |||
| 119 | font-lock, we use face names as capture names. And the captured node | 117 | font-lock, we use face names as capture names. And the captured node |
| 120 | will be fontified in their capture name. | 118 | will be fontified in their capture name. |
| 121 | 119 | ||
| 122 | The capture name could also be a function, in which case (NODE | 120 | The capture name could also be a function, in which case (NODE OVERRIDE |
| 123 | OVERRIDE START END) is passed to the function for fontification. START | 121 | START END) is passed to the function for fontification. START and END |
| 124 | and END are the start and end of the region to be fontified. The | 122 | are the start and end of the region to be fontified. The function |
| 125 | function should only fontify within that region. The function should | 123 | should only fontify within that region. The function should also allow |
| 126 | also allow more optional arguments with (&rest _), for future | 124 | more optional arguments with (&rest _), for future extensibility. For |
| 127 | extensibility. For OVERRIDE check out the docstring of | 125 | OVERRIDE check out the docstring of treesit-font-lock-rules. |
| 128 | treesit-font-lock-rules. | ||
| 129 | 126 | ||
| 130 | ** Query syntax | 127 | ** Query syntax |
| 131 | 128 | ||
| 132 | There are two types of nodes, named, like (identifier), | 129 | There 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 | ||
| 158 | The query above captures all the keywords with capture name | 155 | The query above captures all the keywords with capture name "keyword". |
| 159 | "keyword". | ||
| 160 | 156 | ||
| 161 | These are the common syntax, see all of them in the manual | 157 | These 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 | |||
| 168 | you should see the parse-tree in a separate window, automatically | 164 | you should see the parse-tree in a separate window, automatically |
| 169 | updated as you select text or edit the buffer. Besides this, you can | 165 | updated as you select text or edit the buffer. Besides this, you can |
| 170 | consult the grammar of the language definition. For example, Python’s | 166 | consult the grammar of the language definition. For example, Python’s |
| 171 | grammar file is at | 167 | grammar 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 | ||
| 265 | Indent works like this: We have a bunch of rules that look like | 261 | Indent 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 | ||
| 357 | Set ‘treesit-defun-type-regexp’ and call | 353 | Set ‘treesit-defun-type-regexp’ and call ‘treesit-major-mode-setup’. |
| 358 | ‘treesit-major-mode-setup’. You can additionally set | 354 | You 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 | ||
| 406 | I suggest you read the manual section for tree-sitter in Info. The | 401 | I suggest you read the manual section for tree-sitter in Info. The |
| 407 | section is Parsing Program Source. Typing | 402 | section 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 | ||
| 411 | will bring you to that section. You don’t need to read through every | 406 | will bring you to that section. You don’t need to read through every |
| 412 | sentence, just read the text paragraphs and glance over function | 407 | sentence, just read the text paragraphs and glance over function names. |
| 413 | names. | ||
| 414 | 408 | ||
| 415 | * Appendix 1 | 409 | * Appendix 1 |
| 416 | 410 | ||