Skip to content

Commit 50e15c7

Browse files
psanfordmuirdm
authored andcommitted
Add major mode for go.mod files: go-dot-mod-mode
This is a simple major mode for go.mod files. It's named go-dot-mod-mode instead of go-mod-mode to avoid tab completion conflicts with go-mode* functions.
1 parent c020e2c commit 50e15c7

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

go-mode.el

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2620,6 +2620,50 @@ If BUFFER, return the number of characters in that buffer instead."
26202620
(with-current-buffer (or buffer (current-buffer))
26212621
(1- (position-bytes (point-max)))))
26222622

2623+
(defvar go-dot-mod-mode-map
2624+
(let ((map (make-sparse-keymap)))
2625+
map)
2626+
"Keymap for `go-dot-mod-mode'.")
2627+
2628+
(defvar go-dot-mod-mode-syntax-table
2629+
(let ((st (make-syntax-table)))
2630+
;; handle '//' comment syntax
2631+
(modify-syntax-entry ?/ ". 124b" st)
2632+
(modify-syntax-entry ?\n "> b" st)
2633+
st)
2634+
"Syntax table for `go-dot-mod-mode'.")
2635+
2636+
(defconst go-dot-mod-mode-keywords
2637+
'("module" "go" "require" "replace" "exclude")
2638+
"All keywords in the Go language. Used for font locking.")
2639+
2640+
(defvar go-dot-mod-font-lock-keywords
2641+
`(
2642+
(,(concat "\\_<" (regexp-opt go-dot-mod-mode-keywords t) "\\_>") . font-lock-keyword-face))
2643+
"Keyword highlighting specification for `go-dot-mod-mode'.")
2644+
2645+
;;;###autoload
2646+
(define-derived-mode go-dot-mod-mode fundamental-mode "Go Mod"
2647+
"A major mode for editing go.mod files."
2648+
:syntax-table go-dot-mod-mode-syntax-table
2649+
(set (make-local-variable 'comment-start) "// ")
2650+
(set (make-local-variable 'comment-end) "")
2651+
(set (make-local-variable 'comment-use-syntax) t)
2652+
(set (make-local-variable 'comment-start-skip) "\\(//+\\)\\s *")
2653+
2654+
(set (make-local-variable 'font-lock-defaults)
2655+
'(go-dot-mod-font-lock-keywords))
2656+
(set (make-local-variable 'indent-line-function) 'go-mode-indent-line)
2657+
2658+
;; Go style
2659+
(setq indent-tabs-mode t)
2660+
2661+
;; we borrow the go-mode-indent function so we need this buffer cache
2662+
(set (make-local-variable 'go-dangling-cache) (make-hash-table :test 'eql))
2663+
(add-hook 'before-change-functions #'go--reset-dangling-cache-before-change t t))
2664+
2665+
;;;###autoload
2666+
(add-to-list 'auto-mode-alist '("go\\.mod\\'" . go-dot-mod-mode))
26232667

26242668
;; Polyfills for functions added in Emacs 26. Remove these once we don’t
26252669
;; support Emacs 25 any more.

test/go-indentation-test.el

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
(indent-region (point-min) (point-max) nil)
1717
(should (string= contents-before-indent (buffer-string)))))))
1818

19+
(ert-deftest go-dot-mod--indent-line ()
20+
(with-temp-buffer
21+
(go-dot-mod-mode)
22+
(insert-file-contents "testdata/indentation_tests/go.mod")
23+
(let ((contents-before-indent (buffer-string)))
24+
(indent-region (point-min) (point-max) nil)
25+
(should (string= contents-before-indent (buffer-string))))))
26+
1927
(defun go--should-indent (input expected)
2028
"Run `indent-region' against INPUT and make sure it matches EXPECTED."
2129
(with-temp-buffer
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module my/thing
2+
3+
// comment
4+
go 1.12
5+
6+
require other/thing v1.0.2
7+
require new/thing/v2 v2.3.4
8+
exclude old/thing v1.2.3
9+
replace bad/thing v1.4.5 => good/thing v1.4.5
10+
11+
require (
12+
// comment inside block
13+
new/thing v2.3.4
14+
old/thing v1.2.3
15+
)
16+
17+
replace (
18+
bad/thing v1.4.5 => good/thing v1.4.5
19+
)

0 commit comments

Comments
 (0)