From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: emacs-tree-sitter and font-lock (was: emacs rendering comparisson between emacs23 and emacs26.3) Date: Sun, 29 Mar 2020 14:54:51 -0400 Message-ID: References: <86tv2h2vww.fsf@gmail.com> <20200322123818.GB32470@ACM> <87eetk5swm.fsf@gnu.org> <20200326193128.GC14092@ACM> <86d08y4zsx.fsf@gmail.com> <83sghs7qdz.fsf@gnu.org> <7ee94ed4-7a11-90bd-df69-c0eeacaf191c@gmail.com> <835zeo7c92.fsf@gnu.org> <05de335b-383e-b32d-2e7e-e79192d364de@yandex.ru> <03981ead-3aca-c238-a9d0-88407ffae451@yandex.ru> <83wo745tw6.fsf@gnu.org> <837dz35j30.fsf@gnu.org> <9b013d7f-664a-711d-0b7b-d7bd961b8ef0@yandex.ru> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="74690"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: =?utf-8?Q?Tu=E1=BA=A5n-Anh_Nguy=E1=BB=85n?= , cpitclaudel@gmail.com, Eli Zaretskii , emacs-devel@gnu.org To: Dmitry Gutov Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sun Mar 29 20:56:15 2020 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jId6V-000JJn-G5 for ged-emacs-devel@m.gmane-mx.org; Sun, 29 Mar 2020 20:56:15 +0200 Original-Received: from localhost ([::1]:40472 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jId6U-0002Dn-I9 for ged-emacs-devel@m.gmane-mx.org; Sun, 29 Mar 2020 14:56:14 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:60304) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jId5J-00018w-EW for emacs-devel@gnu.org; Sun, 29 Mar 2020 14:55:03 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jId5G-0005GL-Tb for emacs-devel@gnu.org; Sun, 29 Mar 2020 14:55:00 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:1137) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jId5E-0005CV-Ag; Sun, 29 Mar 2020 14:54:56 -0400 Original-Received: from pmg2.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 5DC6480DFC; Sun, 29 Mar 2020 14:54:55 -0400 (EDT) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 5060980D58; Sun, 29 Mar 2020 14:54:53 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1585508093; bh=6VkMdr5wMrQDmP6hBWG8WUP3RXiz+2y+YZNTX0Sri08=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From; b=nc393Vgu89sQ4Apt3ybSnodbr+aXSmSseX9H0Sr7gy9YX3QNGbUYrDcCmnluXyN1p ePXAiSUkKQ4GVMkP86BK/1BW1ikaFLf1QeJ9OA4riew9hooseBkNi0sB0uOyW21OoW RVQirdxY7j8zVV/gR76oBHQvSc3nRYRmljmVTe/mMUIpydEUE33qXzuUYP8XTWqZgS uWao9Nvn6OPWLn0EnfBFxU8xXq4myY0zU3ZsQNuTxlLspypLTqQ7NLowxsVvWwxiyf YNvUmxYhJxJmyhV/Pgia1TxTiL+jWTMBhH8c5h+PAb6Vc4L5IdnUruujs02S9oIxCp uzCGZb0D7VepQ== Original-Received: from alfajor (unknown [104.247.241.114]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id E7DD512089E; Sun, 29 Mar 2020 14:54:52 -0400 (EDT) In-Reply-To: <9b013d7f-664a-711d-0b7b-d7bd961b8ef0@yandex.ru> (Dmitry Gutov's message of "Sun, 29 Mar 2020 21:10:51 +0300") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 132.204.25.50 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:245962 Archived-At: >> I think it's just a reflection of the fact that author doesn't know >> enough about jit-lock and font-lock. > Indeed, that font-lock rule can be a function is not very well-known. Although it seems fairly clearly documented. Not sure how we could solve this. >> I sent him some patches that should help. > That's mighty good of you. > I was curious and wanted to take a look, though, but couldn't find them > neither among pull requests, nor the issue comments. :-( I don't how to participate in a Github pull request with Free Software. IIUC there are some Elisp packages for that, but I haven't investigated how that would work. So I sent the patches via email. I put the main one after my sig. Stefan diff --git a/lisp/tree-sitter-highlight.el b/lisp/tree-sitter-highlight.el index 53ecb32..3246bd6 100644 --- a/lisp/tree-sitter-highlight.el +++ b/lisp/tree-sitter-highlight.el @@ -167,7 +167,6 @@ to faces. Each function takes no arguments." (defvar-local tree-sitter-highlight--injections nil) (defvar-local tree-sitter-highlight--injections-query nil) (defvar-local tree-sitter-highlight--jit-function nil) -(defvar-local tree-sitter-highlight--orig-scroll-functions nil) (defvar-local tree-sitter-highlight--query nil) (defvar-local tree-sitter-highlight--query-cursor nil) @@ -240,19 +239,11 @@ to faces. Each function takes no arguments." (defun tree-sitter-highlight--jit (old-tree) (when old-tree - (let ((changes (ts-changed-ranges old-tree tree-sitter-tree)) - (wstart (window-start)) - (wend (window-end))) - ;; TODO: Remember what we've highlighted, similar to how font-lock does it. - ;; Already highlighted regions shouldn't be re-highlighted. - - ;; Find changes that are within the current window - (mapc #'(lambda (range) - (let ((start (aref range 0)) - (end (aref range 1))) - ;; TODO: Improve this - (tree-sitter-highlight--highlight (max wstart start) (min wend end)))) - changes)))) + (mapc #'(lambda (range) + (let ((start (aref range 0)) + (end (aref range 1))) + (font-lock-flush start end))) + (ts-changed-ranges old-tree tree-sitter-tree)))) (defun tree-sitter-highlight--highlight (start end) (ts--save-context @@ -282,9 +273,6 @@ to faces. Each function takes no arguments." ;; (ts-byte-from-position start) ;; (ts-byte-from-position end))))) -(defun tree-sitter-highlight--highlight-window (_window start) - (tree-sitter-highlight--highlight start (window-end nil t))) - (defun tree-sitter-highlight--enable () "Enable `tree-sitter-highlight' in this buffer." (run-hooks 'tree-sitter-highlight-setup-functions) @@ -301,10 +289,8 @@ to faces. Each function takes no arguments." tree-sitter-highlight--injections-query (cadr x))) (setq tree-sitter-highlight--capture-names (ts-query-capture-names tree-sitter-highlight--query)) (setq tree-sitter-highlight--query-cursor (ts-make-query-cursor)) - (make-variable-buffer-local 'window-scroll-functions) - (setq tree-sitter-highlight--orig-scroll-functions window-scroll-functions) - (setq window-scroll-functions (cons #'tree-sitter-highlight--highlight-window window-scroll-functions)) - (tree-sitter-highlight--highlight-window nil (window-start)) + (add-function :override (local 'font-lock-fontify-region-function) + #'tree-sitter-highlight--highlight) (add-hook 'tree-sitter-after-change-functions #'tree-sitter-highlight--jit nil t) ) @@ -314,7 +300,8 @@ to faces. Each function takes no arguments." (remove-text-properties (point-min) (point-max) '(face nil))) - (setq window-scroll-functions tree-sitter-highlight--orig-scroll-functions) + (remove-function (local 'font-lock-fontify-region-function) + #'tree-sitter-highlight--highlight) (remove-hook 'tree-sitter-after-change-functions #'tree-sitter-highlight--jit t)) (define-minor-mode tree-sitter-highlight-mode