Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hover): better highlighting #70

Merged
merged 1 commit into from
Mar 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 36 additions & 21 deletions src/jupynium/lua/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -611,27 +611,42 @@ function Jupynium_kernel_hover(bufnr)
elseif inspect.found == false then
out = "No information from kernel"
elseif inspect.found == true then
-- Strip ANSI Escape code: https://stackoverflow.com/a/55324681
-- The above regexes do the following:
-- 1. \x1b is the escape character
-- 2. %[%d+; is the ANSI escape code for a digit color
-- and so on
out = inspect.data["text/plain"]
:gsub("\x1b%[%d+;%d+;%d+;%d+;%d+m", "")
:gsub("\x1b%[%d+;%d+;%d+;%d+m", "")
:gsub("\x1b%[%d+;%d+;%d+m", "")
:gsub("\x1b%[%d+;%d+m", "")
:gsub("\x1b%[%d+m", "")
-- The following regex convert ansi code for tab
-- out = out:gsub("\x1b%[H", "\t")
end

local lines = {}
for line in vim.gsplit(out, "\n") do
table.insert(lines, line)
end

vim.lsp.util.open_floating_preview(lines, "markdown", {
local sections = vim.split(inspect.data["text/plain"], "\x1b%[0;31m")
for _, section in ipairs(sections) do
section = section
-- Strip ANSI Escape code: https://stackoverflow.com/a/55324681
-- \x1b is the escape character
-- %[%d+; is the ANSI escape code for a digit color
:gsub(
"\x1b%[%d+;%d+;%d+;%d+;%d+m",
""
)
:gsub("\x1b%[%d+;%d+;%d+;%d+m", "")
:gsub("\x1b%[%d+;%d+;%d+m", "")
:gsub("\x1b%[%d+;%d+m", "")
:gsub("\x1b%[%d+m", "")
:gsub("\x1b%[H", "\t")
-- Groups: name, 0 or more new line, content till end
-- TODO: Fix for non-python kernel
:gsub(
"^(Init signature:)(\n*)(.-)$",
"%1\n```python\n%3```"
)
:gsub("^(Signature:)(\n*)(.-)$", "%1\n```python\n%3```")
:gsub("^(String form:)(\n*)(.-)$", "%1\n```python\n%3```")
:gsub("^(Docstring:)(\n*)(.-)$", "%1\n```rst \n%3```")
:gsub("^(Class docstring:)(\n*)(.-)$", "%1\n```rst \n%3```")
:gsub("^(.-):", "_%1_:") -- Surround header with "_" to italicize
if section:match "%S" ~= nil and section:match "%S" ~= "" then
-- Only add non-empty section
out = out .. section
end
end
end

local markdown_lines = vim.lsp.util.convert_input_to_markdown_lines(out)
markdown_lines = vim.lsp.util.trim_empty_lines(markdown_lines)
vim.lsp.util.open_floating_preview(markdown_lines, "markdown", {
max_width = 84,
})
end
Expand Down