请教一下如何编写一个新的 company backend

如题. 需要用到 go-zero, 接口要用其 idl 进行定义, 想根据其语法写一个 company backend 进行简单的关键字补全

自助而后人助之:How to write company-mode backends - Google Search

有搜索过,看了都不是很系统的介绍。company 官方文档也很零碎 :joy:

关掉欢迎页,emacs启动后是*scratch*,我原来在配置文件里直接写insert,把要补全的词插入*scratch*里(当然scratch原本的内容清掉),这样company补全的时候就有这些词了,这个应该最简单

刚刚问了下ChatGPT:How to write company-mode backends in Emacs

------ Assistant ------

To write a company-mode backend in Emacs, you need to follow these steps:

  1. Define a function that takes a prefix and returns a list of possible completions. This function is called the “backend” function.

  2. Register the backend function with company-mode using the add-to-list function. The company-backends variable is a list of all registered backends.

  3. Customize the behavior of the backend function by setting its properties. For example, you can set the company-backend property to a symbol that represents the backend function.

  4. Implement the backend function to return a list of possible completions based on the prefix. You can use any method to generate the list, such as searching a database or parsing a file.

Here is an example of a simple backend function that returns a list of completions based on a hardcoded list of words:

(defun my-backend (command &optional arg &rest ignored)
  (interactive (list 'interactive))
  (case command
    (interactive (company-begin-backend 'my-backend))
    (prefix (and (eq major-mode 'text-mode)
                 (company-grab-word)))
    (candidates '("apple" "banana" "cherry" "date"))))

To register this backend with company-mode, you can use the following code:

(add-to-list 'company-backends 'my-backend)

This will add the my-backend function to the list of backends that company-mode uses to generate completions.

像这种问题,用 chatgpt 或 bing chat 比搜索引擎有优势

emacs 有一个补全框架叫 company ,如何为它编写一个新的 backend

你好,这是Bing。根据我的搜索结果,emacs 的 company 是一个文本补全框架,它使用可插拔的后端和前端来获取和显示补全候选[^1^]。为它编写一个新的后端的步骤大致如下[^5^]:

  • 定义一个函数,接受一个命令参数和一个可选的参数,并根据命令参数的不同值执行不同的操作,例如返回元数据、生成候选列表、过滤候选列表等。
  • 将这个函数添加到 company-backends 列表中,可以使用 hook 来针对不同的模式设置不同的后端[^3^]。
  • 测试和调试你的后端函数,确保它能够正确地与 company 框架交互,并且没有错误或漏洞。

如果你想要了解更多关于 emacs 的 company 和后端开发的知识,请参考以下链接[^1^] [^2^] [^3^] [^4^] [^5^]。

  1. company-mode for Emacs
  2. company-mode for Emacs
  3. Best company-backends lists - Emacs Stack Exchange
  4. GitHub - minad/corfu: corfu.el - Completion Overlay Region FUnction
  5. Good With Computers

有示例代码吗

你好,这是Bing。根据我的搜索结果,有一些示例代码可以参考,例如[^1^] [^5^] [^6^]。其中一个简单的后端函数的代码如下[^6^]:

(defun company-my-backend (command &optional arg &rest ignored)
  (interactive (list 'interactive))
  (case command
    (interactive (company-begin-backend 'company-my-backend))
    (prefix (and (eq major-mode 'emacs-lisp-mode)
                 (company-grab-symbol)))
    (candidates
     (remove-if-not
      (lambda (c) (string-prefix-p arg c))
      '("foo" "bar" "baz")))))

这个后端函数只在 emacs-lisp-mode 下工作,根据输入的前缀返回一个固定的候选列表(foo, bar, baz)。你可以将这个函数添加到 company-backends 列表中,或者使用 M-x company-begin-backend 来启动它。

  1. Backends (Company User Manual) - GitHub Pages
  2. company-mode for Emacs
  3. How do I change backend for company? - Emacs Stack Exchange
  4. Best company-backends lists - Emacs Stack Exchange
  5. EmacsWiki: Company Mode
  6. Difficulty creating simple company backend #181 - GitHub
1 个赞