From 107e108455e08761bf986179d162b66e8d589642 Mon Sep 17 00:00:00 2001 From: Nikos Papadakis Date: Tue, 25 Apr 2023 20:23:42 +0300 Subject: [PATCH] nvim --- .../nvim/after/plugin/lspconfig.lua | 59 +++++++++++-------- nvim/dot-config/nvim/lua/my/tab.lua | 2 +- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/nvim/dot-config/nvim/after/plugin/lspconfig.lua b/nvim/dot-config/nvim/after/plugin/lspconfig.lua index e582101..9f66d83 100644 --- a/nvim/dot-config/nvim/after/plugin/lspconfig.lua +++ b/nvim/dot-config/nvim/after/plugin/lspconfig.lua @@ -4,8 +4,6 @@ if not ok then return end --- local has_lint, lint = pcall(require, "lint") - local nmap = require("my.utils").nmap local border = { @@ -44,6 +42,23 @@ local format_augroup = setmetatable({}, { end, }) +local format_saving = {} + +local format_handler = function(err, result, ctx) + if not result then + return + end + + local client = vim.lsp.get_client_by_id(ctx.client_id) + vim.lsp.util.apply_text_edits(result, ctx.bufnr, client.offset_encoding) + + if ctx.bufnr == vim.api.nvim_get_current_buf() then + format_saving[ctx.bufnr] = true + vim.cmd [[update]] + format_saving[ctx.bufnr] = false + end +end + local on_attach = function(client, bufnr) nmap("r", vim.lsp.buf.rename, "Rename", bufnr) nmap("cf", vim.lsp.buf.format, "Format document", bufnr) @@ -82,10 +97,20 @@ local on_attach = function(client, bufnr) -- Format on save if client.server_capabilities.documentFormattingProvider then - vim.api.nvim_create_autocmd({ "BufWritePre" }, { + vim.lsp.handlers["textDocument/formatting"] = vim.lsp.with(format_handler, {}) + vim.api.nvim_clear_autocmds { + buffer = bufnr, group = format_augroup[client.id], + } + vim.api.nvim_create_autocmd({ "BufWritePost" }, { + group = format_augroup[client.id], + desc = "format on save", + pattern = "", callback = function() - vim.lsp.buf.format { async = false } + if format_saving[bufnr] == true then + return + end + vim.lsp.buf.format({ async = true }) end, }) end @@ -93,29 +118,12 @@ local on_attach = function(client, bufnr) if has_lsp_status then lsp_status.on_attach(client) end - - -- if has_lint then - -- if vim.filetype.match({ buf = bufnr }) == "php" then - -- lint.linters.psalm.cmd = "./vendor/bin/psalm" - -- end - - -- lint.linters_by_ft = { - -- php = { "psalm" } - -- } - - -- vim.api.nvim_create_autocmd({ "BufWritePost" }, { - -- pattern = { "*.php" }, - -- callback = function() - -- lint.try_lint() - -- end - -- }) - -- end end local servers = { gopls = {}, - phpactor = {}, - pylsp = {}, + -- phpactor = {}, + -- pylsp = {}, rust_analyzer = { assist = { importGranularity = "module", @@ -148,8 +156,9 @@ local servers = { }, }, }, - denols = {}, - svelte = {}, + ocamllsp = {}, + -- denols = {}, + -- svelte = {}, -- tailwindcss = { -- root_dir = lspconfig.util.root_pattern("tailwind.config.js", "tailwind.config.ts", "tailwind.config.cjs"), -- }, diff --git a/nvim/dot-config/nvim/lua/my/tab.lua b/nvim/dot-config/nvim/lua/my/tab.lua index ad4a54b..addc1b7 100644 --- a/nvim/dot-config/nvim/lua/my/tab.lua +++ b/nvim/dot-config/nvim/lua/my/tab.lua @@ -23,7 +23,7 @@ local get_new_position = function(node, direction) end local is_wrapped = function() - local text = vim.treesitter.query.get_node_text(node, 0) + local text = vim.treesitter.get_node_text(node, 0) local first = string.sub(text, 1, 1) local last = string.sub(text, -1) return pairs[first] == last