From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuan Fu Newsgroups: gmane.emacs.devel Subject: Re: Treesitter query question, matching only substring of a node Date: Sun, 4 Dec 2022 20:05:45 -0800 Message-ID: References: <87sfhvdn6k.fsf@dfreeman.email> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.120.41.1.1\)) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="32925"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Danny Freeman Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon Dec 05 05:06:28 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 1p22kM-0008Mr-Mh for ged-emacs-devel@m.gmane-mx.org; Mon, 05 Dec 2022 05:06:26 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p22jr-0005qj-L6; Sun, 04 Dec 2022 23:05:55 -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 1p22jq-0005qa-1n for emacs-devel@gnu.org; Sun, 04 Dec 2022 23:05:54 -0500 Original-Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p22jo-0007fb-97 for emacs-devel@gnu.org; Sun, 04 Dec 2022 23:05:53 -0500 Original-Received: by mail-pl1-x62f.google.com with SMTP id m4so2751728pls.4 for ; Sun, 04 Dec 2022 20:05:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=T+kaR7sDVJBBxqU+xZNwK15JD1h7gTOEz0C14Qe93pg=; b=nunTm2+bfu93SAm7JOAgF4nXqwiUYL6mTAgaOfOXpHIRXrbmp8ZbjqzaVnmWrOJv+3 8IierCivqA1kz0wXoqiYdn0oga70RbpRf8rGuihK2+hQhXyJKzEBrREFs1JCjzcnMCB6 w5dg5QeLj+T+d4wFsWUWH4YlAv7By+TWXxZDLkL31ITQGPu9rklLMZQ0skd5Mp/WyTHF Snj7qRYrnUK7DNX9oT5gFsHiT4JHt9Qg+m0+KKJSTi87ajIx0qr7cy8DzBM06HfXC+H6 nJSCPAdG+cnrB8/pWKYf4gxxtyLiPL0DmVCJjWhDG7DA61CIZDYRi9wKRVBkVxR9Krec Mq9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=T+kaR7sDVJBBxqU+xZNwK15JD1h7gTOEz0C14Qe93pg=; b=uSiIqB64I9CPAK7L/aR/ObSinTjq4LhgQ72/agvCIZdZVLBc6rvmBVaWMw5tol8QwC KIBTms9/hoFcSBThxuK63ikUihDtfboLOIMm0VAeIaPy1KwN0DOhn6UocWJPnoQsXIso WC0BfW8FJ2TOOahV+8Y+IMZJEiGBcETatqAYmTSBe4LYAg5KLu51XWcVqSWgPt18C7aa acFfag0tWKWjyASex8EGor7h31tES/y5w5pfdJTZKNDwiU38ewkq22kc3XidgzykUSaW MORZFdv2FFQfAAHk0ibrX2XfPenQl1X3AgGUe467PZC642LQ3nb/OhXElDS5fZIjWWHA wNAQ== X-Gm-Message-State: ANoB5plYzMrpCsOgbJd3xp1HtTNuZhgwhCi9ufa5KS6Unogdy92PYR5l ipk56IvJoo98J7mn+ajRsLNO1hy91yk= X-Google-Smtp-Source: AA0mqf6NeyOJ7IFZM9dG97okPmTGM69NZ58M2wR7Qk1/az0oDnemNeIFG3X+4U6R3aKiqHioegcSGw== X-Received: by 2002:a17:90a:5897:b0:219:c38c:1423 with SMTP id j23-20020a17090a589700b00219c38c1423mr5698011pji.113.1670213150543; Sun, 04 Dec 2022 20:05:50 -0800 (PST) Original-Received: from smtpclient.apple (cpe-172-117-161-177.socal.res.rr.com. [172.117.161.177]) by smtp.gmail.com with ESMTPSA id u184-20020a6279c1000000b00571cdbd0771sm8754193pfc.102.2022.12.04.20.05.49 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Dec 2022 20:05:50 -0800 (PST) In-Reply-To: <87sfhvdn6k.fsf@dfreeman.email> X-Mailer: Apple Mail (2.3696.120.41.1.1) Received-SPF: pass client-ip=2607:f8b0:4864:20::62f; envelope-from=casouri@gmail.com; helo=mail-pl1-x62f.google.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, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, 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:300919 Archived-At: > On Dec 4, 2022, at 10:27 AM, Danny Freeman = wrote: >=20 > Hello, >=20 > 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. >=20 > It has definitions for nodes that match full namespaced keywords and > symbols, such that >=20 > :plain-keyword matches (kwd_lit) node > :namespace/keyword matches (kwd_lit) node >=20 > and similarly for symbols >=20 > 'plain-symbol matches (sym_lit) node > 'namespaced/symbol matches (sym_lit) node >=20 > In clojure, the part before the / character is the NAMESPACE, and the > part after the / character is the NAME. >=20 > The current clojure-mode highlights the namespaced part with a = different > face than the name part. >=20 > 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` >=20 > ``` > (treesit-font-lock-rules > ... > :feature 'keyword > :language 'clojure > '((kwd_lit) @clojure-keyword-face) > ... > ) > ``` > This works fine. >=20 > What I'm trying to do now is capture a substring of that node, the > namespaced part. >=20 > I can use the `:match` predicate to identify the namespace part, but = the > matched group doesn't get captured by anything >=20 > ``` > (defvar kw-query > `((((kwd_lit) @kw) > (:match "^:.*/" @kw) ;; Can I capture this?? > ))) >=20 > (treesit-query-string ":namespaced/keyword" kw-query 'clojure) > ``` >=20 > 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? >=20 > 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=20 I suggest capturing the whole node and using a function to fontify the = symbol. With a function you can do anything you want, including applying = different faces to substrings. To do that, simply define a function and = use the function name as the capture name. You can have a look at c-ts-mode--fontify-variable for a simple example. Yuan=