diff options
| -rw-r--r-- | lisp/treesit.el | 40 |
1 files changed, 15 insertions, 25 deletions
diff --git a/lisp/treesit.el b/lisp/treesit.el index 5a408b09507..3cf3be5122c 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el | |||
| @@ -165,7 +165,8 @@ of max unsigned 32-bit value for byte offsets into buffer text." | |||
| 165 | The primary parser should be a parser that parses the entire buffer, as | 165 | The primary parser should be a parser that parses the entire buffer, as |
| 166 | opposed to embedded parsers which parses only part of the buffer.") | 166 | opposed to embedded parsers which parses only part of the buffer.") |
| 167 | 167 | ||
| 168 | (defvar-local treesit-language-at-point-function nil | 168 | (defvar-local treesit-language-at-point-function |
| 169 | #'treesit-language-at-point-default | ||
| 169 | "A function that returns the language at point. | 170 | "A function that returns the language at point. |
| 170 | This is used by `treesit-language-at', which is used by various | 171 | This is used by `treesit-language-at', which is used by various |
| 171 | functions to determine which parser to use at point. | 172 | functions to determine which parser to use at point. |
| @@ -183,10 +184,15 @@ When there are multiple parsers that covers POSITION, determine | |||
| 183 | the most relevant parser (hence language) by their embed level. | 184 | the most relevant parser (hence language) by their embed level. |
| 184 | If `treesit-language-at-point-function' is non-nil, return | 185 | If `treesit-language-at-point-function' is non-nil, return |
| 185 | the return value of that function instead." | 186 | the return value of that function instead." |
| 186 | (if treesit-language-at-point-function | 187 | (funcall (or treesit-language-at-point-function |
| 187 | (funcall treesit-language-at-point-function position) | 188 | #'treesit-language-at-point-default) |
| 188 | (treesit-parser-language | 189 | position)) |
| 189 | (car (treesit-parsers-at position))))) | 190 | |
| 191 | (defun treesit-language-at-point-default (position) | ||
| 192 | "Default function for `treesit-language-at-point-function'. | ||
| 193 | Return the deepest parser by embed level." | ||
| 194 | (treesit-parser-language | ||
| 195 | (car (treesit-parsers-at position)))) | ||
| 190 | 196 | ||
| 191 | ;;; Node API supplement | 197 | ;;; Node API supplement |
| 192 | 198 | ||
| @@ -238,12 +244,8 @@ language and doesn't match the language of the local parser." | |||
| 238 | ;; 2. Given a language, try local parser, then global | 244 | ;; 2. Given a language, try local parser, then global |
| 239 | ;; parser. | 245 | ;; parser. |
| 240 | (parser-or-lang | 246 | (parser-or-lang |
| 241 | (let* ((local-parser (car (treesit-local-parsers-at | 247 | (let ((parser (car (treesit-parsers-at |
| 242 | pos parser-or-lang))) | 248 | pos parser-or-lang)))) |
| 243 | (global-parser (car (treesit-parsers-at | ||
| 244 | pos parser-or-lang nil | ||
| 245 | '(primary global)))) | ||
| 246 | (parser (or local-parser global-parser))) | ||
| 247 | (when parser | 249 | (when parser |
| 248 | (treesit-parser-root-node parser)))) | 250 | (treesit-parser-root-node parser)))) |
| 249 | ;; 3. No given language, try to get a language at point. | 251 | ;; 3. No given language, try to get a language at point. |
| @@ -252,20 +254,8 @@ language and doesn't match the language of the local parser." | |||
| 252 | ;; finding parser, try local parser first, then global | 254 | ;; finding parser, try local parser first, then global |
| 253 | ;; parser. | 255 | ;; parser. |
| 254 | (t | 256 | (t |
| 255 | ;; LANG can be nil. We don't want to use the fallback | 257 | ;; LANG can be nil. Use the parser deepest by embed level. |
| 256 | ;; in `treesit-language-at', so here we call | 258 | (let ((parser (car (treesit-parsers-at pos)))) |
| 257 | ;; `treesit-language-at-point-function' directly. | ||
| 258 | (let* ((lang (and treesit-language-at-point-function | ||
| 259 | (funcall treesit-language-at-point-function | ||
| 260 | pos))) | ||
| 261 | (local-parser | ||
| 262 | ;; Find the local parser with highest | ||
| 263 | ;; embed-level at point. | ||
| 264 | (car (treesit-local-parsers-at pos lang))) | ||
| 265 | (global-parser (car (treesit-parsers-at | ||
| 266 | pos lang nil | ||
| 267 | '(primary global)))) | ||
| 268 | (parser (or local-parser global-parser))) | ||
| 269 | (when parser | 259 | (when parser |
| 270 | (treesit-parser-root-node parser)))))) | 260 | (treesit-parser-root-node parser)))))) |
| 271 | (node root) | 261 | (node root) |