Skip to content

Commit

Permalink
Add function to automatically add package header
Browse files Browse the repository at this point in the history
  • Loading branch information
phikal committed Apr 28, 2020
1 parent 734d523 commit 7d179ae
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions go-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,17 @@ See `go-play-region' for more details."
(function :tag "Call function"))
:group 'go)

(defcustom go-mode-add-package-line nil
"Should `go-mode' attempt to add a package line to empty files.
See function `go-mode-add-package-line' for more details."
:type 'boolean
:set (lambda (sym val)
(if val
(add-hook 'go-mode-hook #'go-mode-add-package-line)
(remove-hook 'go-mode-hook #'go-mode-add-package-line))
(set-default sym val))
:group 'go)

(defcustom go-coverage-display-buffer-func 'display-buffer-reuse-window
"How `go-coverage' should display the coverage buffer.
See `display-buffer' for a list of possible functions."
Expand Down Expand Up @@ -1823,6 +1834,9 @@ with goflymake (see URL `https://github.com/dougm/goflymake'), gocode
("func" "^func *\\(.*\\) {" 1)))
(imenu-add-to-menubar "Index")

(when go-mode-add-package-line
(add-hook 'go-mode-hook #'go-mode-add-package-line))

;; Go style
(setq indent-tabs-mode t)

Expand Down Expand Up @@ -2871,6 +2885,29 @@ If BUFFER, return the number of characters in that buffer instead."
(with-current-buffer (or buffer (current-buffer))
(1- (position-bytes (point-max)))))

(defun go-mode-add-package-line ()
"Scan all files with \".go\" extensions, to guess a package.
Unless all files have the same package, nothing will be changed.
This function will do nothing if the current buffer isn't empty."
(let (name)
(save-excursion
(goto-char (point-min))
(when (looking-at-p "\\`\\(?:[[:space:]]\\|\n\\)*\\'")
(catch 'differ
(with-temp-buffer
(dolist (file (directory-files "." nil "\\.go\\'" t))
(insert-file-contents-literally file nil 0 4096)
(when (search-forward-regexp "^package \\([[:alnum:]]+\\)$" nil t)
(cond ((not name)
(setq name (match-string 1)))
((not (string= name (match-string 1)))
(setq name nil)
(throw 'differ nil))))
(erase-buffer))))))
(when name
(goto-char (point-min))
(insert "package " name "\n\n"))))

(defvar go-dot-mod-mode-map
(let ((map (make-sparse-keymap)))
map)
Expand Down

0 comments on commit 7d179ae

Please sign in to comment.