Skip to content

Commit

Permalink
chore: import formatting and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
rafi committed May 17, 2024
1 parent dc3e702 commit accc67c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 211 deletions.
120 changes: 6 additions & 114 deletions lua/rafi/plugins/formatting.lua
Original file line number Diff line number Diff line change
@@ -1,125 +1,17 @@
-- This is part of LazyVim's code, with my modifications.
-- See: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/plugins/formatting.lua

local M = {}

---@param opts ConformOpts
function M.setup(_, opts)
for name, formatter in pairs(opts.formatters or {}) do
if type(formatter) == 'table' then
---@diagnostic disable-next-line: undefined-field
if formatter.extra_args then
---@diagnostic disable-next-line: undefined-field
formatter.prepend_args = formatter.extra_args
LazyVim.deprecate(
('opts.formatters.%s.extra_args'):format(name),
('opts.formatters.%s.prepend_args'):format(name)
)
end
end
end

for _, key in ipairs({ 'format_on_save', 'format_after_save' }) do
if opts[key] then
LazyVim.warn(
("Don't set `opts.%s` for `conform.nvim`,\nit is configured automatically."):format(
key
)
)
opts[key] = nil
end
end
require('conform').setup(opts)
end
-- Plugins: Formatting
-- https://github.com/rafi/vim-config

return {

-- Import LazyVim's formatting spec in its entirety.
{ import = 'lazyvim.plugins.formatting' },

-- Lightweight yet powerful formatter plugin
{
'stevearc/conform.nvim',
dependencies = { 'mason.nvim' },
lazy = true,
cmd = 'ConformInfo',
-- stylua: ignore
keys = {
{ '<leader>ci', '<cmd>LazyFormatInfo<CR>:ConformInfo<CR>', silent = true, desc = 'Formatter Info' },
{
'<leader>cF',
function()
require('conform').format({ formatters = { 'injected' }, timeout_ms = 3000 })
end,
mode = { 'n', 'v' },
desc = 'Format Injected Langs',
},
{ '<leader>cic', '<cmd>ConformInfo<CR>', silent = true, desc = 'Conform Info' },
},
init = function()
-- Install the conform formatter on VeryLazy
LazyVim.on_very_lazy(function()
LazyVim.format.register({
name = 'conform.nvim',
priority = 100,
primary = true,
format = function(buf)
local plugin = require('lazy.core.config').plugins['conform.nvim']
local Plugin = require('lazy.core.plugin')
local opts = Plugin.values(plugin, 'opts', false)
require('conform').format(
LazyVim.merge({}, opts.format, { bufnr = buf })
)
end,
sources = function(buf)
local ret = require('conform').list_formatters(buf)
---@param v conform.FormatterInfo
return vim.tbl_map(function(v)
return v.name
end, ret)
end,
})
end)
end,
opts = function()
local plugin = require('lazy.core.config').plugins['conform.nvim']
if plugin.config ~= M.setup then
LazyVim.error({
"Don't set `plugin.config` for `conform.nvim`.\n",
'This will break formatting.',
}, { title = 'Formatter' })
end
---@class ConformOpts
local opts = {
-- Use these options when formatting with the conform.nvim formatter
format = {
timeout_ms = 3000,
async = false, -- not recommended to change
quiet = false, -- not recommended to change
lsp_fallback = true, -- not recommended to change
},
---@type table<string, conform.FormatterUnit[]>
formatters_by_ft = {
lua = { 'stylua' },
fish = { 'fish_indent' },
sh = { 'shfmt' },
},
-- The options you set here will be merged with the builtin formatters.
-- You can also define any custom formatters here.
---@type table<string, conform.FormatterConfigOverride|fun(bufnr: integer): nil|conform.FormatterConfigOverride>
formatters = {
injected = { options = { ignore_errors = true } },
-- # Example of using dprint only when a dprint.json file is present
-- dprint = {
-- condition = function(ctx)
-- return vim.fs.find({ "dprint.json" }, { path = ctx.filename, upward = true })[1]
-- end,
-- },
--
-- # Example of using shfmt with extra args
-- shfmt = {
-- prepend_args = { "-i", "2", "-ci" },
-- },
},
}
return opts
end,
config = M.setup,
},
}
4 changes: 4 additions & 0 deletions lua/rafi/plugins/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ return {

-- Actions
--
map('n', ']h', function() gs.nav_hunk('next') end, { desc = 'Next Hunk' })
map('n', '[h', function() gs.nav_hunk('prev') end, { desc = 'Prev Hunk' })
map('n', ']H', function() gs.nav_hunk('last') end, { desc = 'Last Hunk' })
map('n', '[H', function() gs.nav_hunk('first') end, { desc = 'First Hunk' })
map('n', '<leader>hs', gs.stage_hunk, { silent = true, desc = 'Stage hunk' })
map('n', '<leader>hr', gs.reset_hunk, { silent = true, desc = 'Reset hunk' })
map('x', '<leader>hs', function() gs.stage_hunk({vim.fn.line('.'), vim.fn.line('v')}) end, { desc = 'Stage hunk' })
Expand Down
2 changes: 1 addition & 1 deletion lua/rafi/plugins/lazyvim.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
return {

-- LazyVim framework.
{
'LazyVim/LazyVim',
version = '*',
Expand Down Expand Up @@ -98,5 +99,4 @@ return {
},
},
},

}
109 changes: 13 additions & 96 deletions lua/rafi/plugins/linting.lua
Original file line number Diff line number Diff line change
@@ -1,106 +1,23 @@
-- This is part of LazyVim's code, with my modifications.
-- See: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/plugins/linting.lua
-- Plugins: Linting
-- https://github.com/rafi/vim-config

