Skip to content

Commit

Permalink
feat: added python import
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonvermeulen committed Oct 17, 2024
1 parent d471601 commit e282e60
Showing 1 changed file with 182 additions and 130 deletions.
312 changes: 182 additions & 130 deletions lua/ramon/plugins/lsp/lsp.lua
Original file line number Diff line number Diff line change
@@ -1,135 +1,187 @@
return {
"neovim/nvim-lspconfig",
event = { "BufReadPre", "BufNewFile" },
dependencies = {
"hrsh7th/cmp-nvim-lsp",
{ "antosha417/nvim-lsp-file-operations", config = true },
{ "folke/neodev.nvim", opts = {} },
},
config = function()
local lspconfig = require("lspconfig")
local mason_lspconfig = require("mason-lspconfig")
local cmp_nvim_lsp = require("cmp_nvim_lsp")

local keymap = vim.keymap

vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup("UserLspConfig", {}),
callback = function(ev)
-- Buffer local mappings.
-- See `:help vim.lsp.*` for documentation on any of the below functions
local opts = { buffer = ev.buf, silent = true }

-- set keybinds
opts.desc = "Show LSP references"
keymap.set("n", "gR", "<cmd>Telescope lsp_references<CR>", opts) -- show definition, references

opts.desc = "Go to declaration"
keymap.set("n", "gD", vim.lsp.buf.declaration, opts) -- go to declaration

opts.desc = "Show LSP definitions"
keymap.set("n", "gd", "<cmd>Telescope lsp_definitions<CR>", opts) -- show lsp definitions

opts.desc = "Show LSP implementations"
keymap.set("n", "gi", "<cmd>Telescope lsp_implementations<CR>", opts) -- show lsp implementations

opts.desc = "Show LSP type definitions"
keymap.set("n", "gt", "<cmd>Telescope lsp_type_definitions<CR>", opts) -- show lsp type definitions

opts.desc = "See available code actions"
keymap.set({ "n", "v" }, "<leader>ca", vim.lsp.buf.code_action, opts) -- see available code actions, in visual mode will apply to selection

opts.desc = "Smart rename"
keymap.set("n", "<leader>rn", vim.lsp.buf.rename, opts) -- smart rename

opts.desc = "Show buffer diagnostics"
keymap.set("n", "<leader>D", "<cmd>Telescope diagnostics bufnr=0<CR>", opts) -- show diagnostics for file

opts.desc = "Show line diagnostics"
keymap.set("n", "<leader>d", vim.diagnostic.open_float, opts) -- show diagnostics for line

opts.desc = "Go to previous diagnostic"
keymap.set("n", "[d", vim.diagnostic.goto_prev, opts) -- jump to previous diagnostic in buffer

opts.desc = "Go to next diagnostic"
keymap.set("n", "]d", vim.diagnostic.goto_next, opts) -- jump to next diagnostic in buffer

opts.desc = "Show documentation for what is under cursor"
keymap.set("n", "K", vim.lsp.buf.hover, opts) -- show documentation for what is under cursor

opts.desc = "Restart LSP"
keymap.set("n", "<leader>rs", ":LspRestart<CR>", opts) -- mapping to restart lsp if necessary
end,
})

-- used to enable autocompletion (assign to every lsp server config)
local capabilities = cmp_nvim_lsp.default_capabilities()

-- Change the Diagnostic symbols in the sign column (gutter)
local signs = { Error = "", Warn = "", Hint = "󰠠 ", Info = "" }
for type, icon in pairs(signs) do
local hl = "DiagnosticSign" .. type
vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = "" })
end

