From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Gabriel Santos via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#74461: [PATCH] Add go-work-ts-mode Date: Thu, 21 Nov 2024 21:13:42 -0300 Message-ID: <871pz4dt1l.fsf@disroot.org> References: <877c8wvp34.fsf@disroot.org> Reply-To: Gabriel Santos Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="12282"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 74461@debbugs.gnu.org To: eliz@gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Nov 22 04:57:26 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1tEKnO-000347-I1 for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 22 Nov 2024 04:57:26 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tEKn5-0007q1-Gl; Thu, 21 Nov 2024 22:57:07 -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 1tEKn1-0007pL-Hd for bug-gnu-emacs@gnu.org; Thu, 21 Nov 2024 22:57:03 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tEKn1-0001fn-9o for bug-gnu-emacs@gnu.org; Thu, 21 Nov 2024 22:57:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:From:To:In-Reply-To:References:Subject; bh=nYYJCCIp5dchP+Vr8l9MStPZ+0dxWkyixCXmekNQAcA=; b=OH/UgoQ/nmBvMnwOHX23pjnvzTqvmn0dHCMR4/+B0Crs9qYNrwBBxPqIdZSF4XqDmwqAG9t5+F33EeToOqY9SAqe6ps+zW7kYrp0JhZqKI4b2utinlIpAdnXBlvlk2Z4La1D/m9G0Fw1PAjVpZ7VA60fpOV5evaJSGwU7jqavaTCEC9tb8g1EPnPUNtaD9Rp01h7zaLHaTxIcKvVP3P9vNQnDQnhygzw4UZ85/nQLDty20mXHfDnFuFn6FWjWnVXuiG3vX56urM9ANeRaTozZsG0C4opzaUIewF9frA/mxRXUIGMWeq/PEBecErnu7w8bC2gVxnOgI00s/8dShPIQg==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tEKn1-0000vI-5L for bug-gnu-emacs@gnu.org; Thu, 21 Nov 2024 22:57:03 -0500 X-Loop: help-debbugs@gnu.org In-Reply-To: <877c8wvp34.fsf@disroot.org> Resent-From: Gabriel Santos Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 22 Nov 2024 03:57:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 74461 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 74461-submit@debbugs.gnu.org id=B74461.17322478003504 (code B ref 74461); Fri, 22 Nov 2024 03:57:03 +0000 Original-Received: (at 74461) by debbugs.gnu.org; 22 Nov 2024 03:56:40 +0000 Original-Received: from localhost ([127.0.0.1]:52584 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tEKmd-0000uM-9Q for submit@debbugs.gnu.org; Thu, 21 Nov 2024 22:56:40 -0500 Original-Received: from layka.disroot.org ([178.21.23.139]:51976) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tEHJ2-0007D4-Vt for 74461@debbugs.gnu.org; Thu, 21 Nov 2024 19:13:55 -0500 Original-Received: from mail01.disroot.lan (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id A87BC24DC4; Fri, 22 Nov 2024 01:13:51 +0100 (CET) X-Virus-Scanned: SPAM Filter at disroot.org Original-Received: from layka.disroot.org ([127.0.0.1]) by localhost (disroot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id rV98yvSPhnDk; Fri, 22 Nov 2024 01:13:47 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1732234427; bh=PmbAhhgYVfqnYdeCGG29zZ2Mh1ebzknj//sxm1ii2mc=; h=From:To:Cc:Subject:Date; b=J7RvX5H+S6FUG+gUkPZOu3yM+VMwCrY0LmlQ2fI4qXkzr7W94Rcq7kHVmy3xCcZjU SABmXxJan6WJ+Om7gmeTH9bgr9/9K5rA8DkWHxlbyqUGmZwbWwWqK7s2zU1pHNWul2 OK146bOdsjdMrlRLo6NfdGFKSxMBhszidUveRNapl62fj8fOfGg1kRxFd3iib/8V81 isQd7rxH4dfiBeoEEs7epsVP/T8XdGYRZh8FuUwZNCSfWcblji2cRmkMrF1YBHnTRz xiAFN9PGqhT0JpSQNrG6RJrfNqrkr0PTtGotZeQrNHkmSnl3SBeAlf495cxpOGMPUu yLxCTkP6d00bg== X-Mailman-Approved-At: Thu, 21 Nov 2024 22:56:36 -0500 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:295765 Archived-At: --=-=-= Content-Type: text/plain >[Please use Reply All to reply, to keep the bug tracker CC'ed.] Thanks, got it. I didn't reply to all because the last e-mail was sent from my phone. This one is from Emacs, and I added the following to the header (before the e-mail body): To: eliz@gnu.org Cc: 74461@debbugs.gnu.org Subject: Re: bug#74461: [PATCH] Add go-work-ts-mode Let me know if I made any other mistakes in this e-mail. >> Also, this is my first time on an e-mail workflow like this. Updating the patch based on your requests would >> only require a rebase + new e-mail, or a rebase + reply to this thread? > >The latter. Using the same thread keeps all the discussions recorded >as part of the same issue number, which is good for future reviewing >of the discussion. For the same reason, please don't edit the Subject >of the responses, so that they all keep the same Subject. > >Thanks. > Thanks, I used the current subject for this e-mail now. Also, attached is the rebased commit featuring the changes you requested. I also went ahead and clarified the documentation for `go-mod-ts-mode--in-directive-p', to make it less generalized. Through this contribution, I think I'll get more comfortable with the e-mail workflow, and end up contributing more here. My next one would be mentioning the parser URLs in the other tree-sitter modes. treesit-auto (https://github.com/renzmann/treesit-auto) has a collection of them, so this shouldn't be particularly difficult. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Add-go-work-ts-mode.patch Content-Description: [PATCH] Add go-work-ts-mode >From 41571fc2d5515f0ede893eba0cc1ce316ef575a2 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Wed, 20 Nov 2024 23:07:28 -0300 Subject: [PATCH] Add go-work-ts-mode * admin/notes/tree-sitter/build-module/batch.sh: * admin/notes/tree-sitter/build-module/build.sh: Add go-work support. * etc/NEWS: Mention go-work-ts-mode. * lisp/progmodes/eglot.el (eglot-server-programs): Add go-work-ts-mode. * lisp/progmodes/go-ts-mode: Add go-work-ts-mode for working with workspace files. * lisp/progmodes/go-ts-mode: Add the repositories for the grammars to the commentary section. * lisp/progmodes/go-ts-mode (go-mod-ts-mode--in-directive-p): Be more specific on the directive location. --- admin/notes/tree-sitter/build-module/batch.sh | 1 + admin/notes/tree-sitter/build-module/build.sh | 5 + etc/NEWS | 6 ++ lisp/progmodes/eglot.el | 2 +- lisp/progmodes/go-ts-mode.el | 101 +++++++++++++++++- 5 files changed, 113 insertions(+), 2 deletions(-) diff --git a/admin/notes/tree-sitter/build-module/batch.sh b/admin/notes/tree-sitter/build-module/batch.sh index 012b5882e83..1b5214267f5 100755 --- a/admin/notes/tree-sitter/build-module/batch.sh +++ b/admin/notes/tree-sitter/build-module/batch.sh @@ -11,6 +11,7 @@ languages=( 'elixir' 'go' 'go-mod' + 'go-work' 'heex' 'html' 'java' diff --git a/admin/notes/tree-sitter/build-module/build.sh b/admin/notes/tree-sitter/build-module/build.sh index 9a567bb094d..263de388b00 100755 --- a/admin/notes/tree-sitter/build-module/build.sh +++ b/admin/notes/tree-sitter/build-module/build.sh @@ -39,6 +39,11 @@ case "${lang}" in lang="gomod" org="camdencheek" ;; + "go-work") + # The parser is called "gowork". + lang="gomod" + org="omertuc" + ;; "heex") org="phoenixframework" ;; diff --git a/etc/NEWS b/etc/NEWS index b6c21018779..a07d9300fe9 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -925,6 +925,12 @@ current buffer, if the major mode supports it. (Support for Transformed images are smoothed using the bilinear interpolation by means of the GDI+ library. +--- +** New major mode 'go-work-ts-mode'. +A major mode based on the tree-sitter library for editing "go.work" +files. If tree-sitter is properly set-up by the user, it can be +enabled for files named "go.work". + ---------------------------------------------------------------------- This file is part of GNU Emacs. diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el index e5c27de81fc..4dbb0906b9e 100644 --- a/lisp/progmodes/eglot.el +++ b/lisp/progmodes/eglot.el @@ -273,7 +273,7 @@ automatically)." (elm-mode . ("elm-language-server")) (mint-mode . ("mint" "ls")) ((kotlin-mode kotlin-ts-mode) . ("kotlin-language-server")) - ((go-mode go-dot-mod-mode go-dot-work-mode go-ts-mode go-mod-ts-mode) + ((go-mode go-dot-mod-mode go-dot-work-mode go-ts-mode go-mod-ts-mode go-work-ts-mode) . ("gopls")) ((R-mode ess-r-mode) . ("R" "--slave" "-e" "languageserver::run()")) diff --git a/lisp/progmodes/go-ts-mode.el b/lisp/progmodes/go-ts-mode.el index 86e74ad58a8..288b543ae5c 100644 --- a/lisp/progmodes/go-ts-mode.el +++ b/lisp/progmodes/go-ts-mode.el @@ -24,6 +24,9 @@ ;;; Commentary: ;; +;; - Go grammar: https://github.com/tree-sitter/tree-sitter-go +;; - Go module grammar: https://github.com/camdencheek/tree-sitter-go-mod +;; - Go workspace grammar: https://github.com/omertuc/tree-sitter-go-work ;;; Code: @@ -483,7 +486,7 @@ be run." "Tree-sitter indent rules for `go-mod-ts-mode'.") (defun go-mod-ts-mode--in-directive-p () - "Return non-nil if point is inside a directive. + "Return non-nil if point is inside a Go module directive. When entering an empty directive or adding a new entry to one, no node will be present meaning none of the indentation rules will match, because there is no parent to match against. This function determines @@ -565,6 +568,102 @@ what the parent of the node would be if it were a node." (if (treesit-ready-p 'gomod) (add-to-list 'auto-mode-alist '("/go\\.mod\\'" . go-mod-ts-mode))) +;; go.work support. + +(defvar go-work-ts-mode--syntax-table + (let ((table (make-syntax-table))) + (modify-syntax-entry ?/ ". 124b" table) + (modify-syntax-entry ?\n "> b" table) + table) + "Syntax table for `go-work-ts-mode'.") + +(defvar go-work-ts-mode--indent-rules + `((gowork + ((node-is ")") parent-bol 0) + ((parent-is "replace_directive") parent-bol go-ts-mode-indent-offset) + ((parent-is "use_directive") parent-bol go-ts-mode-indent-offset) + ((go-work-ts-mode--in-directive-p) no-indent go-ts-mode-indent-offset) + (no-node no-indent 0))) + "Tree-sitter indent rules for `go-work-ts-mode'.") + +(defun go-work-ts-mode--in-directive-p () + "Return non-nil if point is inside a Go workspace directive. +When entering an empty directive or adding a new entry to one, no node +will be present meaning none of the indentation rules will match, +because there is no parent to match against. This function determines +what the parent of the node would be if it were a node." + (lambda (node _ _ &rest _) + (unless (treesit-node-type node) + (save-excursion + (backward-up-list) + (back-to-indentation) + (pcase (treesit-node-type (treesit-node-at (point))) + ("replace" t) + ("use" t)))))) + +(defvar go-work-ts-mode--keywords + '("go" "replace" "use") + "go.work keywords for tree-sitter font-locking.") + +(defvar go-work-ts-mode--font-lock-settings + (treesit-font-lock-rules + :language 'gowork + :feature 'bracket + '((["(" ")"]) @font-lock-bracket-face) + + :language 'gowork + :feature 'comment + '((comment) @font-lock-comment-face) + + :language 'gowork + :feature 'keyword + `([,@go-work-ts-mode--keywords] @font-lock-keyword-face) + + :language 'gowork + :feature 'number + '([(go_version) (version)] @font-lock-number-face) + + :language 'gowork + :feature 'operator + '((["=>"]) @font-lock-warning-face) + + :language 'gowork + :feature 'error + :override t + '((ERROR) @font-lock-warning-face)) + "Tree-sitter font-lock settings for `go-work-ts-mode'.") + +;;;###autoload +(define-derived-mode go-work-ts-mode prog-mode "Go Work" + "Major mode for editing go.work files, powered by tree-sitter." + :group 'go + :syntax-table go-work-ts-mode--syntax-table + + (when (treesit-ready-p 'gowork) + (setq treesit-primary-parser (treesit-parser-create 'gowork)) + + ;; Comments. + (setq-local comment-start "// ") + (setq-local comment-end "") + (setq-local comment-start-skip (rx "//" (* (syntax whitespace)))) + + ;; Indent. + (setq-local indent-tabs-mode t + treesit-simple-indent-rules go-work-ts-mode--indent-rules) + + ;; Font-lock. + (setq-local treesit-font-lock-settings go-work-ts-mode--font-lock-settings) + (setq-local treesit-font-lock-feature-list + '((comment) + (keyword) + (number) + (bracket error operator))) + + (treesit-major-mode-setup))) + +(if (treesit-ready-p 'gowork) + (add-to-list 'auto-mode-alist '("/go\\.work\\'" . go-work-ts-mode))) + (provide 'go-ts-mode) ;;; go-ts-mode.el ends here -- 2.47.0 --=-=-=--