Comments (4)
Below is a quick proof-of-concept.
There is one interactive function treesitter-cycle-hl
-- the idea is to keep invoking it to get different highlighting applied.
(defvar treesitter-cycle-hl--current-index-alist
'((clojure . 0)))
(defun treesitter-cycle-hl--current-index ()
(when-let ((lang-sym (alist-get major-mode tree-sitter-major-mode-language-alist)))
(alist-get lang-sym treesitter-cycle-hl--current-index-alist)))
(defun treesitter-cycle-hl--set-current-index (new-val)
(when-let ((lang-sym (alist-get major-mode tree-sitter-major-mode-language-alist)))
(when (treesitter-cycle-hl--current-index)
(setf (alist-get lang-sym treesitter-cycle-hl--current-index-alist)
new-val))))
(defvar treesitter-cycle-hl--file-paths-alist
'((clojure .
("~/.emacs.d/straight/repos/emacs-tree-sitter/langs/queries/clojure/highlights.scm"
"~/src/emacs-tree-sitter.sogaiu/langs/queries/clojure/highlights.scm"))))
(defun treesitter-cycle-hl--current-file-paths ()
(when-let ((lang-sym (alist-get major-mode tree-sitter-major-mode-language-alist)))
(alist-get lang-sym treesitter-cycle-hl--file-paths-alist)))
(defun treesitter-cycle-hl ()
"Cycle to logically next highlight patterns and highlight based on result."
(interactive)
(when tree-sitter-hl-mode
(tree-sitter-hl-mode -1))
(treesitter-cycle-hl--set-current-index (1+ (treesitter-cycle-hl--current-index)))
(when (>= (treesitter-cycle-hl--current-index) (length (treesitter-cycle-hl--current-file-paths)))
(treesitter-cycle-hl--set-current-index 0))
;; tree-sitter-langs--hl-default-patterns
(let ((index (treesitter-cycle-hl--current-index))
(file-paths (treesitter-cycle-hl--current-file-paths)))
(setq tree-sitter-hl-default-patterns
(condition-case nil
(with-temp-buffer
(insert-file-contents (nth index file-paths))
(goto-char (point-max))
(insert "\n")
(buffer-string))
(file-missing nil))))
(tree-sitter-hl-mode))
from elisp-tree-sitter.
I scanned the current emacs lisp files a bit and the impression I got was that the idea is to have basically have one type of highlighting (possibly with customizations?). Does that seem correct?
tree-sitter-hl
's interface is the variable tree-sitter-hl-default-patterns
, to be set by major modes, which can implement any pattern-cycling scheme they see fit. (Its role is similar to that of font-lock-defaults
.) The "one type of highlighting" limitation is from the package tree-sitter-langs
, which acts as a temporary surrogate for future tree-sitter
-based language-specific modes.
If the idea of cycling through some different highlighting schemes sounds palatable, any suggestions / pointers on implementing such functionality?
Your example shows how to do it currently: disable tree-sitter-hl-mode
, set tree-sitter-hl-default-patterns
, and re-enable tree-sitter-hl-mode
. It should work well, apart from the minor inefficiency of recreating the query cursor.
To give a few examples, it makes comments stand out, which I find helpful sometimes but not usually. It also does not highlight "built-in" functions / macros / special forms. When I am not yet familiar with a particular language, I would prefer to have such things highlighted and if possible be able to tell built-ins apart from "user-defined". Even if I am familiar, I thought I might be helped to be able to temporarily turn on such highlighting.
FWIW, the aforementioned repository has some details on the rationale behind some of his decisions and I found this to be interesting reading. I don't happen to agree with multiple issues, but I would guess that whatever I were to express as a list of important points, someone else would likely have their own differing opinion.
These experiences suggest to me that perhaps it would be useful to have a few different highlighting schemes one can switch among for different purposes. Perhaps one can think of them as different "glasses" for different occasions.
I agree that being able to quickly change highlighting scheme on-the-fly is very useful. I just don't know what's the reasonable API tree-sitter-langs
can provide for that, yet. It would take some design work. As a temporary solution, I don't want tree-sitter-langs
to dictate too much of an API too early (e.g. global vs local toggles, customization options, whether the schemes should be sorted, or named...)
P.S. (alist-get major-mode tree-sitter-major-mode-language-alist)
can be replaced with (tsc--lang-symbol tree-sitter-language)
. (I'll probably make that function non-internal soon.)
from elisp-tree-sitter.
Thank you very much for the detailed response, including the explanation, code tips, and heads up about possibly upcoming changes 👍
I hope over time some good / reasonable API is arrived at.
from elisp-tree-sitter.
On a related note, @pedrorgirardi told me about the following repository: https://github.com/rougier/nano-emacs
There is an associated paper that has the following text within it:
The most striking example is certainly the syntax colorization that seems to go against every good design principles in a majority of text editors and the motto guiding design could be summarized by "Let's add mor colors" (using regex).
There are other interesting observations in the paper too that are not necessarily relevant here, but the paper does have a section titled "Colorization" that might be of interest to some.
from elisp-tree-sitter.
Related Issues (20)
- Doesn't highlight Rust documentation code examples
- Swift support, revisited. HOT 1
- wrong ELF class: ELFCLASS64 on RPI4 HOT 1
- `Unsupported Operating System: OpenBSD` HOT 7
- bin/build not work on linux because builtin cd will output the folder name. HOT 1
- tree-sitter languages download of windows binaries failed with error
- tsc-lang-abi-too-new when installing new language HOT 11
- Crash when large file contains an ERROR node
- Invalid version syntax: ‘[email protected] HOT 1
- Colors look wrong in haskell mode. HOT 3
- `tree-sitter-debug-mode` is broken
- Crash when loading one particular Markdown file HOT 7
- Reporting Vulnerability
- (ts-fold-indicators-mode) not working when running emacs from terminal
- How do I add a new grammer to this mode? HOT 2
- why the submodule langs pointed to an old commit?
- tree-sitter-hl-mode doesn't honor the :extend font face in single line comments HOT 1
- request to rebuild tsc-dyn.so with older Linux OS
- Provide `aarch64` build of `tsc-dyn.so` for AArch64 Linux
- Repetitions and Predicate Problems
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from elisp-tree-sitter.