diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..a9ec436 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*.lua] +charset = utf-8 +indent_style = space +indent_size = 2 +quote_style = single +trim_trailing_whitespace = true +insert_final_newline = true diff --git a/README.md b/README.md index f87cb2b..23b4d54 100644 --- a/README.md +++ b/README.md @@ -158,16 +158,16 @@ Call `require('hover').register()` with a table containing the followi ```lua -- Simple require('hover').register { - name = 'Simple', - --- @param bufnr integer - enabled = function(bufnr) - return true - end, - --- @param opts Hover.Options - --- @param done fun(result?: Hover.Result) - execute = function(opts, done) - done{lines={'TEST'}, filetype="markdown"} - end + name = 'Simple', + --- @param bufnr integer + enabled = function(bufnr) + return true + end, + --- @param opts Hover.Options + --- @param done fun(result?: Hover.Result) + execute = function(opts, done) + done { lines = { 'TEST' }, filetype = 'markdown' } + end } ``` diff --git a/lua/hover/providers.lua b/lua/hover/providers.lua index f2252b5..5c0232f 100644 --- a/lua/hover/providers.lua +++ b/lua/hover/providers.lua @@ -1,6 +1,6 @@ local async = require('hover.async') -local M = {} +local M = {} --- @class Hover.PartialOptions --- @field bufnr? integer @@ -54,7 +54,7 @@ function M.register(provider) end end end - providers[#providers+1] = provider + providers[#providers + 1] = provider end return M diff --git a/lua/hover/providers/dap.lua b/lua/hover/providers/dap.lua index 885acc3..f40b3fc 100644 --- a/lua/hover/providers/dap.lua +++ b/lua/hover/providers/dap.lua @@ -4,7 +4,7 @@ local ui = require('dap.ui') local widgets = require('dap.ui.widgets') local hover = require('hover') -local function find_window (buf) +local function find_window(buf) for _, win in ipairs(vim.api.nvim_list_wins()) do if vim.api.nvim_win_get_buf(win) == buf then return win @@ -47,15 +47,15 @@ end local function set_default_bufopts(buf) vim.bo[buf].modifiable = false - vim.bo[buf].buftype = "nofile" + vim.bo[buf].buftype = 'nofile' api.nvim_buf_set_keymap( - buf, "n", "", "lua require('dap.ui').trigger_actions({ mode = 'first' })", {}) + buf, 'n', '', "lua require('dap.ui').trigger_actions({ mode = 'first' })", {}) api.nvim_buf_set_keymap( - buf, "n", "a", "lua require('dap.ui').trigger_actions()", {}) + buf, 'n', 'a', "lua require('dap.ui').trigger_actions()", {}) api.nvim_buf_set_keymap( - buf, "n", "o", "lua require('dap.ui').trigger_actions()", {}) + buf, 'n', 'o', "lua require('dap.ui').trigger_actions()", {}) api.nvim_buf_set_keymap( - buf, "n", "<2-LeftMouse>", "lua require('dap.ui').trigger_actions()", {}) + buf, 'n', '<2-LeftMouse>', "lua require('dap.ui').trigger_actions()", {}) end local function new_buf() @@ -67,13 +67,13 @@ end hover.register { name = 'DAP', enabled = function(bufnr) - return dap.status() ~= "" + return dap.status() ~= '' end, execute = function(opts, done) local buf = new_buf() local layer = resizing_layer(buf) local fake_view = { - layer = function () + layer = function() return layer end, } diff --git a/lua/hover/providers/dictionary.lua b/lua/hover/providers/dictionary.lua index e7846f3..d36ffdc 100644 --- a/lua/hover/providers/dictionary.lua +++ b/lua/hover/providers/dictionary.lua @@ -22,10 +22,10 @@ local function process(result) } for _, def in ipairs(json.meanings[1].definitions) do - lines[#lines+1] = '' - lines[#lines+1] = def.definition + lines[#lines + 1] = '' + lines[#lines + 1] = def.definition if def.example then - lines[#lines+1] = 'Example: '..def.example + lines[#lines + 1] = 'Example: ' .. def.example end end @@ -44,14 +44,14 @@ local execute = async.void(function(opts, done) ---@type string[] local output = job { - 'curl', 'https://api.dictionaryapi.dev/api/v2/entries/en/'..word + 'curl', 'https://api.dictionaryapi.dev/api/v2/entries/en/' .. word } - local results = process(output) or {'no definition for '..word} + local results = process(output) or { 'no definition for ' .. word } cache[word] = results end - done({lines=cache[word], filetype="markdown"}) + done({ lines = cache[word], filetype = 'markdown' }) end) require('hover').register { diff --git a/lua/hover/providers/gh.lua b/lua/hover/providers/gh.lua index 04a0677..68a20df 100644 --- a/lua/hover/providers/gh.lua +++ b/lua/hover/providers/gh.lua @@ -10,14 +10,14 @@ end ---@param stderr string|nil local function process(result, stderr) if result == nil then - vim.notify(vim.trim(stderr or "(Unknown error)"), vim.log.levels.ERROR, { title = "hover.nvim (gh)" }) + vim.notify(vim.trim(stderr or '(Unknown error)'), vim.log.levels.ERROR, { title = 'hover.nvim (gh)' }) return end local ok, json = pcall(vim.json.decode, result) if not ok then async.scheduler() - vim.notify("Failed to parse gh result: " .. json, vim.log.levels.ERROR) + vim.notify('Failed to parse gh result: ' .. json, vim.log.levels.ERROR) return end @@ -33,7 +33,7 @@ local function process(result, stderr) } for _, l in ipairs(vim.split(json.body, '\r?\n')) do - lines[#lines+1] = l + lines[#lines + 1] = l end return lines @@ -75,7 +75,7 @@ local execute = async.void(function(opts, done) end local results = process(output, stderr) - done(results and {lines=results, filetype="markdown"}) + done(results and { lines = results, filetype = 'markdown' }) end) require('hover').register { diff --git a/lua/hover/providers/gh_user.lua b/lua/hover/providers/gh_user.lua index fdeeb8c..353a7fc 100644 --- a/lua/hover/providers/gh_user.lua +++ b/lua/hover/providers/gh_user.lua @@ -15,21 +15,21 @@ local function enabled() end local function first_to_upper(str) - return str:gsub("^%l", string.upper) + return str:gsub('^%l', string.upper) end ---@param result string|nil ---@param stderr string|nil local function process(result, stderr) if result == nil then - vim.notify(vim.trim(stderr or "(Unknown error)"), vim.log.levels.ERROR, { title = "hover.nvim (gh_user)" }) + vim.notify(vim.trim(stderr or '(Unknown error)'), vim.log.levels.ERROR, { title = 'hover.nvim (gh_user)' }) return end local ok, json = pcall(vim.json.decode, result) if not ok then async.scheduler() - vim.notify("Failed to parse gh user result" .. json, vim.log.levels.ERROR) + vim.notify('Failed to parse gh user result' .. json, vim.log.levels.ERROR) return end @@ -50,20 +50,20 @@ local function process(result, stderr) } do local field = json[key] if field and field ~= vim.NIL then - res[#res+1] = string.format('**%s**: `%s`', first_to_upper(key), field) + res[#res + 1] = string.format('**%s**: `%s`', first_to_upper(key), field) end end if json.bio and json.bio ~= vim.NIL then - res[#res+1] = '**Bio**:' + res[#res + 1] = '**Bio**:' for _, l in ipairs(vim.split(json.bio:gsub('\r', ''), '\n')) do - res[#res+1] = '> '..l + res[#res + 1] = '> ' .. l end end -- 404 (user does not exist) if #res == 0 and json.message and json.message ~= vim.NIL then - res[#res+1] = 'ERROR: ' .. json.message + res[#res + 1] = 'ERROR: ' .. json.message end return res @@ -82,10 +82,10 @@ local execute = async.void(function(opts, done) ---@type string local output, stderr - output, stderr = job { 'gh', 'api', 'users/'..user, cwd = cwd } + output, stderr = job { 'gh', 'api', 'users/' .. user, cwd = cwd } local results = process(output, stderr) - done(results and {lines=results, filetype="markdown"}) + done(results and { lines = results, filetype = 'markdown' }) end) require('hover').register { diff --git a/lua/hover/providers/jira.lua b/lua/hover/providers/jira.lua index 81fe279..328b4af 100644 --- a/lua/hover/providers/jira.lua +++ b/lua/hover/providers/jira.lua @@ -1,40 +1,39 @@ +-- Match 2 or more uppercase letters followed by a '-' and 1 or more digits. local ISSUE_PATTERN = '%u[%u%d]+-%d+' local function enabled() - -- Match 2 or more uppercase letters followed by a '-' and 1 or more digits. - return vim.fn.expand(''):match(ISSUE_PATTERN) ~= nil + return vim.fn.expand(''):match(ISSUE_PATTERN) ~= nil end --- @param opts Hover.Options --- @param done fun(result?: Hover.Result) local function execute(opts, done) - local query = vim.fn.expand(''):match(ISSUE_PATTERN) - - local job = require('hover.async.job').job - - job({'jira', 'issue', 'view', query, '--plain'}, function(result) - if result == nil then - done() - return - end - - local lines = {} - for line in result:gmatch('[^\r\n]+') do - -- Remove lines starting with \27, which is not formatted well and - -- is only there for help/context/suggestion lines anyway. - if line:find('^\27') == nil then - table.insert(lines, line) - end - end - - done {lines = lines, filetype = 'markdown'} - end) + local query = vim.fn.expand(''):match(ISSUE_PATTERN) + + local job = require('hover.async.job').job + + job({ 'jira', 'issue', 'view', query, '--plain' }, function(result) + if result == nil then + done() + return + end + + local lines = {} + for line in result:gmatch('[^\r\n]+') do + -- Remove lines starting with \27, which is not formatted well and + -- is only there for help/context/suggestion lines anyway. + if line:find('^\27') == nil then + table.insert(lines, line) + end + end + + done { lines = lines, filetype = 'markdown' } + end) end require('hover').register { - name = 'Jira', - priority = 175, - enabled = enabled, - execute = execute + name = 'Jira', + priority = 175, + enabled = enabled, + execute = execute } - diff --git a/lua/hover/providers/lsp.lua b/lua/hover/providers/lsp.lua index c6c7d18..1aaae23 100644 --- a/lua/hover/providers/lsp.lua +++ b/lua/hover/providers/lsp.lua @@ -1,4 +1,3 @@ - --- @diagnostic disable-next-line:deprecated local get_clients = vim.lsp.get_clients or vim.lsp.get_active_clients @@ -54,22 +53,22 @@ end --- @return fun(client: vim.lsp.Client): table local function create_params(bufnr, row, col) return function(client) - local offset_encoding = client.offset_encoding - local ok, lines = pcall(vim.api.nvim_buf_get_lines, bufnr, row, row + 1, true) + local offset_encoding = client.offset_encoding + local ok, lines = pcall(vim.api.nvim_buf_get_lines, bufnr, row, row + 1, true) - if not ok then - print(debug.traceback(string.format('ERROR: row %d is out of range: %s', row, lines))) - end + if not ok then + print(debug.traceback(string.format('ERROR: row %d is out of range: %s', row, lines))) + end - local ccol = lines and str_utfindex(lines[1], col, offset_encoding) or col + local ccol = lines and str_utfindex(lines[1], col, offset_encoding) or col - return { - textDocument = { uri = vim.uri_from_bufnr(bufnr) }, - position = { - line = row, - character = ccol - } + return { + textDocument = { uri = vim.uri_from_bufnr(bufnr) }, + position = { + line = row, + character = ccol } + } end end @@ -97,7 +96,7 @@ require('hover').register { if result.contents then local lines = util.convert_input_to_markdown_lines(result.contents) if not vim.tbl_isempty(lines) then - done{lines=lines, filetype="markdown"} + done { lines = lines, filetype = 'markdown' } return end end diff --git a/lua/hover/providers/man.lua b/lua/hover/providers/man.lua index 56c1231..a8c3ae6 100644 --- a/lua/hover/providers/man.lua +++ b/lua/hover/providers/man.lua @@ -25,7 +25,7 @@ local execute = async.void(function(opts, done) end) if not ok or api.nvim_buf_line_count(bufnr) <= 1 then - api.nvim_buf_delete(bufnr, {force = true}) + api.nvim_buf_delete(bufnr, { force = true }) done() return end @@ -39,7 +39,7 @@ local execute = async.void(function(opts, done) end }) - done{ bufnr = bufnr } + done { bufnr = bufnr } end) require('hover').register { diff --git a/lua/hover/util.lua b/lua/hover/util.lua index 301ce61..f90148b 100644 --- a/lua/hover/util.lua +++ b/lua/hover/util.lua @@ -57,13 +57,13 @@ local function trim_empty_lines(lines) end local default_border = { - { '' , 'NormalFloat' }, - { '' , 'NormalFloat' }, - { '' , 'NormalFloat' }, + { '', 'NormalFloat' }, + { '', 'NormalFloat' }, + { '', 'NormalFloat' }, { ' ', 'NormalFloat' }, - { '' , 'NormalFloat' }, - { '' , 'NormalFloat' }, - { '' , 'NormalFloat' }, + { '', 'NormalFloat' }, + { '', 'NormalFloat' }, + { '', 'NormalFloat' }, { ' ', 'NormalFloat' }, }