143 lines
4.6 KiB
Lua

-- TODO
-- reuse existing split
-- use fqcn as buffer name
-- add function to convert to fqcn, with telescope dropdown if there are multiple options
-- telescope window to search through all available module names, enter inserts, other key opens doc in split
local ts_utils = require("nvim-treesitter.ts_utils")
local M = {}
M.example = function()
local lines = {}
-- remove all whitespace, we will create new blocks separated by empty lines after
local last_search = vim.fn.getreg('/')
vim.api.nvim_command('g/^$/d')
vim.fn.setreg('/', last_search)
local current_line_nr = 1
local buf_handle = vim.api.nvim_win_get_buf(0)
local last_line_nr = vim.api.nvim_buf_line_count(buf_handle)
while (current_line_nr <= last_line_nr) do
-- skip empty lines
if vim.fn.getline(current_line_nr) == '' then
current_line_nr = current_line_nr + 1
else
vim.api.nvim_win_set_cursor(0, {current_line_nr, 1})
local node = ts_utils.get_node_at_cursor(0)
local parent = node:parent()
while (parent ~= nil and node:type() ~= 'block_mapping_pair') do
node = parent
parent = node:parent()
end
local start_row, start_column, end_row, end_column = node:range()
lines[current_line_nr] = vim.fn.getline(start_row + 1, end_row + 1)
current_line_nr = current_line_nr + 1 + (end_row - start_row)
end
end
-- sort does not work if the index is not a continuous integer, so lets make sure it is. if we are sure that we dont need the original line number, we can do this from the start in `lines`
a = {}
for i,v in pairs(lines) do table.insert(a, v) end
table.sort(
a,
function(k1, k2)
return k1[1] < k2[1]
end
)
insert_row = 1
last_prefix = vim.split(a[1][1], '__')[1]
for _, lines in ipairs(a) do
current_prefix = vim.split(lines[1], '__')[1]
if current_prefix ~= last_prefix then
vim.fn.setline(insert_row, '')
insert_row = insert_row + 1
end
last_prefix = current_prefix
for i, line in ipairs(lines) do
vim.fn.setline(insert_row, line)
insert_row = insert_row + 1
end
end
end
M.display_ansible_doc = function(keyword)
local Job = require'plenary.job'
-- local keyword = 'get_url'
local doc = {}
Job:new({
command = 'ansible-doc',
args = { keyword },
on_exit = function(j, return_val)
-- print(return_val)
-- print(vim.inspect(j:result()))
doc = j:result()
end,
}):sync() -- or start()
-- TODO unlisted / scratch buffer?
local fqcn = string.lower(vim.split(doc[1], ' ')[2])
vim.api.nvim_command('vsplit ' .. fqcn)
local win_handle = vim.api.nvim_tabpage_get_win(0)
local buf_handle = vim.api.nvim_win_get_buf(0)
vim.api.nvim_buf_set_lines(buf_handle, 0, -1, false, doc)
vim.api.nvim_buf_set_option(buf_handle, 'readonly', true)
vim.api.nvim_buf_set_option(buf_handle, 'modifiable', false)
-- Now we set some options for our buffer.
-- nofile prevent mark buffer as modified so we never get warnings about not saved changes.
-- Also some plugins treat nofile buffers different.
-- For example coc.nvim don't triggers aoutcompletation for these.
vim.api.nvim_buf_set_option(buf_handle, 'buftype', 'nofile')
-- We do not need swapfile for this buffer.
vim.api.nvim_buf_set_option(buf_handle, 'swapfile', false)
-- And we would rather prefer that this buffer will be destroyed when hide.
vim.api.nvim_buf_set_option(buf_handle, 'bufhidden', 'wipe')
-- It's not necessary but it is good practice to set custom filetype.
-- This allows users to create their own autocommand or colorschemes on filetype.
-- and prevent collisions with other plugins.
vim.api.nvim_buf_set_option(buf_handle, 'filetype', 'ansible-doc')
-- vim.api.nvim_command('wincmd p') -- go back to previous window
end
M.replace_fqcn = function()
local Job = require'plenary.job'
-- keyword = 'get_url'
doc = {}
Job:new({
command = 'ansible-doc',
args = { keyword },
on_exit = function(j, return_val)
-- print(return_val)
-- print(vim.inspect(j:result()))
doc = j:result()
end,
}):sync() -- or start()
fqcn = string.lower(vim.split(doc[1], ' ')[2])
cur_pos = vim.api.nvim_win_get_cursor(0)
buf_handle = vim.api.nvim_win_get_buf(0)
vim.api.nvim_buf_set_lines(buf_handle, 0, -1, false, doc)
end
return M