Alexander-Miller / treemacs

GNU General Public License v3.0
2.11k stars 154 forks source link

File opens in wrong split #936

Open mattmc3 opened 2 years ago

mattmc3 commented 2 years ago

I'm experiencing a really weird issue with Treemacs. When I click to open a file it typically opens in the main frame, but occasionally Treemacs will decide to create a new split. I can reproduce this pretty consistently, but I am stumped why it's happening.

In this example, you can see when I click on my init-undo.el file in my project, it opens in a new split, and it also for some reason opens the init-whitespace.el file beneath it. My treemacs config is nothing special - it's the default config from the readme. Other than initially opening the Treemacs sidebar, everything in this was done with the mouse, not keyboard shortcuts:

split-bug

Here's another example. This one happens whenever I click up.fish, which perhaps not coincidentally is also the second to last file:

second-example

Any help I can provide troubleshooting?

mattmc3 commented 2 years ago

On closer observation, I think what is happening is that the Treemacs window scrolls when I click on one of those lower files, which the scrolling then detects my click as a drag event. If I comment out this line the problem goes away:

https://github.com/Alexander-Miller/treemacs/blob/700bf4fb00c2f05f809d65ed320d47a3dd436d59/src/elisp/treemacs-mode.el#L151

Obviously that's not the fix, but that unintentional drag event may help point to what causes this issue.

Alexander-Miller commented 2 years ago

If it's an accidental mouse-drag then what happens is treemacs calls find-file from inside the window the event happened in. If that window is treemacs then it's your Emacs that's deciding whether to split or not.

The obvious solution to ignore drag events inside treemacs will not work for long since I plan to implement moving files by dragging them.

It might be mostly fixable when we ignore mouse drag when the happens in the same row. To test that try this:

(defun treemacs-dragleftclick-action (event)
  "Drag a file/dir node to be opened in a window.
Must be bound to a mouse click, or EVENT will not be supplied."
  (interactive "e")
  (when (eq 'drag-mouse-1 (elt event 0))
    (let* ((info1 (elt (cdr event) 0))
           (info2 (elt (cdr event) 1))
           (source-window (elt info1 0))
           (target-window (elt info2 0))
           (source-pos (elt info1 1))
           (target-pos (elt info2 1))
           (treemacs-buffer (treemacs-get-local-buffer)))
      (unless (and treemacs-buffer
                   (eq source-window target-window)
                   (with-current-buffer treemacs-buffer
                     (string=
                      (treemacs-button-get (treemacs--button-at source-pos) :key)
                      (treemacs-button-get (treemacs--button-at target-pos) :key))))
        (let* ((node (with-current-buffer treemacs-buffer (treemacs-node-at-point)))
               (path (-some-> node (treemacs-button-get :path))))
          (treemacs-with-path path
            :file-action (progn (select-window target-window)
                                (find-file path))
            :no-match-action (ignore)))))))

You can also try getting rid of the scrolling. This particular behavior is controlled by scroll-margin.

mattmc3 commented 2 years ago

The treemacs-dragleftclick-action function you provided still exhibits the same behavior (see below), however setting (setq scroll-margin 0) did resolve it for me.

test-code

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity.