Skip to content

Commit

Permalink
refactor: easier efm plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
konradmalik committed Aug 29, 2023
1 parent afe394e commit 8ea7d32
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 145 deletions.
89 changes: 48 additions & 41 deletions config/native/lua/git-conflict/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- adapted from https://github.com/akinsho/git-conflict.nvim
local color = require('git-conflict.colors')
local keymaps = require('git-conflict.keymaps')
local color = require("git-conflict.colors")
local keymaps = require("git-conflict.keymaps")
local api = vim.api

--- @class ConflictHighlights
Expand All @@ -19,36 +19,36 @@ local api = vim.api
--- @field content_start integer
--- @field content_end integer

local CURRENT_HL = 'GitConflictCurrent'
local INCOMING_HL = 'GitConflictIncoming'
local ANCESTOR_HL = 'GitConflictAncestor'
local CURRENT_LABEL_HL = 'GitConflictCurrentLabel'
local INCOMING_LABEL_HL = 'GitConflictIncomingLabel'
local ANCESTOR_LABEL_HL = 'GitConflictAncestorLabel'
local CURRENT_HL = "GitConflictCurrent"
local INCOMING_HL = "GitConflictIncoming"
local ANCESTOR_HL = "GitConflictAncestor"
local CURRENT_LABEL_HL = "GitConflictCurrentLabel"
local INCOMING_LABEL_HL = "GitConflictIncomingLabel"
local ANCESTOR_LABEL_HL = "GitConflictAncestorLabel"
local PRIORITY = vim.highlight.priorities.diagnostics - 1
local NAME = 'git-conflict'
local NAME = "git-conflict"
local NAMESPACE = api.nvim_create_namespace(NAME)
local AUGROUP_NAME = 'GitConflictCommands'
local AUGROUP_NAME = "GitConflictCommands"

local conflict_start = '^<<<<<<<'
local conflict_middle = '^======='
local conflict_end = '^>>>>>>>'
local conflict_ancestor = '^|||||||'
local conflict_start = "^<<<<<<<"
local conflict_middle = "^======="
local conflict_end = "^>>>>>>>"
local conflict_ancestor = "^|||||||"

local DEFAULT_CURRENT_BG_COLOR = 4218238 -- #405d7e
local DEFAULT_CURRENT_BG_COLOR = 4218238 -- #405d7e
local DEFAULT_INCOMING_BG_COLOR = 3229523 -- #314753
local DEFAULT_ANCESTOR_BG_COLOR = 6824314 -- #68217A

