From 870ca1de52b240926b88f01afa697cd9b119bdac Mon Sep 17 00:00:00 2001 From: Sebastian Lyng Johansen Date: Tue, 10 Jan 2023 11:22:41 +0100 Subject: feat(float): open float relative to mouse #21531 Problem: No easy way to position a LSP hover window relative to mouse. Solution: Introduce another option to the `relative` key in `nvim_open_win()`. With this PR it should be possible to override the handler and do something similar to this https://github.com/neovim/neovim/pull/19481#issuecomment-1193248674 to have hover information displayed from the mouse. Test case: ```lua local util = require('vim.lsp.util') local function make_position_param(window, offset_encoding) window = window or 0 local buf = vim.api.nvim_win_get_buf(window) local row, col local mouse = vim.fn.getmousepos() row = mouse.line col = mouse.column offset_encoding = offset_encoding or util._get_offset_encoding(buf) row = row - 1 local line = vim.api.nvim_buf_get_lines(buf, row, row + 1, true)[1] if not line then return { line = 0, character = 0 } end if #line < col then return { line = 0, character = 0 } end col = util._str_utfindex_enc(line, col, offset_encoding) return { line = row, character = col } end local make_params = function(window, offset_encoding) window = window or 0 local buf = vim.api.nvim_win_get_buf(window) offset_encoding = offset_encoding or util._get_offset_encoding(buf) return { textDocument = util.make_text_document_params(buf), position = make_position_param(window, offset_encoding), } end local hover_timer = nil vim.o.mousemoveevent = true vim.keymap.set({ '', 'i' }, '', function() if hover_timer then hover_timer:close() end hover_timer = vim.defer_fn(function() hover_timer = nil local params = make_params() vim.lsp.buf_request( 0, 'textDocument/hover', params, vim.lsp.with(vim.lsp.handlers.hover, { silent = true, focusable = false, relative = 'mouse', }) ) end, 500) return '' end, { expr = true }) ``` --- runtime/doc/api.txt | 1 + runtime/doc/lsp.txt | 1 + runtime/doc/news.txt | 4 ++++ 3 files changed, 6 insertions(+) (limited to 'runtime/doc') diff --git a/runtime/doc/api.txt b/runtime/doc/api.txt index c827da4554..d74657dc8e 100644 --- a/runtime/doc/api.txt +++ b/runtime/doc/api.txt @@ -2995,6 +2995,7 @@ nvim_open_win({buffer}, {enter}, {*config}) *nvim_open_win()* • "win" Window given by the `win` field, or current window. • "cursor" Cursor position in current window. + • "mouse" Mouse position • win: |window-ID| for relative="win". • anchor: Decides which corner of the float to place at diff --git a/runtime/doc/lsp.txt b/runtime/doc/lsp.txt index cf82cbf204..aff0f9a793 100644 --- a/runtime/doc/lsp.txt +++ b/runtime/doc/lsp.txt @@ -1612,6 +1612,7 @@ make_floating_popup_options({width}, {height}, {opts}) • border (string or table) override `border` • focusable (string or table) override `focusable` • zindex (string or table) override `zindex`, defaults to 50 + • relative ("mouse"|"cursor") defaults to "cursor" Return: ~ (table) Options diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index 88bad1b7df..7dceaa3318 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -50,6 +50,10 @@ NEW FEATURES *news-features* The following new APIs or features were added. +• |nvim_open_win()| now accepts a relative `mouse` option to open a floating win + relative to the mouse. Note that the mouse doesn't update frequently without + setting `vim.o.mousemoveevent = true` + • EditorConfig support is now builtin. This is enabled by default and happens automatically. To disable it, users should add >lua -- cgit