return {

-- Import LazyVim's linting spec in its entirety.
{ import = 'lazyvim.plugins.linting' },

-- Asynchronous linter plugin
{
'mfussenegger/nvim-lint',
event = 'LazyFile',
opts = {
-- Event to trigger linters
events = { 'BufWritePost', 'BufReadPost', 'InsertLeave' },
linters_by_ft = {
fish = { 'fish' },
-- Use the "*" filetype to run linters on all filetypes.
-- ['*'] = { 'global linter' },
-- Use the "_" filetype to run linters on filetypes that don't have other linters configured.
-- ['_'] = { 'fallback linter' },
-- ['*'] = { 'typos' },
},
-- LazyVim extension to easily override linter options
-- or add custom linters.
---@type table<string,table>
linters = {
-- -- Example of using selene only when a selene.toml file is present
-- selene = {
-- -- `condition` is another LazyVim extension that allows you to
-- -- dynamically enable/disable linters based on the context.
-- condition = function(ctx)
-- return vim.fs.find({ "selene.toml" }, { path = ctx.filename, upward = true })[1]
-- end,
-- },
keys = {
{
'<leader>cin',
function()
vim.notify(vim.inspect(require('lint').linters[vim.bo.filetype]))
end,
silent = true,
desc = 'Linter Info',
},
},
config = function(_, opts)
local M = {}

local lint = require('lint')
for name, linter in pairs(opts.linters) do
if type(linter) == 'table' and type(lint.linters[name]) == 'table' then
lint.linters[name] =
vim.tbl_deep_extend('force', lint.linters[name], linter)
else
lint.linters[name] = linter
end
end
lint.linters_by_ft = opts.linters_by_ft

function M.debounce(ms, fn)
local timer = vim.uv.new_timer()
return function(...)
local argv = { ... }
timer:start(ms, 0, function()
timer:stop()
vim.schedule_wrap(fn)(unpack(argv))
end)
end
end

function M.lint()
-- Use nvim-lint's logic first:
-- * checks if linters exist for the full filetype first
-- * otherwise will split filetype by "." and add all those linters
-- * this differs from conform.nvim which only uses the first filetype that has a formatter
local names = lint._resolve_linter_by_ft(vim.bo.filetype)

-- Create a copy of the names table to avoid modifying the original.
names = vim.list_extend({}, names)

-- Add fallback linters.
if #names == 0 then
vim.list_extend(names, lint.linters_by_ft['_'] or {})
end

-- Add global linters.
vim.list_extend(names, lint.linters_by_ft['*'] or {})

-- Filter out linters that don't exist or don't match the condition.
local ctx = { filename = vim.api.nvim_buf_get_name(0) }
ctx.dirname = vim.fn.fnamemodify(ctx.filename, ':h')
names = vim.tbl_filter(function(name)
local linter = lint.linters[name]
if not linter then
LazyVim.warn('Linter not found: ' .. name, { title = 'nvim-lint' })
end
return linter
and not (
type(linter) == 'table'
and linter.condition
and not linter.condition(ctx)
)
end, names)

-- Run linters.
if #names > 0 then
lint.try_lint(names)
end
end

vim.api.nvim_create_autocmd(opts.events, {
group = vim.api.nvim_create_augroup('nvim-lint', { clear = true }),
callback = M.debounce(100, M.lint),
})
end,
},
}

0 comments on commit accc67c

Please sign in to comment.