syscfg/emacs/module/module-golang.el

62 lines
1.9 KiB
EmacsLisp

;;; module-golang.el --- support golang -*- lexical-binding: t; -*-
;;;
;;; Commentary:
;;;
;;; Adds support for golang to Emacs.
;;;
;;; Code:
(require 'module-pkg)
(pkg/require-defer 'go-mode)
(defun project-find-go-module (dir)
(when-let ((root (locate-dominating-file dir "go.mod")))
(cons 'go-module root)))
(cl-defmethod project-root ((project (head go-module)))
(cdr project))
(add-hook 'project-find-functions #'project-find-go-module)
(with-eval-after-load 'go-mode
(add-to-list 'auto-mode-alist '("\\.go$" . go-mode))
(add-hook 'before-save-hook #'gofmt-before-save))
(defun my/godoc-at-point ()
"Run godoc on the selected region or symbol at point, including dot-separated parts."
(interactive)
(let (default start end)
(if (use-region-p)
(setq start (region-beginning)
end (region-end)
default (buffer-substring-no-properties start end))
(let ((bounds (bounds-of-thing-at-point 'symbol)))
(setq start (if bounds (car bounds) (point))
end (if bounds (cdr bounds) (point))
default (if bounds
(buffer-substring-no-properties start end)
nil))))
(unless (use-region-p)
(save-excursion
(goto-char start)
(while (and (> (point) (point-min))
(eq (char-before) ?.))
(backward-sexp)
(setq start (point)))
(goto-char end)
(while (and (< (point) (point-max))
(eq (char-after) ?.))
(forward-sexp)
(setq end (point)))
(setq default (buffer-substring-no-properties start end))))
(godoc (read-string "Go documentation: " default))))
(add-hook 'go-mode-hook
(lambda ()
(local-set-key (kbd "C-c C-d") 'godoc-at-point-with-default)))
(provide 'module-golang)
;;; module-golang.el ends here.