copilot-emacs / copilot.el

An unofficial Copilot plugin for Emacs.
MIT License
1.72k stars 122 forks source link

Always warning indent offest in elisp files. #220

Closed JimMoen closed 5 months ago

JimMoen commented 6 months ago

Hi! This is mostly a request for comments, more than an actual issue.

The original lisp-indent-offset was nil and it works well for many elisp files. Which means that lisp indentation in the default configuration is not expected to be a fixed value. But copilot always warn it.

``` ⛔ Warning (copilot): copilot--infer-indentation-offset found no mode-specific indentation offset. ``` https://github.com/zerolfx/copilot.el/blob/main/copilot.el#L299-L317: ```elisp (defvar-local copilot--indent-warning-printed-p nil "Flag indicating whether indent warning was already printed.") (defun copilot--infer-indentation-offset () "Infer indentation offset." (or (let ((mode major-mode)) (while (and (not (assq mode copilot--indentation-alist)) (setq mode (get mode 'derived-mode-parent)))) (when mode (cl-some (lambda (s) (when (and (boundp s) (numberp (symbol-value s))) (symbol-value s))) (alist-get mode copilot--indentation-alist)))) (progn (when (not copilot--indent-warning-printed-p) (display-warning '(copilot copilot-no-mode-indent) "copilot--infer-indentation-offset found no mode-specific indentation offset.") (setq-local copilot--indent-warning-printed-p t)) tab-width))) ```

Solution:

I used hook for emacs-lisp-mode:

(use-package copilot
  :straight (:host github :repo "zerolfx/copilot.el" :files ("dist" "*.el"))
  :ensure t
  :hook
  (prog-mode . copilot-mode)
  (emacs-lisp-mode . (lambda ()
                       (setq-local copilot--indent-warning-printed-p t)))
  :config
  (with-eval-after-load 'company
    ;; disable inline previews
    (delq 'company-preview-if-just-one-frontend company-frontends))

  (define-key copilot-completion-map (kbd "<tab>") 'copilot-accept-completion)
  (define-key copilot-completion-map (kbd "TAB") 'copilot-accept-completion))

Question:

Is it possible to provide an option to turn off this warning by default?

bclark-videra commented 6 months ago

Thanks @JimMoen for the diagnostics. I see the same thing with clojure-mode which derives from prog-mode. There's no specific indentation offset so this warning pops up on the first edit of every buffer.

I'm going to use your hook idea to avoid seeing the warning, but it would be nice to have an option to turn it off.

JimMoen commented 6 months ago

@bclark-videra a better hook to disable the warn:

(use-package copilot
  :hook
  (prog-mode . copilot-mode)
  (copilot-mode . (lambda ()
                    (setq-local copilot--indent-warning-printed-p t))))
emil-vdw commented 6 months ago

I think it makes sense to add a defcustom setting to disable this warning. It might also be useful to allow the user to customize copilot--indentation-alist to add indentation values for other modes themselves.

emil-vdw commented 6 months ago

My proposal is:

  1. Add a new defcustom to disable indentation offset warning entirely (instead of reusing a private var).
  2. Make copilot--indentation-alist public (allow user to customize this, adding presets for other modes).

thoughts?