From b73a829837bbc05840ae00cbe514fb1786695614 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Tue, 14 Nov 2023 05:15:45 -0800 Subject: refactor: vim.ui.clipboard #26040 Problem: Platform-specific UI providers should live in `vim.ui.*`. #24164 Solution: - Move `vim.clipboard.osc52` module to `vim.ui.clipboard.osc52`. - TODO: move all of `clipboard.vim` to `vim.ui.clipboard`. ref #25872 --- runtime/doc/api.txt | 3 +- runtime/doc/lsp.txt | 2 +- runtime/doc/provider.txt | 8 ++--- runtime/lua/vim/clipboard/osc52.lua | 60 ---------------------------------- runtime/lua/vim/ui/clipboard/osc52.lua | 60 ++++++++++++++++++++++++++++++++++ src/nvim/api/ui.c | 2 +- 6 files changed, 68 insertions(+), 67 deletions(-) delete mode 100644 runtime/lua/vim/clipboard/osc52.lua create mode 100644 runtime/lua/vim/ui/clipboard/osc52.lua diff --git a/runtime/doc/api.txt b/runtime/doc/api.txt index b38524bd55..6970479a78 100644 --- a/runtime/doc/api.txt +++ b/runtime/doc/api.txt @@ -3598,7 +3598,8 @@ nvim_ui_set_option({name}, {value}) *nvim_ui_set_option()* |RPC| only nvim_ui_term_event({event}, {value}) *nvim_ui_term_event()* - Tells Nvim when a terminal event has occurred. + Tells Nvim when a terminal event has occurred: sets |v:termresponse| and + fires |TermResponse|. The following terminal events are supported: diff --git a/runtime/doc/lsp.txt b/runtime/doc/lsp.txt index d70c24bb54..3cbadb9bdb 100644 --- a/runtime/doc/lsp.txt +++ b/runtime/doc/lsp.txt @@ -1484,7 +1484,7 @@ get({filter}) *vim.lsp.inlay_hint.get()* vim.lsp.util.apply_text_edits(resolved_hint.textEdits, 0, client.encoding) location = resolved_hint.label[1].location - client.request("textDocument/hover", { + client.request('textDocument/hover', { textDocument = { uri = location.uri }, position = location.range.start, }) diff --git a/runtime/doc/provider.txt b/runtime/doc/provider.txt index 1b49ee3a3d..23bde05072 100644 --- a/runtime/doc/provider.txt +++ b/runtime/doc/provider.txt @@ -269,12 +269,12 @@ into explicitly by setting the following |g:clipboard| definition: >lua vim.g.clipboard = { name = 'OSC 52', copy = { - ['+'] = require('vim.clipboard.osc52').copy, - ['*'] = require('vim.clipboard.osc52').copy, + ['+'] = require('vim.ui.clipboard.osc52').copy, + ['*'] = require('vim.ui.clipboard.osc52').copy, }, paste = { - ['+'] = require('vim.clipboard.osc52').paste, - ['*'] = require('vim.clipboard.osc52').paste, + ['+'] = require('vim.ui.clipboard.osc52').paste, + ['*'] = require('vim.ui.clipboard.osc52').paste, }, } < diff --git a/runtime/lua/vim/clipboard/osc52.lua b/runtime/lua/vim/clipboard/osc52.lua deleted file mode 100644 index 035a6abb86..0000000000 --- a/runtime/lua/vim/clipboard/osc52.lua +++ /dev/null @@ -1,60 +0,0 @@ -local M = {} - -function M.copy(lines) - local s = table.concat(lines, '\n') - io.stdout:write(string.format('\027]52;;%s\027\\', vim.base64.encode(s))) -end - -function M.paste() - local contents = nil - local id = vim.api.nvim_create_autocmd('TermResponse', { - callback = function(args) - local resp = args.data ---@type string - local encoded = resp:match('\027%]52;%w?;([A-Za-z0-9+/=]*)') - if encoded then - contents = vim.base64.decode(encoded) - return true - end - end, - }) - - io.stdout:write('\027]52;;?\027\\') - - local ok, res - - -- Wait 1s first for terminals that respond quickly - ok, res = vim.wait(1000, function() - return contents ~= nil - end) - - if res == -1 then - -- If no response was received after 1s, print a message and keep waiting - vim.api.nvim_echo( - { { 'Waiting for OSC 52 response from the terminal. Press Ctrl-C to interrupt...' } }, - false, - {} - ) - ok, res = vim.wait(9000, function() - return contents ~= nil - end) - end - - if not ok then - vim.api.nvim_del_autocmd(id) - if res == -1 then - vim.notify( - 'Timed out waiting for a clipboard response from the terminal', - vim.log.levels.WARN - ) - elseif res == -2 then - -- Clear message area - vim.api.nvim_echo({ { '' } }, false, {}) - end - return 0 - end - - -- If we get here, contents should be non-nil - return vim.split(assert(contents), '\n') -end - -return M diff --git a/runtime/lua/vim/ui/clipboard/osc52.lua b/runtime/lua/vim/ui/clipboard/osc52.lua new file mode 100644 index 0000000000..035a6abb86 --- /dev/null +++ b/runtime/lua/vim/ui/clipboard/osc52.lua @@ -0,0 +1,60 @@ +local M = {} + +function M.copy(lines) + local s = table.concat(lines, '\n') + io.stdout:write(string.format('\027]52;;%s\027\\', vim.base64.encode(s))) +end + +function M.paste() + local contents = nil + local id = vim.api.nvim_create_autocmd('TermResponse', { + callback = function(args) + local resp = args.data ---@type string + local encoded = resp:match('\027%]52;%w?;([A-Za-z0-9+/=]*)') + if encoded then + contents = vim.base64.decode(encoded) + return true + end + end, + }) + + io.stdout:write('\027]52;;?\027\\') + + local ok, res + + -- Wait 1s first for terminals that respond quickly + ok, res = vim.wait(1000, function() + return contents ~= nil + end) + + if res == -1 then + -- If no response was received after 1s, print a message and keep waiting + vim.api.nvim_echo( + { { 'Waiting for OSC 52 response from the terminal. Press Ctrl-C to interrupt...' } }, + false, + {} + ) + ok, res = vim.wait(9000, function() + return contents ~= nil + end) + end + + if not ok then + vim.api.nvim_del_autocmd(id) + if res == -1 then + vim.notify( + 'Timed out waiting for a clipboard response from the terminal', + vim.log.levels.WARN + ) + elseif res == -2 then + -- Clear message area + vim.api.nvim_echo({ { '' } }, false, {}) + end + return 0 + end + + -- If we get here, contents should be non-nil + return vim.split(assert(contents), '\n') +end + +return M diff --git a/src/nvim/api/ui.c b/src/nvim/api/ui.c index e6d9035b0d..c898925af8 100644 --- a/src/nvim/api/ui.c +++ b/src/nvim/api/ui.c @@ -512,7 +512,7 @@ void nvim_ui_pum_set_bounds(uint64_t channel_id, Float width, Float height, Floa ui->pum_pos = true; } -/// Tells Nvim when a terminal event has occurred. +/// Tells Nvim when a terminal event has occurred: sets |v:termresponse| and fires |TermResponse|. /// /// The following terminal events are supported: /// -- cgit