-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2023-11-16-using-the-golang-mode-shipped-with-emacs.html
108 lines (97 loc) · 8.32 KB
/
2023-11-16-using-the-golang-mode-shipped-with-emacs.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="How I set up the golang mode, go-ts-mode, that's shipped with Emacs.">
<link rel="alternate"
type="application/rss+xml"
href="https://magnus.therning.org/feed.xml"
title="RSS feed for https://magnus.therning.org/">
<title>Using the golang mode shipped with Emacs</title>
<meta name="author" content="Magnus Therning"><meta name="referrer" content="no-referrer"><link href= "static/style.css" rel="stylesheet" type="text/css" /><link href= "static/htmlize.css" rel="stylesheet" type="text/css" /><link href= "static/extra_style.css" rel="stylesheet" type="text/css" /></head>
<body>
<div id="preamble" class="status"><div class="nav-bar"><a class="nav-link" href="./index.html">Top</a><a class="nav-link" href="./archive.html">Archive</a><a class="nav-link align-right" href="./feed.xml"><img src="static/rss-feed-icon.png" style="height: 24px;" /></a></div></div>
<div id="content">
<div class="post-date">16 Nov 2023</div><h1 class="post-title"><a href="https://magnus.therning.org/2023-11-16-using-the-golang-mode-shipped-with-emacs.html">Using the golang mode shipped with Emacs</a></h1>
<p>
A few weeks ago I wanted to try out tree-sitter and switched a few of the modes
I use for coding to their <code>-ts-mode</code> variants. Based on the excellent <a href="https://www.masteringemacs.org/article/how-to-get-started-tree-sitter">How to Get
Started with Tree-Sitter</a> I added bits like this to the setup I have for coding
modes:<sup><a id="fnr.1" class="footref" href="#fn.1" role="doc-backlink">1</a></sup>
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span class="org-rainbow-delimiters-depth-1">(</span><span class="org-keyword">use-package</span> <span class="org-constant">X-mode</span>
<span class="org-builtin">:init</span>
<span class="org-rainbow-delimiters-depth-2">(</span>add-to-list 'treesit-language-source-alist '<span class="org-rainbow-delimiters-depth-3">(</span>X <span class="org-string">"https://github.com/tree-sitter/tree-sitter-X"</span><span class="org-rainbow-delimiters-depth-3">)</span><span class="org-rainbow-delimiters-depth-2">)</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">(treesit-install-language-grammar 'X)</span>
<span class="org-rainbow-delimiters-depth-2">(</span>add-to-list 'major-mode-remap-alist '<span class="org-rainbow-delimiters-depth-3">(</span>X-mode . X-ts-mode<span class="org-rainbow-delimiters-depth-3">)</span><span class="org-rainbow-delimiters-depth-2">)</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">...</span>
<span class="org-rainbow-delimiters-depth-1">)</span>
</pre>
</div>
<p>
I then manually evaluated the expression that's commented out to download and
compile the tree-sitter grammar. It's a rather small change, it works, and I can
switch over language by language. I swapped a couple of languages to the
tree-sitter modes like this, including golang. The only mode that I noticed
changes in was golang, in particular my adding of <code>gofmt-before-save</code> to
<code>before-save-hook</code> had stopped having any effect.
</p>
<p>
What I hadn't realised was that the <a href="https://github.com/dominikh/go-mode.el"><code>go-mode</code> I was using</a> didn't ship with Emacs
and that when I switched to <code>go-ts-mode</code> I switched to one that was. It turns
out that <code>gofmt-before-save</code> is hard-wired to work only in <code>go-mode</code>,
something others have <a href="https://github.com/dominikh/go-mode.el/issues/396#issuecomment-1366716882">noticed</a>.
</p>
<p>
I don't feel like waiting for <code>go-mode</code> to fix that though, especially not when
there's a perfectly fine golang mode shipping with Emacs now, and not when
<a href="https://github.com/purcell/emacs-reformatter">emacs-reformatter</a> make it so easy to define formatters (as I've <a href="https://magnus.therning.org/2023-09-24-defining-a-formatter-for-cabal-files.html">written about
before</a>).
</p>
<p>
My golang setup, sans keybindings, now looks like this:<sup><a id="fnr.2" class="footref" href="#fn.2" role="doc-backlink">2</a></sup>
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span class="org-rainbow-delimiters-depth-1">(</span><span class="org-keyword">use-package</span> <span class="org-constant">go-ts-mode</span>
<span class="org-builtin">:hook</span>
<span class="org-rainbow-delimiters-depth-2">(</span>go-ts-mode . lsp-deferred<span class="org-rainbow-delimiters-depth-2">)</span>
<span class="org-rainbow-delimiters-depth-2">(</span>go-ts-mode . go-format-on-save-mode<span class="org-rainbow-delimiters-depth-2">)</span>
<span class="org-builtin">:init</span>
<span class="org-rainbow-delimiters-depth-2">(</span>add-to-list 'treesit-language-source-alist '<span class="org-rainbow-delimiters-depth-3">(</span>go <span class="org-string">"https://github.com/tree-sitter/tree-sitter-go"</span><span class="org-rainbow-delimiters-depth-3">)</span><span class="org-rainbow-delimiters-depth-2">)</span>
<span class="org-rainbow-delimiters-depth-2">(</span>add-to-list 'treesit-language-source-alist '<span class="org-rainbow-delimiters-depth-3">(</span>gomod <span class="org-string">"https://github.com/camdencheek/tree-sitter-go-mod"</span><span class="org-rainbow-delimiters-depth-3">)</span><span class="org-rainbow-delimiters-depth-2">)</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">(dolist (lang '(go gomod)) (treesit-install-language-grammar lang))</span>
<span class="org-rainbow-delimiters-depth-2">(</span>add-to-list 'auto-mode-alist '<span class="org-rainbow-delimiters-depth-3">(</span><span class="org-string">"\\.go\\'"</span> . go-ts-mode<span class="org-rainbow-delimiters-depth-3">)</span><span class="org-rainbow-delimiters-depth-2">)</span>
<span class="org-rainbow-delimiters-depth-2">(</span>add-to-list 'auto-mode-alist '<span class="org-rainbow-delimiters-depth-3">(</span><span class="org-string">"/go\\.mod\\'"</span> . go-mod-ts-mode<span class="org-rainbow-delimiters-depth-3">)</span><span class="org-rainbow-delimiters-depth-2">)</span>
<span class="org-builtin">:config</span>
<span class="org-rainbow-delimiters-depth-2">(</span><span class="org-keyword">reformatter-define</span> go-format
<span class="org-builtin">:program</span> <span class="org-string">"goimports"</span>
<span class="org-builtin">:args</span> '<span class="org-rainbow-delimiters-depth-3">(</span><span class="org-string">"/dev/stdin"</span><span class="org-rainbow-delimiters-depth-3">)</span><span class="org-rainbow-delimiters-depth-2">)</span>
<span class="org-builtin">:general</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">...</span>
<span class="org-rainbow-delimiters-depth-1">)</span>
</pre>
</div>
<p>
So far I'm happy with the built-in <code>go-ts-mode</code> and I've got to say that using a
minor mode for the format-on-save functionality is more elegant than adding a
function to <code>before-save-hook</code> (something that <code>go-mode</code> may get through this
<a href="https://github.com/dominikh/go-mode.el/pull/426">PR</a>).
</p>
<div id="footnotes">
<h2 class="footnotes">Footnotes: </h2>
<div id="text-footnotes">
<div class="footdef"><sup><a id="fn.1" class="footnum" href="#fnr.1" role="doc-backlink">1</a></sup> <div class="footpara" role="doc-footnote"><p class="footpara">
There were a few more things that I needed to modify. As the tree-sitter
modes are completely separate from the non-tree-sitter modes things like hooks
and keybindings in the modes' keymaps.
</p></div></div>
<div class="footdef"><sup><a id="fn.2" class="footnum" href="#fnr.2" role="doc-backlink">2</a></sup> <div class="footpara" role="doc-footnote"><p class="footpara">
The full file is <a href="https://gitlab.com/magus/mes/-/blob/main/lisp/mes-dev-go.el">here</a>.
</p></div></div>
</div>
</div><div class="taglist"><a href="https://magnus.therning.org/tags.html">Tags</a>: <a href="https://magnus.therning.org/tag-emacs.html">emacs</a> <a href="https://magnus.therning.org/tag-tree-sitter.html">tree-sitter</a> </div>
<div id="comments">Comment <a href=https://www.reddit.com/r/emacs/comments/17wgpsp/using_the_golang_mode_shipped_with_emacs/>here</a>.</div></div>
<div id="postamble" class="status"><!-- org-static-blog-page-postamble --></div>
</body>
</html>