Skip to content

Commit

Permalink
style: add .editorconfig and format
Browse files Browse the repository at this point in the history
  • Loading branch information
AThePeanut4 committed Apr 30, 2024
1 parent c40b115 commit c64fb9a
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 89 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,16 @@ Call `require('hover').register(<provider>)` 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
}
```

Expand Down
4 changes: 2 additions & 2 deletions lua/hover/providers.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local async = require('hover.async')

local M = {}
local M = {}

--- @class Hover.PartialOptions
--- @field bufnr? integer
Expand Down Expand Up @@ -54,7 +54,7 @@ function M.register(provider)
end
end
end
providers[#providers+1] = provider
providers[#providers + 1] = provider
end

return M
16 changes: 8 additions & 8 deletions lua/hover/providers/dap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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", "<CR>", "<Cmd>lua require('dap.ui').trigger_actions({ mode = 'first' })<CR>", {})
buf, 'n', '<CR>', "<Cmd>lua require('dap.ui').trigger_actions({ mode = 'first' })<CR>", {})
api.nvim_buf_set_keymap(
buf, "n", "a", "<Cmd>lua require('dap.ui').trigger_actions()<CR>", {})
buf, 'n', 'a', "<Cmd>lua require('dap.ui').trigger_actions()<CR>", {})
api.nvim_buf_set_keymap(
buf, "n", "o", "<Cmd>lua require('dap.ui').trigger_actions()<CR>", {})
buf, 'n', 'o', "<Cmd>lua require('dap.ui').trigger_actions()<CR>", {})
api.nvim_buf_set_keymap(
buf, "n", "<2-LeftMouse>", "<Cmd>lua require('dap.ui').trigger_actions()<CR>", {})
buf, 'n', '<2-LeftMouse>', "<Cmd>lua require('dap.ui').trigger_actions()<CR>", {})
end

local function new_buf()
Expand All @@ -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,
}
Expand Down
12 changes: 6 additions & 6 deletions lua/hover/providers/dictionary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions lua/hover/providers/gh.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 9 additions & 9 deletions lua/hover/providers/gh_user.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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 {
Expand Down
55 changes: 27 additions & 28 deletions lua/hover/providers/jira.lua
Original file line number Diff line number Diff line change
@@ -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('<cWORD>'):match(ISSUE_PATTERN) ~= nil
return vim.fn.expand('<cWORD>'):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('<cWORD>'):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('<cWORD>'):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
}

27 changes: 13 additions & 14 deletions lua/hover/providers/lsp.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

--- @diagnostic disable-next-line:deprecated
local get_clients = vim.lsp.get_clients or vim.lsp.get_active_clients

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lua/hover/providers/man.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,7 +39,7 @@ local execute = async.void(function(opts, done)
end
})

done{ bufnr = bufnr }
done { bufnr = bufnr }
end)

require('hover').register {
Expand Down
12 changes: 6 additions & 6 deletions lua/hover/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
}

Expand Down

0 comments on commit c64fb9a

Please sign in to comment.