简体   繁体   English

使用go-mode自动完成

[英]Auto-complete with go-mode

I'm trying to enable auto-complete-mode whenever a .go file is loaded through go-mode. 我试图通过go-mode加载.go文件时启用自动完成模式。 It works if I invoke auto-complete-mode manually for Go source files, but when I tried adding it to .emacs as below, it doesn't work: 如果我为Go源文件手动调用自动完成模式,它会起作用,但当我尝试将它添加到.emacs时,如下所示,它不起作用:

(add-hook 'go-mode-hook auto-complete-mode)

I've tried a few variations around it but none seem to work. 我尝试了一些变化,但似乎都没有。 Following is what the Go-Mode snippet currently looks like in my .emacs: 以下是我的.emacs中Go-Mode代码段的样子:

;; Load Go Mode
(require 'go-mode-load)
(add-hook 'go-mode-hook 'auto-complete-mode)

I tried creating my own hook function like this: 我尝试创建自己的钩子函数,如下所示:

;; Load Go Mode
(require 'go-mode-load)
(defun auto-complete-for-go ()
  (auto-complete-mode 1))
(add-hook 'go-mode-hook 'auto-complete-for-go)

I also tried including the hook in go-mode-load.el and go-mode.el , as well as calling auto-complete-mode like this: 我也尝试在go-mode-load.elgo-mode.el ,以及像这样调用auto-complete-mode

(auto-complete-mode t)
(provide 'go-mode)

Doesn't work either way. 两种方式都不起作用。 I also added the go-mode-hook to auto-complete-default function like so: 我还将go-mode-hookauto-complete-default函数中,如下所示:

(defun ac-config-default ()
  (setq-default ac-sources '(ac-source-abbrev ac-source-dictionary ac-source-words-in-same-mode-buffers))
  (add-hook 'go-mode-hook 'ac-common-setup)
  ;; Other hooks
  (global-auto-complete-mode t))

That doesn't work either. 这也行不通。 What's the best way to trigger a command just after a major mode is enabled for a buffer? 在为缓冲区启用主模式后触发命令的最佳方法是什么?

Here is workaround for now: 现在是解决方法:

(add-to-list 'ac-modes 'go-mode)

I fixed the problem in v1.4 branch with the following commits. 我使用以下提交修复了v1.4分支中的问题。

Which variations have you tried? 您尝试了哪些变体? It should work if you add a single-quote in front of auto-complete-mode : 如果您在auto-complete-mode前添加单引号,它应该可以工作:

(add-hook 'go-mode-hook 'auto-complete-mode)

Without this quote, auto-complete-mode is interpreted as a variable and the value of that variable is added to go-mode-hook . 如果没有此引号,则auto-complete-mode被解释为变量,并且该变量的值将添加到go-mode-hook For this to make sense, such a variable should contain a function reference as its value. 为此,有意义的是,这样的变量应该包含一个函数引用作为其值。 Most likely though there will be no variable named auto-complete-mode and Emacs will complain. 最有可能的是,没有名为auto-complete-mode变量,Emacs会抱怨。

By adding a quote, you tell Emacs that this is not a variable, but the actual function you want the hook to call. 通过添加引号,您可以告诉Emacs这不是变量,而是您希望钩子调用的实际函数。 See also here and here . 另见这里这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM