From 5881f5fc532b9397f285104cc8f2702f172f31e1 Mon Sep 17 00:00:00 2001 From: Gerard Vermeulen Date: Thu, 23 Jan 2025 10:55:03 +0100 Subject: [PATCH] Improve tab stop handling and electric operators for Go --- README.org | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/README.org b/README.org index 998275d..4cc0bfa 100644 --- a/README.org +++ b/README.org @@ -4509,7 +4509,22 @@ Links to learn [[https://go.dev/][Go]] are: #+name: lst:setup-go #+begin_src emacs-lisp -n :results silent (when (featurep 'treesit) - (setopt go-ts-mode-indent-offset 2) + (setopt go-ts-mode-indent-offset 8) ; for compatibility with Go tools. + + (defun gots2tabs () + "Set tab stops to 2 spaces locally in current go-ts buffer." + (interactive) + (when (eq major-mode 'go-ts-mode) + (setq-local go-ts-mode-indent-offset 2) + (setq-local tab-width 2))) + + (defun gots8tabs () + "Set tab stops to 8 spaces locally in current go-ts buffer." + (interactive) + (when (eq major-mode 'go-ts-mode) + (setq-local go-ts-mode-indent-offset 8) + (setq-local tab-width 9))) + (add-to-list 'auto-mode-alist `(,(rx ".go" eos) . go-ts-mode))) #+end_src @@ -5264,8 +5279,30 @@ add spaces around operators for [[https://go.dev/][Go]] and [[https://www.python #+begin_src emacs-lisp -n :results silent (when (ensure-package-installation 'electric-operator) ;; electric-operator-mode does not handle all operators in Go correctly. - ;; (add-hook 'go-ts-mode-hook #'electric-operator-mode) + (add-hook 'go-ts-mode-hook #'electric-operator-mode) (add-hook 'python-mode-hook #'electric-operator-mode)) + +(with-eval-after-load 'electric-operator + ;; https://go.dev/ref/spec#Operators + (apply #'electric-operator-add-rules-for-mode 'go-mode + (electric-operator-get-rules-for-mode 'prog-mode)) + (electric-operator-add-rules-for-mode 'go-mode + (cons "*" #'electric-operator-c-mode-*) + (cons "&" #'electric-operator-c-mode-&) + (cons "++" #'electric-operator-c-mode-++) + (cons "--" #'electric-operator-c-mode---) + (cons "!=" " != ") + (cons "&&" " && ") + (cons "&^" " &^" ) + (cons "//" " // ") + (cons ":=" " := ") + (cons "<-" " <- ") + (cons "<<" " << ") + (cons "<=" " <= ") + (cons "==" " == ") + (cons ">=" " >= ") + (cons ">>" " >> ") + (cons "||" " || "))) #+end_src ** [[https://joaotavora.github.io/yasnippet/][Smart snippets]]