Skip to content

Instantly share code, notes, and snippets.

@cyrus01337
Created July 9, 2024 17:05
Show Gist options
  • Save cyrus01337/defd891b7924176d47a747cadcec5842 to your computer and use it in GitHub Desktop.
Save cyrus01337/defd891b7924176d47a747cadcec5842 to your computer and use it in GitHub Desktop.
Full configuratin for debugging why my configuration borks lazydev.nvim (or the other way around? Shurg)
local EMPTY = ""
local NORMAL_MODE = "n"
local INSERT_MODE = "i"
local function setup_language_server(serverName)
local lsp_configuration = require("lspconfig")
local stylua = require("stylua-nvim")
local server_found = lsp_configuration[serverName]
if not server_found then
print("Cannot find LSP named", serverName)
return
end
print(serverName)
server_found.setup({
commands = {
Format = {
stylua.format_file,
},
},
settings = {
Lua = {
diagnostics = {
globals = { "vim" },
},
},
},
})
end
local function preserve_cursor_position()
local previous_row = vim.api.nvim_win_get_cursor(0)[1] + 1
return function()
local current_row = vim.api.nvim_win_get_cursor(0)[1] + 1
local current_buffer = vim.api.nvim_get_current_buf()
local last_line_number = vim.api.nvim_buf_line_count(current_buffer)
if previous_row == current_row then
return
end
vim.api.nvim_win_set_cursor(0, { last_line_number, 0 })
end
end
local function add_trailing_newlines()
local line_count = vim.api.nvim_buf_line_count(0)
local last_line_with_content = vim.fn.prevnonblank(line_count)
if last_line_with_content > line_count then
return
end
local to_last_line = line_count
vim.api.nvim_buf_set_lines(0, last_line_with_content, to_last_line, true, { EMPTY })
end
local function format_file_on_save()
local reset_cursor_position = preserve_cursor_position()
vim.lsp.buf.format()
add_trailing_newlines()
-- when a file is formatted and new-lines are added, it can look like the
-- cursor jumped if it had been placed at the bottom of the file - this
-- moves the cursor back to its rightful position when that happens
reset_cursor_position()
end
return {
{
"nvim-treesitter/nvim-treesitter",
event = { "BufReadPre", "BufNewFile" },
build = ":TSUpdate",
config = function()
local treesitter_configuration = require("nvim-treesitter.configs")
treesitter_configuration.setup({
ensure_installed = {
-- meta
--- (neo)vim
"vim",
"vimdoc",
--- project management
"gitignore",
"markdown",
-- web dev
--- front-end
"html",
"css",
"javascript",
"typescript",
"astro",
--- back-end
"php",
"sql",
-- dev-ops
"dockerfile",
-- software/cli
"bash",
"python",
"lua",
-- configuration formats
"json",
"jsonc",
"yaml",
"toml",
-- documentation
"markdown",
},
sync_install = false,
auto_install = true,
highlight = {
enable = true,
additional_vim_regex_highlighting = false,
},
indent = { enable = true },
})
end,
},
{
"VonHeikemen/lsp-zero.nvim",
branch = "v3.x",
event = { "BufReadPre", "BufNewFile" },
lazy = true,
config = false,
opts = {
handlers = {
setup_language_server,
},
},
keys = {
{ "<leader>l", ":Mason<CR>" },
{ "<F2>", vim.lsp.buf.rename, mode = { NORMAL_MODE, INSERT_MODE } },
},
init = function()
vim.g.lsp_zero_extend_cmp = 0
vim.g.lsp_zero_extend_lspconfig = 0
end,
},
{
"williamboman/mason.nvim",
lazy = false,
config = true,
},
{
"folke/lazydev.nvim",
event = { "BufReadPre", "BufNewFile" },
ft = "lua",
opts = {
library = {
{ path = "luvit-meta/library", words = { "vim%.uv" } },
},
},
},
{
"Bilal2453/luvit-meta",
lazy = true
},
{
"windwp/nvim-autopairs",
event = "InsertEnter",
config = true,
opts = {
check_ts = true,
},
},
{
"hrsh7th/nvim-cmp",
event = "InsertEnter",
config = function()
local lsp = require("lsp-zero")
local cmp = require("cmp")
local cmp_autopairs = require("nvim-autopairs.completion.cmp")
lsp.extend_cmp()
cmp.setup({
formatting = lsp.cmp_format({ details = true }),
mapping = cmp.mapping.preset.insert({
["<C-Space>"] = cmp.mapping.complete(),
["<CR>"] = cmp.mapping.confirm({ select = true }),
["<Esc>"] = cmp.mapping.abort(),
["<Up>"] = cmp.mapping.select_prev_item({ behavior = "select" }),
["<Down>"] = cmp.mapping.select_next_item({ behavior = "select" }),
}),
sources = {
name = "lazydev",
group_index = 0,
}
})
cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done())
end,
},
{
"neovim/nvim-lspconfig",
cmd = { "LspInfo", "LspInstall", "LspStart" },
event = { "BufReadPre", "BufNewFile" },
dependencies = {
"hrsh7th/cmp-nvim-lsp",
"williamboman/mason-lspconfig.nvim",
},
config = function()
local lsp = require("lsp-zero")
local mason_lsp_configuration = require("mason-lspconfig")
lsp.extend_lspconfig()
lsp.on_attach(function(_, buffer)
lsp.default_keymaps({ buffer = buffer })
end)
mason_lsp_configuration.setup({
ensure_installed = {
-- web dev
--- front-end
"html",
"cssls",
"tailwindcss",
"eslint",
"tsserver",
"astro",
"mdx_analyzer",
--- back-end
"phpactor",
"sqlls",
-- dev-ops
"dockerls",
"docker_compose_language_service",
-- software/cli
"bashls",
"pyright",
"sourcery",
"lua_ls",
-- configuration formats
"jsonls",
"taplo",
"yamlls",
-- documentation
"markdown_oxide",
},
handlers = { setup_language_server },
})
end,
},
{
"ckipp01/stylua-nvim",
event = "BufWritePre",
dependencies = {
"nvim-treesitter/nvim-treesitter",
},
build = {
"which stylua &> /dev/null || cargo install stylua --features lua54",
":TSInstall lua",
},
config = function()
vim.api.nvim_create_autocmd({ "BufWritePre" }, {
group = vim.api.nvim_create_augroup("AutoFormatOnSave", {}),
pattern = "*",
callback = format_file_on_save,
})
end,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment