Skip to content

Commit

Permalink
refac(formatters): keep wip
Browse files Browse the repository at this point in the history
- add stevears/conform.nvim
- remove/comment null-ls formatters conform handles
- remove utils functions conform handles
- todo: remove false positive message saying no server could handle lsp.format
  • Loading branch information
n3wborn committed Sep 22, 2023
1 parent 2a820ae commit f7c120d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 62 deletions.
1 change: 1 addition & 0 deletions lazy-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"cmp-rg": { "branch": "master", "commit": "677a7874ee8f1afc648c2e7d63a97bc21a7663c5" },
"cmp-under-comparator": { "branch": "master", "commit": "6857f10272c3cfe930cece2afa2406e1385bfef8" },
"cmp_luasnip": { "branch": "master", "commit": "18095520391186d634a0045dacaa346291096566" },
"conform.nvim": { "branch": "master", "commit": "4a4c92715b174b847ba0fcdccf9dfea71c8ed33e" },
"friendly-snippets": { "branch": "main", "commit": "ebf6d6e83494cdd88a54a429340256f4dbb6a052" },
"git-conflict.nvim": { "branch": "main", "commit": "896261933afe2fddf6fb043d9cd4d88301b151a9" },
"gitsigns.nvim": { "branch": "main", "commit": "bce4576a9047085a528c479a7fe1e2f6b787b6c1" },
Expand Down
7 changes: 1 addition & 6 deletions lua/custom/autocommands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ vim.api.nvim_create_autocmd({ 'InsertLeave', 'WinEnter' }, {
end
end,
})

vim.api.nvim_create_autocmd({ 'InsertEnter', 'WinLeave' }, {
callback = function()
local cl = vim.wo.cursorline
Expand All @@ -32,12 +33,6 @@ vim.api.nvim_create_autocmd({ 'FileType' }, {
end,
})

-- Trim buffer whitespaces
vim.api.nvim_create_autocmd({ 'BufWritePre' }, {
pattern = '*',
command = 'TrimTrailingWhitespace',
})

-- Check if we need to reload the file when it changed
vim.api.nvim_create_autocmd({ 'FocusGained', 'TermClose', 'TermLeave' }, { command = 'checktime' })

Expand Down
14 changes: 7 additions & 7 deletions lua/lsp/null-ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ local sources = {
--- code actions
b.code_actions.eslint,
b.code_actions.gitrebase,
b.code_actions.refactoring,
-- b.code_actions.refactoring,
b.code_actions.shellcheck,
require('typescript.extensions.null-ls.code-actions'),

--- diagnostics
b.diagnostics.gitlint,
b.diagnostics.markdownlint,
-- b.diagnostics.markdownlint,
b.diagnostics.php,
b.diagnostics.shellcheck.with({ diagnostics_format = '#{m} [#{c}]' }),
-- b.diagnostics.shellcheck.with({ diagnostics_format = '#{m} [#{c}]' }),
b.diagnostics.todo_comments,
b.diagnostics.trail_space,
-- b.diagnostics.trail_space,
b.diagnostics.tsc,
b.diagnostics.zsh,

--- formatting
b.formatting.blade_formatter,
b.formatting.eslint_d,
-- b.formatting.eslint_d,
b.formatting.fixjson,
b.formatting.goimports,
b.formatting.phpcsfixer.with({
Expand Down Expand Up @@ -63,13 +63,13 @@ local sources = {
return not utils.root_has_file('.php-cs-fixer.php')
end,
}),
b.formatting.prettier.with({
--[[ b.formatting.prettier.with({
disabled_filetypes = { 'typescript', 'typescriptreact', 'markdown' },
}),
b.formatting.rustfmt,
b.formatting.shfmt,
b.formatting.sqlfmt,
with_root_file(b.formatting.stylua, 'stylua.toml'),
with_root_file(b.formatting.stylua, 'stylua.toml'), ]]
}

local M = {
Expand Down
31 changes: 31 additions & 0 deletions lua/plugins/conform.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
return {
'stevearc/conform.nvim',
opts = {
formatters_by_ft = {
javascript = { { 'eslint_d', 'eslint', 'prettier_d', 'prettier' } },
json = { 'jq' },
lua = { 'stylua' },
markdown = { 'markdownlint' },
rust = { 'rustfmt' },
sh = { 'shfmt', 'shellcheck' },
sql = { 'sql_formtter' },
typescript = { { 'eslint_d', 'eslint' } },
typescriptreact = { { 'eslint_d', 'eslint' } },
_ = { 'trim_whitespace', 'squeeze_blanks', 'trim_newlines' },
},
},
format_on_save = {
timeout_ms = 1000,
lsp_fallback = true,
},
config = function(_, opts)
require('conform').setup(opts)

vim.api.nvim_create_autocmd('BufWritePre', {
pattern = '*',
callback = function(args)
require('conform').format({ bufnr = args.buf })
end,
})
end,
}
49 changes: 0 additions & 49 deletions lua/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,6 @@ local get_map_options = function(custom_options)
return options
end

-- (see :h command-preview or https://github.com/neovim/neovim/commit/7380ebfc17723662f6fe1e38372f54b3d67fe082)
local function trim_space(opts, preview_ns, preview_buf)
local line1 = opts.line1
local line2 = opts.line2
local buf = vim.api.nvim_get_current_buf()
local lines = vim.api.nvim_buf_get_lines(buf, line1 - 1, line2, true)
local new_lines = {}
local preview_buf_line = 0
for i, line in ipairs(lines) do
local startidx, endidx = string.find(line, '%s+$')
if startidx ~= nil then
-- Highlight the match if in command preview mode
if preview_ns ~= nil then
api.nvim_buf_add_highlight(buf, preview_ns, 'Substitute', line1 + i - 2, startidx - 1, endidx)
-- Add lines and highlight to the preview buffer
-- if inccommand=split
if preview_buf ~= nil then
local prefix = string.format('|%d| ', line1 + i - 1)
api.nvim_buf_set_lines(preview_buf, preview_buf_line, preview_buf_line, true, { prefix .. line })
api.nvim_buf_add_highlight(
preview_buf,
preview_ns,
'Substitute',
preview_buf_line,
#prefix + startidx - 1,
#prefix + endidx
)
preview_buf_line = preview_buf_line + 1
end
end
end
if not preview_ns then
new_lines[#new_lines + 1] = string.gsub(line, '%s+$', '')
end
end
-- Don't make any changes to the buffer if previewing
if not preview_ns then
api.nvim_buf_set_lines(buf, line1 - 1, line2, true, new_lines)
end
-- When called as a preview callback, return the value of the
-- preview type
if preview_ns ~= nil then
return 2
end
end

local M = {}

M.map = function(mode, target, source, opts)
Expand Down Expand Up @@ -148,9 +102,6 @@ M.get_cwd = function()
return uv.cwd
end

M.trim_trailing_whitespace =
M.command('TrimTrailingWhitespace', trim_space, { nargs = '?', range = '%', addr = 'lines', preview = trim_space })

---@param level number|nil
---@param msg string
---@param title string
Expand Down

0 comments on commit f7c120d

Please sign in to comment.