From debbugs-submit-bounces@debbugs.gnu.org Tue Mar 25 14:43:45 2025 Received: (at submit) by debbugs.gnu.org; 25 Mar 2025 18:43:45 +0000 Received: from localhost ([127.0.0.1]:39995 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tx9FY-0003TX-Pq for submit@debbugs.gnu.org; Tue, 25 Mar 2025 14:43:45 -0400 Received: from lists.gnu.org ([2001:470:142::17]:56234) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1tx9FV-0003TA-Qd for submit@debbugs.gnu.org; Tue, 25 Mar 2025 14:43:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tx9FO-0001qO-O2 for bug-gnu-emacs@gnu.org; Tue, 25 Mar 2025 14:43:34 -0400 Received: from relay2-d.mail.gandi.net ([2001:4b98:dc4:8::222]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tx9FL-0001ja-F1 for bug-gnu-emacs@gnu.org; Tue, 25 Mar 2025 14:43:34 -0400 Received: by mail.gandi.net (Postfix) with ESMTPSA id B5304441C0 for ; Tue, 25 Mar 2025 18:43:23 +0000 (UTC) From: Juri Linkov To: bug-gnu-emacs@gnu.org Subject: Treesit language-at-point Organization: LINKOV.NET X-Debbugs-Cc: Yuan Fu , Vincenzo Pupillo Date: Tue, 25 Mar 2025 20:42:02 +0200 Message-ID: <87a5990x6t.fsf@mail.linkov.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain X-GND-State: clean X-GND-Score: 0 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdduieefgedtucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecunecujfgurhephffvufhofffkfgggtgesthdtredttdertdenucfhrhhomheplfhurhhiucfnihhnkhhovhcuoehjuhhriheslhhinhhkohhvrdhnvghtqeenucggtffrrghtthgvrhhnpeejgffgtddthfelteejkeejueegvdekgfdufffgtdehvddtkeetveehgfffjeeuteenucfkphepledurdduvdelrddutdehrdduudejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepledurdduvdelrddutdehrdduudejpdhhvghlohepmhgrihhlrdhgrghnughirdhnvghtpdhmrghilhhfrhhomhepjhhurhhisehlihhnkhhovhdrnhgvthdpnhgspghrtghpthhtohepuddprhgtphhtthhopegsuhhgqdhgnhhuqdgvmhgrtghssehgnhhurdhorhhg X-GND-Sasl: juri@linkov.net Received-SPF: pass client-ip=2001:4b98:dc4:8::222; envelope-from=juri@linkov.net; helo=relay2-d.mail.gandi.net X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: 0.7 (/) X-Debbugs-Envelope-To: submit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -0.3 (/) Do we still need such complicated functions as mhtml-ts-mode--language-at-point, js--treesit-language-at-point, etc. that duplicate the rules from 'treesit-range-rules' when now the default language-at-point function could be implemented just as (treesit-parser-language (or (seq-some (lambda (o) (overlay-get o 'treesit-parser)) (overlays-at (point) t)) treesit-primary-parser)) From debbugs-submit-bounces@debbugs.gnu.org Sat Mar 29 04:26:05 2025 Received: (at 77256) by debbugs.gnu.org; 29 Mar 2025 08:26:05 +0000 Received: from localhost ([127.0.0.1]:56688 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tyRW0-00012K-NW for submit@debbugs.gnu.org; Sat, 29 Mar 2025 04:26:05 -0400 Received: from mail-pj1-x102f.google.com ([2607:f8b0:4864:20::102f]:55451) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1tyRVz-00011h-48 for 77256@debbugs.gnu.org; Sat, 29 Mar 2025 04:26:03 -0400 Received: by mail-pj1-x102f.google.com with SMTP id 98e67ed59e1d1-301e05b90caso5189232a91.2 for <77256@debbugs.gnu.org>; Sat, 29 Mar 2025 01:26:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1743236757; x=1743841557; darn=debbugs.gnu.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=dc3H3BQYLJt/SYMfyz0rpgpmWhLw32naL4Dc0knb6GM=; b=NAmFz6T0klCWANlfDKk4j4Vl6SflSrhgMZwj0QwFh4zGX7s0CQ3pt6+Jw5BPOgDtGz wLK9Dwg6MYwv3k53u8Cq9PDxVisrcNq937EP97aYa5xC7V0yeBKor2U+rbrWh59Fsh/u eUXaXzY9uiK/6NE0KSq7gBqFkt2NCsy0xweA4UGUqCCUZVimFLbaUceMBL5EbNePbWhy UgaRKAbd5erG4T4pBtxNIMgL4PGDAd/gUzTrJQ08BPMCxET3pVFS0+0F/rC3QzXQN2ie lUdHcSEdPPGsSF9CYAA9goADtspuk9OrlTKvSL/BrgWBLxjQWDIbJbhwuyabPcxwSPl+ wRfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743236757; x=1743841557; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dc3H3BQYLJt/SYMfyz0rpgpmWhLw32naL4Dc0knb6GM=; b=t/eTIxlMcalRtP5nxdF/ISeDArwtihp+IfrbdIP3Oph5eg9Ja2nGHIU+nVLMSJqcal NYfi+CrjlYITlXnSBJPdwTElKDMJpumsl7MlVGHWocL1IsHQZK6xmvdzqd5HfhqRkoPp CqQZFgax2CtzHq7PGPhc2a3G3TQYh1yAtUmKIzQDyIiQasjdodfYY5EFARrhRN8hVgE9 TPrtx6O4ZKHHhuHn8H0MLh1o9nHa2BFmxY25l4GaUFGJ1JFp5z3VAEkOICtTgL62UFON ZQzf80LXMlrNpwwh09goYHuoO8rZMTBgrIuuL6YaxsOYErwzhgnf0VuBZg1vhWDCUZEd p0Dg== X-Gm-Message-State: AOJu0YyY9aOpqyM4W2XO+n3OCdcSovr+XaGVtI2s5MQkdYA79M9tQlN4 68HttSLUNK8y0ZM7amxTSn2SEzkWoKU9hb6iXZaCR/uHc4vZOQl9 X-Gm-Gg: ASbGncsCwkCc86GkC6xhbCuziVJfQD9fbg7jSDHAUKhQLcTHT8eyjp8B3npsOX8x4m5 OqrND1dcW8uQLONjQTNC/vroqAMmwxrrRk+3cMc+0Ne7FjnIMjFBkdHu6CMQtRSh+g4xFUaKTFn 08LCh+nz2/J4/IKfGc7Rw0z5Q6UzVqvNuH7CjCZF2RrovMQ69FswomEWT620EuCcRFzFEd1JKi0 3JRKUVSxeuAWTDQwYsCEYUoo40wrty+ho6juMY2eBWAgKWjAOAHc/092ZbpB+QXewTNoXxRrFpa RIoChxqFU2dcg8av0N8Kx6toOmjsbUkeD0lxoTa//W5ZBeoAaX/W6SQjcmN+BUkHVfBr X-Google-Smtp-Source: AGHT+IFmF9lr2L/GrfaXGtoSRzIiLg5uHOR7SRBgpl9KQzhKiLEiPLv8cmiav9W79u0RiN7ppyLTKw== X-Received: by 2002:a17:90b:53c3:b0:305:2d9b:f8d1 with SMTP id 98e67ed59e1d1-30532164b3cmr3780219a91.24.1743236756733; Sat, 29 Mar 2025 01:25:56 -0700 (PDT) Received: from smtpclient.apple ([2601:646:8f81:6120:b008:a4e4:1666:e0f6]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30516d623a5sm3188537a91.25.2025.03.29.01.25.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 29 Mar 2025 01:25:56 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.400.131.1.6\)) Subject: Re: bug#77256: Treesit language-at-point From: Yuan Fu In-Reply-To: <87a5990x6t.fsf@mail.linkov.net> Date: Sat, 29 Mar 2025 01:25:44 -0700 Content-Transfer-Encoding: quoted-printable Message-Id: <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> References: <87a5990x6t.fsf@mail.linkov.net> To: Juri Linkov X-Mailer: Apple Mail (2.3826.400.131.1.6) X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 77256 Cc: 77256@debbugs.gnu.org, Vincenzo Pupillo X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) > On Mar 25, 2025, at 11:42=E2=80=AFAM, Juri Linkov = wrote: >=20 > Do we still need such complicated functions as > mhtml-ts-mode--language-at-point, js--treesit-language-at-point, etc. > that duplicate the rules from 'treesit-range-rules' > when now the default language-at-point function could be implemented > just as >=20 > (treesit-parser-language > (or (seq-some (lambda (o) (overlay-get o 'treesit-parser)) > (overlays-at (point) t)) > treesit-primary-parser)) Yeah, we can provide a default language-at-point function now that = determines the =E2=80=9Cmost relevant parser=E2=80=9D by embed level. = But we should keep treesit-language-at-point-function because a) it=E2=80=99= s already in a release version and b) we want major modes to be able to = customize what parser to pick at any given point. Yuan= From debbugs-submit-bounces@debbugs.gnu.org Mon Mar 31 13:04:44 2025 Received: (at 77256) by debbugs.gnu.org; 31 Mar 2025 17:04:44 +0000 Received: from localhost ([127.0.0.1]:42664 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tzIZ1-00007C-DN for submit@debbugs.gnu.org; Mon, 31 Mar 2025 13:04:44 -0400 Received: from relay5-d.mail.gandi.net ([217.70.183.197]:51633) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1tzIYy-00006B-MC for 77256@debbugs.gnu.org; Mon, 31 Mar 2025 13:04:41 -0400 Received: by mail.gandi.net (Postfix) with ESMTPSA id 578514433C; Mon, 31 Mar 2025 17:04:31 +0000 (UTC) From: Juri Linkov To: Yuan Fu Subject: Re: bug#77256: Treesit language-at-point In-Reply-To: <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> Organization: LINKOV.NET References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> Date: Mon, 31 Mar 2025 19:53:14 +0300 Message-ID: <87plhxw7fh.fsf@mail.linkov.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddukedtgeejucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufgjohhffffkfgggtgesmhdtreertderjeenucfhrhhomheplfhurhhiucfnihhnkhhovhcuoehjuhhriheslhhinhhkohhvrdhnvghtqeenucggtffrrghtthgvrhhnpedtiedthfehhfetteefgfefgffftedttdefjeefveekfefgteettddutefgleethfenucfkphepledurdduvdelrddutdehrdduudejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepledurdduvdelrddutdehrdduudejpdhhvghlohepmhgrihhlrdhgrghnughirdhnvghtpdhmrghilhhfrhhomhepjhhurhhisehlihhnkhhovhdrnhgvthdpnhgspghrtghpthhtohepfedprhgtphhtthhopehvrdhpuhhpihhllhhosehgmhgrihhlrdgtohhmpdhrtghpthhtohepjeejvdehieesuggvsggsuhhgshdrghhnuhdrohhrghdprhgtphhtthhopegtrghsohhurhhisehgmhgrihhlrdgtohhm X-GND-Sasl: juri@linkov.net X-Spam-Score: -0.7 (/) X-Debbugs-Envelope-To: 77256 Cc: 77256@debbugs.gnu.org, Vincenzo Pupillo X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.7 (-) --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit >> Do we still need such complicated functions as >> mhtml-ts-mode--language-at-point, js--treesit-language-at-point, etc. >> that duplicate the rules from 'treesit-range-rules' >> when now the default language-at-point function could be implemented >> just as >> >> (treesit-parser-language >> (or (seq-some (lambda (o) (overlay-get o 'treesit-parser)) >> (overlays-at (point) t)) >> treesit-primary-parser)) > > Yeah, we can provide a default language-at-point function now that > determines the “most relevant parser” by embed level. But we should keep > treesit-language-at-point-function because a) it’s already in a release > version and b) we want major modes to be able to customize what parser to > pick at any given point. Ok, so here is a complete patch: --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=treesit-parsers-at.patch diff --git a/lisp/textmodes/mhtml-ts-mode.el b/lisp/textmodes/mhtml-ts-mode.el index 22c0455a4ee..fdb286fc8c4 100644 --- a/lisp/textmodes/mhtml-ts-mode.el +++ b/lisp/textmodes/mhtml-ts-mode.el @@ -221,21 +222,6 @@ mhtml-ts-mode-menu "Menu bar for `mhtml-ts-mode'." css-mode--menu) -;; To enable some basic treesiter functionality, you should define -;; a function that recognizes which grammar is used at-point. -;; This function should be assigned to `treesit-language-at-point-function' -(defun mhtml-ts-mode--language-at-point (point) - "Return the language at POINT assuming the point is within a HTML buffer." - (let* ((node (treesit-node-at point 'html)) - (parent (treesit-node-parent node)) - (node-query (format "(%s (%s))" - (treesit-node-type parent) - (treesit-node-type node)))) - (cond - ((equal "(script_element (raw_text))" node-query) (js--treesit-language-at-point point)) - ((equal "(style_element (raw_text))" node-query) 'css) - (t 'html)))) - ;; Custom font-lock function that's used to apply color to css color ;; The signature of the function should be conforming to signature ;; QUERY-SPEC required by `treesit-font-lock-rules'. @@ -448,7 +434,7 @@ mhtml-ts-mode-flymake-mhtml ;;;###autoload (define-derived-mode mhtml-ts-mode html-ts-mode - '("HTML+" (:eval (let ((lang (mhtml-ts-mode--language-at-point (point)))) + '("HTML+" (:eval (let ((lang (treesit-language-at (point)))) (cond ((eq lang 'html) "") ((eq lang 'javascript) "JS") ((eq lang 'css) "CSS"))))) @@ -527,10 +520,6 @@ mhtml-ts-mode (setq-local c-ts-common--comment-regexp js--treesit-jsdoc-comment-regexp)) - - ;; Many treesit functions need to know the language at-point. - ;; So you should define such a function. - (setq-local treesit-language-at-point-function #'mhtml-ts-mode--language-at-point) (setq-local prettify-symbols-alist mhtml-ts-mode--prettify-symbols-alist) ;; Indent. diff --git a/lisp/treesit.el b/lisp/treesit.el index 54c29326df2..75c04912216 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -185,15 +185,15 @@ treesit-language-at This function assumes that parser ranges are up-to-date. It returns the return value of `treesit-language-at-point-function' if it's non-nil, otherwise it returns the language of the first -parser in `treesit-parser-list', or nil if there is no parser. +parser from `treesit-parsers-at', or the primary parser. -In a multi-language buffer, make sure -`treesit-language-at-point-function' is implemented! Otherwise -`treesit-language-at' wouldn't return the correct result." +In a multi-language buffer, optionally you can implement +`treesit-language-at-point-function' to return more correct result." (if treesit-language-at-point-function (funcall treesit-language-at-point-function position) - (when-let* ((parser (car (treesit-parser-list)))) - (treesit-parser-language parser)))) + (treesit-parser-language + (or (car (treesit-parsers-at position)) + treesit-primary-parser)))) ;;; Node API supplement @@ -247,8 +247,9 @@ treesit-node-at (parser-or-lang (let* ((local-parser (car (treesit-local-parsers-at pos parser-or-lang))) - (global-parser (car (treesit-parser-list - nil parser-or-lang))) + (global-parser (or (car (treesit-parsers-at + pos parser-or-lang)) + treesit-primary-parser)) (parser (or local-parser global-parser))) (when parser (treesit-parser-root-node parser)))) @@ -267,13 +268,10 @@ treesit-node-at (local-parser ;; Find the local parser with highest ;; embed-level at point. - (car (seq-sort-by #'treesit-parser-embed-level - (lambda (a b) - (> (or a 0) (or b 0))) - (treesit-local-parsers-at - pos lang)))) - (global-parser (car (treesit-parser-list - nil lang))) + (car (treesit-local-parsers-at pos lang))) + (global-parser (or (car (treesit-parsers-at + pos lang)) + treesit-primary-parser)) (parser (or local-parser global-parser))) (when parser (treesit-parser-root-node parser)))))) @@ -851,6 +849,38 @@ treesit--clip-ranges if (<= start (car range) (cdr range) end) collect range)) +(defun treesit-parsers-at (&optional pos language with-host local-only) + "Return all the non-primary parsers at POS. + +POS defaults to point. +If LANGUAGE is non-nil, only return parsers for LANGUAGE. + +If WITH-HOST is non-nil, return a list of (PARSER . HOST-PARSER) +instead. HOST-PARSER is the host parser which created the PARSER. + +If LOCAL-ONLY is non-nil, return only local parsers. +Local parsers are those which only parse a limited region marked +by an overlay with non-nil `treesit-parser' property." + (let ((res nil)) + ;; Refer to (ref:local-parser-overlay) for more explanation of local + ;; parser overlays. + (dolist (ov (overlays-at (or pos (point)))) + (when-let* ((parser (overlay-get ov 'treesit-parser)) + (host-parser (or (null with-host) + (overlay-get ov 'treesit-host-parser))) + (_ (or (null local-only) + (overlay-get ov 'treesit-parser-local-p))) + (_ (or (null language) + (eq (treesit-parser-language parser) + language)))) + (push (if with-host (cons parser host-parser) parser) res))) + (seq-sort-by (lambda (p) + (treesit-parser-embed-level + (or (car-safe p) p))) + (lambda (a b) + (> (or a 0) (or b 0))) + res))) + (defun treesit-local-parsers-at (&optional pos language with-host) "Return all the local parsers at POS. @@ -862,19 +892,7 @@ treesit-local-parsers-at If WITH-HOST is non-nil, return a list of (PARSER . HOST-PARSER) instead. HOST-PARSER is the host parser which created the local PARSER." - (let ((res nil)) - ;; Refer to (ref:local-parser-overlay) for more explanation of local - ;; parser overlays. - (dolist (ov (overlays-at (or pos (point)))) - (let ((parser (overlay-get ov 'treesit-parser)) - (host-parser (overlay-get ov 'treesit-host-parser)) - (local-p (overlay-get ov 'treesit-parser-local-p))) - (when (and parser host-parser local-p - (or (null language) - (eq (treesit-parser-language parser) - language))) - (push (if with-host (cons parser host-parser) parser) res)))) - (nreverse res))) + (treesit-parsers-at pos language with-host t)) (defun treesit-local-parsers-on (&optional beg end language with-host) "Return the list of local parsers that cover the region between BEG and END. @@ -3135,9 +3159,7 @@ treesit-up-list (setq parent (treesit-parent-until parent pred))) (unless parent - (let ((parsers (seq-keep (lambda (o) - (overlay-get o 'treesit-host-parser)) - (overlays-at (point) t)))) + (let ((parsers (mapcar #'cdr (treesit-parsers-at (point) nil t)))) (while (and (not parent) parsers) (setq parent (treesit-parent-until (treesit-node-at (point) (car parsers)) pred) @@ -3887,9 +3929,9 @@ treesit-simple-imenu (lambda (entry) (let* ((lang (car entry)) (settings (cdr entry)) - (global-parser (car (treesit-parser-list nil lang))) - (local-parsers - (treesit-parser-list nil lang 'embedded))) + (global-parser (or (car (treesit-parsers-at nil lang)) + treesit-primary-parser)) + (local-parsers (treesit-local-parsers-at nil lang))) (cons (treesit-language-display-name lang) ;; No one says you can't have both global and local ;; parsers for the same language. E.g., Rust uses @@ -4029,9 +4074,7 @@ treesit-outline-level (setq level (1+ level))) ;; Continue counting the host nodes. - (dolist (parser (seq-keep (lambda (o) - (overlay-get o 'treesit-host-parser)) - (overlays-at (point) t))) + (dolist (parser (mapcar #'cdr (treesit-parsers-at (point) nil t))) (let* ((node (treesit-node-at (point) parser)) (lang (treesit-parser-language parser)) (pred (alist-get lang treesit-aggregated-outline-predicate))) --=-=-=-- From debbugs-submit-bounces@debbugs.gnu.org Mon Mar 31 20:27:44 2025 Received: (at 77256) by debbugs.gnu.org; 1 Apr 2025 00:27:44 +0000 Received: from localhost ([127.0.0.1]:43570 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tzPTi-0006Zq-Cc for submit@debbugs.gnu.org; Mon, 31 Mar 2025 20:27:44 -0400 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]:54364) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1tzPTf-0006Yc-2L for 77256@debbugs.gnu.org; Mon, 31 Mar 2025 20:27:40 -0400 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-2295d78b45cso3729955ad.0 for <77256@debbugs.gnu.org>; Mon, 31 Mar 2025 17:27:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1743467253; x=1744072053; darn=debbugs.gnu.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=brq+Pb+/MwrPSdOVi0p6fHfy6DtUWfBsxybcULKimYc=; b=JFHPyUfUYRCjAAgwCozUtsP6nnjO+oDhqqYRhNA4ck0dGiiqYW9ZXWIa4jCtGkZ//B EPdJ2Lkt4ohXp2lAa14+5x+bnfDItjcRQQeHcIEYNudgaSi/lIAe3TNHX4QlL4p4YWcK 3FxCLlgfIIwl/iCii/DmuCYxbJvhF1bSDBimAdM6pszN2lfHDacDEdos99Xv45ourCzS P5D6tR11IQ6KhS5ka49rBsUSUQZLpcBFM44B53k6pUxzj0EIsM9Oh8iSbtVbzPovHQpb ZflhBtYAagHH+KpBiJG2IzwOmlAJlMsCzyGv+Mj+gooKd56XImFVBOr5RbC4PXzrgCuX nucg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743467253; x=1744072053; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=brq+Pb+/MwrPSdOVi0p6fHfy6DtUWfBsxybcULKimYc=; b=dQvM7PuHm/XqAT+DvDsHcW9nA//mX2bW6SloLXlz5VUyW9PxlfzQ6m1ueSbeiZPxdJ UHITsvivRa+EJn1wX4QrvPWYsc9RcTLEXABDqOeBCaQPIOV7Q7ZohMZe12k8wNKQQTz3 W46i1XOI0kY0ogFpunnbq6g4exxTnosnRtY/+lFW7cMsMZyhZxvZRYCL263sYI/rp0lZ yjwxax3YPNltRrwANQmdS+3pRE3NUW+waU6QEpL7MhFTS7HA/fJaoIOSXuqDObp5qPhb hqrO4rmYg16i9XRxvECIWUX9NQ5FAQ1P48agpAvKMc2HX1IUGSGA+88P/4K5XkV/P08E nbKQ== X-Gm-Message-State: AOJu0YyDZGuNO6sek92m0PuwAW5WotnNdTcnaWYfJsebRMI6VxysK9lT p2aSM9kMqwUr4wBiBcUjpBpAX4po7jgqDo7ER/HNFTFjSg1Fcjqq X-Gm-Gg: ASbGncsmzGny8KSo/cjKd40sd2RUkPvWqGh3qsq98ZjnFd65JoBYSpXqhy+vVF7tkHZ FmY0w0h4O7e8g36H/dDnspJH/IDlT8QHUvOzksktiCiVA0YT0CWeuYhx2Fg49uS1233kW+sT58e nDEDBL4FLYuhFnaW+w7DxXkb0LqsQwEtrd6nTHcLjuq+27MZ9MgPXggcz0AiD64Qm331ZKzHOdL 84LeVV+7ftpRoUjgMlbLdWk+Q9BxklH4pzwDNzGC+u/bUza3e1Fi8RCEi1KAR114njfO2FdEfdT hhdoejH+xqWFfSkRMnjufUZUht1norprpEGj7zNRSL1g1WYob9/MOseMm9WUGbS3E/i6WzwLvnc tKQ== X-Google-Smtp-Source: AGHT+IED0JzlkIMztFDzYN1VM/p0erbcF/mqsB6sk/rRxaUIvTt3EG3lelvNherw/fmLWpj+qf8oiA== X-Received: by 2002:a05:6a21:9214:b0:1f5:7873:304b with SMTP id adf61e73a8af0-2009f75b1f2mr18224964637.26.1743467252543; Mon, 31 Mar 2025 17:27:32 -0700 (PDT) Received: from smtpclient.apple ([2601:646:8f81:6120:7c76:5133:615:6915]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-af93ba0df86sm6951517a12.71.2025.03.31.17.27.31 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 31 Mar 2025 17:27:31 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.400.131.1.6\)) Subject: Re: bug#77256: Treesit language-at-point From: Yuan Fu In-Reply-To: <87plhxw7fh.fsf@mail.linkov.net> Date: Mon, 31 Mar 2025 17:27:20 -0700 Content-Transfer-Encoding: quoted-printable Message-Id: <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> To: Juri Linkov X-Mailer: Apple Mail (2.3826.400.131.1.6) X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 77256 Cc: 77256@debbugs.gnu.org, Vincenzo Pupillo X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) > On Mar 31, 2025, at 9:53=E2=80=AFAM, Juri Linkov = wrote: >=20 >>> Do we still need such complicated functions as >>> mhtml-ts-mode--language-at-point, js--treesit-language-at-point, = etc. >>> that duplicate the rules from 'treesit-range-rules' >>> when now the default language-at-point function could be implemented >>> just as >>>=20 >>> (treesit-parser-language >>> (or (seq-some (lambda (o) (overlay-get o 'treesit-parser)) >>> (overlays-at (point) t)) >>> treesit-primary-parser)) >>=20 >> Yeah, we can provide a default language-at-point function now that >> determines the =E2=80=9Cmost relevant parser=E2=80=9D by embed level. = But we should keep >> treesit-language-at-point-function because a) it=E2=80=99s already in = a release >> version and b) we want major modes to be able to customize what = parser to >> pick at any given point. >=20 > Ok, so here is a complete patch: Thanks for working on this! >=20 > diff --git a/lisp/textmodes/mhtml-ts-mode.el = b/lisp/textmodes/mhtml-ts-mode.el > index 22c0455a4ee..fdb286fc8c4 100644 > --- a/lisp/textmodes/mhtml-ts-mode.el > +++ b/lisp/textmodes/mhtml-ts-mode.el > @@ -221,21 +222,6 @@ mhtml-ts-mode-menu > "Menu bar for `mhtml-ts-mode'." > css-mode--menu) >=20 > -;; To enable some basic treesiter functionality, you should define > -;; a function that recognizes which grammar is used at-point. > -;; This function should be assigned to = `treesit-language-at-point-function' > -(defun mhtml-ts-mode--language-at-point (point) > - "Return the language at POINT assuming the point is within a HTML = buffer." > - (let* ((node (treesit-node-at point 'html)) > - (parent (treesit-node-parent node)) > - (node-query (format "(%s (%s))" > - (treesit-node-type parent) > - (treesit-node-type node)))) > - (cond > - ((equal "(script_element (raw_text))" node-query) = (js--treesit-language-at-point point)) > - ((equal "(style_element (raw_text))" node-query) 'css) > - (t 'html)))) > - > ;; Custom font-lock function that's used to apply color to css color > ;; The signature of the function should be conforming to signature > ;; QUERY-SPEC required by `treesit-font-lock-rules'. > @@ -448,7 +434,7 @@ mhtml-ts-mode-flymake-mhtml >=20 > ;;;###autoload > (define-derived-mode mhtml-ts-mode html-ts-mode > - '("HTML+" (:eval (let ((lang (mhtml-ts-mode--language-at-point = (point)))) > + '("HTML+" (:eval (let ((lang (treesit-language-at (point)))) > (cond ((eq lang 'html) "") > ((eq lang 'javascript) "JS") > ((eq lang 'css) "CSS"))))) > @@ -527,10 +520,6 @@ mhtml-ts-mode > (setq-local c-ts-common--comment-regexp > js--treesit-jsdoc-comment-regexp)) >=20 > - > - ;; Many treesit functions need to know the language at-point. > - ;; So you should define such a function. > - (setq-local treesit-language-at-point-function = #'mhtml-ts-mode--language-at-point) > (setq-local prettify-symbols-alist = mhtml-ts-mode--prettify-symbols-alist) >=20 > ;; Indent. > diff --git a/lisp/treesit.el b/lisp/treesit.el > index 54c29326df2..75c04912216 100644 > --- a/lisp/treesit.el > +++ b/lisp/treesit.el > @@ -185,15 +185,15 @@ treesit-language-at > This function assumes that parser ranges are up-to-date. It > returns the return value of `treesit-language-at-point-function' > if it's non-nil, otherwise it returns the language of the first > -parser in `treesit-parser-list', or nil if there is no parser. > +parser from `treesit-parsers-at', or the primary parser. If we handle the fallback case in treesit-language-at directly, rather = than defining a separate default function, IMO we should describe the = fallback behavior as the default. So something like =E2=80=9CReturn the = language at POS. When there are multiple parsers that covers POS, = determine the most relevant parser (hence language) by their embed = level. If treesit-language-at-point-function is non-nil, return the = return value of that function instead.=E2=80=9D (Not saying we should = use this exact docstring but to illustrate the point.) >=20 > -In a multi-language buffer, make sure > -`treesit-language-at-point-function' is implemented! Otherwise > -`treesit-language-at' wouldn't return the correct result." > +In a multi-language buffer, optionally you can implement > +`treesit-language-at-point-function' to return more correct result." > (if treesit-language-at-point-function > (funcall treesit-language-at-point-function position) > - (when-let* ((parser (car (treesit-parser-list)))) > - (treesit-parser-language parser)))) > + (treesit-parser-language > + (or (car (treesit-parsers-at position)) > + treesit-primary-parser)))) >=20 > ;;; Node API supplement >=20 > @@ -247,8 +247,9 @@ treesit-node-at > (parser-or-lang > (let* ((local-parser (car (treesit-local-parsers-at > pos parser-or-lang))) > - (global-parser (car (treesit-parser-list > - nil parser-or-lang))) > + (global-parser (or (car (treesit-parsers-at > + pos parser-or-lang)) > + treesit-primary-parser)) > (parser (or local-parser global-parser))) > (when parser > (treesit-parser-root-node parser)))) > @@ -267,13 +268,10 @@ treesit-node-at > (local-parser > ;; Find the local parser with highest > ;; embed-level at point. > - (car (seq-sort-by = #'treesit-parser-embed-level > - (lambda (a b) > - (> (or a 0) (or b 0))) > - (treesit-local-parsers-at > - pos lang)))) > - (global-parser (car (treesit-parser-list > - nil lang))) > + (car (treesit-local-parsers-at pos lang))) > + (global-parser (or (car (treesit-parsers-at > + pos lang)) > + treesit-primary-parser)) > (parser (or local-parser global-parser))) > (when parser > (treesit-parser-root-node parser)))))) > @@ -851,6 +849,38 @@ treesit--clip-ranges > if (<=3D start (car range) (cdr range) end) > collect range)) >=20 > +(defun treesit-parsers-at (&optional pos language with-host = local-only) > + "Return all the non-primary parsers at POS. I get why you used treesit-parsers-at rather than a more technically = correct name like treesit-non-primary-parsers-at, or = treesit-embeded-parsers-at. But this is confusing for not much benefit = IMO. I suggest either use treesit-parsers-at, and add an optional = argument exclude-primary; or use a more correct name and don=E2=80=99t = include the primary parser. Also, instead of using LOCAL-ONLY, we might = be more future-prove to use a ONLY parameter, and let user pass =E2=80=98l= ocal to mean local-only. This way we can add the option of returning = only non-local non-primary parsers in the future, should the need arises = (it already kinda does in treesit-node-at and treesit-simple-imenu, = technically global-parser should be picked from non-local parsers). > + > +POS defaults to point. > +If LANGUAGE is non-nil, only return parsers for LANGUAGE. > + > +If WITH-HOST is non-nil, return a list of (PARSER . HOST-PARSER) > +instead. HOST-PARSER is the host parser which created the PARSER. > + > +If LOCAL-ONLY is non-nil, return only local parsers. > +Local parsers are those which only parse a limited region marked > +by an overlay with non-nil `treesit-parser' property." > + (let ((res nil)) > + ;; Refer to (ref:local-parser-overlay) for more explanation of = local > + ;; parser overlays. > + (dolist (ov (overlays-at (or pos (point)))) > + (when-let* ((parser (overlay-get ov 'treesit-parser)) > + (host-parser (or (null with-host) > + (overlay-get ov = 'treesit-host-parser))) > + (_ (or (null local-only) > + (overlay-get ov 'treesit-parser-local-p))) > + (_ (or (null language) > + (eq (treesit-parser-language parser) > + language)))) > + (push (if with-host (cons parser host-parser) parser) res))) > + (seq-sort-by (lambda (p) > + (treesit-parser-embed-level > + (or (car-safe p) p))) > + (lambda (a b) > + (> (or a 0) (or b 0))) > + res))) > + > (defun treesit-local-parsers-at (&optional pos language with-host) > "Return all the local parsers at POS. >=20 > @@ -862,19 +892,7 @@ treesit-local-parsers-at > If WITH-HOST is non-nil, return a list of (PARSER . HOST-PARSER) > instead. HOST-PARSER is the host parser which created the local > PARSER." > - (let ((res nil)) > - ;; Refer to (ref:local-parser-overlay) for more explanation of = local > - ;; parser overlays. > - (dolist (ov (overlays-at (or pos (point)))) > - (let ((parser (overlay-get ov 'treesit-parser)) > - (host-parser (overlay-get ov 'treesit-host-parser)) > - (local-p (overlay-get ov 'treesit-parser-local-p))) > - (when (and parser host-parser local-p > - (or (null language) > - (eq (treesit-parser-language parser) > - language))) > - (push (if with-host (cons parser host-parser) parser) = res)))) > - (nreverse res))) > + (treesit-parsers-at pos language with-host t)) >=20 > (defun treesit-local-parsers-on (&optional beg end language with-host) > "Return the list of local parsers that cover the region between BEG = and END. > @@ -3135,9 +3159,7 @@ treesit-up-list > (setq parent (treesit-parent-until parent pred))) >=20 > (unless parent > - (let ((parsers (seq-keep (lambda (o) > - (overlay-get o = 'treesit-host-parser)) > - (overlays-at (point) t)))) > + (let ((parsers (mapcar #'cdr (treesit-parsers-at (point) = nil t)))) > (while (and (not parent) parsers) > (setq parent (treesit-parent-until > (treesit-node-at (point) (car parsers)) = pred) > @@ -3887,9 +3929,9 @@ treesit-simple-imenu > (lambda (entry) > (let* ((lang (car entry)) > (settings (cdr entry)) > - (global-parser (car (treesit-parser-list nil lang))) > - (local-parsers > - (treesit-parser-list nil lang 'embedded))) > + (global-parser (or (car (treesit-parsers-at nil lang)) > + treesit-primary-parser)) > + (local-parsers (treesit-local-parsers-at nil lang))) > (cons (treesit-language-display-name lang) > ;; No one says you can't have both global and local > ;; parsers for the same language. E.g., Rust uses > @@ -4029,9 +4074,7 @@ treesit-outline-level > (setq level (1+ level))) >=20 > ;; Continue counting the host nodes. > - (dolist (parser (seq-keep (lambda (o) > - (overlay-get o 'treesit-host-parser)) > - (overlays-at (point) t))) > + (dolist (parser (mapcar #'cdr (treesit-parsers-at (point) nil = t))) > (let* ((node (treesit-node-at (point) parser)) > (lang (treesit-parser-language parser)) > (pred (alist-get lang = treesit-aggregated-outline-predicate))) We should also update manual sections that mentions treesit-language-at. Yuan From debbugs-submit-bounces@debbugs.gnu.org Tue Apr 01 12:55:47 2025 Received: (at 77256) by debbugs.gnu.org; 1 Apr 2025 16:55:47 +0000 Received: from localhost ([127.0.0.1]:50761 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tzett-0001gl-Hv for submit@debbugs.gnu.org; Tue, 01 Apr 2025 12:55:47 -0400 Received: from relay8-d.mail.gandi.net ([2001:4b98:dc4:8::228]:48033) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1tzetq-0001fD-JB for 77256@debbugs.gnu.org; Tue, 01 Apr 2025 12:55:43 -0400 Received: by mail.gandi.net (Postfix) with ESMTPSA id DE25444440; Tue, 1 Apr 2025 16:55:29 +0000 (UTC) From: Juri Linkov To: Yuan Fu Subject: Re: bug#77256: Treesit language-at-point In-Reply-To: <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> Organization: LINKOV.NET References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> Date: Tue, 01 Apr 2025 19:53:37 +0300 Message-ID: <871pucdti6.fsf@mail.linkov.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddukeeffedvucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufgjohhffffkfgggtgesmhdtreertderjeenucfhrhhomheplfhurhhiucfnihhnkhhovhcuoehjuhhriheslhhinhhkohhvrdhnvghtqeenucggtffrrghtthgvrhhnpedtiedthfehhfetteefgfefgffftedttdefjeefveekfefgteettddutefgleethfenucfkphepledurdduvdelrddutdehrdduudejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepledurdduvdelrddutdehrdduudejpdhhvghlohepmhgrihhlrdhgrghnughirdhnvghtpdhmrghilhhfrhhomhepjhhurhhisehlihhnkhhovhdrnhgvthdpnhgspghrtghpthhtohepfedprhgtphhtthhopehvrdhpuhhpihhllhhosehgmhgrihhlrdgtohhmpdhrtghpthhtohepjeejvdehieesuggvsggsuhhgshdrghhnuhdrohhrghdprhgtphhtthhopegtrghsohhurhhisehgmhgrihhlrdgtohhm X-GND-Sasl: juri@linkov.net X-Spam-Score: -0.0 (/) X-Debbugs-Envelope-To: 77256 Cc: 77256@debbugs.gnu.org, Vincenzo Pupillo X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit >> @@ -185,15 +185,15 @@ treesit-language-at >> This function assumes that parser ranges are up-to-date. It >> returns the return value of `treesit-language-at-point-function' >> if it's non-nil, otherwise it returns the language of the first >> -parser in `treesit-parser-list', or nil if there is no parser. >> +parser from `treesit-parsers-at', or the primary parser. > > If we handle the fallback case in treesit-language-at directly, rather than > defining a separate default function, IMO we should describe the fallback > behavior as the default. So something like “Return the language at > POS. When there are multiple parsers that covers POS, determine the most > relevant parser (hence language) by their embed level. If > treesit-language-at-point-function is non-nil, return the return value of > that function instead.” (Not saying we should use this exact docstring but > to illustrate the point.) Now fixed in the next patch. >> +(defun treesit-parsers-at (&optional pos language with-host local-only) >> + "Return all the non-primary parsers at POS. > > I get why you used treesit-parsers-at rather than a more technically > correct name like treesit-non-primary-parsers-at, or > treesit-embeded-parsers-at. But this is confusing for not much benefit > IMO. I suggest either use treesit-parsers-at, and add an optional argument > exclude-primary; or use a more correct name and don’t include the primary > parser. Also, instead of using LOCAL-ONLY, we might be more future-prove to > use a ONLY parameter, and let user pass ‘local to mean local-only. This way > we can add the option of returning only non-local non-primary parsers in > the future, should the need arises (it already kinda does in > treesit-node-at and treesit-simple-imenu, technically global-parser should > be picked from non-local parsers). I can't find a better function signature than to add the argument ONLY with the following combinations of symbols for its list, wrt existing uses of treesit-parsers-at: nil treesit-language-at (nil returns all parsers) (local) treesit-local-parsers-at (primary global) treesit-node-at, treesit-simple-imenu (global local) treesit-outline-level, treesit-up-list > We should also update manual sections that mentions treesit-language-at. Will do when everything is ok with this patch: --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=treesit-parsers-at.patch diff --git a/lisp/treesit.el b/lisp/treesit.el index 54c29326df2..4ba46e5b7ce 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -182,18 +182,14 @@ treesit-language-at-point-function (defun treesit-language-at (position) "Return the language at POSITION. -This function assumes that parser ranges are up-to-date. It -returns the return value of `treesit-language-at-point-function' -if it's non-nil, otherwise it returns the language of the first -parser in `treesit-parser-list', or nil if there is no parser. - -In a multi-language buffer, make sure -`treesit-language-at-point-function' is implemented! Otherwise -`treesit-language-at' wouldn't return the correct result." +When there are multiple parsers that covers POSITION, determine +the most relevant parser (hence language) by their embed level. +If `treesit-language-at-point-function' is non-nil, return +the return value of that function instead." (if treesit-language-at-point-function (funcall treesit-language-at-point-function position) - (when-let* ((parser (car (treesit-parser-list)))) - (treesit-parser-language parser)))) + (treesit-parser-language + (car (treesit-parsers-at position))))) ;;; Node API supplement @@ -247,8 +243,9 @@ treesit-node-at (parser-or-lang (let* ((local-parser (car (treesit-local-parsers-at pos parser-or-lang))) - (global-parser (car (treesit-parser-list - nil parser-or-lang))) + (global-parser (car (treesit-parsers-at + pos parser-or-lang nil + '(primary global)))) (parser (or local-parser global-parser))) (when parser (treesit-parser-root-node parser)))) @@ -267,13 +264,10 @@ treesit-node-at (local-parser ;; Find the local parser with highest ;; embed-level at point. - (car (seq-sort-by #'treesit-parser-embed-level - (lambda (a b) - (> (or a 0) (or b 0))) - (treesit-local-parsers-at - pos lang)))) - (global-parser (car (treesit-parser-list - nil lang))) + (car (treesit-local-parsers-at pos lang))) + (global-parser (car (treesit-parsers-at + pos lang nil + '(primary global)))) (parser (or local-parser global-parser))) (when parser (treesit-parser-root-node parser)))))) @@ -851,6 +845,56 @@ treesit--clip-ranges if (<= start (car range) (cdr range) end) collect range)) +(defun treesit-parsers-at (&optional pos language with-host only) + "Return all parsers at POS. + +POS defaults to point. The returned parsers are sorted by +the decreasing embed level. + +If LANGUAGE is non-nil, only return parsers for LANGUAGE. + +If WITH-HOST is non-nil, return a list of (PARSER . HOST-PARSER) +instead. HOST-PARSER is the host parser which created the PARSER. + +If ONLY is nil, return all parsers including the primary parser. + +The argument ONLY can be a list of symbols that specify what +parsers to include in the return value. + +If ONLY contains the symbol `local', include local parsers. +Local parsers are those which only parse a limited region marked +by an overlay with non-nil `treesit-parser' property. + +If ONLY contains the symbol `global', include non-local parsers +excluding the primary parser. + +If ONLY contains the symbol `primary', include the primary parser." + (let ((res nil)) + ;; Refer to (ref:local-parser-overlay) for more explanation of local + ;; parser overlays. + (dolist (ov (overlays-at (or pos (point)))) + (when-let* ((parser (overlay-get ov 'treesit-parser)) + (host-parser (or (null with-host) + (overlay-get ov 'treesit-host-parser))) + (_ (or (null language) + (eq (treesit-parser-language parser) + language))) + (_ (or (null only) + (and (memq 'local only) (memq 'global only)) + (and (memq 'local only) + (overlay-get ov 'treesit-parser-local-p)) + (and (memq 'global only) + (not (overlay-get ov 'treesit-parser-local-p)))))) + (push (if with-host (cons parser host-parser) parser) res))) + (when (or (null only) (memq 'primary only)) + (setq res (cons treesit-primary-parser res))) + (seq-sort-by (lambda (p) + (treesit-parser-embed-level + (or (car-safe p) p))) + (lambda (a b) + (> (or a 0) (or b 0))) + res))) + (defun treesit-local-parsers-at (&optional pos language with-host) "Return all the local parsers at POS. @@ -862,19 +906,7 @@ treesit-local-parsers-at If WITH-HOST is non-nil, return a list of (PARSER . HOST-PARSER) instead. HOST-PARSER is the host parser which created the local PARSER." - (let ((res nil)) - ;; Refer to (ref:local-parser-overlay) for more explanation of local - ;; parser overlays. - (dolist (ov (overlays-at (or pos (point)))) - (let ((parser (overlay-get ov 'treesit-parser)) - (host-parser (overlay-get ov 'treesit-host-parser)) - (local-p (overlay-get ov 'treesit-parser-local-p))) - (when (and parser host-parser local-p - (or (null language) - (eq (treesit-parser-language parser) - language))) - (push (if with-host (cons parser host-parser) parser) res)))) - (nreverse res))) + (treesit-parsers-at pos language with-host '(local))) (defun treesit-local-parsers-on (&optional beg end language with-host) "Return the list of local parsers that cover the region between BEG and END. @@ -3135,9 +3173,7 @@ treesit-up-list (setq parent (treesit-parent-until parent pred))) (unless parent - (let ((parsers (seq-keep (lambda (o) - (overlay-get o 'treesit-host-parser)) - (overlays-at (point) t)))) + (let ((parsers (mapcar #'cdr (treesit-parsers-at (point) nil t '(global local))))) (while (and (not parent) parsers) (setq parent (treesit-parent-until (treesit-node-at (point) (car parsers)) pred) @@ -3887,9 +3943,8 @@ treesit-simple-imenu (lambda (entry) (let* ((lang (car entry)) (settings (cdr entry)) - (global-parser (car (treesit-parser-list nil lang))) - (local-parsers - (treesit-parser-list nil lang 'embedded))) + (global-parser (car (treesit-parsers-at nil lang nil '(primary global)))) + (local-parsers (treesit-local-parsers-at nil lang))) (cons (treesit-language-display-name lang) ;; No one says you can't have both global and local ;; parsers for the same language. E.g., Rust uses @@ -4029,9 +4087,7 @@ treesit-outline-level (setq level (1+ level))) ;; Continue counting the host nodes. - (dolist (parser (seq-keep (lambda (o) - (overlay-get o 'treesit-host-parser)) - (overlays-at (point) t))) + (dolist (parser (mapcar #'cdr (treesit-parsers-at (point) nil t '(global local)))) (let* ((node (treesit-node-at (point) parser)) (lang (treesit-parser-language parser)) (pred (alist-get lang treesit-aggregated-outline-predicate))) --=-=-=-- From debbugs-submit-bounces@debbugs.gnu.org Tue Apr 01 19:59:24 2025 Received: (at 77256) by debbugs.gnu.org; 1 Apr 2025 23:59:24 +0000 Received: from localhost ([127.0.0.1]:52521 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tzlVs-0000Ea-2R for submit@debbugs.gnu.org; Tue, 01 Apr 2025 19:59:24 -0400 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]:45264) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1tzlVp-0000Du-J5 for 77256@debbugs.gnu.org; Tue, 01 Apr 2025 19:59:22 -0400 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-22423adf751so114524375ad.2 for <77256@debbugs.gnu.org>; Tue, 01 Apr 2025 16:59:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1743551955; x=1744156755; darn=debbugs.gnu.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=yvheMTCFO5vEgT3FR4m7QCwshOXpLPqWZn06dBf6VdI=; b=NPSOUGRTqCV6SgwJo1XQgzGm4cS4gx4OUgR2LoY/bTXpl4LphJA1YexaEswoe7NokK K5KuT4FmGUihbOT8zDyvwGYtdlnMpLe0Jw9ew1dRYy4/ykvfkVSRqXdfjRklcHDaEnVb wayYzVwej0NOpWXHVpi34kNOJtMf7mcxL9+wY86+tpb4442Yxd4JyEVfcxCy4G7DjTnN R2zRcT4aIYAzYlph2FO9Q3rIFWfrtVsOklqGEczwIBfvgx6PLWK73dv7hCF2s9iWHGgF uq9a1Kc0r3/NwLdxGFHTMmlfTObOfejivi97FmqyTlOFR7Z0eph879zq0G9AA6gGIFYp KCgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743551955; x=1744156755; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yvheMTCFO5vEgT3FR4m7QCwshOXpLPqWZn06dBf6VdI=; b=utoh2zjPnnzGMJxMHCAsEgo0amhTGwhtuwY2XGfhfWfcYdzBheYUy8muELCjYyaVFF LcXArChzF7MEJmohaWlzhuoUZYXrdhP4hspoSolqXn7Q7OSOTUcj+ONQKJvHx8zB/50X NdZN1e56IH2boZIk5c+8GCC3Zht/lITs95EO1pid+gURnJOA4P4WD20BLoVV+aM1Fxam F4IDxaspoPMwPTOIW62hRCMokIMW6+rsreEVaF23dcDMXfrYH8MVmFkKymAGxIbKupRy iEobYiearA6lse7/ugeQKqrGVUlz5okSI4X6au9I13VdKeG1teB9E0eL8TNW1mQNAYWb Eupg== X-Gm-Message-State: AOJu0YxQnXRPGTet50/EmohFUw7lcR0qeYogXV4/GM3zbldRhpEdKxnY RDS0aTKAQJpzBKKph2+qDsZEBNxwIj/HzXGZQrqM9USnPOQD/YTgDP+zCw== X-Gm-Gg: ASbGnctv5+bB0srSoCsfn7BLecd/yl1CEVVheGg7KGh2RQuKD9eb/yabguCvRTGi4M4 1zjS3twADEmEfGKTiDIkwT9aeFzpKaXwv+PohkzdB6uF+iJ+33loQVLLlflKp+Xma7dVRAOT7KO 5MdL1Ytq+7o+4D1Ehlznjffk0SHfncdP4o01KUgnKvc2RWKo4gwi5mrF0uuIx5JFbvf3FiDfcFP E+0YwK3pNpW0mdIYl3guN0oF94ykzEJt/9znyFleiv9yZ08fPtqfem+k/3wSr7XW1Ok4ukybATk 2okRhJpZkSY7hXIRPbLbMry/2y4pG5w3f/KuKkChUhY0z1f6/5XRJjdQpIjOLbpqsAs= X-Google-Smtp-Source: AGHT+IGkttlkMdBw+vxLkrJg5UyaPhRjCmTXeBLBaOpEDTgtr7N4T8xTVWEcW1I0kiBdYvCBXSNMDg== X-Received: by 2002:a17:903:22c7:b0:224:1c41:a4c0 with SMTP id d9443c01a7336-2295be31750mr67648785ad.9.1743551955506; Tue, 01 Apr 2025 16:59:15 -0700 (PDT) Received: from smtpclient.apple ([2601:646:8f81:6120:7c76:5133:615:6915]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2291f1cf131sm95763045ad.134.2025.04.01.16.59.14 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Apr 2025 16:59:14 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.400.131.1.6\)) Subject: Re: bug#77256: Treesit language-at-point From: Yuan Fu In-Reply-To: <871pucdti6.fsf@mail.linkov.net> Date: Tue, 1 Apr 2025 16:59:03 -0700 Content-Transfer-Encoding: quoted-printable Message-Id: <8B15AB89-2A73-4260-AFD5-400B11342625@gmail.com> References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> <871pucdti6.fsf@mail.linkov.net> To: Juri Linkov X-Mailer: Apple Mail (2.3826.400.131.1.6) X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 77256 Cc: 77256@debbugs.gnu.org, Vincenzo Pupillo X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) > On Apr 1, 2025, at 9:53=E2=80=AFAM, Juri Linkov = wrote: >=20 >>> @@ -185,15 +185,15 @@ treesit-language-at >>> This function assumes that parser ranges are up-to-date. It >>> returns the return value of `treesit-language-at-point-function' >>> if it's non-nil, otherwise it returns the language of the first >>> -parser in `treesit-parser-list', or nil if there is no parser. >>> +parser from `treesit-parsers-at', or the primary parser. >>=20 >> If we handle the fallback case in treesit-language-at directly, = rather than >> defining a separate default function, IMO we should describe the = fallback >> behavior as the default. So something like =E2=80=9CReturn the = language at >> POS. When there are multiple parsers that covers POS, determine the = most >> relevant parser (hence language) by their embed level. If >> treesit-language-at-point-function is non-nil, return the return = value of >> that function instead.=E2=80=9D (Not saying we should use this exact = docstring but >> to illustrate the point.) >=20 > Now fixed in the next patch. >=20 >>> +(defun treesit-parsers-at (&optional pos language with-host = local-only) >>> + "Return all the non-primary parsers at POS. >>=20 >> I get why you used treesit-parsers-at rather than a more technically >> correct name like treesit-non-primary-parsers-at, or >> treesit-embeded-parsers-at. But this is confusing for not much = benefit >> IMO. I suggest either use treesit-parsers-at, and add an optional = argument >> exclude-primary; or use a more correct name and don=E2=80=99t include = the primary >> parser. Also, instead of using LOCAL-ONLY, we might be more = future-prove to >> use a ONLY parameter, and let user pass =E2=80=98local to mean = local-only. This way >> we can add the option of returning only non-local non-primary parsers = in >> the future, should the need arises (it already kinda does in >> treesit-node-at and treesit-simple-imenu, technically global-parser = should >> be picked from non-local parsers). >=20 > I can't find a better function signature than to add the argument ONLY > with the following combinations of symbols for its list, wrt existing = uses > of treesit-parsers-at: >=20 > nil treesit-language-at (nil returns all parsers) > (local) treesit-local-parsers-at > (primary global) treesit-node-at, treesit-simple-imenu > (global local) treesit-outline-level, treesit-up-list >=20 >> We should also update manual sections that mentions = treesit-language-at. >=20 > Will do when everything is ok with this patch: >=20 Thanks! LGTM. Yuan= From debbugs-submit-bounces@debbugs.gnu.org Fri Apr 04 12:15:49 2025 Received: (at 77256) by debbugs.gnu.org; 4 Apr 2025 16:15:49 +0000 Received: from localhost ([127.0.0.1]:39699 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u0jht-00009h-Eu for submit@debbugs.gnu.org; Fri, 04 Apr 2025 12:15:49 -0400 Received: from relay3-d.mail.gandi.net ([2001:4b98:dc4:8::223]:43409) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1u0jhY-000069-Ag for 77256@debbugs.gnu.org; Fri, 04 Apr 2025 12:15:28 -0400 Received: by mail.gandi.net (Postfix) with ESMTPSA id 7FF362047C; Fri, 4 Apr 2025 16:15:19 +0000 (UTC) From: Juri Linkov To: Yuan Fu Subject: Re: bug#77256: Treesit language-at-point In-Reply-To: <8B15AB89-2A73-4260-AFD5-400B11342625@gmail.com> Organization: LINKOV.NET References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> <871pucdti6.fsf@mail.linkov.net> <8B15AB89-2A73-4260-AFD5-400B11342625@gmail.com> Date: Fri, 04 Apr 2025 19:14:11 +0300 Message-ID: <87y0wgx8l8.fsf@mail.linkov.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdduledukeelucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufgjohhffffkfgggtgesthdtredttdertdenucfhrhhomheplfhurhhiucfnihhnkhhovhcuoehjuhhriheslhhinhhkohhvrdhnvghtqeenucggtffrrghtthgvrhhnpeffgeetfeevlefhleejfeeuheeiudeitdffhfdutdekfeffgffhveehteegueekheenucfkphepledurdduvdelrddutdehrdduudejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepledurdduvdelrddutdehrdduudejpdhhvghlohepmhgrihhlrdhgrghnughirdhnvghtpdhmrghilhhfrhhomhepjhhurhhisehlihhnkhhovhdrnhgvthdpnhgspghrtghpthhtohepfedprhgtphhtthhopehvrdhpuhhpihhllhhosehgmhgrihhlrdgtohhmpdhrtghpthhtohepjeejvdehieesuggvsggsuhhgshdrghhnuhdrohhrghdprhgtphhtthhopegtrghsohhurhhisehgmhgrihhlrdgtohhm X-GND-Sasl: juri@linkov.net X-Spam-Score: -0.7 (/) X-Debbugs-Envelope-To: 77256 Cc: 77256@debbugs.gnu.org, Vincenzo Pupillo X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.7 (-) >>> We should also update manual sections that mentions treesit-language-at. >> >> Will do when everything is ok with this patch: > > Thanks! LGTM. Unfortunately, I discovered that it causes treesit-node-outdated errors. This is because overlays are updated by 'pre-redisplay-functions' later than outline-minor-mode uses them by 'after-change-functions', since 'after-change-functions' runs before 'pre-redisplay-functions'. The reproducible test case: with the latest patch for treesit.el, and another patch that removes 'mhtml-ts-mode--language-at-point' from mhtml-ts-mode.el, open a simple html file like this: test then type: 1. M-x load-library RET treesit-x RET 2. M-x liquid-generic-ts-mode RET 3. M-x outline-minor-mode RET 4. move point before 5. type SPC then with non-nil debug-on-error: Debugger entered--Lisp error: (treesit-node-outdated #) treesit-node-enclosed-p(# #) treesit-navigate-thing(73 1 beg html-ts-mode--outline-predicate) treesit-outline-search(#) outline-map-region(# 74 82) outline--fix-up-all-buttons(74 82) outline--fix-buttons-after-change(74 75 0) self-insert-command(1 32) funcall-interactively(self-insert-command 1 32) command-execute(self-insert-command) From debbugs-submit-bounces@debbugs.gnu.org Sat Apr 05 14:38:07 2025 Received: (at 77256) by debbugs.gnu.org; 5 Apr 2025 18:38:07 +0000 Received: from localhost ([127.0.0.1]:44752 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u18P9-0007bA-8L for submit@debbugs.gnu.org; Sat, 05 Apr 2025 14:38:07 -0400 Received: from relay9-d.mail.gandi.net ([2001:4b98:dc4:8::229]:43473) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1u18P6-0007aY-JE for 77256@debbugs.gnu.org; Sat, 05 Apr 2025 14:38:05 -0400 Received: by mail.gandi.net (Postfix) with ESMTPSA id 16850431EB; Sat, 5 Apr 2025 18:37:54 +0000 (UTC) From: Juri Linkov To: Yuan Fu Subject: Re: bug#77256: Treesit language-at-point In-Reply-To: <87y0wgx8l8.fsf@mail.linkov.net> Organization: LINKOV.NET References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> <871pucdti6.fsf@mail.linkov.net> <8B15AB89-2A73-4260-AFD5-400B11342625@gmail.com> <87y0wgx8l8.fsf@mail.linkov.net> Date: Sat, 05 Apr 2025 21:35:54 +0300 Message-ID: <87r026pib9.fsf@mail.linkov.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdduleehuddtucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufgjohhffffkfgggtgesthdtredttdertdenucfhrhhomheplfhurhhiucfnihhnkhhovhcuoehjuhhriheslhhinhhkohhvrdhnvghtqeenucggtffrrghtthgvrhhnpeffgeetfeevlefhleejfeeuheeiudeitdffhfdutdekfeffgffhveehteegueekheenucfkphepledurdduvdelrddutdehrdduudejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepledurdduvdelrddutdehrdduudejpdhhvghlohepmhgrihhlrdhgrghnughirdhnvghtpdhmrghilhhfrhhomhepjhhurhhisehlihhnkhhovhdrnhgvthdpnhgspghrtghpthhtohepfedprhgtphhtthhopehvrdhpuhhpihhllhhosehgmhgrihhlrdgtohhmpdhrtghpthhtohepjeejvdehieesuggvsggsuhhgshdrghhnuhdrohhrghdprhgtphhtthhopegtrghsohhurhhisehgmhgrihhlrdgtohhm X-GND-Sasl: juri@linkov.net X-Spam-Score: -0.0 (/) X-Debbugs-Envelope-To: 77256 Cc: 77256@debbugs.gnu.org, Vincenzo Pupillo X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) > Unfortunately, I discovered that it causes treesit-node-outdated errors. > > This is because overlays are updated by 'pre-redisplay-functions' > later than outline-minor-mode uses them by 'after-change-functions', > since 'after-change-functions' runs before 'pre-redisplay-functions'. This fixes the problem: (defun outline--fix-buttons-after-change (beg end _len) + (when (fboundp 'treesit-update-ranges) + (treesit-update-ranges beg end)) ;; Handle whole lines (save-excursion (goto-char beg) (setq beg (pos-bol))) (save-excursion (goto-char end) (setq end (pos-eol))) But can we do better? I see that 'treesit-major-mode-setup' adds the notifier (treesit-parser-add-notifier treesit-primary-parser #'treesit--font-lock-mark-ranges-to-fontify) to 'after_change_functions'. But why the treesit 'after_change_functions' notifier is called after the 'after-change-functions' hook? Can we change their order? From debbugs-submit-bounces@debbugs.gnu.org Sun Apr 06 00:51:52 2025 Received: (at 77256) by debbugs.gnu.org; 6 Apr 2025 04:51:52 +0000 Received: from localhost ([127.0.0.1]:45676 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u1Hz5-0001Ft-LJ for submit@debbugs.gnu.org; Sun, 06 Apr 2025 00:51:52 -0400 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]:61461) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1u1Hz2-0001Fd-Fe for 77256@debbugs.gnu.org; Sun, 06 Apr 2025 00:51:49 -0400 Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-736c3e7b390so2912251b3a.2 for <77256@debbugs.gnu.org>; Sat, 05 Apr 2025 21:51:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1743915102; x=1744519902; darn=debbugs.gnu.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Dxl4VVMMO1fXosinFNz2AOGMfMKY2m0te62ST5yVoek=; b=l0L+hhMV+P7kGDPdkjJRCrfPJJlILMoWzko+rBD64X0sQHKo2q/KL5O5vUlNVo2zD5 xWvCykd9/n+HyDmq2YpRLcThnz6NMEu/vecd1vh75r9beqn2lQ/ns1PEfkAYqCvazbVa PydA0AX4iUriWsWmZAkDitQKs4kkiCDIiPTIhFz0r31tzHFwyFyEfpgpuq9wGK+zkWqk Ppkyc2qglXvsk+KGYFcXX8gD0xudFtRRnYFqHwn+tj68qHnM8PLIDgrEUTyjvCOldgoe yx8OLLek/HZN1TI9Nbf3cLWV1p+4DpO8dkdesMydNgNxNFuUKEgjfOuJ82lF0ZaPQGk/ dReg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743915102; x=1744519902; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Dxl4VVMMO1fXosinFNz2AOGMfMKY2m0te62ST5yVoek=; b=rwfQG3AyeIJReX2i/z12yaFDjnULUArPMjFlxwsTk5jG32mzUO4/QUb8jzRt5SM5pd MVKDvh4FP4AX8h9a6FPHUJ5EqJq4w33Ef3/AGFlwk8iAQW82Te3H8WfLosKZA7jZF0ag kppjO/BkmCDn5MIfauUkzxZxSTDN+ziH50oM6fhZJ3Q1ZB2nJElAvotIqKieNJSTFJF9 Pm5IYkPeF4qox1pkYYK0f1xzkUeC9lbH82ZWArh1xDFslwxJ8PteBacV0Y6pOWqIN9FV GuO1+pudjMk23t0lYLsS3ae+booM6qEbSJT/syHew22xUGwSc0wi03acCYEAr/LI/zSs 2ZQw== X-Gm-Message-State: AOJu0Ywpl6D5M3HgvhNEVCNFToSQ9X8CLuMpmFq6K2dyym06aQDNXwof ykhomCAa4r+6XWfQQ1coEQ7zUZlo/lf3j+nF9Kt67/HpAaELW3gg X-Gm-Gg: ASbGncvg//pjscxRebyYjjnwchJTw4vhv3k+Wd2GUa2cgdT1vGN6HINt25qP2jpAvQY yf8Qj+AsubK+yNLn69PcL+vCsQOnwx5emGnIq5itpyypNQB9+7+gGvImmbNVVKNQYXhMlqKo2mN 51Ymtefj/eGlCsdATLgrsOUIsMbfnc0zrvhUuXzVjOaNCvctwuhag6GZxAalfavshhj5lE1Rln3 fnN8V3PUYU+6+dbnKeOfMKeWFsfaT5lU9pl9pmxilChEctuyQhEsfK3eoRtjMnBAGK//0Jcu0zH GQr1/W/N1waodsBewZQ/8NWfzcUrJ5Fm7T2KAbFeKqbH608GaPw1bp2e29mlGEON5Mc= X-Google-Smtp-Source: AGHT+IHhqRVs0oWbN/0I4OBqbDVdWOlN52YB+tbhcYGztE/iYHXoh0rtAEiJwdcJ5TBu5ErEUkzubA== X-Received: by 2002:a05:6a21:6d93:b0:1f5:7ba7:69d7 with SMTP id adf61e73a8af0-20107ea2353mr11502087637.3.1743915102224; Sat, 05 Apr 2025 21:51:42 -0700 (PDT) Received: from smtpclient.apple ([2601:646:8f81:6120:c894:9229:b93:122a]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-739d97ee7a0sm6040159b3a.41.2025.04.05.21.51.40 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 05 Apr 2025 21:51:41 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.400.131.1.6\)) Subject: Re: bug#77256: Treesit language-at-point From: Yuan Fu In-Reply-To: <87r026pib9.fsf@mail.linkov.net> Date: Sat, 5 Apr 2025 21:51:29 -0700 Content-Transfer-Encoding: quoted-printable Message-Id: <76B8FD44-EFDD-4A75-AAF6-3EA9A91A60A4@gmail.com> References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> <871pucdti6.fsf@mail.linkov.net> <8B15AB89-2A73-4260-AFD5-400B11342625@gmail.com> <87y0wgx8l8.fsf@mail.linkov.net> <87r026pib9.fsf@mail.linkov.net> To: Juri Linkov X-Mailer: Apple Mail (2.3826.400.131.1.6) X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 77256 Cc: 77256@debbugs.gnu.org, Vincenzo Pupillo X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) > On Apr 5, 2025, at 11:35=E2=80=AFAM, Juri Linkov = wrote: >=20 >> Unfortunately, I discovered that it causes treesit-node-outdated = errors. >>=20 >> This is because overlays are updated by 'pre-redisplay-functions' >> later than outline-minor-mode uses them by 'after-change-functions', >> since 'after-change-functions' runs before 'pre-redisplay-functions'. >=20 > This fixes the problem: >=20 > (defun outline--fix-buttons-after-change (beg end _len) > + (when (fboundp 'treesit-update-ranges) > + (treesit-update-ranges beg end)) > ;; Handle whole lines > (save-excursion (goto-char beg) (setq beg (pos-bol))) > (save-excursion (goto-char end) (setq end (pos-eol))) Using treesit-update-ranges is the right approach. But is there no = better place to put it? I assume there should be a function that = involves tree-sitter, in front of which you can put this call. > But can we do better? I see that 'treesit-major-mode-setup' > adds the notifier >=20 > (treesit-parser-add-notifier > treesit-primary-parser = #'treesit--font-lock-mark-ranges-to-fontify) >=20 > to 'after_change_functions'. But why the treesit > 'after_change_functions' notifier is called after the > 'after-change-functions' hook? Can we change their order? Because we parse lazily. And I should=E2=80=99ve called them after-parse = functions in the docstring (good thing I named them notifiers, not = after-change-functions)=E2=80=94they get called after the parser = re-parses, but the parser doesn=E2=80=99t have to re-parse immediately = after a buffer change. It only re-parses when some Lisp asks for a node = from the parse tree. In practice, that usually happens in pre-redisplay-function where we set = ranges for embedded parsers, which causes the primary parser to re-parse = and return a root node for querying. As a (intended) side-effect, the = notifiers get called and mark relevant regions to be re-fontified. Yuan From debbugs-submit-bounces@debbugs.gnu.org Sun Apr 06 02:56:12 2025 Received: (at 77256) by debbugs.gnu.org; 6 Apr 2025 06:56:12 +0000 Received: from localhost ([127.0.0.1]:45813 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u1JvP-0007Y1-IR for submit@debbugs.gnu.org; Sun, 06 Apr 2025 02:56:11 -0400 Received: from relay8-d.mail.gandi.net ([217.70.183.201]:37501) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1u1JvJ-0007XI-K8 for 77256@debbugs.gnu.org; Sun, 06 Apr 2025 02:56:09 -0400 Received: by mail.gandi.net (Postfix) with ESMTPSA id DE17144430; Sun, 6 Apr 2025 06:55:57 +0000 (UTC) From: Juri Linkov To: Yuan Fu Subject: Re: bug#77256: Treesit language-at-point In-Reply-To: <76B8FD44-EFDD-4A75-AAF6-3EA9A91A60A4@gmail.com> Organization: LINKOV.NET References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> <871pucdti6.fsf@mail.linkov.net> <8B15AB89-2A73-4260-AFD5-400B11342625@gmail.com> <87y0wgx8l8.fsf@mail.linkov.net> <87r026pib9.fsf@mail.linkov.net> <76B8FD44-EFDD-4A75-AAF6-3EA9A91A60A4@gmail.com> Date: Sun, 06 Apr 2025 09:51:27 +0300 Message-ID: <87semlpzah.fsf@mail.linkov.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdduleeiiedtucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufgjohhffffkfgggtgfgsehtkeertddtreejnecuhfhrohhmpefluhhrihcunfhinhhkohhvuceojhhurhhisehlihhnkhhovhdrnhgvtheqnecuggftrfgrthhtvghrnhepieffteejgeehffejuedtiefhudekgeejteekiefgveeuheetvdefgeekkeevkedunecukfhppeeluddruddvledruddthedruddujeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepihhnvghtpeeluddruddvledruddthedruddujedphhgvlhhopehmrghilhdrghgrnhguihdrnhgvthdpmhgrihhlfhhrohhmpehjuhhriheslhhinhhkohhvrdhnvghtpdhnsggprhgtphhtthhopeefpdhrtghpthhtohepvhdrphhuphhilhhlohesghhmrghilhdrtghomhdprhgtphhtthhopeejjedvheeiseguvggssghughhsrdhgnhhurdhorhhgpdhrtghpthhtoheptggrshhouhhrihesghhmrghilhdrtghomh X-GND-Sasl: juri@linkov.net X-Spam-Score: -0.7 (/) X-Debbugs-Envelope-To: 77256 Cc: 77256@debbugs.gnu.org, Vincenzo Pupillo X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.7 (-) >>> Unfortunately, I discovered that it causes treesit-node-outdated errors. >>> >>> This is because overlays are updated by 'pre-redisplay-functions' >>> later than outline-minor-mode uses them by 'after-change-functions', >>> since 'after-change-functions' runs before 'pre-redisplay-functions'. >> >> This fixes the problem: >> >> (defun outline--fix-buttons-after-change (beg end _len) >> + (when (fboundp 'treesit-update-ranges) >> + (treesit-update-ranges beg end)) >> ;; Handle whole lines >> (save-excursion (goto-char beg) (setq beg (pos-bol))) >> (save-excursion (goto-char end) (setq end (pos-eol))) > > Using treesit-update-ranges is the right approach. But is there no > better place to put it? I assume there should be a function that > involves tree-sitter, in front of which you can put this call. 'treesit-outline-search' has no information about the positions of the beginning and end of the range of changed text. Only 'after-change-functions' has access to this information. So what we could do is this, and I've tested that this approach works: #+begin_src diff diff --git a/lisp/treesit.el b/lisp/treesit.el index 07861603244..c77b28c4647 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -4044,6 +4112,10 @@ treesit-outline-level level)) +(defun treesit--after-change (beg end _len) + "Force updating the ranges after each text change." + (treesit-update-ranges beg end)) + ;;; Hideshow mode (defun treesit-hs-block-end () @@ -4338,7 +4410,16 @@ treesit-major-mode-setup (setq treesit-outline-predicate #'treesit-outline-predicate--from-imenu)) (setq-local outline-search-function #'treesit-outline-search - outline-level #'treesit-outline-level)) + outline-level #'treesit-outline-level) + (add-hook 'outline-minor-mode-hook + (lambda () + (if (bound-and-true-p outline-minor-mode) + (add-hook 'after-change-functions + #'treesit--after-change + 0 t) + (remove-hook 'after-change-functions + #'treesit--after-change t))) + nil t)) ;; Remove existing local parsers. (dolist (ov (overlays-in (point-min) (point-max))) #+end_src >> But can we do better? I see that 'treesit-major-mode-setup' >> adds the notifier >> >> (treesit-parser-add-notifier >> treesit-primary-parser #'treesit--font-lock-mark-ranges-to-fontify) >> >> to 'after_change_functions'. But why the treesit >> 'after_change_functions' notifier is called after the >> 'after-change-functions' hook? Can we change their order? > > Because we parse lazily. And I should’ve called them after-parse functions > in the docstring (good thing I named them notifiers, not > after-change-functions)—they get called after the parser re-parses, but the > parser doesn’t have to re-parse immediately after a buffer change. It only > re-parses when some Lisp asks for a node from the parse tree. > > In practice, that usually happens in pre-redisplay-function where we set > ranges for embedded parsers, which causes the primary parser to re-parse > and return a root node for querying. As a (intended) side-effect, the > notifiers get called and mark relevant regions to be re-fontified. Would it be possible on the first attempt of Lisp code to access a node to detect such a situation that there are some outdated nodes and to update their ranges automatically? From debbugs-submit-bounces@debbugs.gnu.org Tue Apr 08 13:45:59 2025 Received: (at 77256) by debbugs.gnu.org; 8 Apr 2025 17:46:00 +0000 Received: from localhost ([127.0.0.1]:35330 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u2D1L-0006lc-Hq for submit@debbugs.gnu.org; Tue, 08 Apr 2025 13:45:59 -0400 Received: from relay4-d.mail.gandi.net ([2001:4b98:dc4:8::224]:42645) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1u2D1I-0006kP-NX; Tue, 08 Apr 2025 13:45:57 -0400 Received: by mail.gandi.net (Postfix) with ESMTPSA id F169644553; Tue, 8 Apr 2025 17:45:35 +0000 (UTC) From: Juri Linkov To: Yuan Fu Subject: Re: bug#77256: Treesit language-at-point In-Reply-To: <8B15AB89-2A73-4260-AFD5-400B11342625@gmail.com> Organization: LINKOV.NET References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> <871pucdti6.fsf@mail.linkov.net> <8B15AB89-2A73-4260-AFD5-400B11342625@gmail.com> Date: Tue, 08 Apr 2025 20:40:44 +0300 Message-ID: <877c3upn4z.fsf@mail.linkov.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvtdefjedvucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufgjohhffffkfgggtgesthdtredttdertdenucfhrhhomheplfhurhhiucfnihhnkhhovhcuoehjuhhriheslhhinhhkohhvrdhnvghtqeenucggtffrrghtthgvrhhnpeffgeetfeevlefhleejfeeuheeiudeitdffhfdutdekfeffgffhveehteegueekheenucfkphepledurdduvdelrddutdehrdduudejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepledurdduvdelrddutdehrdduudejpdhhvghlohepmhgrihhlrdhgrghnughirdhnvghtpdhmrghilhhfrhhomhepjhhurhhisehlihhnkhhovhdrnhgvthdpnhgspghrtghpthhtohepgedprhgtphhtthhopegtohhnthhrohhlseguvggssghughhsrdhgnhhurdhorhhgpdhrtghpthhtohepvhdrphhuphhilhhlohesghhmrghilhdrtghomhdprhgtphhtthhopeejjedvheeiseguvggssghughhsrdhgnhhurdhorhhgpdhrtghpthhtoheptggrshhouhhrihesghhmrghilhdrtghomh X-GND-Sasl: juri@linkov.net X-Spam-Score: -0.0 (/) X-Debbugs-Envelope-To: 77256 Cc: 77256@debbugs.gnu.org, Vincenzo Pupillo X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) close 77256 31.0.50 thanks >>> We should also update manual sections that mentions treesit-language-at. >> >> Will do when everything is ok with this patch: > > Thanks! LGTM. Now pushed together with updates in the manual. Please check the commit 5e0daa1ef77. From debbugs-submit-bounces@debbugs.gnu.org Tue Apr 08 13:47:02 2025 Received: (at 77256) by debbugs.gnu.org; 8 Apr 2025 17:47:02 +0000 Received: from localhost ([127.0.0.1]:35337 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u2D2L-0006s6-U9 for submit@debbugs.gnu.org; Tue, 08 Apr 2025 13:47:02 -0400 Received: from relay5-d.mail.gandi.net ([2001:4b98:dc4:8::225]:45807) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1u2D1T-0006lB-2h for 77256@debbugs.gnu.org; Tue, 08 Apr 2025 13:46:07 -0400 Received: by mail.gandi.net (Postfix) with ESMTPSA id 3E2EE44363; Tue, 8 Apr 2025 17:45:51 +0000 (UTC) From: Juri Linkov To: Yuan Fu Subject: Re: bug#77256: Treesit language-at-point In-Reply-To: <87semlpzah.fsf@mail.linkov.net> Organization: LINKOV.NET References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> <871pucdti6.fsf@mail.linkov.net> <8B15AB89-2A73-4260-AFD5-400B11342625@gmail.com> <87y0wgx8l8.fsf@mail.linkov.net> <87r026pib9.fsf@mail.linkov.net> <76B8FD44-EFDD-4A75-AAF6-3EA9A91A60A4@gmail.com> <87semlpzah.fsf@mail.linkov.net> Date: Tue, 08 Apr 2025 20:44:05 +0300 Message-ID: <871pu2pmze.fsf@mail.linkov.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvtdefjedvucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufgjohhffffkfgggtgesthdtredttdertdenucfhrhhomheplfhurhhiucfnihhnkhhovhcuoehjuhhriheslhhinhhkohhvrdhnvghtqeenucggtffrrghtthgvrhhnpeffgeetfeevlefhleejfeeuheeiudeitdffhfdutdekfeffgffhveehteegueekheenucfkphepledurdduvdelrddutdehrdduudejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepledurdduvdelrddutdehrdduudejpdhhvghlohepmhgrihhlrdhgrghnughirdhnvghtpdhmrghilhhfrhhomhepjhhurhhisehlihhnkhhovhdrnhgvthdpnhgspghrtghpthhtohepfedprhgtphhtthhopehvrdhpuhhpihhllhhosehgmhgrihhlrdgtohhmpdhrtghpthhtohepjeejvdehieesuggvsggsuhhgshdrghhnuhdrohhrghdprhgtphhtthhopegtrghsohhurhhisehgmhgrihhlrdgtohhm X-GND-Sasl: juri@linkov.net X-Spam-Score: -0.0 (/) X-Debbugs-Envelope-To: 77256 Cc: 77256@debbugs.gnu.org, Vincenzo Pupillo X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) >> Using treesit-update-ranges is the right approach. But is there no >> better place to put it? I assume there should be a function that >> involves tree-sitter, in front of which you can put this call. > > 'treesit-outline-search' has no information about the positions of > the beginning and end of the range of changed text. Only > 'after-change-functions' has access to this information. > > So what we could do is this, and I've tested that this approach works: This is pushed as well. From debbugs-submit-bounces@debbugs.gnu.org Tue Apr 08 14:22:12 2025 Received: (at 77256) by debbugs.gnu.org; 8 Apr 2025 18:22:13 +0000 Received: from localhost ([127.0.0.1]:35410 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u2DaO-0003wy-Hz for submit@debbugs.gnu.org; Tue, 08 Apr 2025 14:22:12 -0400 Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:23193) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1u2DaL-0003wg-QY for 77256@debbugs.gnu.org; Tue, 08 Apr 2025 14:22:10 -0400 Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 34C66442745; Tue, 8 Apr 2025 14:22:03 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1744136522; bh=ad6UpOn7JFx1PE8i6khtXx32X+VWl+kt3ARpdol43SQ=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=DRkK/pYa/6ACqWg5vXDtRj6s7lrry0Ewy78GpFhznZhEccS+wvPlf5XTjdGubYj2n QdX3gcpJs3Ap4f+3vCIwKUNAAa/ZwPb7KoQ3R15IJM2LFcrO0ZkrivBEcK3ZTj/8rh J2D/zMFMntdAyt91fIC7wMgo1L7vmOvGWwlh0L7YA9obaxHm/kqRZnfrEqjF0455AT PGFdxYQVt49SrLe1LMXa9cQ5MjStSD1py9mzu5fSelb2Y+Gj4WuPoyAspo8ZEIDBqL tIH1WTKuKlKdPIS7E//0KuLZV2RKiU3mfnPZkdwqJwlpMg9jZxmvbVl+c0r7ymxltz nYFYUIjpY5gHA== Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 53A88442516; Tue, 8 Apr 2025 14:22:02 -0400 (EDT) Received: from pastel (unknown [104.247.242.5]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 1E9631202F4; Tue, 8 Apr 2025 14:22:02 -0400 (EDT) From: Stefan Monnier To: Yuan Fu Subject: Re: bug#77256: Treesit language-at-point In-Reply-To: <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> Message-ID: References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> Date: Tue, 08 Apr 2025 14:22:01 -0400 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain X-SPAM-INFO: Spam detection results: 0 ALL_TRUSTED -1 Passed through trusted hosts only via SMTP AWL -0.370 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DKIM_SIGNED 0.1 Message has a DKIM or DK signature, not necessarily valid DKIM_VALID -0.1 Message has at least one valid DKIM or DK signature DKIM_VALID_AU -0.1 Message has a valid DKIM or DK signature from author's domain DKIM_VALID_EF -0.1 Message has a valid DKIM or DK signature from envelope-from domain X-SPAM-LEVEL: X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: 77256 Cc: 77256@debbugs.gnu.org, Vincenzo Pupillo , Juri Linkov X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) >> (if treesit-language-at-point-function >> (funcall treesit-language-at-point-function position) BTW, can we please avoid this anti-pattern? Instead, we should unconditionally call `treesit-language-at-point-function` and give a non-nil default value to this variable. This would make sure the `treesit-language-at-point-function` is good enough to implement the default behavior and it makes it possible to use `add-function` on `treesit-language-at-point-function`. Stefan From debbugs-submit-bounces@debbugs.gnu.org Tue Apr 08 14:48:52 2025 Received: (at 77256) by debbugs.gnu.org; 8 Apr 2025 18:48:52 +0000 Received: from localhost ([127.0.0.1]:35489 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u2E0C-0005IH-69 for submit@debbugs.gnu.org; Tue, 08 Apr 2025 14:48:52 -0400 Received: from relay7-d.mail.gandi.net ([2001:4b98:dc4:8::227]:52771) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1u2E07-0005Hs-GY for 77256@debbugs.gnu.org; Tue, 08 Apr 2025 14:48:48 -0400 Received: by mail.gandi.net (Postfix) with ESMTPSA id 2533D44385; Tue, 8 Apr 2025 18:48:33 +0000 (UTC) From: Juri Linkov To: Stefan Monnier Subject: Re: bug#77256: Treesit language-at-point In-Reply-To: Organization: LINKOV.NET References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> Date: Tue, 08 Apr 2025 21:43:20 +0300 Message-ID: <874iyylcj5.fsf@mail.linkov.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvtdefkeehucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufgjohhffffkfgggtgesthdtredttdertdenucfhrhhomheplfhurhhiucfnihhnkhhovhcuoehjuhhriheslhhinhhkohhvrdhnvghtqeenucggtffrrghtthgvrhhnpeffgeetfeevlefhleejfeeuheeiudeitdffhfdutdekfeffgffhveehteegueekheenucfkphepledurdduvdelrddutdehrdduudejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepledurdduvdelrddutdehrdduudejpdhhvghlohepmhgrihhlrdhgrghnughirdhnvghtpdhmrghilhhfrhhomhepjhhurhhisehlihhnkhhovhdrnhgvthdpnhgspghrtghpthhtohepgedprhgtphhtthhopehvrdhpuhhpihhllhhosehgmhgrihhlrdgtohhmpdhrtghpthhtohepjeejvdehieesuggvsggsuhhgshdrghhnuhdrohhrghdprhgtphhtthhopegtrghsohhurhhisehgmhgrihhlrdgtohhmpdhrtghpthhtohepmhhonhhnihgvrhesihhrohdruhhmohhnthhrvggrlhdrtggr X-GND-Sasl: juri@linkov.net X-Spam-Score: -0.0 (/) X-Debbugs-Envelope-To: 77256 Cc: Yuan Fu , Vincenzo Pupillo , 77256@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) >>> (if treesit-language-at-point-function >>> (funcall treesit-language-at-point-function position) > > BTW, can we please avoid this anti-pattern? > Instead, we should unconditionally call > `treesit-language-at-point-function` and give a non-nil default value to > this variable. > > This would make sure the `treesit-language-at-point-function` is good > enough to implement the default behavior and it makes it possible to use > `add-function` on `treesit-language-at-point-function`. Unfortunately, it's too late to add a non-nil default value. I already tried to do the same by adding 'forward-sexp-default-function' to 'forward-sexp-function', but failed spectacularly (bug#70426), because there are existing uses that expect the default value to be nil. From debbugs-submit-bounces@debbugs.gnu.org Tue Apr 08 15:52:21 2025 Received: (at 77256) by debbugs.gnu.org; 8 Apr 2025 19:52:21 +0000 Received: from localhost ([127.0.0.1]:35590 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u2Ezd-0008Tl-9x for submit@debbugs.gnu.org; Tue, 08 Apr 2025 15:52:21 -0400 Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:38081) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1u2EzZ-0008TU-9C for 77256@debbugs.gnu.org; Tue, 08 Apr 2025 15:52:18 -0400 Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id DE62B442E92; Tue, 8 Apr 2025 15:52:10 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1744141929; bh=7kO/tPW85z+aZf+TTtRMnd24JViO/q3+/D6AuWzn3AM=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=TpMR3YvI3A0Fy5kJQrhAYaRO13OtFIUcJ14P4Wp6FFVs6PAiPJ1kdV0SRdMtBZyzC Tas775v0JX9gHZtJaSIpgRTiFpXOHYvb6YHVaVEeIEh4vrd4oMa/U2qv2DXczg0dmL 0hWNvRo8xx6ceWDY/3PuPKxAqmsmwtJc4bnK/euTQNOGJ3PIoTEm7vkfvUVpxMjs2p gO7/LAT46PuhPik5Y3LjCoO9Mju0eZixQwb8RgC2TFJ/fx5m1FEG+Bv3bwtSkHWzA2 O4tlMISryL0EqsR7cPxv62Ygxc59iUmZYeGEEvt3gHio+dyP2WUu9QA9GRpNDqvRiZ 88KtyV2CETHog== Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id E565E442744; Tue, 8 Apr 2025 15:52:09 -0400 (EDT) Received: from pastel (unknown [104.247.242.5]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id B127A1203CA; Tue, 8 Apr 2025 15:52:09 -0400 (EDT) From: Stefan Monnier To: Juri Linkov Subject: Re: bug#77256: Treesit language-at-point In-Reply-To: <874iyylcj5.fsf@mail.linkov.net> Message-ID: References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> <874iyylcj5.fsf@mail.linkov.net> Date: Tue, 08 Apr 2025 15:52:08 -0400 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain X-SPAM-INFO: Spam detection results: 0 ALL_TRUSTED -1 Passed through trusted hosts only via SMTP AWL -0.365 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DKIM_SIGNED 0.1 Message has a DKIM or DK signature, not necessarily valid DKIM_VALID -0.1 Message has at least one valid DKIM or DK signature DKIM_VALID_AU -0.1 Message has a valid DKIM or DK signature from author's domain DKIM_VALID_EF -0.1 Message has a valid DKIM or DK signature from envelope-from domain X-SPAM-LEVEL: X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: 77256 Cc: Yuan Fu , Vincenzo Pupillo , 77256@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) >> This would make sure the `treesit-language-at-point-function` is good >> enough to implement the default behavior and it makes it possible to use >> `add-function` on `treesit-language-at-point-function`. > > Unfortunately, it's too late to add a non-nil default value. Is it? I can't find any code out there that seems to care what is its default value (all users set it but doesn't look at it, AFAICT). > I already tried to do the same by adding 'forward-sexp-default-function' > to 'forward-sexp-function', but failed spectacularly (bug#70426), Yes, that one is much harder (it's one of the first I tried to change), Stefan From debbugs-submit-bounces@debbugs.gnu.org Wed Apr 09 02:25:19 2025 Received: (at 77256) by debbugs.gnu.org; 9 Apr 2025 06:25:19 +0000 Received: from localhost ([127.0.0.1]:36928 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u2OsB-0006Xs-5V for submit@debbugs.gnu.org; Wed, 09 Apr 2025 02:25:19 -0400 Received: from relay2-d.mail.gandi.net ([217.70.183.194]:35271) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1u2Os8-0006Wz-D1 for 77256@debbugs.gnu.org; Wed, 09 Apr 2025 02:25:16 -0400 Received: by mail.gandi.net (Postfix) with ESMTPSA id 78225442A4; Wed, 9 Apr 2025 06:25:05 +0000 (UTC) From: Juri Linkov To: Stefan Monnier Subject: Re: bug#77256: Treesit language-at-point In-Reply-To: Organization: LINKOV.NET References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> <874iyylcj5.fsf@mail.linkov.net> Date: Wed, 09 Apr 2025 09:18:52 +0300 Message-ID: <8734ehanab.fsf@mail.linkov.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvtdehvdeiucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufgjohhffffkfgggtgesmhdtreertdertdenucfhrhhomheplfhurhhiucfnihhnkhhovhcuoehjuhhriheslhhinhhkohhvrdhnvghtqeenucggtffrrghtthgvrhhnpeettdeiieegfeehtddvgeekhfelvdeggfevfeevgeelvdffudekkedtledtgfekvdenucfkphepledurdduvdelrddutdehrdduudejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepledurdduvdelrddutdehrdduudejpdhhvghlohepmhgrihhlrdhgrghnughirdhnvghtpdhmrghilhhfrhhomhepjhhurhhisehlihhnkhhovhdrnhgvthdpnhgspghrtghpthhtohepgedprhgtphhtthhopehvrdhpuhhpihhllhhosehgmhgrihhlrdgtohhmpdhrtghpthhtohepjeejvdehieesuggvsggsuhhgshdrghhnuhdrohhrghdprhgtphhtthhopegtrghsohhurhhisehgmhgrihhlrdgtohhmpdhrtghpthhtohepmhhonhhnihgvrhesihhrohdruhhmohhnthhrvggrlhdrtggr X-GND-Sasl: juri@linkov.net X-Spam-Score: -0.7 (/) X-Debbugs-Envelope-To: 77256 Cc: Yuan Fu , Vincenzo Pupillo , 77256@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.7 (-) --=-=-= Content-Type: text/plain >>> This would make sure the `treesit-language-at-point-function` is good >>> enough to implement the default behavior and it makes it possible to use >>> `add-function` on `treesit-language-at-point-function`. >> >> Unfortunately, it's too late to add a non-nil default value. > > Is it? > I can't find any code out there that seems to care what is its default > value (all users set it but doesn't look at it, AFAICT). This case looks problematic: ;; LANG can be nil. We don't want to use the fallback ;; in `treesit-language-at', so here we call ;; `treesit-language-at-point-function' directly. (let* ((lang (and treesit-language-at-point-function (funcall treesit-language-at-point-function pos))) But I believe this whole 'let*' can be replaced by just the 'treesit-parsers-at' call. I hope Yuan could confirm this. >> I already tried to do the same by adding 'forward-sexp-default-function' >> to 'forward-sexp-function', but failed spectacularly (bug#70426), > > Yes, that one is much harder (it's one of the first I tried to change), If no more code outside relies on the default value, the default function could be added like this: --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=treesit-language-at-point-default.patch diff --git a/lisp/treesit.el b/lisp/treesit.el index 8e57a6dae14..a010ff24afb 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -165,7 +165,8 @@ treesit-primary-parser The primary parser should be a parser that parses the entire buffer, as opposed to embedded parsers which parses only part of the buffer.") -(defvar-local treesit-language-at-point-function nil +(defvar-local treesit-language-at-point-function + #'treesit-language-at-point-default "A function that returns the language at point. This is used by `treesit-language-at', which is used by various functions to determine which parser to use at point. @@ -183,10 +184,13 @@ treesit-language-at the most relevant parser (hence language) by their embed level. If `treesit-language-at-point-function' is non-nil, return the return value of that function instead." - (if treesit-language-at-point-function - (funcall treesit-language-at-point-function position) - (treesit-parser-language - (car (treesit-parsers-at position))))) + (funcall treesit-language-at-point-function position)) + +(defun treesit-language-at-point-default (position) + "Default function for `treesit-language-at-point-function'. +Return the deepest parser by embed level." + (treesit-parser-language + (car (treesit-parsers-at position)))) ;;; Node API supplement --=-=-= Content-Type: text/plain BTW, I noticed now that indentation of the default value in defvar-local is incorrect and differs from defvar and defcustom. Here is the fix: diff --git a/lisp/subr.el b/lisp/subr.el index be847aab28a..164ed2a6f63 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -197,7 +197,7 @@ defvar-local Like `defvar' but additionally marks the variable as being automatically buffer-local wherever it is set. \n(fn SYMBOL &optional VALUE DOCSTRING)" - (declare (debug defvar) (doc-string 3) (indent 2)) + (declare (debug defvar) (doc-string 3) (indent defun)) ;; Can't use backquote here, it's too early in the bootstrap. (let ((value (car-safe args)) (docstring (car-safe (cdr-safe args)))) --=-=-=-- From debbugs-submit-bounces@debbugs.gnu.org Wed Apr 09 03:26:26 2025 Received: (at 77256) by debbugs.gnu.org; 9 Apr 2025 07:26:26 +0000 Received: from localhost ([127.0.0.1]:37076 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u2PpJ-0001iH-O1 for submit@debbugs.gnu.org; Wed, 09 Apr 2025 03:26:26 -0400 Received: from mail-pg1-x530.google.com ([2607:f8b0:4864:20::530]:60463) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1u2PpG-0001i0-R8 for 77256@debbugs.gnu.org; Wed, 09 Apr 2025 03:26:23 -0400 Received: by mail-pg1-x530.google.com with SMTP id 41be03b00d2f7-af59c920d32so4550510a12.0 for <77256@debbugs.gnu.org>; Wed, 09 Apr 2025 00:26:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744183576; x=1744788376; darn=debbugs.gnu.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=RPL7IyW5Vu711oKbwYI0jnC9XvrcJLbzP/AM4p+iAqU=; b=dbKzJVQ1yhglhJ74G0vqb/SjwlEVM3TUgiXsi6UqKlYUtQML9vMwCuaDsnJVNDbQZJ Srz0dpyMbtT2o9zS1mkCEQy44Mee9e0hvl0tL5s26LlM8/K8L7kZI08CvykQUQVPng7/ QtMCvIlHnuXW1wRA09LVsY/q3G8QQ/iTqVccpsyDNHC5k58DozVX09V9Bah6SKLm2x0A J4qiW4PS295OEVkS5+YKGbHY/mJHustfsNvPRlF4zmVx36nBAgpcKEp0rZ11zqoKrDfW EVEucQZa9WfjksNKFaYEHVy6kCbXGeRCK1uhapY+3+mnnH044T2M+bwOvKJONcLr9iX7 Agfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744183576; x=1744788376; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RPL7IyW5Vu711oKbwYI0jnC9XvrcJLbzP/AM4p+iAqU=; b=Se7lRT1af0fjoZrxoi73Kx66tH9aQVBj6ZJky4pwMz8ePDA7uA5rQsbDcgo0/dYfkc 3LLxkDI3/Php3VMhCRAK8HO7RSYtQRUfPnVetu92362zzI2ab5QFmyvcbdGWEd7/lqDb GOiLP2fuMJ4PkBxFvqdQLBt6A9WCcjgXqxyB3Wsr0Q0O9umM9hHSBNLzlX2/QLXyIKDd Rk0eqWsjjXRa9zgob9xlF34dsWyAasFXhFMMbB16f4t6jHoGd/RsbZJGh6b9iiLA45mr deJjGkC/3oPgFnehxAOOJMrq89r8i/eDHOiYqp25ISLF+hNNuaduaMbNOJr1NdV97yQ6 IsHQ== X-Gm-Message-State: AOJu0YyIs3s9tGRT0JwAlnKT2XnaULZt+Qjv8TXWfa3e/BDL9gpPuxNI bE2ccV9eWhu7eGho7mMy/Ka6p9u8OsE1I3dcG583lXvL1M/CAr7z X-Gm-Gg: ASbGncvlMosKRk34SBkkJ8ZDARlTYxq+mI1XtsxYlDlRHQ3CjoVKHkMDE9IMQ1hJNUX 1T2op/E/5df4Fm4oc227fa8YKR+w/7VyghWUlygrvTbp/IyLptsF/D8RhElzTF//uuomZYv+nsT HuI/yOrMHoPU8RAIX/mO8GLkGZ/GKpF21IFvCOvMrUuGr5oxOubLawOSAh0UvH6JgrXKPieAT1K sMtw8FSJDfNoC2L0HRVtYRlBE2Z29yMf2T9Mvh228zkgmKodCkDyz4RFg8qwvfjJ1NSWcDTRfCF 9hIQHkWzrsJ27vmj8MWEK+re+wPSObK4l/KO+ficLsRKkDxK6KzVRSisnsqPG9I= X-Google-Smtp-Source: AGHT+IHrEDhi+Z9C15KfvmaA3B3kI/hyGhSZHjM2jYe7gx1lRbPKcXZKeoPTof7uTGDi4FoIj0atxw== X-Received: by 2002:a17:90b:2e10:b0:2ff:71ad:e84e with SMTP id 98e67ed59e1d1-306dbbae2femr3351329a91.10.1744183576239; Wed, 09 Apr 2025 00:26:16 -0700 (PDT) Received: from smtpclient.apple ([2601:646:8f81:6120:c6d:280f:35ab:ba87]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-306dd185adcsm838961a91.44.2025.04.09.00.26.15 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 09 Apr 2025 00:26:15 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.400.131.1.6\)) Subject: Re: bug#77256: Treesit language-at-point From: Yuan Fu In-Reply-To: <87semlpzah.fsf@mail.linkov.net> Date: Wed, 9 Apr 2025 00:26:04 -0700 Content-Transfer-Encoding: quoted-printable Message-Id: References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> <871pucdti6.fsf@mail.linkov.net> <8B15AB89-2A73-4260-AFD5-400B11342625@gmail.com> <87y0wgx8l8.fsf@mail.linkov.net> <87r026pib9.fsf@mail.linkov.net> <76B8FD44-EFDD-4A75-AAF6-3EA9A91A60A4@gmail.com> <87semlpzah.fsf@mail.linkov.net> To: Juri Linkov X-Mailer: Apple Mail (2.3826.400.131.1.6) X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 77256 Cc: 77256@debbugs.gnu.org, Vincenzo Pupillo X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) > On Apr 5, 2025, at 11:51=E2=80=AFPM, Juri Linkov = wrote: >=20 >>>> Unfortunately, I discovered that it causes treesit-node-outdated = errors. >>>>=20 >>>> This is because overlays are updated by 'pre-redisplay-functions' >>>> later than outline-minor-mode uses them by = 'after-change-functions', >>>> since 'after-change-functions' runs before = 'pre-redisplay-functions'. >>>=20 >>> This fixes the problem: >>>=20 >>> (defun outline--fix-buttons-after-change (beg end _len) >>> + (when (fboundp 'treesit-update-ranges) >>> + (treesit-update-ranges beg end)) >>> ;; Handle whole lines >>> (save-excursion (goto-char beg) (setq beg (pos-bol))) >>> (save-excursion (goto-char end) (setq end (pos-eol))) >>=20 >> Using treesit-update-ranges is the right approach. But is there no >> better place to put it? I assume there should be a function that >> involves tree-sitter, in front of which you can put this call. >=20 > 'treesit-outline-search' has no information about the positions of > the beginning and end of the range of changed text. Only > 'after-change-functions' has access to this information. >=20 > So what we could do is this, and I've tested that this approach works: >=20 > #+begin_src diff > diff --git a/lisp/treesit.el b/lisp/treesit.el > index 07861603244..c77b28c4647 100644 > --- a/lisp/treesit.el > +++ b/lisp/treesit.el > @@ -4044,6 +4112,10 @@ treesit-outline-level >=20 > level)) >=20 > +(defun treesit--after-change (beg end _len) > + "Force updating the ranges after each text change." > + (treesit-update-ranges beg end)) > + > ;;; Hideshow mode >=20 > (defun treesit-hs-block-end () > @@ -4338,7 +4410,16 @@ treesit-major-mode-setup > (setq treesit-outline-predicate > #'treesit-outline-predicate--from-imenu)) > (setq-local outline-search-function #'treesit-outline-search > - outline-level #'treesit-outline-level)) > + outline-level #'treesit-outline-level) > + (add-hook 'outline-minor-mode-hook > + (lambda () > + (if (bound-and-true-p outline-minor-mode) > + (add-hook 'after-change-functions > + #'treesit--after-change > + 0 t) > + (remove-hook 'after-change-functions > + #'treesit--after-change t))) > + nil t)) >=20 > ;; Remove existing local parsers. > (dolist (ov (overlays-in (point-min) (point-max))) > #+end_src This seems a bit convoluted to me. What do you think about adding some = hook or generic function to outline-minor-mode? >>> But can we do better? I see that 'treesit-major-mode-setup' >>> adds the notifier >>>=20 >>> (treesit-parser-add-notifier >>> treesit-primary-parser = #'treesit--font-lock-mark-ranges-to-fontify) >>>=20 >>> to 'after_change_functions'. But why the treesit >>> 'after_change_functions' notifier is called after the >>> 'after-change-functions' hook? Can we change their order? >>=20 >> Because we parse lazily. And I should=E2=80=99ve called them = after-parse functions >> in the docstring (good thing I named them notifiers, not >> after-change-functions)=E2=80=94they get called after the parser = re-parses, but the >> parser doesn=E2=80=99t have to re-parse immediately after a buffer = change. It only >> re-parses when some Lisp asks for a node from the parse tree. >>=20 >> In practice, that usually happens in pre-redisplay-function where we = set >> ranges for embedded parsers, which causes the primary parser to = re-parse >> and return a root node for querying. As a (intended) side-effect, the >> notifiers get called and mark relevant regions to be re-fontified. >=20 > Would it be possible on the first attempt of Lisp code to access a = node > to detect such a situation that there are some outdated nodes and > to update their ranges automatically? Probably not, since you need to have correct ranges to know which parser = to use to get nodes in the first place. Yuan= From debbugs-submit-bounces@debbugs.gnu.org Wed Apr 09 11:02:45 2025 Received: (at 77256) by debbugs.gnu.org; 9 Apr 2025 15:02:45 +0000 Received: from localhost ([127.0.0.1]:41131 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u2Wwv-0002Mr-6D for submit@debbugs.gnu.org; Wed, 09 Apr 2025 11:02:45 -0400 Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:4659) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1u2Wws-0002M0-Ms for 77256@debbugs.gnu.org; Wed, 09 Apr 2025 11:02:43 -0400 Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 67C4F442EE7; Wed, 9 Apr 2025 11:02:36 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1744210955; bh=zf5oEdFfXVQBGrC/IksJWqfDQ/e8ZE9cD05Pu0wfF64=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=dZl3lK+lmcMOGvezWAHNHzFkdxg5ymvVGv0imRjW2Fj6TJ74SQa7rAy7MxAwLJls4 ShzKSpoc5dAqCz6GTTnJHChWBNA3rsbz/OBaoiB/SPDd4cjtPQP2ZAW+DQkmuinHWa +8CqDN58poduA0SlOb63l38hzLKVjPJ0zPV76VzPI044/IhGdgCykLLZQvBTDDrzJL yPqywBZfMmcKB0iksZuCZMQ+BX5gueh4WLQeDI2KBTpzMv+j78bQt9WtrY5yH8ruR1 D1+AGL88zUrp+9iIIDRWqaWFT8nlvEoYWpusDCALpUI7kHYnUJd1/H0CCX2Lr9ZbZe Bi1VmHgpb+iLg== Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 3A971442EDD; Wed, 9 Apr 2025 11:02:35 -0400 (EDT) Received: from pastel (unknown [104.247.242.5]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id EC6D4120406; Wed, 9 Apr 2025 11:02:34 -0400 (EDT) From: Stefan Monnier To: Juri Linkov Subject: Re: bug#77256: Treesit language-at-point In-Reply-To: <8734ehanab.fsf@mail.linkov.net> Message-ID: References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> <874iyylcj5.fsf@mail.linkov.net> <8734ehanab.fsf@mail.linkov.net> Date: Wed, 09 Apr 2025 11:02:28 -0400 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain X-SPAM-INFO: Spam detection results: 0 ALL_TRUSTED -1 Passed through trusted hosts only via SMTP AWL -0.362 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DKIM_SIGNED 0.1 Message has a DKIM or DK signature, not necessarily valid DKIM_VALID -0.1 Message has at least one valid DKIM or DK signature DKIM_VALID_AU -0.1 Message has a valid DKIM or DK signature from author's domain DKIM_VALID_EF -0.1 Message has a valid DKIM or DK signature from envelope-from domain X-SPAM-LEVEL: X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: 77256 Cc: Yuan Fu , Vincenzo Pupillo , 77256@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) >> Is it? >> I can't find any code out there that seems to care what is its default >> value (all users set it but doesn't look at it, AFAICT). > > This case looks problematic: > > ;; LANG can be nil. We don't want to use the fallback > ;; in `treesit-language-at', so here we call > ;; `treesit-language-at-point-function' directly. > (let* ((lang (and treesit-language-at-point-function > (funcall treesit-language-at-point-function > pos))) Ah, indeed you're right. > But I believe this whole 'let*' can be replaced by just > the 'treesit-parsers-at' call. I hope Yuan could confirm this. [ Not familiar enough with this code, so I'll let Yuan chime in. ] > If no more code outside relies on the default value, > the default function could be added like this: [...] > - (if treesit-language-at-point-function > - (funcall treesit-language-at-point-function position) > - (treesit-parser-language > - (car (treesit-parsers-at position))))) > + (funcall treesit-language-at-point-function position)) Usually, I use something like: (funcall (or treesit-language-at-point-function #'treesit-language-at-point-default) position) when I make such a change, so as to preserve some compatibility with code that sets the var to nil. > BTW, I noticed now that indentation of the default value > in defvar-local is incorrect and differs from defvar and defcustom. > Here is the fix: > > diff --git a/lisp/subr.el b/lisp/subr.el > index be847aab28a..164ed2a6f63 100644 > --- a/lisp/subr.el > +++ b/lisp/subr.el > @@ -197,7 +197,7 @@ defvar-local > Like `defvar' but additionally marks the variable as being automatically > buffer-local wherever it is set. > \n(fn SYMBOL &optional VALUE DOCSTRING)" > - (declare (debug defvar) (doc-string 3) (indent 2)) > + (declare (debug defvar) (doc-string 3) (indent defun)) > ;; Can't use backquote here, it's too early in the bootstrap. > (let ((value (car-safe args)) > (docstring (car-safe (cdr-safe args)))) Good call, feel free to push, thanks. Stefan From debbugs-submit-bounces@debbugs.gnu.org Wed Apr 09 13:24:04 2025 Received: (at 77256) by debbugs.gnu.org; 9 Apr 2025 17:24:05 +0000 Received: from localhost ([127.0.0.1]:41487 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u2Z9g-0004fk-8l for submit@debbugs.gnu.org; Wed, 09 Apr 2025 13:24:04 -0400 Received: from relay3-d.mail.gandi.net ([2001:4b98:dc4:8::223]:43909) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1u2Z9d-0004fC-ST for 77256@debbugs.gnu.org; Wed, 09 Apr 2025 13:24:02 -0400 Received: by mail.gandi.net (Postfix) with ESMTPSA id B7CCE2047C; Wed, 9 Apr 2025 17:23:46 +0000 (UTC) From: Juri Linkov To: Yuan Fu Subject: Re: bug#77256: Treesit language-at-point In-Reply-To: Organization: LINKOV.NET References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> <871pucdti6.fsf@mail.linkov.net> <8B15AB89-2A73-4260-AFD5-400B11342625@gmail.com> <87y0wgx8l8.fsf@mail.linkov.net> <87r026pib9.fsf@mail.linkov.net> <76B8FD44-EFDD-4A75-AAF6-3EA9A91A60A4@gmail.com> <87semlpzah.fsf@mail.linkov.net> Date: Wed, 09 Apr 2025 20:16:19 +0300 Message-ID: <87lds96z2c.fsf@mail.linkov.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvtdeiheelucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufgjohhffffkfgggtgesmhdtreertdertdenucfhrhhomheplfhurhhiucfnihhnkhhovhcuoehjuhhriheslhhinhhkohhvrdhnvghtqeenucggtffrrghtthgvrhhnpeettdeiieegfeehtddvgeekhfelvdeggfevfeevgeelvdffudekkedtledtgfekvdenucfkphepledurdduvdelrddutdehrdduudejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepledurdduvdelrddutdehrdduudejpdhhvghlohepmhgrihhlrdhgrghnughirdhnvghtpdhmrghilhhfrhhomhepjhhurhhisehlihhnkhhovhdrnhgvthdpnhgspghrtghpthhtohepgedprhgtphhtthhopehvrdhpuhhpihhllhhosehgmhgrihhlrdgtohhmpdhrtghpthhtohepmhhonhhnihgvrhesihhrohdruhhmohhnthhrvggrlhdrtggrpdhrtghpthhtohepjeejvdehieesuggvsggsuhhgshdrghhnuhdrohhrghdprhgtphhtthhopegtrghsohhurhhisehgmhgrihhlrdgtohhm X-GND-Sasl: juri@linkov.net X-Spam-Score: -0.7 (/) X-Debbugs-Envelope-To: 77256 Cc: 77256@debbugs.gnu.org, Vincenzo Pupillo , Stefan Monnier X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.7 (-) --=-=-= Content-Type: text/plain >>>>> Unfortunately, I discovered that it causes treesit-node-outdated errors. >>>>> >>>>> This is because overlays are updated by 'pre-redisplay-functions' >>>>> later than outline-minor-mode uses them by 'after-change-functions', >>>>> since 'after-change-functions' runs before 'pre-redisplay-functions'. >>>> >>>> This fixes the problem: >>>> >>>> (defun outline--fix-buttons-after-change (beg end _len) >>>> + (when (fboundp 'treesit-update-ranges) >>>> + (treesit-update-ranges beg end)) >>>> ;; Handle whole lines >>>> (save-excursion (goto-char beg) (setq beg (pos-bol))) >>>> (save-excursion (goto-char end) (setq end (pos-eol))) >>> >>> Using treesit-update-ranges is the right approach. But is there no >>> better place to put it? I assume there should be a function that >>> involves tree-sitter, in front of which you can put this call. >> >> 'treesit-outline-search' has no information about the positions of >> the beginning and end of the range of changed text. Only >> 'after-change-functions' has access to this information. >> >> So what we could do is this, and I've tested that this approach works: >> >> #+begin_src diff >> diff --git a/lisp/treesit.el b/lisp/treesit.el >> index 07861603244..c77b28c4647 100644 >> --- a/lisp/treesit.el >> +++ b/lisp/treesit.el >> @@ -4044,6 +4112,10 @@ treesit-outline-level >> >> level)) >> >> +(defun treesit--after-change (beg end _len) >> + "Force updating the ranges after each text change." >> + (treesit-update-ranges beg end)) >> + >> ;;; Hideshow mode >> >> (defun treesit-hs-block-end () >> @@ -4338,7 +4410,16 @@ treesit-major-mode-setup >> (setq treesit-outline-predicate >> #'treesit-outline-predicate--from-imenu)) >> (setq-local outline-search-function #'treesit-outline-search >> - outline-level #'treesit-outline-level)) >> + outline-level #'treesit-outline-level) >> + (add-hook 'outline-minor-mode-hook >> + (lambda () >> + (if (bound-and-true-p outline-minor-mode) >> + (add-hook 'after-change-functions >> + #'treesit--after-change >> + 0 t) >> + (remove-hook 'after-change-functions >> + #'treesit--after-change t))) >> + nil t)) >> >> ;; Remove existing local parsers. >> (dolist (ov (overlays-in (point-min) (point-max))) >> #+end_src > > This seems a bit convoluted to me. What do you think about adding > some hook or generic function to outline-minor-mode? Is this better? --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=outline-after-change.patch diff --git a/lisp/outline.el b/lisp/outline.el index 1209b1ce766..ff1322b1e93 100644 --- a/lisp/outline.el +++ b/lisp/outline.el @@ -1980,7 +1980,11 @@ outline--fix-up-all-buttons (outline--insert-button (if close-p 'close 'open)))) (or from (point-min)) (or to (point-max))))) -(defun outline--fix-buttons-after-change (beg end _len) +(defvar outline-after-change nil + "List of functions to call after each text change in outline-mode.") + +(defun outline--fix-buttons-after-change (beg end len) + (run-hook-with-args 'outline-after-change beg end len) ;; Handle whole lines (save-excursion (goto-char beg) (setq beg (pos-bol))) (save-excursion (goto-char end) (setq end (pos-eol))) diff --git a/lisp/treesit.el b/lisp/treesit.el index 8e57a6dae14..05523bc857c 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -4069,7 +4069,8 @@ treesit-outline-level level)) (defun treesit--after-change (beg end _len) - "Force updating the ranges after each text change." + "Force updating the ranges in BEG...END. +Expected to be called after each text change." (treesit-update-ranges beg end)) ;;; Hideshow mode @@ -4367,15 +4368,7 @@ treesit-major-mode-setup #'treesit-outline-predicate--from-imenu)) (setq-local outline-search-function #'treesit-outline-search outline-level #'treesit-outline-level) - (add-hook 'outline-minor-mode-hook - (lambda () - (if (bound-and-true-p outline-minor-mode) - (add-hook 'after-change-functions - #'treesit--after-change - 0 t) - (remove-hook 'after-change-functions - #'treesit--after-change t))) - nil t)) + (add-hook 'outline-after-change #'treesit--after-change nil t)) ;; Remove existing local parsers. (dolist (ov (overlays-in (point-min) (point-max))) --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit >>>> But can we do better? I see that 'treesit-major-mode-setup' >>>> adds the notifier >>>> >>>> (treesit-parser-add-notifier >>>> treesit-primary-parser #'treesit--font-lock-mark-ranges-to-fontify) >>>> >>>> to 'after_change_functions'. But why the treesit >>>> 'after_change_functions' notifier is called after the >>>> 'after-change-functions' hook? Can we change their order? >>> >>> Because we parse lazily. And I should’ve called them after-parse functions >>> in the docstring (good thing I named them notifiers, not >>> after-change-functions)—they get called after the parser re-parses, but the >>> parser doesn’t have to re-parse immediately after a buffer change. It only >>> re-parses when some Lisp asks for a node from the parse tree. >>> >>> In practice, that usually happens in pre-redisplay-function where we set >>> ranges for embedded parsers, which causes the primary parser to re-parse >>> and return a root node for querying. As a (intended) side-effect, the >>> notifiers get called and mark relevant regions to be re-fontified. >> >> Would it be possible on the first attempt of Lisp code to access a node >> to detect such a situation that there are some outdated nodes and >> to update their ranges automatically? > > Probably not, since you need to have correct ranges to know > which parser to use to get nodes in the first place. --=-=-=-- From debbugs-submit-bounces@debbugs.gnu.org Wed Apr 09 13:24:58 2025 Received: (at 77256) by debbugs.gnu.org; 9 Apr 2025 17:24:58 +0000 Received: from localhost ([127.0.0.1]:41490 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u2ZAY-0004hM-5a for submit@debbugs.gnu.org; Wed, 09 Apr 2025 13:24:58 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:56949) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1u2Z9m-0004fu-UY for 77256@debbugs.gnu.org; Wed, 09 Apr 2025 13:24:11 -0400 Received: by mail.gandi.net (Postfix) with ESMTPSA id 77DF820488; Wed, 9 Apr 2025 17:24:00 +0000 (UTC) From: Juri Linkov To: Yuan Fu Subject: Re: bug#77256: Treesit language-at-point In-Reply-To: Organization: LINKOV.NET References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> <874iyylcj5.fsf@mail.linkov.net> <8734ehanab.fsf@mail.linkov.net> Date: Wed, 09 Apr 2025 20:20:47 +0300 Message-ID: <87a58p6ykw.fsf@mail.linkov.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvtdeiheelucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufgjohhffffkfgggtgesmhdtreertdertdenucfhrhhomheplfhurhhiucfnihhnkhhovhcuoehjuhhriheslhhinhhkohhvrdhnvghtqeenucggtffrrghtthgvrhhnpeettdeiieegfeehtddvgeekhfelvdeggfevfeevgeelvdffudekkedtledtgfekvdenucfkphepledurdduvdelrddutdehrdduudejnecuvehluhhsthgvrhfuihiivgepudenucfrrghrrghmpehinhgvthepledurdduvdelrddutdehrdduudejpdhhvghlohepmhgrihhlrdhgrghnughirdhnvghtpdhmrghilhhfrhhomhepjhhurhhisehlihhnkhhovhdrnhgvthdpnhgspghrtghpthhtohepgedprhgtphhtthhopehvrdhpuhhpihhllhhosehgmhgrihhlrdgtohhmpdhrtghpthhtohepjeejvdehieesuggvsggsuhhgshdrghhnuhdrohhrghdprhgtphhtthhopehmohhnnhhivghrsehirhhordhumhhonhhtrhgvrghlrdgtrgdprhgtphhtthhopegtrghsohhurhhisehgmhgrihhlrdgtohhm X-GND-Sasl: juri@linkov.net X-Spam-Score: -0.7 (/) X-Debbugs-Envelope-To: 77256 Cc: 77256@debbugs.gnu.org, Vincenzo Pupillo , Stefan Monnier X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.7 (-) --=-=-= Content-Type: text/plain >>> I can't find any code out there that seems to care what is its default >>> value (all users set it but doesn't look at it, AFAICT). >> >> This case looks problematic: >> >> ;; LANG can be nil. We don't want to use the fallback >> ;; in `treesit-language-at', so here we call >> ;; `treesit-language-at-point-function' directly. >> (let* ((lang (and treesit-language-at-point-function >> (funcall treesit-language-at-point-function >> pos))) > > Ah, indeed you're right. > >> But I believe this whole 'let*' can be replaced by just >> the 'treesit-parsers-at' call. I hope Yuan could confirm this. > > [ Not familiar enough with this code, so I'll let Yuan chime in. ] Yuan, please confirm whether the reference to 'treesit-language-at-point-function' can be removed from 'treesit-node-at' this way: --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=treesit-language-at-point-function.patch diff --git a/lisp/treesit.el b/lisp/treesit.el index 8e57a6dae14..5a2721cdda4 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -165,7 +165,8 @@ treesit-primary-parser The primary parser should be a parser that parses the entire buffer, as opposed to embedded parsers which parses only part of the buffer.") -(defvar-local treesit-language-at-point-function nil +(defvar-local treesit-language-at-point-function + #'treesit-language-at-point-default "A function that returns the language at point. This is used by `treesit-language-at', which is used by various functions to determine which parser to use at point. @@ -183,10 +184,15 @@ treesit-language-at the most relevant parser (hence language) by their embed level. If `treesit-language-at-point-function' is non-nil, return the return value of that function instead." - (if treesit-language-at-point-function - (funcall treesit-language-at-point-function position) - (treesit-parser-language - (car (treesit-parsers-at position))))) + (funcall (or treesit-language-at-point-function + #'treesit-language-at-point-default) + position)) + +(defun treesit-language-at-point-default (position) + "Default function for `treesit-language-at-point-function'. +Return the deepest parser by embed level." + (treesit-parser-language + (car (treesit-parsers-at position)))) ;;; Node API supplement @@ -238,12 +244,8 @@ treesit-node-at ;; 2. Given a language, try local parser, then global ;; parser. (parser-or-lang - (let* ((local-parser (car (treesit-local-parsers-at - pos parser-or-lang))) - (global-parser (car (treesit-parsers-at - pos parser-or-lang nil - '(primary global)))) - (parser (or local-parser global-parser))) + (let ((parser (car (treesit-parsers-at + pos parser-or-lang)))) (when parser (treesit-parser-root-node parser)))) ;; 3. No given language, try to get a language at point. @@ -252,20 +254,8 @@ treesit-node-at ;; finding parser, try local parser first, then global ;; parser. (t - ;; LANG can be nil. We don't want to use the fallback - ;; in `treesit-language-at', so here we call - ;; `treesit-language-at-point-function' directly. - (let* ((lang (and treesit-language-at-point-function - (funcall treesit-language-at-point-function - pos))) - (local-parser - ;; Find the local parser with highest - ;; embed-level at point. - (car (treesit-local-parsers-at pos lang))) - (global-parser (car (treesit-parsers-at - pos lang nil - '(primary global)))) - (parser (or local-parser global-parser))) + ;; LANG can be nil. Use the parser deepest by embed level. + (let ((parser (car (treesit-parsers-at pos)))) (when parser (treesit-parser-root-node parser)))))) (node root) --=-=-=-- From debbugs-submit-bounces@debbugs.gnu.org Thu Apr 10 04:07:10 2025 Received: (at 77256) by debbugs.gnu.org; 10 Apr 2025 08:07:10 +0000 Received: from localhost ([127.0.0.1]:43630 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u2mwH-00081a-G2 for submit@debbugs.gnu.org; Thu, 10 Apr 2025 04:07:09 -0400 Received: from mail-pf1-x434.google.com ([2607:f8b0:4864:20::434]:45562) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1u2mwE-000810-52 for 77256@debbugs.gnu.org; Thu, 10 Apr 2025 04:07:06 -0400 Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-739525d4e12so411268b3a.3 for <77256@debbugs.gnu.org>; Thu, 10 Apr 2025 01:07:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744272419; x=1744877219; darn=debbugs.gnu.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=9KLHg6+Z4LEhRqJCt0r49Vcz5Uw/AOhcRvFTKiOMJ2k=; b=Fs2lA4sFsGJFDJlpTeILFc0sDku2YXDTMpw03n89NfxsmD1DFSj2wyuEg8ZkfDWj6J dX9NOWp9xDNDyS5kNRh2vXs/M5cAsG90mZC8ZdNwdyLOW2Yowfcy5hHgZb76CsH49nPv 0xuVrMSG6rTWxKeQTiVYtiDyE5lVzoFybuFqjyiWWWHGyWKC3VdbDQls4KV6zYBixDs1 eT9I/3+09TFVcS9kRO7xrhm4mXKi+aYk5k/S3L/xDI1HQodDVR605+UEuw0IqhWbpqgk jygk5zHZXIDPzQkSAOnuQ3FRwla6SNosKOppAhzLoj8/cLyMDh4sgYk/yQA8HiQjuxof GMMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744272419; x=1744877219; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9KLHg6+Z4LEhRqJCt0r49Vcz5Uw/AOhcRvFTKiOMJ2k=; b=UzCVRLbcFU2M76uqnbxWBkJhUzDAhe6R7RaZUPPKvXaQ3KaYzEipGIeUyXM6COBrAU rqEis3b+kZoWJu+vxK3CMdX/NOTEjEBjFHBveFrGUy9kKMaIiS0DYaTs466XVwyuR5gL ZjnGpQmo8pYNK/nhQnYOtgGrIdL0b+J0vvUjgxRrvGXUOVSH/7Uf4pnyggYp/0H4yOQ+ 9kBYnJbGKt3RqS/0KoQJYD1rqXAnxiFcqH8kt981jqyvMmfEhWcswEO69Lc2XCzFBCws DX8d1bvHm5950QDYWStF/mYmYCVD01tCEpD0uO9Z88ZILRxxfO19B1SkElklEkx6Ae2b PjMQ== X-Forwarded-Encrypted: i=1; AJvYcCWluEEX/btAemrHK1PkEjf8Am4lPt1rMgxUcSlrEXxFGjFkVaXqa6KJZxYE7YOLRJ6Vr59bQg==@debbugs.gnu.org X-Gm-Message-State: AOJu0Yz8KPFgBZ5NNeqFpiOnU/rn5Uo0/fFr8OeJPuruEvGaIZfs3ZKf TOkzl/QvILShXGGV8XnxZT4eDjCvnrKLqlPFpb9hjVw9vqFDO9A6 X-Gm-Gg: ASbGncvF2oUDBSGTyYvBUXVcTu81XZWxxdEwP9nMfiwo2dE8gvyddWMCECCGnwDkFqt iWIKHF18p7oPzOkbND0xPeBcIXwGLl3q1TgPilTmK/p1GBeE+M2c5Hi+UMfdVmYW5IZ2jbl2dGT 61gzzfPLO1U4QhCo9ZYVLwVI2hxg8RLajGMCEO3N8I81EOob3vzxIcPjdMrGofY+YsaXjaC4zOV jAnK/gQlhcCT+vr0VUGT78/AVIyfXPbNVQhhmZINxIAwfCzxnDYYhaxcW+Roup8S9TAvoQMSAEh iOSPNb6OKbZgMzB6U7mdphA6YBhJq143wyMsIkWG5H/gO2PUi9vKxZWp6BN7x8dw X-Google-Smtp-Source: AGHT+IFboLSy9uw4FlIeLsz5jcz1CZRqvjM3li8S4xXAbPPqxBHXM9cQmf6/LHcp/z7EikcSB5Hrqw== X-Received: by 2002:a05:6a21:68a:b0:1f5:64fd:68eb with SMTP id adf61e73a8af0-2016945a43amr4180012637.7.1744272419545; Thu, 10 Apr 2025 01:06:59 -0700 (PDT) Received: from smtpclient.apple ([2601:646:8f81:6120:1135:3dc2:d406:1e75]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bb1e69505sm2635467b3a.168.2025.04.10.01.06.58 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 10 Apr 2025 01:06:58 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.400.131.1.6\)) Subject: Re: bug#77256: Treesit language-at-point From: Yuan Fu In-Reply-To: <8734ehanab.fsf@mail.linkov.net> Date: Thu, 10 Apr 2025 01:06:47 -0700 Content-Transfer-Encoding: quoted-printable Message-Id: <780FB757-2E74-4412-A2B5-CD31F362C2C5@gmail.com> References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> <874iyylcj5.fsf@mail.linkov.net> <8734ehanab.fsf@mail.linkov.net> To: Juri Linkov X-Mailer: Apple Mail (2.3826.400.131.1.6) X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 77256 Cc: 77256@debbugs.gnu.org, Vincenzo Pupillo , Stefan Monnier X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) > On Apr 8, 2025, at 11:18=E2=80=AFPM, Juri Linkov = wrote: >=20 >>>> This would make sure the `treesit-language-at-point-function` is = good >>>> enough to implement the default behavior and it makes it possible = to use >>>> `add-function` on `treesit-language-at-point-function`. >>>=20 >>> Unfortunately, it's too late to add a non-nil default value. >>=20 >> Is it? >> I can't find any code out there that seems to care what is its = default >> value (all users set it but doesn't look at it, AFAICT). The default value shouldn't matter. What=E2=80=99s the difference = between a major-mode-set non-nil value and a default non-nil value? >=20 > This case looks problematic: >=20 > ;; LANG can be nil. We don't want to use the fallback > ;; in `treesit-language-at', so here we call > ;; `treesit-language-at-point-function' directly. > (let* ((lang (and treesit-language-at-point-function > (funcall = treesit-language-at-point-function > pos))) >=20 > But I believe this whole 'let*' can be replaced by just > the 'treesit-parsers-at' call. I hope Yuan could confirm this. Why is this problematic? And yes, treesit-parsers-at is a good fit. >=20 >>> I already tried to do the same by adding = 'forward-sexp-default-function' >>> to 'forward-sexp-function', but failed spectacularly (bug#70426), >>=20 >> Yes, that one is much harder (it's one of the first I tried to = change), >=20 > If no more code outside relies on the default value, > the default function could be added like this: > diff --git a/lisp/treesit.el b/lisp/treesit.el > index 8e57a6dae14..a010ff24afb 100644 > --- a/lisp/treesit.el > +++ b/lisp/treesit.el > @@ -165,7 +165,8 @@ treesit-primary-parser > The primary parser should be a parser that parses the entire buffer, = as > opposed to embedded parsers which parses only part of the buffer.") >=20 > -(defvar-local treesit-language-at-point-function nil > +(defvar-local treesit-language-at-point-function > + #'treesit-language-at-point-default > "A function that returns the language at point. > This is used by `treesit-language-at', which is used by various > functions to determine which parser to use at point. > @@ -183,10 +184,13 @@ treesit-language-at > the most relevant parser (hence language) by their embed level. > If `treesit-language-at-point-function' is non-nil, return > the return value of that function instead." > - (if treesit-language-at-point-function > - (funcall treesit-language-at-point-function position) > - (treesit-parser-language > - (car (treesit-parsers-at position))))) > + (funcall treesit-language-at-point-function position)) > + > +(defun treesit-language-at-point-default (position) > + "Default function for `treesit-language-at-point-function'. > +Return the deepest parser by embed level." > + (treesit-parser-language > + (car (treesit-parsers-at position)))) >=20 > ;;; Node API supplement >=20 LGTM. > BTW, I noticed now that indentation of the default value > in defvar-local is incorrect and differs from defvar and defcustom. > Here is the fix: >=20 > diff --git a/lisp/subr.el b/lisp/subr.el > index be847aab28a..164ed2a6f63 100644 > --- a/lisp/subr.el > +++ b/lisp/subr.el > @@ -197,7 +197,7 @@ defvar-local > Like `defvar' but additionally marks the variable as being = automatically > buffer-local wherever it is set. > \n(fn SYMBOL &optional VALUE DOCSTRING)" > - (declare (debug defvar) (doc-string 3) (indent 2)) > + (declare (debug defvar) (doc-string 3) (indent defun)) > ;; Can't use backquote here, it's too early in the bootstrap. > (let ((value (car-safe args)) > (docstring (car-safe (cdr-safe args)))) From debbugs-submit-bounces@debbugs.gnu.org Thu Apr 10 10:48:01 2025 Received: (at 77256) by debbugs.gnu.org; 10 Apr 2025 14:48:01 +0000 Received: from localhost ([127.0.0.1]:46249 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u2tCC-0001qG-Mi for submit@debbugs.gnu.org; Thu, 10 Apr 2025 10:48:00 -0400 Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:46605) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1u2tCB-0001px-3V for 77256@debbugs.gnu.org; Thu, 10 Apr 2025 10:47:59 -0400 Received: from pmg1.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 3E4E110004C; Thu, 10 Apr 2025 10:47:52 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1744296471; bh=E08YMKM3du1a/5P/zEFIoL0K7Bntl3Szaw0w0LLF6/I=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=gD8lgeodg/AWyGVfPOcXpi0JtBCIetusWqO1/rPb3TwjXPmDCc7NFq0dNK5Qf1NCM +fZWdshIv5omCxScrwuQQIdOx6wmioVGsIepWpE14IK1uWBcDEJHs9biZPCY+2cK4b hKcqUMKjoAMGvQzIA3Ppm5jTy/Qz/a/hdQT0XzRhIZl376RvsoUaVdtEakQeVpMWv+ F8D6S2y1o2APVrU/2mXv4n4aKtSDIXfzwHfjpIUbb3MnV6fYGuHmv4Yr3zLPO4am3T Upnwkk77NycP4w8kKo/l91VrzZCHYACltC53yvEFr3MWg6bJLAfckA6FnACbg5WK9r UDZzFUC06PSsw== Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 8C038100029; Thu, 10 Apr 2025 10:47:51 -0400 (EDT) Received: from pastel (unknown [104.247.242.5]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 5C2C912014D; Thu, 10 Apr 2025 10:47:51 -0400 (EDT) From: Stefan Monnier To: Yuan Fu Subject: Re: bug#77256: Treesit language-at-point In-Reply-To: <780FB757-2E74-4412-A2B5-CD31F362C2C5@gmail.com> Message-ID: References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> <874iyylcj5.fsf@mail.linkov.net> <8734ehanab.fsf@mail.linkov.net> <780FB757-2E74-4412-A2B5-CD31F362C2C5@gmail.com> Date: Thu, 10 Apr 2025 10:47:50 -0400 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-SPAM-INFO: Spam detection results: 0 ALL_TRUSTED -1 Passed through trusted hosts only via SMTP AWL -0.370 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DKIM_SIGNED 0.1 Message has a DKIM or DK signature, not necessarily valid DKIM_VALID -0.1 Message has at least one valid DKIM or DK signature DKIM_VALID_AU -0.1 Message has a valid DKIM or DK signature from author's domain DKIM_VALID_EF -0.1 Message has a valid DKIM or DK signature from envelope-from domain X-SPAM-LEVEL: X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: 77256 Cc: 77256@debbugs.gnu.org, Vincenzo Pupillo , Juri Linkov X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) >>> Is it? >>> I can't find any code out there that seems to care what is its default >>> value (all users set it but doesn't look at it, AFAICT). > The default value shouldn't matter. What=E2=80=99s the difference between > a major-mode-set non-nil value and a default non-nil value? No difference, in theory, indeed. The problem is when code cares about the nil -vs- non-nil value of the vari= able. >> This case looks problematic: >>=20 >> ;; LANG can be nil. We don't want to use the fallback >> ;; in `treesit-language-at', so here we call >> ;; `treesit-language-at-point-function' directly. >> (let* ((lang (and treesit-language-at-point-function >> (funcall treesit-language-at-point-fun= ction >> pos))) >>=20 >> But I believe this whole 'let*' can be replaced by just >> the 'treesit-parsers-at' call. I hope Yuan could confirm this. > > Why is this problematic? Because the `and` treats `treesit-language-at-point-function` as a boolean rather than as a function (and according to the comment, it does so to avoid the default behavior, so if we set the var to a non-nil value that provides the default behavior, it defeats the purpose of the test). But apparently, Juri's patch gets rid of this problem. =F0=9F=99=82 Stefan From debbugs-submit-bounces@debbugs.gnu.org Tue Apr 15 19:29:17 2025 Received: (at 77256) by debbugs.gnu.org; 15 Apr 2025 23:29:17 +0000 Received: from localhost ([127.0.0.1]:57865 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u4piO-0002yz-9a for submit@debbugs.gnu.org; Tue, 15 Apr 2025 19:29:17 -0400 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]:42202) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1u4piL-0002xh-7o for 77256@debbugs.gnu.org; Tue, 15 Apr 2025 19:29:14 -0400 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-227cf12df27so1445145ad.0 for <77256@debbugs.gnu.org>; Tue, 15 Apr 2025 16:29:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744759747; x=1745364547; darn=debbugs.gnu.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=J7J4RPPSfdwPJWh5sRKDC0Vo65wdtq0iM7ybBmsltuc=; b=kl36klLbe4Y8HXW7AD4QwQJ8mOQsieN3kwdpWVHrE1yREDuXQbc53RUP1oG7eIswTf FDSoaxNX99rv8C8ivVMUXjGwATbvMPxAMj+D5mnY+rNTS4twZeGE7XZYYpRd3b17MMs+ zztCozBmb1fEIGHbYkY1rbB7a+MrXXD43o4LOmi0fomlPXH2MYoCzishfU0A6Yk/eLI2 kgXIefMg7jYl4AEYxOUY3OEB3P+pkIaX86j8dRufS9hWZQjWKogF0GpmHHecpPCYn3s5 6Uc1hV17YDsNU1ohqyYENU81vTGfVx8udbAIcgdUs0ZNKV7TDGNSZ0y0b6xwusHCq85G h4UQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744759747; x=1745364547; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=J7J4RPPSfdwPJWh5sRKDC0Vo65wdtq0iM7ybBmsltuc=; b=HwoEqfjYwo7DynPPvflAv9wt8aiBPmdbi6sQI+hgw55XJyaRdLlIDwRI3eRQioDasH QKcLYml0vipLfnNfwQ1t7xliLg4oivnG6LXBBxFtloYTazlpHznhct+/eeNhMwQrCC0J gjG9VoBVjbmbiLeXlKtlzM47o3V9NJcD198PaCtylBqcZtvvweZbVeg9sZv0T6OAVRGt HoQlwrYIgQmHS77H0AqByMQpsRqOCiQF+pHmcVvnqM3UOYUbZXG7vaQX/sV+Z9LmVil7 B2nbAK8RZl3OIokv08SFTYZLUIK4HLkIIo62Sz9uF5TgmaavnmZjx/7GNPevMYg2yceK n6tA== X-Forwarded-Encrypted: i=1; AJvYcCUsOZNpjDbXW9whvfAekeYfjY+haXLFCwFE/ByIw/i8VfxoRv0SUQwA7C50xc08iRwzmHku9w==@debbugs.gnu.org X-Gm-Message-State: AOJu0YyEDKHOELmqtkZjgwKtHDd9GgXFYYs5C7qGYUz0BSl55g5mC6m/ fx2RBUZmxb/NdGXbZVgtcG0lHJ+GVv6P2UGj81uicYjY/KW0zGUe X-Gm-Gg: ASbGnctBLf/qVB3G98LU64QJVsHmfbD/tzesthJdqxuUgkPOhEoTptGi6rC8VBO34dZ REpI+Q/Ibi93sCNWJudnGYmKRsHO2Rv9j43o52uW61MqNYBAFk+soZKWXhcBV+vji6I1FhBquJh 58bdkOnqvLZLKYqwXSJINVVYsc6+T7uAcgJoaPEzAp+c/zrkPSBkbFK43zZX24yHOJc7sb8XF3N MQBe/6I5qJz6vZRMTl9BUEn4ZeF+QlcxDlvJ6HshnBgemUmrj2C9/dvF3Z75vMZoHwJx6fhia4W jEGIfSvjl6Vy5W56/zFQDO0BX6g9XBWDh+kfwnvF/QZSAO3lBty0I5LZSo1ckP/c X-Google-Smtp-Source: AGHT+IFRTwqg5MN+b/lD5t3bjUJOm+xtmcR0d0p3PD8sd0fRUGeJAO3RB5atRPkYurmmTHnzHZxTAg== X-Received: by 2002:a17:903:946:b0:215:6c5f:d142 with SMTP id d9443c01a7336-22c30d7220amr21754545ad.20.1744759745247; Tue, 15 Apr 2025 16:29:05 -0700 (PDT) Received: from smtpclient.apple ([2601:646:8f81:6120:e563:25ea:273a:4b69]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22c33ef1234sm1059245ad.14.2025.04.15.16.29.04 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 15 Apr 2025 16:29:04 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.400.131.1.6\)) Subject: Re: bug#77256: Treesit language-at-point From: Yuan Fu In-Reply-To: Date: Tue, 15 Apr 2025 16:28:53 -0700 Content-Transfer-Encoding: quoted-printable Message-Id: <282A5076-9321-46F9-AD4F-D0052D351C7F@gmail.com> References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> <874iyylcj5.fsf@mail.linkov.net> <8734ehanab.fsf@mail.linkov.net> <780FB757-2E74-4412-A2B5-CD31F362C2C5@gmail.com> To: Stefan Monnier X-Mailer: Apple Mail (2.3826.400.131.1.6) X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 77256 Cc: 77256@debbugs.gnu.org, Vincenzo Pupillo , Juri Linkov X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) > On Apr 10, 2025, at 7:47=E2=80=AFAM, Stefan Monnier = wrote: >=20 >>>> Is it? >>>> I can't find any code out there that seems to care what is its = default >>>> value (all users set it but doesn't look at it, AFAICT). >> The default value shouldn't matter. What=E2=80=99s the difference = between >> a major-mode-set non-nil value and a default non-nil value? >=20 > No difference, in theory, indeed. > The problem is when code cares about the nil -vs- non-nil value of the = variable. >=20 >>> This case looks problematic: >>>=20 >>> ;; LANG can be nil. We don't want to use the = fallback >>> ;; in `treesit-language-at', so here we call >>> ;; `treesit-language-at-point-function' directly. >>> (let* ((lang (and treesit-language-at-point-function >>> (funcall = treesit-language-at-point-function >>> pos))) >>>=20 >>> But I believe this whole 'let*' can be replaced by just >>> the 'treesit-parsers-at' call. I hope Yuan could confirm this. >>=20 >> Why is this problematic? >=20 > Because the `and` treats `treesit-language-at-point-function` as > a boolean rather than as a function (and according to the comment, it > does so to avoid the default behavior, so if we set the var to a = non-nil > value that provides the default behavior, it defeats the purpose of = the > test). Oops, that was bad code on my part=E2=80=94it wasn=E2=80=99t = intentional.=20 >=20 > But apparently, Juri's patch gets rid of this problem. =F0=9F=99=82 Yeah, feel free to apply the patch, Juri :) Yuan= From debbugs-submit-bounces@debbugs.gnu.org Wed Apr 16 13:13:41 2025 Received: (at 77256) by debbugs.gnu.org; 16 Apr 2025 17:13:41 +0000 Received: from localhost ([127.0.0.1]:41246 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u56KM-0007yA-50 for submit@debbugs.gnu.org; Wed, 16 Apr 2025 13:13:40 -0400 Received: from relay1-d.mail.gandi.net ([217.70.183.193]:34315) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1u56KG-0007xP-8x for 77256@debbugs.gnu.org; Wed, 16 Apr 2025 13:13:30 -0400 Received: by mail.gandi.net (Postfix) with ESMTPSA id 5799643A1E; Wed, 16 Apr 2025 17:13:18 +0000 (UTC) From: Juri Linkov To: Yuan Fu Subject: Re: bug#77256: Treesit language-at-point In-Reply-To: <282A5076-9321-46F9-AD4F-D0052D351C7F@gmail.com> Organization: LINKOV.NET References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> <874iyylcj5.fsf@mail.linkov.net> <8734ehanab.fsf@mail.linkov.net> <780FB757-2E74-4412-A2B5-CD31F362C2C5@gmail.com> <282A5076-9321-46F9-AD4F-D0052D351C7F@gmail.com> Date: Wed, 16 Apr 2025 20:01:47 +0300 Message-ID: <87zfggdoqs.fsf@mail.linkov.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvvdeileegucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufgjohhffffkfgggtgesthdtredttdertdenucfhrhhomheplfhurhhiucfnihhnkhhovhcuoehjuhhriheslhhinhhkohhvrdhnvghtqeenucggtffrrghtthgvrhhnpeffgeetfeevlefhleejfeeuheeiudeitdffhfdutdekfeffgffhveehteegueekheenucfkphepledurdduvdelrddutdehrdduudejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepledurdduvdelrddutdehrdduudejpdhhvghlohepmhgrihhlrdhgrghnughirdhnvghtpdhmrghilhhfrhhomhepjhhurhhisehlihhnkhhovhdrnhgvthdpnhgspghrtghpthhtohepgedprhgtphhtthhopehvrdhpuhhpihhllhhosehgmhgrihhlrdgtohhmpdhrtghpthhtohepjeejvdehieesuggvsggsuhhgshdrghhnuhdrohhrghdprhgtphhtthhopehmohhnnhhivghrsehirhhordhumhhonhhtrhgvrghlrdgtrgdprhgtphhtthhopegtrghsohhurhhisehgmhgrihhlrdgtohhm X-GND-Sasl: juri@linkov.net X-Spam-Score: -0.7 (/) X-Debbugs-Envelope-To: 77256 Cc: 77256@debbugs.gnu.org, Vincenzo Pupillo , Stefan Monnier X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.7 (-) > Yeah, feel free to apply the patch, Juri :) Now the patch is pushed. From debbugs-submit-bounces@debbugs.gnu.org Wed Apr 16 13:16:26 2025 Received: (at 77256) by debbugs.gnu.org; 16 Apr 2025 17:16:27 +0000 Received: from localhost ([127.0.0.1]:41257 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u56N6-0008GO-35 for submit@debbugs.gnu.org; Wed, 16 Apr 2025 13:16:25 -0400 Received: from relay1-d.mail.gandi.net ([217.70.183.193]:46643) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1u56KH-0007xX-9J for 77256@debbugs.gnu.org; Wed, 16 Apr 2025 13:13:32 -0400 Received: by mail.gandi.net (Postfix) with ESMTPSA id 808B343A40; Wed, 16 Apr 2025 17:13:22 +0000 (UTC) From: Juri Linkov To: Yuan Fu Subject: Re: bug#77256: Treesit language-at-point In-Reply-To: <87lds96z2c.fsf@mail.linkov.net> Organization: LINKOV.NET References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> <871pucdti6.fsf@mail.linkov.net> <8B15AB89-2A73-4260-AFD5-400B11342625@gmail.com> <87y0wgx8l8.fsf@mail.linkov.net> <87r026pib9.fsf@mail.linkov.net> <76B8FD44-EFDD-4A75-AAF6-3EA9A91A60A4@gmail.com> <87semlpzah.fsf@mail.linkov.net> <87lds96z2c.fsf@mail.linkov.net> Date: Wed, 16 Apr 2025 20:12:21 +0300 Message-ID: <87sem8do96.fsf@mail.linkov.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvvdeileegucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufgjohhffffkfgggtgesthdtredttdertdenucfhrhhomheplfhurhhiucfnihhnkhhovhcuoehjuhhriheslhhinhhkohhvrdhnvghtqeenucggtffrrghtthgvrhhnpeffgeetfeevlefhleejfeeuheeiudeitdffhfdutdekfeffgffhveehteegueekheenucfkphepledurdduvdelrddutdehrdduudejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepledurdduvdelrddutdehrdduudejpdhhvghlohepmhgrihhlrdhgrghnughirdhnvghtpdhmrghilhhfrhhomhepjhhurhhisehlihhnkhhovhdrnhgvthdpnhgspghrtghpthhtohepgedprhgtphhtthhopehmohhnnhhivghrsehirhhordhumhhonhhtrhgvrghlrdgtrgdprhgtphhtthhopehvrdhpuhhpihhllhhosehgmhgrihhlrdgtohhmpdhrtghpthhtohepjeejvdehieesuggvsggsuhhgshdrghhnuhdrohhrghdprhgtphhtthhopegtrghsohhurhhisehgmhgrihhlrdgtohhm X-GND-Sasl: juri@linkov.net X-Spam-Score: -0.7 (/) X-Debbugs-Envelope-To: 77256 Cc: 77256@debbugs.gnu.org, Vincenzo Pupillo , Stefan Monnier X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.7 (-) >> This seems a bit convoluted to me. What do you think about adding >> some hook or generic function to outline-minor-mode? > > Is this better? > > diff --git a/lisp/outline.el b/lisp/outline.el > -(defun outline--fix-buttons-after-change (beg end _len) > +(defvar outline-after-change nil > + "List of functions to call after each text change in outline-mode.") > + > +(defun outline--fix-buttons-after-change (beg end len) > + (run-hook-with-args 'outline-after-change beg end len) > ;; Handle whole lines > (save-excursion (goto-char beg) (setq beg (pos-bol))) > (save-excursion (goto-char end) (setq end (pos-eol))) This is pushed as well. From debbugs-submit-bounces@debbugs.gnu.org Thu Apr 17 01:21:57 2025 Received: (at 77256) by debbugs.gnu.org; 17 Apr 2025 05:21:57 +0000 Received: from localhost ([127.0.0.1]:45391 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u5HhF-0001Es-3X for submit@debbugs.gnu.org; Thu, 17 Apr 2025 01:21:57 -0400 Received: from mail-pf1-x42f.google.com ([2607:f8b0:4864:20::42f]:48324) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1u5HhB-0001ED-1x for 77256@debbugs.gnu.org; Thu, 17 Apr 2025 01:21:54 -0400 Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-736aa9d0f2aso424347b3a.0 for <77256@debbugs.gnu.org>; Wed, 16 Apr 2025 22:21:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744867307; x=1745472107; darn=debbugs.gnu.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=B57mfFJYzpJkzDF6YKGU7Zmvu4O1hpH7bEt478Wkq6o=; b=ZoXnDvbf+lx5gqelwGI7ci8RQTomesEVOgRE0bAUPSKaBUZMJUjX696mcuKeWxdBpw OyF3K7P1q6B62vALAJLf4JX1EC2u6lIewIGL772JbWLRstvIva5AanEuFR8eRtZWDmGY cgHlih7t90vJM2/RmjmEA+L1pPUvWgF4zNZxsMtRtjlnpugsFPfJ4xj2uOXqsDIQRG7H S8ytjV5NIHjLE/IkSTNlcUGS13J4AtEUkcPKWmT/m/eUqca3Qs0Y0Q7/fukeSgDJa41a TUiC+cT+bT6SdbiHYHEqy4MZDKttaqK3zQ8gWg2Ae7uJMC3yP7F6SKu4dHV0qEMI/NbN RCag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744867307; x=1745472107; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=B57mfFJYzpJkzDF6YKGU7Zmvu4O1hpH7bEt478Wkq6o=; b=d+Qv+uGAFdnNIF0fPDSLfSTkN2c+Afz8YqwADcM3J+aTKk3S7stAKdhGiV4jichwHA ZPXiMGvIA7oYLtCi8kyQUImkhoG16II/ZDznhbQIR6urd0Y2saAIswsbQZXQAiJk6UBI breGiaHnOBKNRzJsfdjkMKvH8YY5Q/Z/jdf7mk+fq4qgdM3J04KuzyV2NRmMSfJQp5qn MK+Hee7HbejMhxwaICH8hYytaxm3uJK10Lw9X+xgXeHrB1rzWAnQGLbuB44/CUUoLkbU DjMJBYqCUrzcbeXQ37WY+s/xlFNqmyg4+t/7UR5mKQ9ZulFl52KYHHmkCFLdcqZ3wkFD o35A== X-Gm-Message-State: AOJu0Yy1oRflHdDF9y5QBQyxhgAxzus0dmngPa+qY8babXKA8uEHEyms ul+BnhxQWJ1/k3MTyNa1E8jIGzF9Cl1Z7pouhO1IJB/ROmuWVDBb X-Gm-Gg: ASbGncvL80GLAdQ8ScSihmZQMiR868hjzcgeGyM4Jut22zTKqChqEjDJ+su2L6CvlNn 7kyIPTytr1bo/NO82UXe/oug+9uyaCWGJtQwrelCoFFQ4s1lHp454YC55wlqqspJGwwsxr5Ctao qC0bgh144jY3VcXkqGbIMCddOHIrkr/ze4gL/1rU0ttzfebZL85tfw0Uqji7nkyDcl6QiHkF0xS oTk+Xb+00FfESttXnYvAOOlsmOcpjX7GkDq5yUiawxbnVnO1IqmRGXz+7lwkKI4VKAQSTDRtHi9 6HB6tmWJknoURngEIm6h4VDKrFUwRpUIKcT/NLqK1tSJiASJKmyO3kVuWxdg7hxy X-Google-Smtp-Source: AGHT+IFEBuPteRV3D9s0JgMd7OBgKEEOXohb6jrm6e/wktnIdnEbwQ6iB6O97ogIDtaKLz3Pnhv9EA== X-Received: by 2002:a05:6a00:1da6:b0:736:eb7e:df39 with SMTP id d2e1a72fcca58-73c267fe3f8mr7289626b3a.24.1744867306764; Wed, 16 Apr 2025 22:21:46 -0700 (PDT) Received: from smtpclient.apple ([2601:646:8f81:6120:9c26:1394:9c81:74c2]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bd21df6b6sm11421696b3a.74.2025.04.16.22.21.45 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 16 Apr 2025 22:21:46 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.400.131.1.6\)) Subject: Re: bug#77256: Treesit language-at-point From: Yuan Fu In-Reply-To: <87sem8do96.fsf@mail.linkov.net> Date: Wed, 16 Apr 2025 22:21:34 -0700 Content-Transfer-Encoding: quoted-printable Message-Id: <62B592A9-4D13-49AB-B181-743B09CC9D68@gmail.com> References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> <871pucdti6.fsf@mail.linkov.net> <8B15AB89-2A73-4260-AFD5-400B11342625@gmail.com> <87y0wgx8l8.fsf@mail.linkov.net> <87r026pib9.fsf@mail.linkov.net> <76B8FD44-EFDD-4A75-AAF6-3EA9A91A60A4@gmail.com> <87semlpzah.fsf@mail.linkov.net> <87lds96z2c.fsf@mail.linkov.net> <87sem8do96.fsf@mail.linkov.net> To: Juri Linkov X-Mailer: Apple Mail (2.3826.400.131.1.6) X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 77256 Cc: 77256@debbugs.gnu.org, Vincenzo Pupillo , Stefan Monnier X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) > On Apr 16, 2025, at 10:12=E2=80=AFAM, Juri Linkov = wrote: >=20 >>> This seems a bit convoluted to me. What do you think about adding >>> some hook or generic function to outline-minor-mode? >>=20 >> Is this better? >>=20 >> diff --git a/lisp/outline.el b/lisp/outline.el >> -(defun outline--fix-buttons-after-change (beg end _len) >> +(defvar outline-after-change nil >> + "List of functions to call after each text change in = outline-mode.") >> + >> +(defun outline--fix-buttons-after-change (beg end len) >> + (run-hook-with-args 'outline-after-change beg end len) >> ;; Handle whole lines >> (save-excursion (goto-char beg) (setq beg (pos-bol))) >> (save-excursion (goto-char end) (setq end (pos-eol))) >=20 > This is pushed as well. FTR I=E2=80=99m not super satisfied with how we are adding after-change = hook to update ranges. But I couldn=E2=80=99t came up with anything in = the past few days so I didn=E2=80=99t suggest any alternatives :-)=20 Ultimately I think you are right that we want guarantee to Lisp programs = that parser ranges are always up-to-date, so they don=E2=80=99t have to = call treesit-update-ranges themselves every where. But the exact = implementation to achieve that is still not super clear in my head. = After thinking about this more and rewriting this reply three times, I = think the best way might be to call treesit--pre-redisplay in = after-change-functions. Treesit--pre-redisplay should make sure all the = parser ranges are updated. The only thing I don=E2=80=99t like about = this is that there=E2=80=99re many cases where a series of buffer edits = are made, and we don=E2=80=99t want to update ranges after each single = buffer edit. Post-command-hook should prevent that, but = post-command-hook feels really user-facing and high-level to me. I=E2=80=99d like to know what do you and Stefan think about this. Yuan= From debbugs-submit-bounces@debbugs.gnu.org Thu Apr 17 11:01:53 2025 Received: (at 77256) by debbugs.gnu.org; 17 Apr 2025 15:01:53 +0000 Received: from localhost ([127.0.0.1]:48083 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u5QkT-0000sY-BT for submit@debbugs.gnu.org; Thu, 17 Apr 2025 11:01:53 -0400 Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:27353) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1u5QkQ-0000rO-50 for 77256@debbugs.gnu.org; Thu, 17 Apr 2025 11:01:51 -0400 Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 6917D441EAA; Thu, 17 Apr 2025 11:01:43 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1744902102; bh=YyFh8Km/3w/4o08qiJwhQ96DNzsNjc35CMZdGwc5748=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=aHwXo4C5JQWHc59Hd8wpiRNgzkZdqBUXzhShsq88lHKY+i4hejOK4y5GZZKkkNNzR m2UC+mHusMHtv3J2rt5hnfO1cRq8brgN/rynietZTXmyJTO5+V5+ZhzZmFTahUG7Oa rBJC7rTxfRBA/5VvBK2pnckCfizqKnoxONuz9alCoCaZMCf1n1iq8G/gJCzC94UGi2 Z5L+za63WgLyZpkJXo2XWC7QQ/dzLopQvlPz/I5i7NkUBNbYJBu981EOljncZ67BYM bfpQ6E2HmUJpyhhBNdIs8Bb+U7N/lWndtfickvjijLuL2HnMCzGwp7ncElyY+t3WB2 znXzqcs2dQiEg== Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 699EF441E6E; Thu, 17 Apr 2025 11:01:42 -0400 (EDT) Received: from pastel (104-195-239-180.cpe.teksavvy.com [104.195.239.180]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 378E1120223; Thu, 17 Apr 2025 11:01:42 -0400 (EDT) From: Stefan Monnier To: Yuan Fu Subject: Re: bug#77256: Treesit language-at-point In-Reply-To: <62B592A9-4D13-49AB-B181-743B09CC9D68@gmail.com> Message-ID: References: <87a5990x6t.fsf@mail.linkov.net> <9271D97C-0820-4C70-9A58-88988F2E6BF9@gmail.com> <87plhxw7fh.fsf@mail.linkov.net> <73DD04E6-D914-4A14-8DA9-876519BBEF4D@gmail.com> <871pucdti6.fsf@mail.linkov.net> <8B15AB89-2A73-4260-AFD5-400B11342625@gmail.com> <87y0wgx8l8.fsf@mail.linkov.net> <87r026pib9.fsf@mail.linkov.net> <76B8FD44-EFDD-4A75-AAF6-3EA9A91A60A4@gmail.com> <87semlpzah.fsf@mail.linkov.net> <87lds96z2c.fsf@mail.linkov.net> <87sem8do96.fsf@mail.linkov.net> <62B592A9-4D13-49AB-B181-743B09CC9D68@gmail.com> Date: Thu, 17 Apr 2025 11:01:41 -0400 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-SPAM-INFO: Spam detection results: 0 ALL_TRUSTED -1 Passed through trusted hosts only via SMTP AWL -0.024 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DKIM_SIGNED 0.1 Message has a DKIM or DK signature, not necessarily valid DKIM_VALID -0.1 Message has at least one valid DKIM or DK signature DKIM_VALID_AU -0.1 Message has a valid DKIM or DK signature from author's domain DKIM_VALID_EF -0.1 Message has a valid DKIM or DK signature from envelope-from domain X-SPAM-LEVEL: X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: 77256 Cc: 77256@debbugs.gnu.org, Vincenzo Pupillo , Juri Linkov X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) > Ultimately I think you are right that we want guarantee to Lisp programs > that parser ranges are always up-to-date, so they don=E2=80=99t have to c= all > treesit-update-ranges themselves every where. But the exact implementation > to achieve that is still not super clear in my head. After thinking about > this more and rewriting this reply three times, I think the best way might > be to call treesit--pre-redisplay in > after-change-functions. Treesit--pre-redisplay should make sure all the > parser ranges are updated. The only thing I don=E2=80=99t like about this= is that > there=E2=80=99re many cases where a series of buffer edits are made, and = we don=E2=80=99t > want to update ranges after each single buffer edit. Post-command-hook > should prevent that, but post-command-hook feels really user-facing and > high-level to me. I don't know how ranges are managed/computed, but the usual way to handle this kind of problem is to separate the "mark as out of date" from the actual update which you do only lazily when a request is made. Stefan From unknown Fri Aug 15 15:34:37 2025 Received: (at fakecontrol) by fakecontrolmessage; To: internal_control@debbugs.gnu.org From: Debbugs Internal Request Subject: Internal Control Message-Id: bug archived. Date: Fri, 16 May 2025 11:24:10 +0000 User-Agent: Fakemail v42.6.9 # This is a fake control message. # # The action: # bug archived. thanks # This fakemail brought to you by your local debbugs # administrator