mirror of
https://github.com/neovim/neovim.git
synced 2024-12-19 02:34:59 -07:00
Compare commits
12 Commits
cec945c66e
...
174f1b528e
Author | SHA1 | Date | |
---|---|---|---|
|
174f1b528e | ||
|
7121983c45 | ||
|
888a803755 | ||
|
07d5dc8938 | ||
|
f9eb68f340 | ||
|
ce818a9a91 | ||
|
a56a5f0117 | ||
|
021b8f6e5d | ||
|
2ad8324092 | ||
|
c50d7747a5 | ||
|
ce26f7c332 | ||
|
25820b5e24 |
@ -1126,6 +1126,9 @@ Lua module: vim.lsp.client *lsp-client*
|
||||
• {server_capabilities} (`lsp.ServerCapabilities?`) Response from the
|
||||
server sent on `initialize` describing the
|
||||
server's capabilities.
|
||||
• {server_info} (`lsp.ServerInfo?`) Response from the server
|
||||
sent on `initialize` describing information
|
||||
about the server.
|
||||
• {progress} (`vim.lsp.Client.Progress`) A ring buffer
|
||||
(|vim.ringbuf()|) containing progress messages
|
||||
sent by the server. See
|
||||
|
@ -115,6 +115,7 @@ LSP
|
||||
• |vim.lsp.util.make_position_params()|, |vim.lsp.util.make_range_params()|
|
||||
and |vim.lsp.util.make_given_range_params()| now require the `position_encoding`
|
||||
parameter.
|
||||
• `:checkhealth vim.lsp` displays the server version (if available).
|
||||
|
||||
LUA
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -31,6 +31,7 @@ for k, v in pairs({
|
||||
loader = true,
|
||||
func = true,
|
||||
F = true,
|
||||
img = true,
|
||||
lsp = true,
|
||||
hl = true,
|
||||
diagnostic = true,
|
||||
|
78
runtime/lua/vim/img.lua
Normal file
78
runtime/lua/vim/img.lua
Normal file
@ -0,0 +1,78 @@
|
||||
local img = vim._defer_require('vim.img', {
|
||||
_backend = ..., --- @module 'vim.img._backend'
|
||||
_detect = ..., --- @module 'vim.img._detect'
|
||||
_image = ..., --- @module 'vim.img._image'
|
||||
_terminal = ..., --- @module 'vim.img._terminal'
|
||||
})
|
||||
|
||||
---Loads an image into memory, returning a wrapper around the image.
|
||||
---
|
||||
---Accepts `data` as base64-encoded bytes, or a `filename` that will be loaded.
|
||||
---@param opts {data?:string, filename?:string}
|
||||
---@return vim.img.Image
|
||||
function img.load(opts)
|
||||
return img._image:new(opts)
|
||||
end
|
||||
|
||||
img.protocol = (function()
|
||||
---@class vim.img.Protocol 'iterm2'|'kitty'|'sixel'
|
||||
|
||||
---@type vim.img.Protocol|nil
|
||||
local protocol = nil
|
||||
|
||||
local loaded = false
|
||||
|
||||
---Determines the preferred graphics protocol to use by default.
|
||||
---
|
||||
---@return vim.img.Protocol|nil
|
||||
return function()
|
||||
if not loaded then
|
||||
local graphics = img._detect().graphics
|
||||
|
||||
---@diagnostic disable-next-line:cast-type-mismatch
|
||||
---@cast graphics vim.img.Protocol|nil
|
||||
protocol = graphics
|
||||
|
||||
loaded = true
|
||||
end
|
||||
|
||||
return protocol
|
||||
end
|
||||
end)()
|
||||
|
||||
---@class vim.img.Opts: vim.img.Backend.RenderOpts
|
||||
---@field backend? vim.img.Protocol|vim.img.Backend
|
||||
|
||||
---Displays the image within the terminal used by neovim.
|
||||
---@param image vim.img.Image
|
||||
---@param opts? vim.img.Opts
|
||||
function img.show(image, opts)
|
||||
opts = opts or {}
|
||||
|
||||
local backend = opts.backend
|
||||
|
||||
-- If no graphics are explicitly defined, attempt to detect the
|
||||
-- preferred graphics. If we still cannot figure out a backend,
|
||||
-- throw an error early versus silently trying a protocol.
|
||||
if not backend then
|
||||
backend = img.protocol()
|
||||
assert(backend, 'no graphics backend available')
|
||||
end
|
||||
|
||||
-- For named protocols, grab the appropriate backend, failing
|
||||
-- if there is not a default backend for the specified protocol.
|
||||
if type(backend) == 'string' then
|
||||
local protocol = backend
|
||||
backend = img._backend[protocol]
|
||||
assert(backend, 'unsupported backend: ' .. protocol)
|
||||
end
|
||||
|
||||
---@cast backend vim.img.Backend
|
||||
backend.render(image, {
|
||||
pos = opts.pos,
|
||||
size = opts.size,
|
||||
crop = opts.crop,
|
||||
})
|
||||
end
|
||||
|
||||
return img
|
12
runtime/lua/vim/img/_backend.lua
Normal file
12
runtime/lua/vim/img/_backend.lua
Normal file
@ -0,0 +1,12 @@
|
||||
---@class vim.img.Backend
|
||||
---@field render fun(image:vim.img.Image, opts?:vim.img.Backend.RenderOpts)
|
||||
|
||||
---@class vim.img.Backend.RenderOpts
|
||||
---@field crop? {x:integer, y:integer, width:integer, height:integer} units are pixels
|
||||
---@field pos? {row:integer, col:integer} units are cells
|
||||
---@field size? {width:integer, height:integer} units are cells
|
||||
|
||||
return vim._defer_require('vim.img._backend', {
|
||||
iterm2 = ..., --- @module 'vim.img._backend.iterm2'
|
||||
kitty = ..., --- @module 'vim.img._backend.kitty'
|
||||
})
|
100
runtime/lua/vim/img/_backend/iterm2.lua
Normal file
100
runtime/lua/vim/img/_backend/iterm2.lua
Normal file
@ -0,0 +1,100 @@
|
||||
---@class vim.img.Iterm2Backend: vim.img.Backend
|
||||
local M = {}
|
||||
|
||||
---@param data string
|
||||
local function write_seq(data)
|
||||
local terminal = require('vim.img._terminal')
|
||||
|
||||
terminal.write(terminal.code.ESC) -- Begin sequence
|
||||
terminal.write(']1337;')
|
||||
|
||||
terminal.write(data) -- Write primary message
|
||||
|
||||
terminal.write(terminal.code.BEL) -- End sequence
|
||||
end
|
||||
|
||||
---@param image vim.img.Image
|
||||
---@param args table<string, string>
|
||||
local function write_multipart_image(image, args)
|
||||
-- Begin the transfer of the image file
|
||||
write_seq('MultipartFile=' .. table.concat(args, ';'))
|
||||
|
||||
-- Begin sending parts as chunks
|
||||
image:for_each_chunk(function(chunk)
|
||||
write_seq('FilePart=' .. chunk)
|
||||
end)
|
||||
|
||||
-- Conclude the image display
|
||||
write_seq('FileEnd')
|
||||
end
|
||||
|
||||
---@param image vim.img.Image
|
||||
---@param args table<string, string>
|
||||
local function write_image(image, args)
|
||||
local data = image.data
|
||||
if not data then
|
||||
return
|
||||
end
|
||||
|
||||
write_seq('File=' .. table.concat(args, ';') .. ':' .. data)
|
||||
end
|
||||
|
||||
---@param image vim.img.Image
|
||||
---@param opts? vim.img.Backend.RenderOpts
|
||||
function M.render(image, opts)
|
||||
local terminal = require('vim.img._terminal')
|
||||
|
||||
if not image:is_loaded() then
|
||||
return
|
||||
end
|
||||
|
||||
opts = opts or {}
|
||||
if opts.pos then
|
||||
terminal.cursor.move(opts.pos.col, opts.pos.row, true)
|
||||
end
|
||||
|
||||
local args = {
|
||||
-- NOTE: We MUST mark as inline otherwise not rendered and put in a downloads folder
|
||||
'inline=1',
|
||||
|
||||
-- This will show a progress indicator for a multipart image
|
||||
'size=' .. tostring(image:size()),
|
||||
}
|
||||
|
||||
-- Specify the name of the image, which iterm2 requires to be base64 encoded
|
||||
if image.name then
|
||||
table.insert(args, 'name=' .. vim.base64.encode(image.name))
|
||||
end
|
||||
|
||||
-- If a size is provided (in cells), we add it as arguments
|
||||
if opts.size then
|
||||
table.insert(args, 'width=' .. tostring(opts.size.width))
|
||||
table.insert(args, 'height=' .. tostring(opts.size.height))
|
||||
|
||||
-- We need to disable aspect ratio preservation, otherwise
|
||||
-- the desired width/height won't be respected
|
||||
table.insert(args, 'preserveAspectRatio=0')
|
||||
end
|
||||
|
||||
-- Only iTerm2 3.5+ supports multipart images
|
||||
--
|
||||
-- WezTerm and others are assumed to NOT support multipart images
|
||||
--
|
||||
-- iTerm2 should have set TERM_PROGRAM and TERM_PROGRAM_VERSION,
|
||||
-- otherwise we assume a different terminal!
|
||||
---@type string|nil
|
||||
local prog = vim.env.TERM_PROGRAM
|
||||
---@type vim.Version|nil
|
||||
local version = vim.version.parse(vim.env.TERM_PROGRAM_VERSION or '')
|
||||
if prog == 'iTerm.app' and version and vim.version.ge(version, { 3, 5, 0 }) then
|
||||
write_multipart_image(image, args)
|
||||
else
|
||||
write_image(image, args)
|
||||
end
|
||||
|
||||
if opts.pos then
|
||||
terminal.cursor.restore()
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
108
runtime/lua/vim/img/_backend/kitty.lua
Normal file
108
runtime/lua/vim/img/_backend/kitty.lua
Normal file
@ -0,0 +1,108 @@
|
||||
---@class vim.img.KittyBackend: vim.img.Backend
|
||||
local M = {}
|
||||
|
||||
---For kitty, we need to write an image in chunks
|
||||
---
|
||||
---Graphics codes are in this form:
|
||||
---
|
||||
--- <ESC>_G<control data>;<payload><ESC>\
|
||||
---
|
||||
---To stream data for a PNG, we specify the format `f=100`.
|
||||
---
|
||||
---To simultaneously transmit and display an image, we use `a=T`.
|
||||
---
|
||||
---Chunking data (such as from over a network) requires the
|
||||
---specification of `m=0|1`, where all chunks must have a
|
||||
---value of `1` except the very last chunk.
|
||||
---@param data string
|
||||
local function write_seq(data)
|
||||
local terminal = require('vim.img._terminal')
|
||||
|
||||
terminal.write(terminal.code.ESC .. '_G') -- Begin sequence
|
||||
terminal.write(data) -- Primary data
|
||||
terminal.write(terminal.code.ESC .. '\\') -- End sequence
|
||||
end
|
||||
|
||||
---Builds a header table of key value pairs.
|
||||
---@param opts vim.img.Backend.RenderOpts
|
||||
---@return table<string, string>
|
||||
local function make_header(opts)
|
||||
---@type table<string, string>
|
||||
local header = {}
|
||||
|
||||
header['a'] = 'T'
|
||||
header['f'] = '100'
|
||||
|
||||
local crop = opts.crop
|
||||
local size = opts.size
|
||||
|
||||
if crop then
|
||||
header['x'] = tostring(crop.x)
|
||||
header['y'] = tostring(crop.y)
|
||||
header['w'] = tostring(crop.width)
|
||||
header['h'] = tostring(crop.height)
|
||||
end
|
||||
|
||||
if size then
|
||||
header['c'] = tostring(size.width)
|
||||
header['r'] = tostring(size.height)
|
||||
end
|
||||
|
||||
return header
|
||||
end
|
||||
|
||||
---@param image vim.img.Image
|
||||
---@param opts vim.img.Backend.RenderOpts
|
||||
local function write_multipart_image(image, opts)
|
||||
image:for_each_chunk(function(chunk, pos, has_more)
|
||||
local data = {}
|
||||
|
||||
-- If at the beginning of our image, mark as a PNG to be
|
||||
-- transmitted and displayed immediately
|
||||
if pos == 1 then
|
||||
-- Add an entry in our data to write out to the terminal
|
||||
-- that is "k=v," for the key-value entries from the header
|
||||
for key, value in pairs(make_header(opts)) do
|
||||
table.insert(data, key .. '=' .. value .. ',')
|
||||
end
|
||||
end
|
||||
|
||||
-- If we are still sending chunks and not at the end
|
||||
if has_more then
|
||||
table.insert(data, 'm=1')
|
||||
else
|
||||
table.insert(data, 'm=0')
|
||||
end
|
||||
|
||||
-- If we have a chunk available, write it
|
||||
if string.len(chunk) > 0 then
|
||||
table.insert(data, ';')
|
||||
table.insert(data, chunk)
|
||||
end
|
||||
|
||||
write_seq(table.concat(data))
|
||||
end)
|
||||
end
|
||||
|
||||
---@param image vim.img.Image
|
||||
---@param opts? vim.img.Backend.RenderOpts
|
||||
function M.render(image, opts)
|
||||
local terminal = require('vim.img._terminal')
|
||||
|
||||
if not image:is_loaded() then
|
||||
return
|
||||
end
|
||||
|
||||
opts = opts or {}
|
||||
if opts.pos then
|
||||
terminal.cursor.move(opts.pos.col, opts.pos.row, true)
|
||||
end
|
||||
|
||||
write_multipart_image(image, opts)
|
||||
|
||||
if opts.pos then
|
||||
terminal.cursor.restore()
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
88
runtime/lua/vim/img/_detect.lua
Normal file
88
runtime/lua/vim/img/_detect.lua
Normal file
@ -0,0 +1,88 @@
|
||||
local terminal = require('vim.img._terminal')
|
||||
|
||||
local TERM_QUERY = {
|
||||
-- Request device attributes (DA2).
|
||||
--
|
||||
-- It typically returns information about the terminal type and supported features.
|
||||
--
|
||||
-- Response format is typically something like '\033[>...;...;...c'
|
||||
DEVICE_ATTRIBUTES = terminal.code.ESC .. '[>q',
|
||||
|
||||
-- Request device status report (DSR), checking if terminal is okay.
|
||||
--
|
||||
-- Response indicates its current state.
|
||||
DEVICE_STATUS_REPORT = terminal.code.ESC .. '[5n',
|
||||
}
|
||||
|
||||
local TERM_RESPONSE = {
|
||||
-- Indicates that the terminal is functioning normally (no error).
|
||||
--
|
||||
-- 0 means 'OK'; other values indicate different states or errors.
|
||||
OK = terminal.code.ESC .. '[0n',
|
||||
}
|
||||
|
||||
---Detects supported graphics of the terminal.
|
||||
---@return {graphics:'iterm2'|'kitty'|'sixel'|nil, tmux:boolean, broken_sixel_cursor_placement:boolean}
|
||||
return function()
|
||||
local results = { graphics = nil, tmux = false, broken_sixel_cursor_placement = false }
|
||||
|
||||
local term = os.getenv('TERM')
|
||||
if term == 'xterm-kitty' or term == 'xterm-ghostty' or term == 'ghostty' then
|
||||
results.graphics = 'kitty'
|
||||
end
|
||||
|
||||
local term_program = os.getenv('TERM_PROGRAM')
|
||||
if term_program == 'vscode' then
|
||||
results.graphics = 'iterm2'
|
||||
results.broken_sixel_cursor_placement = true
|
||||
end
|
||||
|
||||
local _, err = terminal.query({
|
||||
query = table.concat({
|
||||
TERM_QUERY.DEVICE_ATTRIBUTES,
|
||||
TERM_QUERY.DEVICE_STATUS_REPORT,
|
||||
}),
|
||||
handler = function(buffer)
|
||||
local function has(s)
|
||||
return string.find(buffer, s, 1, true) ~= nil
|
||||
end
|
||||
|
||||
if has('iTerm2') or has('Konsole 2') then
|
||||
results.graphics = 'iterm2'
|
||||
end
|
||||
|
||||
if has('WezTerm') then
|
||||
results.graphics = 'iterm2'
|
||||
results.broken_sixel_cursor_placement = true
|
||||
end
|
||||
|
||||
if has('kitty') or has('ghostty') then
|
||||
results.graphics = 'kitty'
|
||||
end
|
||||
|
||||
if has('mlterm') then
|
||||
results.graphics = 'sixel'
|
||||
end
|
||||
|
||||
if has('XTerm') or has('foot') then
|
||||
results.graphics = 'sixel'
|
||||
results.broken_sixel_cursor_placement = true
|
||||
end
|
||||
|
||||
if has('tmux') then
|
||||
results.tmux = true
|
||||
end
|
||||
|
||||
-- Check if we have received the ok terminal response
|
||||
local start = string.find(buffer, TERM_RESPONSE.OK, 1, true)
|
||||
if start then
|
||||
return string.sub(buffer, start)
|
||||
end
|
||||
end,
|
||||
timeout = 250,
|
||||
})
|
||||
|
||||
assert(not err, err)
|
||||
|
||||
return results
|
||||
end
|
144
runtime/lua/vim/img/_image.lua
Normal file
144
runtime/lua/vim/img/_image.lua
Normal file
@ -0,0 +1,144 @@
|
||||
---@class vim.img.Image
|
||||
---@field name string|nil name of the image if loaded from disk
|
||||
---@field data string|nil base64 encoded data
|
||||
local M = {}
|
||||
M.__index = M
|
||||
|
||||
---Creates a new image instance.
|
||||
---@param opts? {data?:string, filename?:string}
|
||||
---@return vim.img.Image
|
||||
function M:new(opts)
|
||||
opts = opts or {}
|
||||
|
||||
local instance = {}
|
||||
setmetatable(instance, M)
|
||||
|
||||
instance.data = opts.data
|
||||
if not instance.data and opts.filename then
|
||||
instance:load_from_file(opts.filename)
|
||||
end
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
---Returns true if the image is loaded into memory.
|
||||
---@return boolean
|
||||
function M:is_loaded()
|
||||
return self.data ~= nil
|
||||
end
|
||||
|
||||
---Returns the size of the base64 encoded image.
|
||||
---@return integer
|
||||
function M:size()
|
||||
return string.len(self.data or '')
|
||||
end
|
||||
|
||||
---Iterates over the chunks of the image, invoking `f` per chunk.
|
||||
---@param f fun(chunk:string, pos:integer, has_more:boolean)
|
||||
---@param opts? {size?:integer}
|
||||
function M:for_each_chunk(f, opts)
|
||||
opts = opts or {}
|
||||
|
||||
-- Chunk size, defaulting to 4k
|
||||
local chunk_size = opts.size or 4096
|
||||
|
||||
local data = self.data
|
||||
if not data then
|
||||
return
|
||||
end
|
||||
|
||||
local pos = 1
|
||||
local len = string.len(data)
|
||||
while pos <= len do
|
||||
-- Get our next chunk from [pos, pos + chunk_size)
|
||||
local end_pos = pos + chunk_size - 1
|
||||
local chunk = data:sub(pos, end_pos)
|
||||
|
||||
-- If we have a chunk available, invoke our callback
|
||||
if string.len(chunk) > 0 then
|
||||
local has_more = end_pos + 1 <= len
|
||||
pcall(f, chunk, pos, has_more)
|
||||
end
|
||||
|
||||
pos = end_pos + 1
|
||||
end
|
||||
end
|
||||
|
||||
---Displays the image within the terminal used by neovim.
|
||||
---@param opts? vim.img.Opts
|
||||
function M:show(opts)
|
||||
vim.img.show(self, opts)
|
||||
end
|
||||
|
||||
---Loads data for an image from a file, replacing any existing data.
|
||||
---If a callback provided, will load asynchronously; otherwise, is blocking.
|
||||
---@param filename string
|
||||
---@param cb fun(err:string|nil, image:vim.img.Image|nil)
|
||||
---@overload fun(filename:string):vim.img.Image
|
||||
function M:load_from_file(filename, cb)
|
||||
local name = vim.fn.fnamemodify(filename, ':t:r')
|
||||
|
||||
if not cb then
|
||||
local stat = vim.uv.fs_stat(filename)
|
||||
assert(stat, 'unable to stat ' .. filename)
|
||||
|
||||
local fd = vim.uv.fs_open(filename, 'r', 644) --[[ @type integer|nil ]]
|
||||
assert(fd, 'unable to open ' .. filename)
|
||||
|
||||
local data = vim.uv.fs_read(fd, stat.size, -1) --[[ @type string|nil ]]
|
||||
assert(data, 'unable to read ' .. filename)
|
||||
|
||||
self.name = name
|
||||
self.data = vim.base64.encode(data)
|
||||
return self
|
||||
end
|
||||
|
||||
---@param err string|nil
|
||||
---@return boolean
|
||||
local function report_err(err)
|
||||
if err then
|
||||
vim.schedule(function()
|
||||
cb(err)
|
||||
end)
|
||||
end
|
||||
|
||||
return err ~= nil
|
||||
end
|
||||
|
||||
vim.uv.fs_stat(filename, function(stat_err, stat)
|
||||
if report_err(stat_err) then
|
||||
return
|
||||
end
|
||||
if not stat then
|
||||
report_err('missing stat')
|
||||
return
|
||||
end
|
||||
|
||||
vim.uv.fs_open(filename, 'r', 644, function(open_err, fd)
|
||||
if report_err(open_err) then
|
||||
return
|
||||
end
|
||||
if not fd then
|
||||
report_err('missing fd')
|
||||
return
|
||||
end
|
||||
|
||||
vim.uv.fs_read(fd, stat.size, -1, function(read_err, data)
|
||||
if report_err(read_err) then
|
||||
return
|
||||
end
|
||||
|
||||
vim.uv.fs_close(fd, function() end)
|
||||
|
||||
self.name = name
|
||||
self.data = vim.base64.encode(data or '')
|
||||
|
||||
vim.schedule(function()
|
||||
cb(nil, self)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
return M
|
140
runtime/lua/vim/img/_terminal.lua
Normal file
140
runtime/lua/vim/img/_terminal.lua
Normal file
@ -0,0 +1,140 @@
|
||||
---@class vim.img.terminal
|
||||
---@field private __tty_name string
|
||||
local M = {}
|
||||
|
||||
local TERM_CODE = {
|
||||
BEL = '\x07', -- aka ^G
|
||||
ESC = '\x1B', -- aka ^[ aka \033
|
||||
}
|
||||
|
||||
---Retrieve the tty name used by the editor.
|
||||
---
|
||||
---E.g. /dev/ttys008
|
||||
---@return string|nil
|
||||
local function get_tty_name()
|
||||
if vim.fn.has('win32') == 1 then
|
||||
-- On windows, we use \\.\CON for reading and writing
|
||||
return '\\\\.\\CON'
|
||||
else
|
||||
-- Linux/Mac: Use `tty` command, which reads the terminal name
|
||||
-- in the form of something like /dev/ttys008
|
||||
local handle = io.popen('tty 2>/dev/null')
|
||||
if not handle then
|
||||
return nil
|
||||
end
|
||||
local result = handle:read('*a')
|
||||
handle:close()
|
||||
result = vim.fn.trim(result)
|
||||
if result == '' then
|
||||
return nil
|
||||
end
|
||||
return result
|
||||
end
|
||||
end
|
||||
|
||||
---Returns the name of the tty associated with the terminal.
|
||||
---@return string
|
||||
function M.tty_name()
|
||||
if not M.__tty_name then
|
||||
M.__tty_name = assert(get_tty_name(), 'failed to read editor tty name')
|
||||
end
|
||||
|
||||
return M.__tty_name
|
||||
end
|
||||
|
||||
---Writes data to the editor tty.
|
||||
---@param ... string|number
|
||||
function M.write(...)
|
||||
local handle = assert(io.open(M.tty_name(), 'w'))
|
||||
handle:write(...)
|
||||
handle:close()
|
||||
end
|
||||
|
||||
---@class vim.img.terminal.cursor
|
||||
M.cursor = {}
|
||||
|
||||
---@param x integer
|
||||
---@param y integer
|
||||
---@param save? boolean
|
||||
function M.cursor.move(x, y, save)
|
||||
if save then
|
||||
M.cursor.save()
|
||||
end
|
||||
M.write(TERM_CODE.ESC .. '[' .. y .. ';' .. x .. 'H')
|
||||
vim.uv.sleep(1)
|
||||
end
|
||||
|
||||
function M.cursor.save()
|
||||
M.write(TERM_CODE.ESC .. '[s')
|
||||
end
|
||||
|
||||
function M.cursor.restore()
|
||||
M.write(TERM_CODE.ESC .. '[u')
|
||||
end
|
||||
|
||||
---Terminal escape codes.
|
||||
M.code = TERM_CODE
|
||||
|
||||
---@param opts {query:string, handler:(fun(buffer:string):string|nil), timeout?:integer}
|
||||
---@return string|nil result, string|nil err
|
||||
function M.query(opts)
|
||||
local uv = vim.uv
|
||||
|
||||
opts = opts or {}
|
||||
local query = opts.query
|
||||
local handler = opts.handler
|
||||
local timeout = opts.timeout or 250
|
||||
|
||||
local tty_fd, err
|
||||
local function cleanup()
|
||||
if tty_fd then
|
||||
uv.fs_close(tty_fd)
|
||||
tty_fd = nil
|
||||
end
|
||||
end
|
||||
|
||||
-- Identify the path to the editor's tty
|
||||
-- NOTE: This only works on Unix-like systems!
|
||||
local ok, tty_path = pcall(M.tty_name)
|
||||
if not ok then
|
||||
return nil, tty_path
|
||||
end
|
||||
|
||||
-- Open the tty so we can write our query
|
||||
tty_fd, err = uv.fs_open(tty_path, 'r+', 438)
|
||||
if not tty_fd then
|
||||
return nil, err
|
||||
end
|
||||
|
||||
-- Write query to terminal.
|
||||
local success, write_err = uv.fs_write(tty_fd, query, -1)
|
||||
if not success then
|
||||
cleanup()
|
||||
return nil, write_err
|
||||
end
|
||||
|
||||
-- Read response with timeout.
|
||||
local buffer = ''
|
||||
local start_time = uv.now()
|
||||
|
||||
while uv.now() - start_time < timeout do
|
||||
local data, read_err = uv.fs_read(tty_fd, 512, -1)
|
||||
if data then
|
||||
buffer = buffer .. data
|
||||
local result = handler(buffer)
|
||||
if result then
|
||||
cleanup()
|
||||
return result
|
||||
end
|
||||
elseif read_err ~= 'EAGAIN' then
|
||||
cleanup()
|
||||
return nil, read_err
|
||||
end
|
||||
uv.sleep(1)
|
||||
end
|
||||
|
||||
cleanup()
|
||||
return nil, 'Timeout'
|
||||
end
|
||||
|
||||
return M
|
@ -211,7 +211,7 @@ local function reuse_client_default(client, config)
|
||||
|
||||
for _, config_folder in ipairs(config_folders) do
|
||||
local found = false
|
||||
for _, client_folder in ipairs(client.workspace_folders) do
|
||||
for _, client_folder in ipairs(client.workspace_folders or {}) do
|
||||
if config_folder.uri == client_folder.uri then
|
||||
found = true
|
||||
break
|
||||
|
@ -174,6 +174,10 @@ local validate = vim.validate
|
||||
--- capabilities.
|
||||
--- @field server_capabilities lsp.ServerCapabilities?
|
||||
---
|
||||
--- Response from the server sent on `initialize` describing information about
|
||||
--- the server.
|
||||
--- @field server_info lsp.ServerInfo?
|
||||
---
|
||||
--- A ring buffer (|vim.ringbuf()|) containing progress messages
|
||||
--- sent by the server.
|
||||
--- @field progress vim.lsp.Client.Progress
|
||||
@ -556,6 +560,8 @@ function Client:initialize()
|
||||
self.offset_encoding = self.server_capabilities.positionEncoding
|
||||
end
|
||||
|
||||
self.server_info = result.serverInfo
|
||||
|
||||
if next(self.settings) then
|
||||
self:notify(ms.workspace_didChangeConfiguration, { settings = self.settings })
|
||||
end
|
||||
|
@ -40,6 +40,8 @@ local function check_active_clients()
|
||||
local clients = vim.lsp.get_clients()
|
||||
if next(clients) then
|
||||
for _, client in pairs(clients) do
|
||||
local server_version = vim.tbl_get(client, 'server_info', 'version')
|
||||
or '? (no serverInfo.version response)'
|
||||
local cmd ---@type string
|
||||
local ccmd = client.config.cmd
|
||||
if type(ccmd) == 'table' then
|
||||
@ -62,6 +64,7 @@ local function check_active_clients()
|
||||
end
|
||||
report_info(table.concat({
|
||||
string.format('%s (id: %d)', client.name, client.id),
|
||||
string.format('- Version: %s', server_version),
|
||||
dirs_info,
|
||||
string.format('- Command: %s', cmd),
|
||||
string.format('- Settings: %s', vim.inspect(client.settings, { newline = '\n ' })),
|
||||
|
@ -8,9 +8,9 @@ vim.api.nvim_create_user_command('Man', function(params)
|
||||
if params.bang then
|
||||
man.init_pager()
|
||||
else
|
||||
local ok, err = pcall(man.open_page, params.count, params.smods, params.fargs)
|
||||
if not ok then
|
||||
vim.notify(man.errormsg or err, vim.log.levels.ERROR)
|
||||
local err = man.open_page(params.count, params.smods, params.fargs)
|
||||
if err then
|
||||
vim.notify('man.lua: ' .. err, vim.log.levels.ERROR)
|
||||
end
|
||||
end
|
||||
end, {
|
||||
@ -31,6 +31,9 @@ vim.api.nvim_create_autocmd('BufReadCmd', {
|
||||
pattern = 'man://*',
|
||||
nested = true,
|
||||
callback = function(params)
|
||||
require('man').read_page(vim.fn.matchstr(params.match, 'man://\\zs.*'))
|
||||
local err = require('man').read_page(assert(params.match:match('man://(.*)')))
|
||||
if err then
|
||||
vim.notify('man.lua: ' .. err, vim.log.levels.ERROR)
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
@ -229,10 +229,9 @@ static Object _call_function(String fn, Array args, dict_T *self, Arena *arena,
|
||||
funcexe.fe_selfdict = self;
|
||||
|
||||
TRY_WRAP(err, {
|
||||
// call_func() retval is deceptive, ignore it. Instead we set `msg_list`
|
||||
// (see above) to capture abort-causing non-exception errors.
|
||||
call_func(fn.data, (int)fn.size, &rettv, (int)args.size,
|
||||
vim_args, &funcexe);
|
||||
// call_func() retval is deceptive, ignore it. Instead TRY_WRAP sets `msg_list` to capture
|
||||
// abort-causing non-exception errors.
|
||||
(void)call_func(fn.data, (int)fn.size, &rettv, (int)args.size, vim_args, &funcexe);
|
||||
});
|
||||
|
||||
if (!ERROR_SET(err)) {
|
||||
@ -280,18 +279,23 @@ Object nvim_call_dict_function(Object dict, String fn, Array args, Arena *arena,
|
||||
typval_T rettv;
|
||||
bool mustfree = false;
|
||||
switch (dict.type) {
|
||||
case kObjectTypeString:
|
||||
case kObjectTypeString: {
|
||||
int eval_ret;
|
||||
TRY_WRAP(err, {
|
||||
eval0(dict.data.string.data, &rettv, NULL, &EVALARG_EVALUATE);
|
||||
clear_evalarg(&EVALARG_EVALUATE, NULL);
|
||||
});
|
||||
eval_ret = eval0(dict.data.string.data, &rettv, NULL, &EVALARG_EVALUATE);
|
||||
clear_evalarg(&EVALARG_EVALUATE, NULL);
|
||||
});
|
||||
if (ERROR_SET(err)) {
|
||||
return rv;
|
||||
}
|
||||
if (eval_ret != OK) {
|
||||
abort(); // Should not happen.
|
||||
}
|
||||
// Evaluation of the string arg created a new dict or increased the
|
||||
// refcount of a dict. Not necessary for a RPC dict.
|
||||
mustfree = true;
|
||||
break;
|
||||
}
|
||||
case kObjectTypeDict:
|
||||
object_to_vim(dict, &rettv, err);
|
||||
break;
|
||||
|
@ -1854,6 +1854,20 @@ describe('LSP', function()
|
||||
end,
|
||||
}
|
||||
end)
|
||||
|
||||
it('vim.lsp.start when existing client has no workspace_folders', function()
|
||||
exec_lua(create_server_definition)
|
||||
eq(
|
||||
{ 2, 'foo', 'foo' },
|
||||
exec_lua(function()
|
||||
local server = _G._create_server()
|
||||
vim.lsp.start { cmd = server.cmd, name = 'foo' }
|
||||
vim.lsp.start { cmd = server.cmd, name = 'foo', root_dir = 'bar' }
|
||||
local foos = vim.lsp.get_clients()
|
||||
return { #foos, foos[1].name, foos[2].name }
|
||||
end)
|
||||
)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('parsing tests', function()
|
||||
|
@ -21,13 +21,12 @@ local function get_search_history(name)
|
||||
local man = require('man')
|
||||
local res = {}
|
||||
--- @diagnostic disable-next-line:duplicate-set-field
|
||||
man.find_path = function(sect, name0)
|
||||
man._find_path = function(name0, sect)
|
||||
table.insert(res, { sect, name0 })
|
||||
return nil
|
||||
end
|
||||
local ok, rv = pcall(man.open_page, -1, { tab = 0 }, args)
|
||||
assert(not ok)
|
||||
assert(rv and rv:match('no manual entry'))
|
||||
local err = man.open_page(-1, { tab = 0 }, args)
|
||||
assert(err and err:match('no manual entry'))
|
||||
return res
|
||||
end)
|
||||
end
|
||||
@ -225,7 +224,7 @@ describe(':Man', function()
|
||||
matches('^/.+', actual_file)
|
||||
local args = { nvim_prog, '--headless', '+:Man ' .. actual_file, '+q' }
|
||||
matches(
|
||||
('Error detected while processing command line:\r\n' .. 'man.lua: "no manual entry for %s"'):format(
|
||||
('Error detected while processing command line:\r\n' .. 'man.lua: no manual entry for %s'):format(
|
||||
pesc(actual_file)
|
||||
),
|
||||
fn.system(args, { '' })
|
||||
@ -235,8 +234,8 @@ describe(':Man', function()
|
||||
|
||||
it('tries variants with spaces, underscores #22503', function()
|
||||
eq({
|
||||
{ '', 'NAME WITH SPACES' },
|
||||
{ '', 'NAME_WITH_SPACES' },
|
||||
{ vim.NIL, 'NAME WITH SPACES' },
|
||||
{ vim.NIL, 'NAME_WITH_SPACES' },
|
||||
}, get_search_history('NAME WITH SPACES'))
|
||||
eq({
|
||||
{ '3', 'some other man' },
|
||||
@ -255,8 +254,8 @@ describe(':Man', function()
|
||||
{ 'n', 'some_other_man' },
|
||||
}, get_search_history('n some other man'))
|
||||
eq({
|
||||
{ '', '123some other man' },
|
||||
{ '', '123some_other_man' },
|
||||
{ vim.NIL, '123some other man' },
|
||||
{ vim.NIL, '123some_other_man' },
|
||||
}, get_search_history('123some other man'))
|
||||
eq({
|
||||
{ '1', 'other_man' },
|
||||
@ -265,11 +264,10 @@ describe(':Man', function()
|
||||
end)
|
||||
|
||||
it('can complete', function()
|
||||
t.skip(t.is_os('mac') and t.is_arch('x86_64'), 'not supported on intel mac')
|
||||
eq(
|
||||
true,
|
||||
exec_lua(function()
|
||||
return #require('man').man_complete('f', 'Man g') > 0
|
||||
return #require('man').man_complete('f', 'Man f') > 0
|
||||
end)
|
||||
)
|
||||
end)
|
||||
|
Loading…
Reference in New Issue
Block a user