mohkale / consult-yasnippet

Other
35 stars 3 forks source link

wrong-type-argument error when `consult-yasnippet-use-thing-at-point` is non-nil #13

Closed LemonBreezes closed 8 months ago

LemonBreezes commented 1 year ago

Hi. I am using Emacs30 and the latest versions of all packages (not sure if that is related). When I have consult-yasnippet-use-thing-at-point set to t and I use consult-yasnippet, I get the following error:

Debugger entered--Lisp error: (wrong-type-argument number-or-marker-p nil)
  #f(compiled-function (action template) #<bytecode 0x5f6aaa87c9c8a98>)(return nil)
  #f(compiled-function () #<bytecode -0x34444d2d5e98bdc>)()
  consult--with-preview-1((:debounce 0.4 any) #f(compiled-function (action template) #<bytecode 0x5f6aaa87c9c8a98>) #f(compiled-function (narrow input cand) #<bytecode -0x136ddb692b07db2c>) #f(compiled-function (&rest args2) #<bytecode -0x14e415de2bb85d7f>) #f(compiled-function () #<bytecode 0x1d2ebe30b58b9a74>))
  consult--read-1((... ... ... ... ... ... ... ... ... ... ... ... ... ... ...) :prompt "Choose a snippet: " :annotate #f(compiled-function (cand) #<bytecode -0x18ce84f80d04b690>) :initial nil :lookup consult--lookup-cdr :require-match t :state #f(compiled-function (action template) #<bytecode 0x5f6aaa87c9c8a98>) :category ...)
  consult--read((... ... ... ... ... ... ... ... ... ... ... ... ... ... ...) :prompt "Choose a snippet: " :annotate #f(compiled-function (cand) #<bytecode -0x18ce84f80d04b690>) :initial nil :lookup consult--lookup-cdr :require-match t :state #f(compiled-function (action template) #<bytecode 0x5f6aaa87c9c8a98>) :category ...)
  consult-yasnippet--read-template(nil)
  consult-yasnippet(nil)
  funcall-interactively(consult-yasnippet nil)
  call-interactively(consult-yasnippet)
  (cond (t (call-interactively #'consult-yasnippet)) (t (call-interactively #'yas-insert-snippet)))
  +default/insert-snippet()
  funcall-interactively(+default/insert-snippet)
  command-execute(+default/insert-snippet)
mohkale commented 1 year ago

It's not quite clear where things are going wrong just from this backtrace. We get to the preview stage and then an error. If you could:

  1. Load consult-yasnippet.el directly instead of the bytecompiled elc file and reproduce the stacktrace.
  2. Share which snippet this is happening with.

Then it would be easier to see where things are going wrong.

LemonBreezes commented 1 year ago

It's not quite clear where things are going wrong just from this backtrace. We get to the preview stage and then an error. If you could:

  1. Load consult-yasnippet.el directly instead of the bytecompiled elc file and reproduce the stacktrace.
  2. Share which snippet this is happening with.

Then it would be easier to see where things are going wrong.

Debugger entered--Lisp error: (wrong-type-argument number-or-marker-p nil)
  -(9762 nil)
  (let* ((yas-verbosity 0) (inhibit-redisplay t) (inhibit-read-only t) (orig-offset (- (point-max) (cdr region))) (yas-prompt-functions '(yas-no-prompt))) (delete-region (car region) (cdr region)) (goto-char (car region)) (setq region (cons (car initial-region) (cdr initial-region))) (insert initial-region-contents) (if (not region-active-initially) (progn (setq region (consult-yasnippet--bounds-of-thing-at-point template)) (setq initial-region region) (setq initial-region-contents (buffer-substring (car region) (cdr region))))) (if (and region-active-initially (eq action 'return)) (progn (activate-mark) (set-mark (car region)) (goto-char (cdr region)))) (if (and template (not (eq action 'return))) (progn (unwind-protect (consult-yasnippet--expand-template template region) (unwind-protect (mapc #'yas--commit-snippet (yas-active-snippets (point-min) (point-max))) (setcdr region (- (point-max) orig-offset)) (deactivate-mark))) (redisplay))))
  (save-current-buffer (set-buffer buf) (let* ((yas-verbosity 0) (inhibit-redisplay t) (inhibit-read-only t) (orig-offset (- (point-max) (cdr region))) (yas-prompt-functions '(yas-no-prompt))) (delete-region (car region) (cdr region)) (goto-char (car region)) (setq region (cons (car initial-region) (cdr initial-region))) (insert initial-region-contents) (if (not region-active-initially) (progn (setq region (consult-yasnippet--bounds-of-thing-at-point template)) (setq initial-region region) (setq initial-region-contents (buffer-substring (car region) (cdr region))))) (if (and region-active-initially (eq action 'return)) (progn (activate-mark) (set-mark (car region)) (goto-char (cdr region)))) (if (and template (not (eq action 'return))) (progn (unwind-protect (consult-yasnippet--expand-template template region) (unwind-protect (mapc #'yas--commit-snippet (yas-active-snippets ... ...)) (setcdr region (- ... orig-offset)) (deactivate-mark))) (redisplay)))))
  (closure ((region) (initial-region-contents . "") (initial-region) (region-active-initially) (buf . #<buffer consult-yasnippet.el>)) (action template) (save-current-buffer (set-buffer buf) (let* ((yas-verbosity 0) (inhibit-redisplay t) (inhibit-read-only t) (orig-offset (- (point-max) (cdr region))) (yas-prompt-functions '(yas-no-prompt))) (delete-region (car region) (cdr region)) (goto-char (car region)) (setq region (cons (car initial-region) (cdr initial-region))) (insert initial-region-contents) (if (not region-active-initially) (progn (setq region (consult-yasnippet--bounds-of-thing-at-point template)) (setq initial-region region) (setq initial-region-contents (buffer-substring ... ...)))) (if (and region-active-initially (eq action 'return)) (progn (activate-mark) (set-mark (car region)) (goto-char (cdr region)))) (if (and template (not (eq action ...))) (progn (unwind-protect (consult-yasnippet--expand-template template region) (unwind-protect ... ... ...)) (redisplay))))))(return #s(yas--template :key "sb" :content "(save-buffer $0)" :name "save-buffer" :condition nil :expand-env nil :load-file "/home/st/.config/emacs/.local/straight/build-30.0.50/doom-snippets/emacs-lisp-mode/save-buffer" :save-file nil :keybinding nil :uuid "save-buffer" :menu-binding-pair ((menu-item "save-buffer" (lambda nil (interactive) (yas--expand-or-visit-from-menu 'emacs-lisp-mode "save-buffer")) :keys "sb =>")) :group nil :perm-group nil :table #s(yas--table :name "emacs-lisp-mode" :hash #<hash-table equal 183/217 0x1c0b9f5 ...> :uuidhash #<hash-table equal 187/217 0x101c951 ...> :parents nil :direct-keymap (keymap))))
  #f(compiled-function () #<bytecode 0x1beef0cfc218267a>)()
  consult--with-preview-1((:debounce 0.4 any) (closure ((region) (initial-region-contents . "") (initial-region) (region-active-initially) (buf . #<buffer consult-yasnippet.el>)) (action template) (save-current-buffer (set-buffer buf) (let* ((yas-verbosity 0) (inhibit-redisplay t) (inhibit-read-only t) (orig-offset (- (point-max) (cdr region))) (yas-prompt-functions '(yas-no-prompt))) (delete-region (car region) (cdr region)) (goto-char (car region)) (setq region (cons (car initial-region) (cdr initial-region))) (insert initial-region-contents) (if (not region-active-initially) (progn (setq region (consult-yasnippet--bounds-of-thing-at-point template)) (setq initial-region region) (setq initial-region-contents (buffer-substring ... ...)))) (if (and region-active-initially (eq action 'return)) (progn (activate-mark) (set-mark (car region)) (goto-char (cdr region)))) (if (and template (not (eq action ...))) (progn (unwind-protect (consult-yasnippet--expand-template template region) (unwind-protect ... ... ...)) (redisplay)))))) #f(compiled-function (narrow input cand) #<bytecode -0x136ddb195b37c32c>) #f(compiled-function (&rest args2) #<bytecode -0x14b9583db153fd7f>) #f(compiled-function () #<bytecode 0x1d2ebe30b58b9a74>))
  consult--read-1((... ... ... ... ... ... ... ... ... ... ... ... ... ... ...) :prompt "Choose a snippet: " :annotate (closure ... ... ...) :initial nil :lookup consult--lookup-cdr :require-match t :state (closure ... ... ...) :category ...)
  consult--read((... ... ... ... ... ... ... ... ... ... ... ... ... ... ...) :prompt "Choose a snippet: " :annotate (closure ... ... ...) :initial nil :lookup consult--lookup-cdr :require-match t :state (closure ... ... ...) :category ...)
  (let* ((buffer-undo-list t) (candidates (consult-yasnippet--candidates (if all-templates (yas--all-templates (map-values yas--tables)) (yas--all-templates (yas--get-snippet-tables)))))) (consult--read candidates :prompt "Choose a snippet: " :annotate (consult-yasnippet--annotate candidates) :initial (if consult-yasnippet-use-thing-at-point (progn (thing-at-point 'symbol))) :lookup 'consult--lookup-cdr :require-match t :state (consult-yasnippet--preview) :category 'yasnippet))
  consult-yasnippet--read-template(nil)
  (and t (consult-yasnippet--read-template arg))
  (let* ((template (and t (consult-yasnippet--read-template arg)))) (if template (let* ((thing-bounds (if (region-active-p) (cons nil nil) (consult-yasnippet--bounds-of-thing-at-point template))) (thing-start (car thing-bounds)) (thing-end (cdr thing-bounds))) (yas-expand-snippet (let* ((cl-x template)) (progn (or (let* ... ...) (signal ... ...)) (aref cl-x 2))) thing-start thing-end (let* ((cl-x template)) (progn (or (let* ... ...) (signal ... ...)) (aref cl-x 5))))) nil))
  consult-yasnippet(nil)
  funcall-interactively(consult-yasnippet nil)
  call-interactively(consult-yasnippet)
  (cond (t (call-interactively #'consult-yasnippet)) (t (call-interactively #'yas-insert-snippet)))
  +default/insert-snippet()
  funcall-interactively(+default/insert-snippet)
  command-execute(+default/insert-snippet)

based on this new backtrace, what's happening is that in consult-yasnippet--preview, (let (... (orig-offset (- (point-max) (cdr region))))) is doing (- 9762 nil).

For me, it's happening with every snippet.

LemonBreezes commented 1 year ago

Replacing that let binding with

(initial-region (if (use-region-p)
                    (cons (region-beginning) (region-end))
                  (or (bounds-of-thing-at-point 'symbol)
                      (cons (point) (point)))))

fixes that error and seems to use the symbol at point that way for me.

mohkale commented 12 months ago

@LemonBreezes

Sorry for the delay. That's interesting. I'm unsure why adding a bounds-of-thing check there would solve the problem. Something else seems to be clearing out the region and causing an error for you but I can't reproduce :cry:. Do you have any extra customisations set for consult-yasnippet or are you just using the default options?

LemonBreezes commented 8 months ago

@LemonBreezes

Sorry for the delay. That's interesting. I'm unsure why adding a bounds-of-thing check there would solve the problem. Something else seems to be clearing out the region and causing an error for you but I can't reproduce 😢. Do you have any extra customisations set for consult-yasnippet or are you just using the default options?

Hi. Sorry, I had forgotten about this issue. I think it is no longer reproducible for me so it must've been Emacs30-related or something. I opened a PR, #14, for a separate issue though.