brotzeit / rustic

Rust development environment for Emacs
Apache License 2.0
727 stars 102 forks source link

Cursor moves to top of source buffer on M-x rustic-cargo-build #216

Closed ve-nt closed 2 years ago

ve-nt commented 3 years ago

I've set up rustic so that I can compile and have my rustic-compilation buffer in a separate frame, so that I can have my code on one monitor, and compiler log on another. I have used the following to set this:

(setq rustic-compile-display-method 'display-buffer-other-frame)

However, I've run into some strange behavior with this. When I call rustic-cargo-build, everything works as expected except my cursor is moved to the top of the buffer I'm currently focused on (i.e. the buffer with my rust source code in). I get the same behavior with rustic-cargo-run and rustic-recompile.

However, weirder, is that this only happens if my rustic-compilation buffer is not in a window on the current frame. For example, if I set up my frame so that I have 2 windows, rust code on the left and rustic-compilation on the right, then my cursor position is left intact.

I have narrowed the problem down to this here, found in rustic-compile.el:

(defun rustic-compilation-setup-buffer (buf dir mode &optional no-mode-line)
  "Prepare BUF for compilation process."
  (let ((inhibit-read-only t))
    (with-current-buffer buf
      (erase-buffer)                    ; <--- This is causing the cursor to move!
      (setq default-directory dir)
      (funcall mode)
      (unless no-mode-line
        (setq mode-line-process
              '((:propertize ":%s" face compilation-mode-line-run)
                compilation-mode-line-errors)))
      (force-mode-line-update)
      (if (or compilation-auto-jump-to-first-error
              (eq compilation-scroll-output 'first-error))
          (set (make-local-variable 'compilation-auto-jump-to-next) t))
      (sit-for 0))))

When I comment out that call to (erase-buffer), my cursor is left intact. Obviously, this isn't desirable since commenting it out stops the compilation buffer from being cleared. I'm not all that good with Elisp, so this is about as much as I'm able to work out.

samhedin commented 3 years ago

This seems related: https://github.com/brotzeit/rustic/issues/172 What happens if you turn off rustic-format-on-save? Similar to that issue, this only seems to happen on some configs as I can not reproduce this either...

ve-nt commented 3 years ago

I just read through #172, and it does sound very similar!

I currently have rustic-format-on-save set to nil already. I also have rustic-format-trigger set to nil as well.

In case it's a configuration issue, here's all my Rust specific configuration:

I use rustic for my Rust environment
#+BEGIN_SRC emacs-lisp
  (require 'rustic)
  (add-hook 'rust-mode-hook #'rustic-mode)
#+END_SRC

I also use LSP Flycheck, and Yas.
#+BEGIN_SRC emacs-lisp
  (add-hook 'rustic-mode-hook #'lsp)
  (add-hook 'rustic-mode-hook #'flycheck-mode)
  (add-hook 'rustic-mode-hook #'yas-minor-mode)
#+END_SRC

Use rust-analyzer for my LSP server.
#+BEGIN_SRC emacs-lisp
  (setq lsp-rust-server 'rust-analyzer)
#+END_SRC

Enable clippy lints through LSP.
#+BEGIN_SRC emacs-lisp
  (setq lsp-rust-clippy-preference "on")
#+END_SRC

Don't run rustfmt on the buffer before saving.
#+BEGIN_SRC emacs-lisp
  (setq rustic-format-trigger nil)
#+END_SRC

Set fill to 80 characters, only used for comments. Setting to 80 is
the closest thing to the Rust style guide for writing comments.
#+BEGIN_SRC emacs-lisp
  (add-hook 'rustic-mode-hook '(lambda ()
                                 (set-fill-column 80)))
#+END_SRC
samhedin commented 3 years ago

Just tried with as vanilla of an emacs that I could think of based on your settings, trying both with and without (setq rustic-compile-display-method 'display-buffer-other-frame)

; init.el
(when (>= emacs-major-version 24)
    (require 'package)
      (add-to-list
       'package-archives
          ;; '("melpa" . "http://stable.melpa.org/packages/") ; many packages won't show if using stable
             '("melpa" . "https://melpa.org/packages/")
            t))
(package-initialize)
(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(package-selected-packages '(yasnippet-snippets lsp-mode rustic vterm)))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )

(require 'rustic)
  (add-hook 'rust-mode-hook #'rustic-mode)
  (setq lsp-rust-server 'rust-analyzer)
  (setq lsp-rust-clippy-preference "on")
  (setq rustic-format-trigger nil)
 (add-hook 'rustic-mode-hook '(lambda ()
                                 (set-fill-column 80)))

With this, I could still not reproduce your issue, though I don't doubt it's there since I have managed to reproduce the issue in the other thread (also with a minimal emacs config). Could you try clearing out as much as possible and then giving it another try? Alternatively think of if you can provide any more info.

brotzeit commented 2 years ago

Sorry that it took so long 47c0004