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: Tree-sitter indentation for js-mode & cc-mode Date: Thu, 27 Oct 2022 08:21:43 -0700 Message-ID: References: <9AF8BFDC-C9A2-4AE5-A8D2-E6AA05DA3C91@gmail.com> <87k04lljh6.fsf@thornhill.no> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.120.41.1.1\)) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="25024"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel , Stefan Monnier To: Theodor Thornhill Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Thu Oct 27 17:22:03 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 1oo4hn-0006Gw-EC for ged-emacs-devel@m.gmane-mx.org; Thu, 27 Oct 2022 17:22:03 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oo4hc-0000aK-Li; Thu, 27 Oct 2022 11:21:52 -0400 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 1oo4hZ-0008As-TS for emacs-devel@gnu.org; Thu, 27 Oct 2022 11:21:49 -0400 Original-Received: from mail-pf1-x42a.google.com ([2607:f8b0:4864:20::42a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oo4hY-00059D-3I for emacs-devel@gnu.org; Thu, 27 Oct 2022 11:21:49 -0400 Original-Received: by mail-pf1-x42a.google.com with SMTP id f140so1885974pfa.1 for ; Thu, 27 Oct 2022 08:21:47 -0700 (PDT) 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=BJ4G7JiSerrJTypIXVwq4KZtbGhjHoV+6TcTbVApHr0=; b=HDpZWIuDu7gOY6V7PvCBuX4GNdkEGAYGhOGyfr5GurF5HfOZRxYwtIeir4eA5yqcUm BomZsRfJqw5rHvOFVg/t/Fhl1i9Tw6fvPfHOEwwdfCP9L5vwVuWDM+dYXlWLF2WSE09Q UoSXPo0o8JCqBnnX1d51qdA6LovOK/c4yXHYoi9oTy8QW84sQNeZcPUsybzgGFtTQ7fX 5vgS3gdf5K8eSrkAtcQu90CVrCiyy7ANZQxd3NI2WVg7KyTbnJnuW6QKeYPLJF9pL1tN zPhGH9vvG/jWOXo5UvCippEI7IpC+jCNOYzxC/6FPF1vp7phCHHD0QoHl4nwCoMG8Es9 X9mQ== 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=BJ4G7JiSerrJTypIXVwq4KZtbGhjHoV+6TcTbVApHr0=; b=TmivCPH6NvyydYPs0G2ta/wLfhOWKSNBXY2fV9r0MjqJ3Z6TTMX6Ida78zgijczO0e MlvMiJ33tn3BGKdw2Zmd6v3Z1oZCQzxLy8Ye0s8kc4b8G+DdSXFapXZWMS2AU+5hp4hL w7tANoNET+NKNRE/AB8sWDexJ5kd7a9lfHg1KF34P7hqr3BAqwJwtI3hKDLumvhYMPvN w/2bMRBHjcJf7k7X2NV6fn11BAzVZRXeLZEF2HmysH0rO79Ex9BWY+01wI+6sruNgOVo so4ppRaxt1eP0rLqBm8zwXr53aWAL8md9WJcuV448/yWCSJJqdvGgsyW0e0YlLDKqTD9 zpiA== X-Gm-Message-State: ACrzQf3wyczBMzzdPpXgGAx/dOJl50vu1xxScjIb6MGAWXGSEpDsFZna 7+LKzxR2vkvBg/nKMWt3pUM= X-Google-Smtp-Source: AMsMyM6Ji0R+bPcEW3RniMrf+HMCE7T/4sUvrpb0ytfgEyNr9gQGbHiBEiqJevAXI9uyQgP7y1mvDA== X-Received: by 2002:aa7:9e1c:0:b0:56c:78fa:2a2 with SMTP id y28-20020aa79e1c000000b0056c78fa02a2mr7055457pfq.65.1666884106418; Thu, 27 Oct 2022 08:21:46 -0700 (PDT) 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 65-20020a620544000000b0056203db46ffsm1337731pff.172.2022.10.27.08.21.45 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Oct 2022 08:21:46 -0700 (PDT) In-Reply-To: <87k04lljh6.fsf@thornhill.no> X-Mailer: Apple Mail (2.3696.120.41.1.1) Received-SPF: pass client-ip=2607:f8b0:4864:20::42a; envelope-from=casouri@gmail.com; helo=mail-pf1-x42a.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: , Original-Sender: "Emacs-devel" Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:298623 Archived-At: > On Oct 27, 2022, at 2:11 AM, Theodor Thornhill = wrote: >=20 >=20 > Hi Yuan! >=20 >> I did some work to allow tree-sitter indentation engine to plug in to >> c-offset-alist. Currently in a tree-sitter indent rule, we have >>=20 >> (MATCHER ANCHOR OFFSET) >>=20 >> OFFSET is normally an integer, but now it can also be a syntax symbol >> recognized by cc-mode=E2=80=99s indentation engine. In that case, = tree-sitter >> indent calculates the indent using c-calc-offset, passing the syntax >> symbol and anchor position to it, and c-calc-offset will give us the >> integer offset based on c-offset-alist. >>=20 >=20 > This is cool, but do we really want/need this? I mean, now we're = really > binding these implementations together and allowing all the legacy of = CC > mode to blend in. We also need knowledge of how CC mode names their > syntactic definitions. IMO one of the big selling points of tree = sitter > is that you can look at other editors implementation and get inspired > immediately. Now we need deep knowledge of cc mode, don't we? Also, > why would we want cc mode to calculate this for us? I see what you're > trying to do, but _I_ think this is a step in the wrong direction. You have a point. I tried to blend in cc-mode because that=E2=80=99ll = allow us support =E2=80=9Cstyles=E2=80=9D and existing user = customization. (Also I started out thinking it will be easier to write = indentation rules this way which turns out to be not true.) Perhaps = it=E2=80=99s better to come up with a new system for customizing = indentation style. I=E2=80=99ll revert this change. >=20 >=20 >> I=E2=80=99ve written indent rules for js-mode, they are in >> js-treesit-cc-indent-rules. Overall it works pretty well. Theo, could >> you give it a try? =46rom my testing it is already an improvement = from >> the original rules. I didn=E2=80=99t finish the JSX part and just = copied your >> original rules for JSX. In the future I can probably port that to >> cc-style too. >>=20 >=20 > I don't think this is better, for example: >=20 > The old variant renders this snippet correctly: > ``` > const fooClient =3D new Foo({ > bucket: process.env.foo, > region: process.env.foo, > }); > ``` >=20 > But the new one renders it like this: > ``` > const fooClient =3D new Foo({ > bucket: process.env.foo, > region: process.env.foo, > }); > ``` >=20 > I know this is a matter of tweaking, but it immediately makes me > question the reasoning to blend them. It=E2=80=99s largely my slip-up rather than inherit defect of the = system, but I agree with your opinion above. >=20 > In addition I profiled indenting a 50k lines js file with messed up > indentation, and received some surprising results. The pure cc mode > variant is slow, but MUCH faster than tree-sitter. It seems we are > looking up way to much the root of the tree, but you know the = internals > here better than me. Is this something we can optimize away? See the > attached report at the bottom. This is very strange, I need to look into it. >=20 >> I also added imenu support for js-mode and ts-mode, and navigation = for >> python-mode. >>=20 >=20 > Very cool - it seems to work pretty nicely! Nice. >=20 > Anyways - can we please revisit the idea that we init and/or use cc = mode > in tandem with tree-sitter? I know we want "feature parity", but I > think we lose too much of the simplicity gained by adding in the old > complexity. My prediction for the future is that this will result in > numerous bug reports where it's hard to know whether this is a fix for > cc mode or treesitter. And in the end people _will_ just skip these > modes altogether and put simpler ones in (m)elpa that only uses = treesit, > to avoid this. The cc mode won't go away at all, for the people that > considers that superior. We can still use the treesit-settings as a > centralized variable to get most, if not all of the "auto-enabling" > benefits by just lifting it up: >=20 > ``` > ;;.... >=20 > (cond > ;; Tree-sitter. > ((treesit-ready-p 'js-mode 'javascript) > ;; init all treesitter relevant stuff - can add in _some_ other > ;; non-cc-mode settinigs, such as comment-start, etc above this. > ;; We don't need the cache, detection of js-jsx or any of the > ;; before-change-functions >=20 > (treesit-major-mode-setup)) > ;; Elisp. > (t > ;; enable in normal cc mode stuff > ))) >=20 > ``` >=20 > This way other hypothetical tree-sitter-v2 in the future is just a > simple cond, and no need to worry. >=20 > If I'm missing something important here, please let me know, but I > _really_ don't understand the reason for merging these = implementations. I don=E2=80=99t have an educated opinion on this. If no one has = objections I=E2=80=99ll follow your professional advice ;-) > Anyway, thanks for your continued hard work! Many thanks to you, too! >=20 > Theo >=20 >