emacs-tree-sitter / elisp-tree-sitter

Emacs Lisp bindings for tree-sitter
https://emacs-tree-sitter.github.io
MIT License
815 stars 73 forks source link

tree-sitter-hl-mode doesn't honor the :extend font face in single line comments #266

Open mlabbe opened 6 months ago

mlabbe commented 6 months ago

In many, if not all supported languages, tree-sitter-hl-mode's font locking does not honor the :extend attribute of a face. The :extend face attribute is used to send a highlight past the newline character in a line.

The easiest way to see this is to produce a // comment in a mode that supports it, such as c-mode. Set the font-lock-comment-face and tree-sitter-hl-face:comment :background to a color other than the default background. Then, observe as comments stop at the newline instead of the end of the frame.

tree-sitter-hl-mode Disabled (no bug)

disabled

tree-sitter-hl-mode Enabled (bug)

enabled

To reproduce this issue:

  1. launch emacs 29.1 with emacs -q --load repro.el (code is below).
  2. Create a blank c file, ensuring c-mode is set. Add a // comment to any line and hit enter.
  3. Observe the comment terminates at the newline (before the right side of the frame).
  4. Type M-x tree-sitter-hl-mode. Observe the comment goes to the right side of the frame.

Languages this bug exists in

Not a complete list, but:

repro.el

;;
;; repro.el

(custom-set-faces
 '(font-lock-comment-face ((t (:background "darkblue" :foreground "yellow" :extend t)))))

(custom-set-faces
 '(tree-sitter-hl-face:comment ((t (:inherit font-lock-comment-face)))))

;; comments look like this

;;
;; tree-sitter
;;

(defvar bootstrap-version)
(setq straight-repository-branch "master")
(let ((bootstrap-file
       (expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
      (bootstrap-version 6))
  (unless (file-exists-p bootstrap-file)
    (with-current-buffer
        (url-retrieve-synchronously
         "https://raw.githubusercontent.com/radian-software/straight.el/develop/install.el"
         'silent 'inhibit-cookies)
      (goto-char (point-max))
      (eval-print-last-sexp)))
  (load bootstrap-file nil 'nomessage))

(require 'package)
(use-package tree-sitter
    :straight t
    :config
    (global-tree-sitter-mode)
    :hook ((python-mode
            rustic-mode
            c-mode cc-mode
            go-mode
            typescript-mode
            javascript-mode) . tree-sitter-hl-mode)
    )

(use-package tree-sitter-langs
    :straight t
    :ensure t
    :after tree-sitter
    )

(require 'tree-sitter-hl)

Note that the same bug was also reported at https://github.com/emacs-tree-sitter/tree-sitter-langs/issues/302. Apologies for the inconvenience of a double-report: I am finding it a little challenging to track down the maintainer of tree-sitter-hl.el for the report, on github.

jcs090218 commented 4 months ago

Have you tried customize faces in tree-sitter-hl.el? 🤔