Skip to content

Instantly share code, notes, and snippets.

@graelo
Last active September 9, 2024 11:06
Show Gist options
  • Save graelo/2d1f5bc6775432a5c8eb6d6c8b624011 to your computer and use it in GitHub Desktop.
Save graelo/2d1f5bc6775432a5c8eb6d6c8b624011 to your computer and use it in GitHub Desktop.
Nvim 0.8 tree-sitter Python conceal

This highlights file should live in <nvim-conf>/after/queries/python/highlights.scm.

Please note that with neovim 0.8, for files which queries extend existing queries, you need to add ; extends at the top of the file (see this discussion).

Here is some Python file:

"""Test conceal."""

import math
from math import sqrt

for element in [0, 1, 2]:
    print(element)
    _ = element or False
    _ = element and True
    _ = element != False
    _ = element not in ["a"]
    _ = 1 * 2
    _ = not True
    sum([1, 2, 3])
    all([True, True])
    any([True, False])
    _ = 1 + math.sqrt(16)
    _ = 1 + sqrt(16)
    _ = 1 + math.pi
    _ = lambda x: x + 1
    _ = len([1, 2, 3])
    _ = None
    int(3)
    float(3)


def func(x: int, y: float) -> float:
    """Increment."""
    return x + y

and how this looks when concealed

image

; extends
; Sources:
; - the python/highlights.scm: https://github.com/nvim-treesitter/nvim-treesitter/blob/master/queries/python/highlights.scm
; - the playground: https://tree-sitter.github.io/tree-sitter/playground
; - tutorial: https://siraben.dev/2022/03/01/tree-sitter.html
; Inspiration:
; - https://gist.github.com/andrusha/952550
; Operators
;
("not" @keyword.operator (#set! conceal "¬"))
("or" @keyword.operator (#set! conceal "∨"))
("and" @keyword.operator (#set! conceal "∧"))
("in" @keyword.operator (#set! conceal "∈"))
; ("not" @keyword.operator (#eq? @keyword.operator "in") (#set! conceal "∉"))
; ("not" @keyword.operator (#eq? @keyword.operator "in") (#set! conceal "∉"))
("==" @operator (#set! conceal "≡"))
("!=" @operator (#set! conceal "≢"))
("<=" @operator (#set! conceal "≤"))
(">=" @operator (#set! conceal "≥"))
("*" @operator (#set! conceal "∙"))
; ("/" @operator (#set! conceal "÷"))
; Functions
;
; used in function call contexts, e.g. `sum(...)` -> `∑(...)`
;
((call function: (identifier) @function.builtin)
(#eq? @function.builtin "sum")
(#set! conceal "∑"))
((call function: (identifier) @function.builtin)
(#eq? @function.builtin "all")
(#set! conceal "∀"))
((call function: (identifier) @function.builtin)
(#eq? @function.builtin "any")
(#set! conceal "∃"))
((call function: (identifier) @function.builtin)
(#eq? @function.builtin "len")
(#set! conceal "#"))
((call function: (identifier) @function.builtin)
(#eq? @function.builtin "int")
(#set! conceal "ℤ"))
((call function: (identifier) @function.builtin)
(#eq? @function.builtin "float")
(#set! conceal "ℝ"))
((call function: (attribute) @method)
(#eq? @method "math\.sqrt")
(#set! conceal "√"))
("lambda" @keyword.function (#set! conceal "λ"))
; Important builtins
;
(((attribute) @constant)
(#eq? @constant "math\.pi")
(#set! conceal "π"))
((none) @constant.builtin (#set! conceal "Ø"))
; Main types
;
((type (identifier) @type) (#eq? @type "int") (#set! conceal "ℤ"))
((type (identifier) @type) (#eq? @type "float") (#set! conceal "ℝ"))
@Davey-Hughes
Copy link

If anyone comes across this in 2024 and after, the set! command needs to be preceded with a #. For example:
(("not" @keyword.operator) (#set! conceal "¬"))

For every set! command.

@graelo
Copy link
Author

graelo commented Sep 9, 2024

Thanks @Davey-Hughes! I just updated the gist. I had changed it on my machine, but had forgotten to update the gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment