Skip to content

add go-asm-mode #445

@adonovan

Description

@adonovan

Gopls issue golang/go#71754 proposes adding basic support for navigation and hover when reading Go assembly files. https://go.dev/cl/649461 adds support for Definition within Go .s files. I have tested it using go-mode and eglot with the additions below. Could they be added to go-mode.el? (There's an unfortunate 3-way chicken-and-egg relationship between eglot, gopls, and go-mode that makes bootstrapping a new feature tricky.)

;; support for Go assembly files
;;
;; Optionally add these lines to your ~/.emacs if using eglot:
;;
;;    (add-hook 'go-asm-mode-hook #'eglot-ensure)
;;
;;    ;; TODO(adonovan): push this upstream to eglot.
;;    (setq eglot-server-programs
;;     (cons '((go-asm-mode :language-id "go.s") . ("gopls")) eglot-server-programs))

(define-derived-mode go-asm-mode asm-mode "Go assembly"
  "Major mode for Go assembly (.s) files."
  (set (make-local-variable 'comment-start) "// ")
  (set (make-local-variable 'comment-end)   "")
  (set (make-local-variable 'comment-use-syntax) t)
  (set (make-local-variable 'comment-start-skip) "\\(//+\\)\\s *")
  (set (make-local-variable 'indent-line-function) 'go-mode-indent-line)
  (setq indent-tabs-mode t))

;; Add a find-file hook to recognize Go assembly.
(add-hook 'find-file-hook #'go-asm-find-file-hook)

(defun go-asm-find-file-hook ()
  "A find-file hook to recognize Go assembly files."
  (if (is-go-asm (buffer-file-name))
      (go-asm-mode)))

(defun is-go-asm (filename)
  "Reports whether `filename' ends with .s and has at least one .go
sibling in its directory."
  (if (string-suffix-p ".s" filename)
      (let ((directory (file-name-directory filename)))
	(if directory
	    (cl-some (lambda (s) (string-suffix-p ".go" s))
		     (condition-case nil
			 (directory-files directory)
		       (error nil)))))))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions