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: Wed, 01 Jan 2025 23:24:38 -0300 Message-ID: <8734i2c5qx.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="11221"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Randy Taylor , Eli Zaretskii , Stefan Monnier To: 74461@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Jan 02 03:25:27 2025 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 1tTAtq-0002mN-5v for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 02 Jan 2025 03:25:26 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tTAtV-00066l-6F; Wed, 01 Jan 2025 21:25:05 -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 1tTAtT-00065r-VC for bug-gnu-emacs@gnu.org; Wed, 01 Jan 2025 21:25:04 -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 1tTAtT-0007Bi-Ln for bug-gnu-emacs@gnu.org; Wed, 01 Jan 2025 21:25: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=oZotkRdVVcZtPXZGPeJaGGr6hLst/N/VvgGODjTvt60=; b=ldZWv3bua8bRV+hL93v3Jzd1G1hDfVPcwKZ7KXKqXSFmPB5AujKB7vj3F4nZTI1jYA2LC2vXf7ZC+lxVzIKzTM1ODduRZpsWR/GR8UrsF69awJxO9AR7mjdxEkFUf4d2PZfhmkzGasMONfnwuSFoJOHOt2dQVQsCrPAjojAlmgsuV+XOKB+zyEBrIRWepaWWhChwMbsFRyBOwGRpG9x0/1D2qgiI9RYrG4CAcJVspJ+Epvzv0OSBYhNx+dZfRopeEFJtbThcxMzj7TJkWhatbzhPHTWxQTxWxtA87jYSEnlDnYiUDvsDp1gyc9fjjSzQrq1+oVcnV+Af82CWb0t9bA==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tTAtS-0003VQ-Et for bug-gnu-emacs@gnu.org; Wed, 01 Jan 2025 21:25:02 -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: Thu, 02 Jan 2025 02:25:02 +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.173578469613459 (code B ref 74461); Thu, 02 Jan 2025 02:25:02 +0000 Original-Received: (at 74461) by debbugs.gnu.org; 2 Jan 2025 02:24:56 +0000 Original-Received: from localhost ([127.0.0.1]:41493 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tTAtL-0003V0-IW for submit@debbugs.gnu.org; Wed, 01 Jan 2025 21:24:56 -0500 Original-Received: from layka.disroot.org ([178.21.23.139]:60312) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1tTAtG-0003Uk-Iy for 74461@debbugs.gnu.org; Wed, 01 Jan 2025 21:24:54 -0500 Original-Received: from mail01.disroot.lan (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id CCC9B25A96; Thu, 2 Jan 2025 03:24:48 +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 8rIoxGp4vBp7; Thu, 2 Jan 2025 03:24:44 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1735784684; bh=Nu+zJi/J8sgDXggJgByKGjNEL0XT0nZgaiCr03Wjt/M=; h=From:To:Cc:Subject:Date; b=XAJ3MJ2C1DbKdcJFOEnZDBjlmXtReW7oZ2vWexexPGFJX9xUI7WZqhcr1A8VtTRf5 HV2XPYA4W2vBI0CVp5HOc1T/iCOc3Mi0xA1OcAcIcwklp5E0mmc9VS417ObbOALcBM 69aj7dOOaK8EGp7J+cXf07llXVkoevE1GIYA02zqlqh7krPc7kcrWmf7A5l8V3wU0X hJ6I1+t1y9kPdE1dmlQeuWZwytJojB7oTTPx9hqd5v3GjalkUjDFtbDhIwL5D8k8i2 /UdVXQS4oZMwR7bD3k3hARrcCgijgviri9TTOQKBfOFTAUznNA5vQIJlEQ2ebtCuyu Ini5knTPG3SFA== 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:298104 Archived-At: --=-=-= Content-Type: text/plain Updated the patch to follow the suggestions made by Randy: - Rename go-mod-ts-mode--in-directive-p - Add latest grammar versions of go-mod and go-work - Removed the mention of the now defunct go-work-ts-mode--syntax-table - Added back the syntax table for the regular Go tree-sitter mode -- Gabriel Santos --=-=-= 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 1ef854544261118bb4c3db08e76ed611157a424f 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: (languages): * admin/notes/tree-sitter/build-module/build.sh: (grammardir): 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.el: Commentary: Add the repositories for the grammars to the commentary section. (go-work-ts-mode--indent-rules): (go-work-ts-mode--keywords): (go-work-ts-mode--font-lock-settings): New variables. (go-work-ts-mode--directive-matcher): (go-work-ts-mode): New functions. (go-mod-ts-mode--in-directive-p): Rename it to go-mod-ts-mode--directive-matcher. Be more specific on the directive location (modules). Replace mention of nil with function. Use member instead of pcase to check node types. * test/lisp/progmodes/go-ts-mode-resources/font-lock-package.go: * test/lisp/progmodes/go-ts-mode-resources/indent-mod.erts: * test/lisp/progmodes/go-ts-mode-resources/indent-work.erts: New files for testing indentation and font-locking for Go module and workspace files. * test/lisp/progmodes/go-ts-mode-tests.el: Add tests for Go module and workspace files. --- 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 | 113 ++++++++++++++++-- .../go-ts-mode-resources/font-lock-package.go | 4 + .../go-ts-mode-resources/indent-mod.erts | 16 +++ .../go-ts-mode-resources/indent-work.erts | 16 +++ test/lisp/progmodes/go-ts-mode-tests.el | 24 ++++ 9 files changed, 176 insertions(+), 11 deletions(-) create mode 100644 test/lisp/progmodes/go-ts-mode-resources/font-lock-package.go create mode 100644 test/lisp/progmodes/go-ts-mode-resources/indent-mod.erts create mode 100644 test/lisp/progmodes/go-ts-mode-resources/indent-work.erts 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..4f3c6da3c5f 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="gowork" + org="omertuc" + ;; "heex") org="phoenixframework" ;; diff --git a/etc/NEWS b/etc/NEWS index 1a6f5ae7816..55ad36fbc30 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1166,6 +1166,12 @@ means of the GDI+ library. In addition to ':file FILE' for playing a sound from a file, ':data DATA' can now be used to play a sound from memory. +--- +** 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 d4e40791e86..f31d0f41d21 100644 --- a/lisp/progmodes/eglot.el +++ b/lisp/progmodes/eglot.el @@ -280,7 +280,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 1ea49e72f07..c82deb1f4e2 100644 --- a/lisp/progmodes/go-ts-mode.el +++ b/lisp/progmodes/go-ts-mode.el @@ -26,6 +26,8 @@ ;; ;; go-ts-mode is known to work with the following languages and version: ;; - tree-sitter-go: v0.23.4-1-g12fe553 +;; - tree-sitter-go-mod: v1.1.0-3b01edce +;; - tree-sitter-go-work: 949a8a47 ;; ;; We try our best to make builtin modes work with latest grammar ;; versions, so a more recent grammar version has a good chance to work. @@ -33,6 +35,9 @@ ;;; Commentary: ;; +;; Go uses tabs as a convention for indentation: +;; https://go.dev/doc/effective_go#formatting +;; so `indent-tabs-mode' is enabled for the modes. ;;; Code: @@ -462,7 +467,7 @@ be run." (go-ts-mode--get-build-tags-flag) default-directory))) -;; go.mod support. +;;;; go.mod support. (defvar go-mod-ts-mode--syntax-table (let ((table (make-syntax-table))) @@ -479,12 +484,12 @@ be run." ((parent-is "replace_directive") parent-bol go-ts-mode-indent-offset) ((parent-is "require_directive") parent-bol go-ts-mode-indent-offset) ((parent-is "retract_directive") parent-bol go-ts-mode-indent-offset) - ((go-mod-ts-mode--in-directive-p) no-indent go-ts-mode-indent-offset) + ((go-mod-ts-mode--directive-matcher) no-indent go-ts-mode-indent-offset) (no-node no-indent 0))) "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. +(defun go-mod-ts-mode--directive-matcher () + "Return a function for determining 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 @@ -494,12 +499,12 @@ what the parent of the node would be if it were a node." (save-excursion (backward-up-list) (back-to-indentation) - (pcase (treesit-node-type (treesit-node-at (point))) - ("exclude" t) - ("module" t) - ("replace" t) - ("require" t) - ("retract" t)))))) + (member (treesit-node-type (treesit-node-at (point))) + '("exclude" + "module" + "replace" + "require" + "retract")))))) (defvar go-mod-ts-mode--keywords '("exclude" "go" "module" "replace" "require" "retract") @@ -566,6 +571,94 @@ 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--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--directive-matcher) 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--directive-matcher () + "Return a function for determining 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) + (member (treesit-node-type (treesit-node-at (point))) + '("replace" + "use")))))) + +(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-operator-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 + + (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))) + +;;;###autoload +(add-to-list 'auto-mode-alist '("/go\\.work\\'" . go-work-ts-mode)) + (provide 'go-ts-mode) ;;; go-ts-mode.el ends here diff --git a/test/lisp/progmodes/go-ts-mode-resources/font-lock-package.go b/test/lisp/progmodes/go-ts-mode-resources/font-lock-package.go new file mode 100644 index 00000000000..7bee6848810 --- /dev/null +++ b/test/lisp/progmodes/go-ts-mode-resources/font-lock-package.go @@ -0,0 +1,4 @@ +replace gnu.org/go/package1 v1.0.0 => gnu.org/go/package2 v1.0.0 +// ^ font-lock-keyword-face +// ^ font-lock-number-face +// ^ font-lock-operator-face diff --git a/test/lisp/progmodes/go-ts-mode-resources/indent-mod.erts b/test/lisp/progmodes/go-ts-mode-resources/indent-mod.erts new file mode 100644 index 00000000000..2f7bfd9030b --- /dev/null +++ b/test/lisp/progmodes/go-ts-mode-resources/indent-mod.erts @@ -0,0 +1,16 @@ +Code: + (lambda () + (go-mod-ts-mode) + (indent-region (point-min) (point-max))) + +Point-Char: | + +Name: Basic + +=-= +require ( + gnu.org/go/package1 v1.0.0 + gnu.org/go/package2 v1.0.0 +) + +=-=-= diff --git a/test/lisp/progmodes/go-ts-mode-resources/indent-work.erts b/test/lisp/progmodes/go-ts-mode-resources/indent-work.erts new file mode 100644 index 00000000000..b210974cedc --- /dev/null +++ b/test/lisp/progmodes/go-ts-mode-resources/indent-work.erts @@ -0,0 +1,16 @@ +Code: + (lambda () + (go-work-ts-mode) + (indent-region (point-min) (point-max))) + +Point-Char: | + +Name: Basic + +=-= +use ( + ./package1 + ./package2 +) + +=-=-= diff --git a/test/lisp/progmodes/go-ts-mode-tests.el b/test/lisp/progmodes/go-ts-mode-tests.el index 2837d5d23d2..7a4d7453799 100644 --- a/test/lisp/progmodes/go-ts-mode-tests.el +++ b/test/lisp/progmodes/go-ts-mode-tests.el @@ -23,6 +23,8 @@ (require 'ert-x) (require 'treesit) +;; go-ts-mode + (ert-deftest go-ts-mode-test-indentation () (skip-unless (treesit-ready-p 'go)) (ert-test-erts-file (ert-resource-file "indent.erts"))) @@ -32,5 +34,27 @@ (let ((treesit-font-lock-level 4)) (ert-font-lock-test-file (ert-resource-file "font-lock.go") 'go-ts-mode))) +;; go-mod-ts-mode + +(ert-deftest go-work-ts-mode-test-indentation () + (skip-unless (treesit-ready-p 'gomod)) + (ert-test-erts-file (ert-resource-file "indent-mod.erts"))) + +(ert-deftest go-mod-ts-test-font-lock () + (skip-unless (treesit-ready-p 'gomod)) + (let ((treesit-font-lock-level 4)) + (ert-font-lock-test-file (ert-resource-file "font-lock-package.go") 'go-mod-ts-mode))) + +;; go-work-ts-mode + +(ert-deftest go-work-ts-mode-test-indentation () + (skip-unless (treesit-ready-p 'gowork)) + (ert-test-erts-file (ert-resource-file "indent-work.erts"))) + +(ert-deftest go-work-ts-test-font-lock () + (skip-unless (treesit-ready-p 'gowork)) + (let ((treesit-font-lock-level 4)) + (ert-font-lock-test-file (ert-resource-file "font-lock-package.go") 'go-work-ts-mode))) + (provide 'go-ts-mode-tests) ;;; go-ts-mode-tests.el ends here -- 2.47.1 --=-=-=--