From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Danny Freeman Newsgroups: gmane.emacs.devel Subject: Treesitter query question, matching only substring of a node Date: Sun, 04 Dec 2022 13:27:07 -0500 Message-ID: <87sfhvdn6k.fsf@dfreeman.email> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="12380"; mail-complaints-to="usenet@ciao.gmane.io" To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sun Dec 04 19:44:49 2022 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p1tyr-00031U-28 for ged-emacs-devel@m.gmane-mx.org; Sun, 04 Dec 2022 19:44:49 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p1ty8-0006br-O7; Sun, 04 Dec 2022 13:44:04 -0500 Original-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 1p1ty4-0006bY-C9 for emacs-devel@gnu.org; Sun, 04 Dec 2022 13:44:01 -0500 Original-Received: from out-198.mta0.migadu.com ([2001:41d0:1004:224b::c6]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p1txz-00035K-Ol for emacs-devel@gnu.org; Sun, 04 Dec 2022 13:43:58 -0500 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dfreeman.email; s=key1; t=1670179431; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=mbx+VwHc4JOE9d/ZTsObOCtmkqmqTU3Cm7zHnIJnV5w=; b=Oyv6mb+B6r4gcvhWY+yof22RjSo1oMtNlFT3wBVPXHHgllpN0OSMJ7g9d9VxFTNE+L+DmP Nnwd7/TadI9onbWpNBFO79vcqwiO9ztAD7HAK6lb7V+xocKedVKVHGsHi3IS+fW9Pgetf0 yjPNSvOGRsHsLRIAvJOLvw1K9ROhzb0= X-Migadu-Flow: FLOW_OUT Received-SPF: pass client-ip=2001:41d0:1004:224b::c6; envelope-from=danny@dfreeman.email; helo=out-198.mta0.migadu.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:300909 Archived-At: Hello, I am currently seeing what it will look like to add treesitter support for clojure-mode. I am using this grammer: https://github.com/sogaiu/tree-sitter-clojure and it is working great so far. It has definitions for nodes that match full namespaced keywords and symbols, such that :plain-keyword matches (kwd_lit) node :namespace/keyword matches (kwd_lit) node and similarly for symbols 'plain-symbol matches (sym_lit) node 'namespaced/symbol matches (sym_lit) node In clojure, the part before the / character is the NAMESPACE, and the part after the / character is the NAME. The current clojure-mode highlights the namespaced part with a different face than the name part. I am trying to use this existing grammer to apply different faces to the namespaced and named part. I can write a query that matches the whole keyword easily, using `treesit-font-lock-rules` ``` (treesit-font-lock-rules ... :feature 'keyword :language 'clojure '((kwd_lit) @clojure-keyword-face) ... ) ``` This works fine. What I'm trying to do now is capture a substring of that node, the namespaced part. I can use the `:match` predicate to identify the namespace part, but the matched group doesn't get captured by anything ``` (defvar kw-query `((((kwd_lit) @kw) (:match "^:.*/" @kw) ;; Can I capture this?? ))) (treesit-query-string ":namespaced/keyword" kw-query 'clojure) ``` Is something like this possible with the treesitter query engine in Emacs? Or do I need to handle this at the grammer level? Perhaps with a field? There is an issue that was closed in the grammer's repository asking about this, but their advise was to do some substring matching in the editor, so here I am https://github.com/sogaiu/tree-sitter-clojure/issues/28 -- Danny Freeman