diff options
| author | Davide Masserut | 2023-02-02 21:00:02 +0100 |
|---|---|---|
| committer | Theodor Thornhill | 2023-02-04 19:26:05 +0100 |
| commit | 89b550eac2909f1fcd7cc5eb3dfe81e853bf5ed0 (patch) | |
| tree | 16e9d11d0045c7b1e6bce7d6c7daedba4d8d56bf | |
| parent | 1a123feb1815d6a2ee4ba6abb747fb62fd8b9e0f (diff) | |
| download | emacs-89b550eac2909f1fcd7cc5eb3dfe81e853bf5ed0.tar.gz emacs-89b550eac2909f1fcd7cc5eb3dfe81e853bf5ed0.zip | |
Fix switch statement indentation for go-ts-mode (bug#61238)
* lisp/progmodes/go-ts-mode.el (go-ts-mode--indent-rules): Add
indentation for type switch and select case blocks
* test/lisp/progmodes/go-ts-mode-resources/indent.erts: New .erts file
to test indentation of Go constructs and prevent regression of bug
fixes.
* test/lisp/progmodes/go-ts-mode-tests.el: New file with go-ts-mode
tests.
| -rw-r--r-- | lisp/progmodes/go-ts-mode.el | 4 | ||||
| -rw-r--r-- | test/lisp/progmodes/go-ts-mode-resources/indent.erts | 47 | ||||
| -rw-r--r-- | test/lisp/progmodes/go-ts-mode-tests.el | 31 |
3 files changed, 82 insertions, 0 deletions
diff --git a/lisp/progmodes/go-ts-mode.el b/lisp/progmodes/go-ts-mode.el index 5f3e1ea3e68..95dcf653fc6 100644 --- a/lisp/progmodes/go-ts-mode.el +++ b/lisp/progmodes/go-ts-mode.el | |||
| @@ -72,6 +72,7 @@ | |||
| 72 | ((node-is "labeled_statement") no-indent) | 72 | ((node-is "labeled_statement") no-indent) |
| 73 | ((parent-is "argument_list") parent-bol go-ts-mode-indent-offset) | 73 | ((parent-is "argument_list") parent-bol go-ts-mode-indent-offset) |
| 74 | ((parent-is "block") parent-bol go-ts-mode-indent-offset) | 74 | ((parent-is "block") parent-bol go-ts-mode-indent-offset) |
| 75 | ((parent-is "communication_case") parent-bol go-ts-mode-indent-offset) | ||
| 75 | ((parent-is "const_declaration") parent-bol go-ts-mode-indent-offset) | 76 | ((parent-is "const_declaration") parent-bol go-ts-mode-indent-offset) |
| 76 | ((parent-is "default_case") parent-bol go-ts-mode-indent-offset) | 77 | ((parent-is "default_case") parent-bol go-ts-mode-indent-offset) |
| 77 | ((parent-is "expression_case") parent-bol go-ts-mode-indent-offset) | 78 | ((parent-is "expression_case") parent-bol go-ts-mode-indent-offset) |
| @@ -82,7 +83,10 @@ | |||
| 82 | ((parent-is "labeled_statement") parent-bol go-ts-mode-indent-offset) | 83 | ((parent-is "labeled_statement") parent-bol go-ts-mode-indent-offset) |
| 83 | ((parent-is "literal_value") parent-bol go-ts-mode-indent-offset) | 84 | ((parent-is "literal_value") parent-bol go-ts-mode-indent-offset) |
| 84 | ((parent-is "parameter_list") parent-bol go-ts-mode-indent-offset) | 85 | ((parent-is "parameter_list") parent-bol go-ts-mode-indent-offset) |
| 86 | ((parent-is "select_statement") parent-bol 0) | ||
| 87 | ((parent-is "type_case") parent-bol go-ts-mode-indent-offset) | ||
| 85 | ((parent-is "type_spec") parent-bol go-ts-mode-indent-offset) | 88 | ((parent-is "type_spec") parent-bol go-ts-mode-indent-offset) |
| 89 | ((parent-is "type_switch_statement") parent-bol 0) | ||
| 86 | ((parent-is "var_declaration") parent-bol go-ts-mode-indent-offset) | 90 | ((parent-is "var_declaration") parent-bol go-ts-mode-indent-offset) |
| 87 | (no-node parent-bol 0))) | 91 | (no-node parent-bol 0))) |
| 88 | "Tree-sitter indent rules for `go-ts-mode'.") | 92 | "Tree-sitter indent rules for `go-ts-mode'.") |
diff --git a/test/lisp/progmodes/go-ts-mode-resources/indent.erts b/test/lisp/progmodes/go-ts-mode-resources/indent.erts new file mode 100644 index 00000000000..a89d69b307c --- /dev/null +++ b/test/lisp/progmodes/go-ts-mode-resources/indent.erts | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | Code: | ||
| 2 | (lambda () | ||
| 3 | (go-ts-mode) | ||
| 4 | (indent-region (point-min) (point-max))) | ||
| 5 | |||
| 6 | Point-Char: | | ||
| 7 | |||
| 8 | Name: Basic | ||
| 9 | |||
| 10 | =-= | ||
| 11 | package main | ||
| 12 | |||
| 13 | func main() { | ||
| 14 | } | ||
| 15 | =-=-= | ||
| 16 | |||
| 17 | Name: Switch and Select | ||
| 18 | |||
| 19 | =-= | ||
| 20 | package main | ||
| 21 | |||
| 22 | func main() { | ||
| 23 | var x any | ||
| 24 | switch x { | ||
| 25 | case 1: | ||
| 26 | println("one") | ||
| 27 | default: | ||
| 28 | println("default case") | ||
| 29 | } | ||
| 30 | |||
| 31 | switch x.(type) { | ||
| 32 | case int: | ||
| 33 | println("integer") | ||
| 34 | default: | ||
| 35 | println("don't know the type") | ||
| 36 | } | ||
| 37 | |||
| 38 | var c chan int | ||
| 39 | select { | ||
| 40 | case x := <-c: | ||
| 41 | println(x) | ||
| 42 | default: | ||
| 43 | println("no communication") | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | =-=-= | ||
diff --git a/test/lisp/progmodes/go-ts-mode-tests.el b/test/lisp/progmodes/go-ts-mode-tests.el new file mode 100644 index 00000000000..548465208f9 --- /dev/null +++ b/test/lisp/progmodes/go-ts-mode-tests.el | |||
| @@ -0,0 +1,31 @@ | |||
| 1 | ;;; go-ts-mode-tests.el --- Tests for Tree-sitter-based Go mode -*- lexical-binding: t; -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2023 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | ;; This file is part of GNU Emacs. | ||
| 6 | |||
| 7 | ;; GNU Emacs is free software: you can redistribute it and/or modify | ||
| 8 | ;; it under the terms of the GNU General Public License as published by | ||
| 9 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 10 | ;; (at your option) any later version. | ||
| 11 | |||
| 12 | ;; GNU Emacs is distributed in the hope that it will be useful, | ||
| 13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | ;; GNU General Public License for more details. | ||
| 16 | |||
| 17 | ;; You should have received a copy of the GNU General Public License | ||
| 18 | ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. | ||
| 19 | |||
| 20 | ;;; Code: | ||
| 21 | |||
| 22 | (require 'ert) | ||
| 23 | (require 'ert-x) | ||
| 24 | (require 'treesit) | ||
| 25 | |||
| 26 | (ert-deftest go-ts-mode-test-indentation () | ||
| 27 | (skip-unless (treesit-ready-p 'go)) | ||
| 28 | (ert-test-erts-file (ert-resource-file "indent.erts"))) | ||
| 29 | |||
| 30 | (provide 'go-ts-mode-tests) | ||
| 31 | ;;; go-ts-mode-tests.el ends here | ||