Emacs Tree-sitter custom highlighting, part 3 #

In the last post I described how, for a particular project with certain naming conventions, I wanted to use emacs tree-sitter font-locking to highlight variables that might be used in the wrong context:

Screenshot showing two subscript expressions, one that seems suspect because it's an array indexed by t values that has an index that is named r

I found a way to make it work for simple expressions but it was relying on regular expression matching rather than tree-sitter parse trees. I set a goal of handling these types of expressions:

elevation_t[t]           // good
elevation_t[r]           // bug
elevation_t[t_from_r[r]] // good
elevation_t[r_from_t[t]] // bug
elevation_t[obj.t]       // good
elevation_t[obj.r]       // bug
elevation_t[t_from_r(r)] // good
elevation_t[r_from_t(t)] // bug
elevation_t[x.t_fn(x)]   // good
elevation_t[x.r_fn(x)]   // bug
elevation_t[x.t_arr[i]]  // good
elevation_t[x.r_arr[i]]  // bug
elevation_t[(t)]         // good
elevation_t[(r)]         // bug

Labels:

Emacs Tree-sitter custom highlighting, part 2 #

In the last post I described my initial attempt at customizing font-lock using Emacs 29 tree-sitter. Regular font locking uses regular expression matching. Tree sitter allows combining regular expression matching with parse tree matching, like "variable declaration" or "function call". The ideas I came up with were mostly things that I could have implemented with regular expressions alone, but I wanted to experiment with the tree sitter approach. Here's the effect of conventional highlighting, using color for keywords and other syntax:

Screenshot showing no use of color Screenshot showing color for syntax
Screenshots showing the use of color for syntax highlighting

Labels:

Emacs Tree-sitter custom highlighting, part 1 #

A few years ago I blogged about custom tree-sitter-based syntax highlighting in emacs. I started out with highlighting certain keywords in red. I wanted to show the keywords that interrupt control flow:

Screenshot showing syntax highlighting of keywords in black or red

I highlight regular keywords (while, if) in bold. I highlight control flow interrupting keywords (return, continue) in red.

That's syntax highlighting.

But I also wanted to highlight names based on their meaning.

Labels:

Emacs consult-buffer filenames #

When working on my web projects I often have same the same filename in different folders, such as projectname/index.html and anotherproject/index.html.

When opening files in Emacs, I might have to open index.html if I'm already in projectname/ or type ../anotherproject/index.html to open from another folder. I switch folders enough that I wanted to have a more consistent way to open things. I now use a global find-file that lets me match on the complete pathname. I can open projectname/index from anywhere, without having to type something different based on which folder I'm in.

When switching buffers, either with the built-in function or with consult-buffer from consult.el, I can type index.html if I only have one index.html file open. If I have more than one open, Emacs will rename them index.html<projectname>, index.html<anotherproject>, etc., and then I would type index projectname. But if I have only one open, then typing index projectname will fail. I wanted to make it more consistent so that I could always type index projectname whether there's only one index.html buffer open or multiple. I implemented this by putting the full filename instead of the buffer name into consult's buffer list:

Labels:

Observational studies #

Star Trek Red Shirt
Star Trek Red Shirt,
Wikipedia, Derek Springer, CC BY-SA 2.0

People who wear red shirts on the original Star Trek have a high mortality rate.

  1. can we conclude that red shirts caused the high mortality rate?
  2. can we solve the problem by having them yellow shirts instead?

Labels: