Skip to content

Commit

Permalink
feat(formatting): add conform.nvim & nvim-lint for linting & formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ecosse3 committed Feb 25, 2024
1 parent 09022a3 commit 325074d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lua/config/plugins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -975,4 +975,21 @@ return {
-- ╭─────────────────────────────────────────────────────────╮
-- │ Format & Lint │
-- ╰─────────────────────────────────────────────────────────╯
{
"stevearc/conform.nvim",
event = { "BufReadPre", "BufNewFile" },
config = function()
require('plugins.formatting')
end,
},
{
"mfussenegger/nvim-lint",
event = {
"BufReadPre",
"BufNewFile",
},
config = function()
require('plugins.linting')
end,
},
}
49 changes: 49 additions & 0 deletions lua/plugins/formatting.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
local conform = require("conform")

conform.setup({
formatters_by_ft = {
css = { { "prettied", "prettier" } },
graphql = { { "prettied", "prettier" } },
html = { { "prettied", "prettier" } },
javascript = { { "prettied", "prettier" } },
javascriptreact = { { "prettied", "prettier" } },
json = { { "prettied", "prettier" } },
lua = { "stylua" },
markdown = { { "prettied", "prettier" } },
python = { "isort", "black" },
sql = { "sql-formatter" },
svelte = { { "prettied", "prettier" } },
typescript = { { "prettied", "prettier" } },
typescriptreact = { { "prettied", "prettier" } },
yaml = { "prettier" },
},
})

vim.keymap.set({ "n" }, "<leader>f", function()
conform.format({
lsp_fallback = true,
async = false,
timeout_ms = 500,
})
end, { desc = "format file" })

vim.keymap.set({ "v" }, "<leader>f", function()
conform.format({
lsp_fallback = true,
async = false,
timeout_ms = 500,
})
end, { desc = "format selection" })

vim.api.nvim_create_user_command("Format", function(args)
local range = nil
if args.count ~= -1 then
local end_line = vim.api.nvim_buf_get_lines(0, args.line2 - 1, args.line2, true)[1]
range = {
start = { args.line1, 0 },
["end"] = { args.line2, end_line:len() },
}
end

conform.format({ async = true, lsp_fallback = true, range = range })
end, { range = true })
14 changes: 14 additions & 0 deletions lua/plugins/linting.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
local lint = require("lint")

lint.linters_by_ft = {
javascript = { "eslint_d" },
typescript = { "eslint_d" },
javascriptreact = { "eslint_d" },
typescriptreact = { "eslint_d" },
svelte = { "eslint_d" },
python = { "pylint" },
}

vim.keymap.set("n", "<leader>l", function()
lint.try_lint()
end, { desc = "lint file" })

0 comments on commit 325074d

Please sign in to comment.