casouri / eldoc-box

childframe doc for eglot and anything that uses eldoc
361 stars 26 forks source link

Determining "side" with multiple windows #75

Closed jeff-phil closed 1 year ago

jeff-phil commented 1 year ago

I noticed with multiple windows, eldoc-box--window-side function was not always returning the correct side. This is especially problematic when window is split vertically and working in the bottom window.

Below seems to work better regardless of splits:

  (defun eldoc-box--window-side ()
    "Return the side of the selected window.
     Symbol ‘left’ if the selected window is on the left, ‘right’ if
     on the right. Return ‘left’ if there is only one window."
    (let* ((y (cdr (posn-x-y (posn-at-point))))
           (top (nth 1 (window-absolute-pixel-edges (selected-window))))
           (left-window (window-at-x-y 0 (+ y top))))
      (if (eq left-window (selected-window))
          'left
        'right)))

Also, different than but maybe related to above fix/change, someone else may find below useful. It is my offset positioning function to show eldoc-box to right (like sideline) unless horizontally split and then moves to more of a hover mode.

  (defun my/eldoc-box-set-offset(child-width child-height)
    "This adjusts the eldoc-box so that it's down and to far right of cursor,
     unless window is split horizontally, then looks more like `hover-at-point'."
    (let* ((win-width (- (nth 2 (window-absolute-pixel-edges)) ;right
                         (nth 0 (window-absolute-pixel-edges)))) ;left
           (small-window (> (frame-pixel-width) win-width))
           (x (if small-window
                  (+ 41 (car (window-inside-pixel-edges))
                     (car (posn-x-y (posn-at-point))))
                (- (caddr (window-inside-pixel-edges)) 8 child-width)))
           (y (+ 41 (cadr (window-inside-pixel-edges))
                 (cdr (posn-x-y (posn-at-point))))))
      (cons x y)))

  (setq eldoc-box-position-function #'my/eldoc-box-set-offset)

Here's a mash-up of positioning based on window splitting:

image
casouri commented 1 year ago

Thanks! I applied the change.