mason_lspconfig.setup_handlers({
-- default handler for installed servers
function(server_name)
lspconfig[server_name].setup({
capabilities = capabilities,
})
end,
-- TODO(ramon) find a way to move these specific lsp configs to separate files
-- and loop over the files here
["lua_ls"] = function()
-- configure lua server (with special settings)
lspconfig["lua_ls"].setup({
capabilities = capabilities,
settings = {
Lua = {
-- make the language server recognize "vim" global
diagnostics = {
globals = { "vim" },
},
completion = {
callSnippet = "Replace",
{
"neovim/nvim-lspconfig",
event = { "BufReadPre", "BufNewFile" },
dependencies = {
"hrsh7th/cmp-nvim-lsp",
{ "antosha417/nvim-lsp-file-operations", config = true },
{ "folke/neodev.nvim", opts = {} },
},
config = function()
local lspconfig = require("lspconfig")
local mason_lspconfig = require("mason-lspconfig")
local cmp_nvim_lsp = require("cmp_nvim_lsp")

local keymap = vim.keymap

vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup("UserLspConfig", {}),
callback = function(ev)
-- Buffer local mappings.
-- See `:help vim.lsp.*` for documentation on any of the below functions
local opts = { buffer = ev.buf, silent = true }

-- set keybinds
opts.desc = "Show LSP references"
keymap.set("n", "gR", "<cmd>Telescope lsp_references<CR>", opts) -- show definition, references

opts.desc = "Go to declaration"
keymap.set("n", "gD", vim.lsp.buf.declaration, opts) -- go to declaration

opts.desc = "Show LSP definitions"
keymap.set("n", "gd", "<cmd>Telescope lsp_definitions<CR>", opts) -- show lsp definitions

opts.desc = "Show LSP implementations"
keymap.set("n", "gi", "<cmd>Telescope lsp_implementations<CR>", opts) -- show lsp implementations

opts.desc = "Show LSP type definitions"
keymap.set("n", "gt", "<cmd>Telescope lsp_type_definitions<CR>", opts) -- show lsp type definitions

opts.desc = "See available code actions"
keymap.set({ "n", "v" }, "<leader>ca", vim.lsp.buf.code_action, opts) -- see available code actions, in visual mode will apply to selection

opts.desc = "Smart rename"
keymap.set("n", "<leader>rn", vim.lsp.buf.rename, opts) -- smart rename

opts.desc = "Show buffer diagnostics"
keymap.set("n", "<leader>D", "<cmd>Telescope diagnostics bufnr=0<CR>", opts) -- show diagnostics for file

opts.desc = "Show line diagnostics"
keymap.set("n", "<leader>d", vim.diagnostic.open_float, opts) -- show diagnostics for line

opts.desc = "Go to previous diagnostic"
keymap.set("n", "[d", vim.diagnostic.goto_prev, opts) -- jump to previous diagnostic in buffer

opts.desc = "Go to next diagnostic"
keymap.set("n", "]d", vim.diagnostic.goto_next, opts) -- jump to next diagnostic in buffer

opts.desc = "Show documentation for what is under cursor"
keymap.set("n", "K", vim.lsp.buf.hover, opts) -- show documentation for what is under cursor

opts.desc = "Restart LSP"
keymap.set("n", "<leader>rs", ":LspRestart<CR>", opts) -- mapping to restart lsp if necessary
end,
})

-- used to enable autocompletion (assign to every lsp server config)
local capabilities = cmp_nvim_lsp.default_capabilities()

-- Change the Diagnostic symbols in the sign column (gutter)
local signs = { Error = "", Warn = "", Hint = "󰠠 ", Info = "" }
for type, icon in pairs(signs) do
local hl = "DiagnosticSign" .. type
vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = "" })
end

mason_lspconfig.setup_handlers({
-- default handler for installed servers
function(server_name)
lspconfig[server_name].setup({
capabilities = capabilities,
})
end,
-- TODO(ramon) find a way to move these specific lsp configs to separate files
-- and loop over the files here
["lua_ls"] = function()
-- configure lua server (with special settings)
lspconfig["lua_ls"].setup({
capabilities = capabilities,
settings = {
Lua = {
-- make the language server recognize "vim" global
diagnostics = {
globals = { "vim" },
},
completion = {
callSnippet = "Replace",
},
},
},
},
})
end,
["yamlls"] = function()
lspconfig["yamlls"].setup({
capabilities = capabilities,
schemaStore = {
enable = true,
url = "https://www.schemastore.org/api/json/catalog.json",
},
schemas = {
kubernetes = "*.yaml",
["http://json.schemastore.org/github-workflow"] = ".github/workflows/*",
["http://json.schemastore.org/github-action"] = ".github/action.{yml,yaml}",
["https://raw.githubusercontent.com/microsoft/azure-pipelines-vscode/master/service-schema.json"] = "azure-pipelines*.{yml,yaml}",
["https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/ansible.json#/$defs/tasks"] = "roles/tasks/*.{yml,yaml}",
["https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/ansible.json#/$defs/playbook"] = "*play*.{yml,yaml}",
["http://json.schemastore.org/prettierrc"] = ".prettierrc.{yml,yaml}",
["http://json.schemastore.org/kustomization"] = "kustomization.{yml,yaml}",
["http://json.schemastore.org/chart"] = "Chart.{yml,yaml}",
["https://json.schemastore.org/dependabot-v2"] = ".github/dependabot.{yml,yaml}",
["https://gitlab.com/gitlab-org/gitlab/-/raw/master/app/assets/javascripts/editor/schema/ci.json"] = "*gitlab-ci*.{yml,yaml}",
["https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/schemas/v3.1/schema.json"] = "*api*.{yml,yaml}",
["https://raw.githubusercontent.com/compose-spec/compose-spec/master/schema/compose-spec.json"] = "*docker-compose*.{yml,yaml}",
["https://raw.githubusercontent.com/argoproj/argo-workflows/master/api/jsonschema/schema.json"] = "*flow*.{yml,yaml}",
},
format = { enabled = false },
-- anabling this conflicts between Kubernetes resources and kustomization.yaml and Helmreleases
-- see utils.custom_lsp_attach() for the workaround
-- how can I detect Kubernetes ONLY yaml files? (no CRDs, Helmreleases, etc.)
validate = false,
completion = true,
hover = true,
})
})
end,
["yamlls"] = function()
lspconfig["yamlls"].setup({
capabilities = capabilities,
schemaStore = {
enable = true,
url = "https://www.schemastore.org/api/json/catalog.json",
},
schemas = {
kubernetes = "*.yaml",
["http://json.schemastore.org/github-workflow"] = ".github/workflows/*",
["http://json.schemastore.org/github-action"] = ".github/action.{yml,yaml}",
["https://raw.githubusercontent.com/microsoft/azure-pipelines-vscode/master/service-schema.json"] = "azure-pipelines*.{yml,yaml}",
["https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/ansible.json#/$defs/tasks"] = "roles/tasks/*.{yml,yaml}",
["https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/ansible.json#/$defs/playbook"] = "*play*.{yml,yaml}",
["http://json.schemastore.org/prettierrc"] = ".prettierrc.{yml,yaml}",
["http://json.schemastore.org/kustomization"] = "kustomization.{yml,yaml}",
["http://json.schemastore.org/chart"] = "Chart.{yml,yaml}",
["https://json.schemastore.org/dependabot-v2"] = ".github/dependabot.{yml,yaml}",
["https://gitlab.com/gitlab-org/gitlab/-/raw/master/app/assets/javascripts/editor/schema/ci.json"] = "*gitlab-ci*.{yml,yaml}",
["https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/schemas/v3.1/schema.json"] = "*api*.{yml,yaml}",
["https://raw.githubusercontent.com/compose-spec/compose-spec/master/schema/compose-spec.json"] = "*docker-compose*.{yml,yaml}",
["https://raw.githubusercontent.com/argoproj/argo-workflows/master/api/jsonschema/schema.json"] = "*flow*.{yml,yaml}",
},
format = { enabled = false },
-- anabling this conflicts between Kubernetes resources and kustomization.yaml and Helmreleases
-- see utils.custom_lsp_attach() for the workaround
-- how can I detect Kubernetes ONLY yaml files? (no CRDs, Helmreleases, etc.)
validate = false,
completion = true,
hover = true,
})
end,
})
end,
},
{
-- pyright doesn't support import code actions ...
"kiyoon/python-import.nvim",
build = "uv tool install . --force --reinstall",
keys = {
{
"<space>i",
function()
require("python_import.api").add_import_current_word_and_move_cursor()
end,
mode = "n",
silent = true,
desc = "Add python import and move cursor",
ft = "python",
},
{
"<space>i",
function()
require("python_import.api").add_import_current_selection_and_move_cursor()
end,
mode = "x",
silent = true,
desc = "Add python import and move cursor",
ft = "python",
},
{
"<space>tr",
function()
require("python_import.api").add_rich_traceback()
end,
silent = true,
desc = "Add rich traceback",
ft = "python",
},
},
opts = {
---Return nil to indicate no match is found and continue with the default lookup
---Return a table to stop the lookup and use the returned table as the result
---Return an empty table to stop the lookup. This is useful when you want to add to wherever you need to.
---@type fun(winnr: integer, word: string, ts_node: TSNode?): string[]?
custom_function = function(winnr, word, ts_node)
-- if vim.endswith(word, "_DIR") then
-- return { "from my_module import " .. word }
-- end
end,
})
end,
},
},
{
"rcarriga/nvim-notify",
},
}

0 comments on commit e282e60

Please sign in to comment.