nv-elisp · 2022-05-19 04:31
Pretty cool. Looking forward to see more packages develop around tree-sitter.
A couple notes on your `tree-sitter-thing-name` function:
- Instead of chaining single branch `if`s, you can leverage the `when-let` or `and-let*` macro.
- Instead of mapping over all the non-nil results and taking the `car` of that list, you can use `cl-some` which will short-circuit and return the first non-nil result.
- You don't have to quote keywords (e.g. `:name`). They evaluate to themselves.
- Prefer the `cl-` lib namespace over the non-prefixed versions of those functions. e.g. `remove-if` -> `cl-remove-if`.
- You have a typo in one of your symbol names: `s/calss/class/g`.
I haven't tested this, but your function could be refactored to something like this:
(defun meain/tree-sitter-thing-name (kind)
"Get name of tree-sitter KIND thing."
(when-let (tree-sitter-mode
(node-types (pcase kind
('class-like meain/tree-sitter-class-like)
('function-like meain/tree-sitter-function-like)))
(node-at-point (cl-some #'tree-sitter-node-at-point
(alist-get major-mode node-types-list)))
(node-name (tsc-get-child-by-field node-at-point :name)))
(tsc-node-text node-name)))
7890yuiop · 2022-05-19 05:36
You might consider getting involved in the (active, ongoing) core integration.
meain · 2022-05-19 05:59
I have been passively following it for some time now.
meain · 2022-05-19 06:06
Ahh, thanks a lot for the pointers. Most of these were just quick hacks that I dropped into my config but this looks much cleaner.
emacs_expert · 2022-05-19 22:42
Cool!
> Show current class/function name in modeline
Is this different from [`which-func`](https://www.emacswiki.org/emacs/WhichFuncMode)?
Alx_xl · 2022-05-19 23:16
Question from a not so knowledgeable person:
You use tree-sitter mostly because of the possible custom grammars? Or to phrase it otherwise shouldn't the language mode know/provide most of the stuff eg: functions?
meain · 2022-05-20 04:21
I was not aware of `which-func`. Technically, the basic functionality seems to be the same except that my snippet also includes class name.
The `which-func` package works based on `imenu` and relies on that to be populated which could come from regex, lsp, or even tree-sitter.
Just FYI, `lsp-mode` has option to show breadcrumb in header-line.
https://emacs-lsp.github.io/lsp-mode/page/main-features/#breadcrumb-on-headerline
meain · 2022-05-20 04:24
Language major modes should also give us this information, and as mentioned in https://www.reddit.com/r/emacs/comments/usu8h2/comment/i99ccao , we could leverage `which-func` to display similar info but not all major modes might provide this information. `tree-sitter` provides a more universal interface plus we can query for specific information instead of relying on the major mode to populate the info.
JohnDoe365 · 2022-05-20 11:40
Works exclusively for Evil.
meain · 2022-05-20 11:57
Ahh, I missed to add that in the blog. The narrowing only works when evil is available as of now. That said, the only reason we have a dependency on that in the package is because of it needing evil to select the region for the operator.
There is an open issue on the package for removing the hard dependency on evil which should be easy but just never got around to it.
emacs_expert · 2022-05-21 00:45
> Technically, the basic functionality seems to be the same except that my snippet also includes class name.
Oh, that's really neat! I would use it if it supported more languages.