local config = {
highlights = {
current = 'diffAdded',
incoming = 'diffChanged',
ancestor = 'diffRemoved',
current = "diffAdded",
incoming = "diffChanged",
ancestor = "diffRemoved",
},
labels = {
current = '(Current Change)',
incoming = '(Incoming Change)',
ancestor = '(Base Change)',
current = "(Current Change)",
incoming = "(Incoming Change)",
ancestor = "(Base Change)",
},
enable_autocommand = true,
enable_diagnostics = true,
Expand All @@ -58,7 +58,9 @@ local config = {
---@param name string?
---@return table<string, string>
local function get_hl(name)
if not name then return {} end
if not name then
return {}
end
return api.nvim_get_hl(NAMESPACE, { name = name })
end

Expand All @@ -69,11 +71,13 @@ end
---@param range_end integer
---@return integer? extmark_id
local function hl_range(bufnr, hl, range_start, range_end)
if not range_start or not range_end then return end
if not range_start or not range_end then
return
end
return api.nvim_buf_set_extmark(bufnr, NAMESPACE, range_start, 0, {
hl_group = hl,
hl_eol = true,
hl_mode = 'combine',
hl_mode = "combine",
end_row = range_end,
priority = PRIORITY,
})
Expand All @@ -89,7 +93,7 @@ local function draw_section_label(bufnr, hl_group, label, lnum)
return api.nvim_buf_set_extmark(bufnr, NAMESPACE, lnum, 0, {
hl_group = hl_group,
virt_text = { { label, hl_group } },
virt_text_pos = 'eol',
virt_text_pos = "eol",
priority = PRIORITY,
})
end
Expand Down Expand Up @@ -194,7 +198,7 @@ end
--
---@return boolean
local function buf_can_have_conflicts()
return vim.fn.search(conflict_start, 'cnw', nil, 500) > 0
return vim.fn.search(conflict_start, "cnw", nil, 500) > 0
end

local function clear_highlights(bufnr)
Expand All @@ -211,15 +215,14 @@ end
local function set_diagnostics(bufnr, positions)
local diagnostics = {}
for _, position in ipairs(positions) do
table.insert(diagnostics,
{
lnum = position.current.range_start,
end_lnum = position.incoming.range_end,
col = 0,
severity = vim.diagnostic.severity.E,
message = "Git conflict",
source = NAME,
})
table.insert(diagnostics, {
lnum = position.current.range_start,
end_lnum = position.incoming.range_end,
col = 0,
severity = vim.diagnostic.severity.E,
message = "Git conflict",
source = NAME,
})
end
vim.diagnostic.set(NAMESPACE, bufnr, diagnostics)
end
Expand Down Expand Up @@ -252,7 +255,9 @@ end

---@param bufnr integer?
M.clear = function(bufnr)
if bufnr and not api.nvim_buf_is_valid(bufnr) then return end
if bufnr and not api.nvim_buf_is_valid(bufnr) then
return
end
bufnr = bufnr or 0
clear_highlights(bufnr)
if config.enable_diagnostics then
Expand All @@ -277,27 +282,29 @@ function M.refresh(bufnr)
end

if buf_can_have_conflicts() then
local positions = run(bufnr);
local positions = run(bufnr)
buf_conflicts[bufnr] = positions
end
end

function M.setup(user_config)
config = vim.tbl_deep_extend('force', config, user_config or {})
config = vim.tbl_deep_extend("force", config, user_config or {})

set_highlights(config.highlights)
if config.enable_keymaps then
keymaps.set_global_keymaps(conflict_start)
end

api.nvim_create_augroup(AUGROUP_NAME, { clear = true })
api.nvim_create_autocmd('ColorScheme', {
api.nvim_create_autocmd("ColorScheme", {
group = AUGROUP_NAME,
callback = function() set_highlights(config.highlights) end,
callback = function()
set_highlights(config.highlights)
end,
})

if config.enable_autocommand then
api.nvim_create_autocmd({ 'BufReadPost', 'BufWritePost' }, {
api.nvim_create_autocmd({ "BufReadPost", "BufWritePost" }, {
group = AUGROUP_NAME,
callback = function(args)
local buf = args.buf
Expand Down
14 changes: 6 additions & 8 deletions config/native/lua/konrad/lsp/efm/black.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
local efm = require('konrad.lsp.efm')

local fts = { "python" }
local entry = {
formatCommand = "black --no-color --quiet --stdin-filename '${INPUT}' -",
formatStdin = true,
return {
filetypes = { "python" },
entry = {
formatCommand = "black --no-color --quiet --stdin-filename '${INPUT}' -",
formatStdin = true,
},
}

return efm.make_languages_entry_for_fts(fts, entry)
14 changes: 6 additions & 8 deletions config/native/lua/konrad/lsp/efm/goimports.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
local efm = require('konrad.lsp.efm')

local fts = { "go" }
local entry = {
formatCommand = "goimports",
formatStdin = true,
return {
filetypes = { "go" },
entry = {
formatCommand = "goimports",
formatStdin = true,
},
}

return efm.make_languages_entry_for_fts(fts, entry)
20 changes: 9 additions & 11 deletions config/native/lua/konrad/lsp/efm/golangci_lint.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
local efm = require('konrad.lsp.efm')

local fts = { "go" }
local entry = {
prefix = "golangci-lint",
lintCommand = "golangci-lint run --color never --out-format tab '${INPUT}'",
lintStdin = false,
lintFormats = { '%.%#:%l:%c %m' },
rootMarkers = {},
return {
filetypes = { "go" },
entry = {
prefix = "golangci-lint",
lintCommand = "golangci-lint run --color never --out-format tab '${INPUT}'",
lintStdin = false,
lintFormats = { "%.%#:%l:%c %m" },
rootMarkers = {},
},
}

return efm.make_languages_entry_for_fts(fts, entry)
24 changes: 15 additions & 9 deletions config/native/lua/konrad/lsp/efm/init.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
-- https://github.com/mattn/efm-langserver
local binaries = require('konrad.binaries')
local binaries = require("konrad.binaries")

local M = {}
--- @class EfmPlugin
--- @field filetypes string[]
--- @field entry table

---@param fts string[]
---@param entry table
---@param plugin EfmPlugin
---@return table
M.make_languages_entry_for_fts = function(fts, entry)
local make_languages_entry_for_plugin = function(plugin)
-- efm requires nested tables here (notice brackets in entry)
local nested = vim.tbl_map(function(t) return { [t] = { entry } } end, fts)
local nested = vim.tbl_map(function(t)
return { [t] = { plugin.entry } }
end, plugin.filetypes)
if #nested < 1 then
error("must have at least one entry")
elseif #nested == 1 then
Expand All @@ -17,15 +20,18 @@ M.make_languages_entry_for_fts = function(fts, entry)
return vim.tbl_extend("error", unpack(nested))
end

local M = {}

M.default_plugins = { "prettier", "jq", "shfmt", "shellcheck" }

---@param plugins string[] names of plugins to add, ex. 'prettier'
---@return table config to be put into lspconfig['efm'].setup(config)
M.build_lspconfig = function(plugins)
local languages = {}
for _, v in ipairs(plugins) do
local plugin = require('konrad.lsp.efm.' .. v)
for key, value in pairs(plugin) do
local plugin = require("konrad.lsp.efm." .. v)
local langages_entry = make_languages_entry_for_plugin(plugin)
for key, value in pairs(langages_entry) do
if languages[key] then
languages[key] = vim.list_extend(languages[key], value)
else
Expand All @@ -43,7 +49,7 @@ M.build_lspconfig = function(plugins)
documentRangeFormatting = true,
},
settings = {
rootMarkers = { '.git/' },
rootMarkers = { ".git/" },
languages = languages,
},
}
Expand Down
14 changes: 6 additions & 8 deletions config/native/lua/konrad/lsp/efm/isort.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
local efm = require('konrad.lsp.efm')

local fts = { "python" }
local entry = {
formatCommand = "isort --stdout --filename '${INPUT}' -",
formatStdin = true,
return {
filetypes = { "python" },
entry = {
formatCommand = "isort --stdout --filename '${INPUT}' -",
formatStdin = true,
},
}

return efm.make_languages_entry_for_fts(fts, entry)
16 changes: 8 additions & 8 deletions config/native/lua/konrad/lsp/efm/jq.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
local efm = require('konrad.lsp.efm')
local binaries = require('konrad.binaries')
local binaries = require("konrad.binaries")

local fts = { "json" }
local entry = {
lintCommand = binaries.jq .. " .",
lintFormats = { "parse %trror: %m at line %l, column %c" },
lintSource = "jq",
return {
filetypes = { "json" },
entry = {
lintCommand = binaries.jq .. " .",
lintFormats = { "parse %trror: %m at line %l, column %c" },
lintSource = "jq",
},
}
return efm.make_languages_entry_for_fts(fts, entry)
62 changes: 40 additions & 22 deletions config/native/lua/konrad/lsp/efm/prettier.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
local efm = require('konrad.lsp.efm')
local binaries = require('konrad.binaries')
local binaries = require("konrad.binaries")

local fts = { "javascript", "javascriptreact", "typescript", "typescriptreact", "vue", "css", "scss", "less",
"html", "json", "jsonc", "yaml", "markdown", "markdown.mdx", "graphql", "handlebars", "toml" }

local entry = {
formatCommand = binaries.prettier .. " --plugin-search-dir " .. binaries.prettier_plugin_toml ..
" --stdin --stdin-filepath '${INPUT}' ${--range-start:charStart} "
.. '${--range-end:charEnd} ${--tab-width:tabSize} ${--use-tabs:!insertSpaces}',
formatStdin = true,
formatCanRange = true,
rootMarkers = {
".prettierrc",
".prettierrc.json",
".prettierrc.js",
".prettierrc.yml",
".prettierrc.yaml",
".prettierrc.json5",
".prettierrc.mjs",
".prettierrc.cjs",
".prettierrc.toml",
return {
filetypes = {
"javascript",
"javascriptreact",
"typescript",
"typescriptreact",
"vue",
"css",
"scss",
"less",
"html",
"json",
"jsonc",
"yaml",
"markdown",
"markdown.mdx",
"graphql",
"handlebars",
"toml",
},
entry = {
formatCommand = binaries.prettier
.. " --plugin-search-dir "
.. binaries.prettier_plugin_toml
.. " --stdin --stdin-filepath '${INPUT}' ${--range-start:charStart} "
.. "${--range-end:charEnd} ${--tab-width:tabSize} ${--use-tabs:!insertSpaces}",
formatStdin = true,
formatCanRange = true,
rootMarkers = {
".prettierrc",
".prettierrc.json",
".prettierrc.js",
".prettierrc.yml",
".prettierrc.yaml",
".prettierrc.json5",
".prettierrc.mjs",
".prettierrc.cjs",
".prettierrc.toml",
},
},
}
return efm.make_languages_entry_for_fts(fts, entry)
Loading

0 comments on commit 8ea7d32

Please sign in to comment.