Skip to content

Commit

Permalink
feat: slightly better highlighting in inspection window
Browse files Browse the repository at this point in the history
  • Loading branch information
lkhphuc committed Mar 21, 2023
1 parent af12898 commit 074fe5d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
47 changes: 28 additions & 19 deletions lua/jupyter_kernel/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,36 @@ function M.inspect()
elseif inspect.found ~= true then
out = "_No information from kernel_"
else
-- 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)
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

vim.lsp.util.open_floating_preview(lines, "markdown", M.opts.inspect.window)
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", M.opts.inspect.window)
end

local default_config = {
Expand Down
19 changes: 8 additions & 11 deletions rplugin/python3/jupyter_kernel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,13 @@ def inspect(self, args):
try:
line_content = self.vim.current.line
row, col = self.vim.current.window.cursor
reply = self.client.inspect(
line_content,
col,
detail_level=0,
reply=True, # type:ignore
timeout=timeout) # type:ignore
# self.vim.out_write("Jupyter kernel inspect reply: {}\n".format(
# reply['content']))
reply = self.client.inspect(line_content,
col,
detail_level=0,
reply=True,
timeout=timeout)
return reply['content']
except TimeoutError:
return {'status': "Kernel timeout"}
except Exception as e:
return {'status': str(e)}
return {'status': "_Kernel timeout_"}
except Exception as exception:
return {'status': f"_{str(exception)}_"}

0 comments on commit 074fe5d

Please sign in to comment.