Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

neovim/lua/bugabinga/lsp/attach.lua

Raw
local map = require 'std.map'
local auto = require 'std.auto'

local lsp_attach = function( args )
	local bufnr = args.buf
	local client_id = args.data.client_id
	local client = vim.lsp.get_client_by_id( client_id )
	if not client then return end

	if client:supports_method 'textDocument/completion' then
		vim.lsp.completion.enable( true, client.id, bufnr, {
			autotrigger = false,
		} )
	end

	if client:supports_method 'textDocument/inlayHint' then
		map.normal {
			description = 'Toggle inlay hints',
			category = 'lsp',
			buffer = bufnr,
			keys = '<leader>ti',
			command = function()
				local current = vim.lsp.inlay_hint.is_enabled { bufnr = bufnr }
				vim.lsp.inlay_hint.enable( not current, { bufnr = bufnr } )
			end,
		}
	end

	map.normal {
		description = 'Go to definition',
		category = 'lsp',
		buffer = bufnr,
		keys = 'grd',
		command = vim.lsp.buf.definition,
	}
	map.normal {
		description = 'List workspaces',
		category = 'lsp',
		buffer = bufnr,
		keys = 'grwl',
		command = vim.lsp.buf.list_workspace_folders,
	}
	map.normal {
		description = 'Add workspace',
		category = 'lsp',
		buffer = bufnr,
		keys = 'grwa',
		command = vim.lsp.buf.add_workspace_folder,
	}
	map.normal {
		description = 'Remove workspace',
		category = 'lsp',
		buffer = bufnr,
		keys = 'grwr',
		command = vim.lsp.buf.remove_workspace_folder,
	}
	map.normal {
		description = 'Incoming calls',
		category = 'lsp',
		buffer = bufnr,
		keys = 'grc',
		command = vim.lsp.buf.incoming_calls,
	}
	map.normal {
		description = 'Outgoing calls',
		category = 'lsp',
		buffer = bufnr,
		keys = 'grC',
		command = vim.lsp.buf.outgoing_calls,
	}
	map.normal {
		description = 'Type definition',
		category = 'lsp',
		buffer = bufnr,
		keys = 'grt',
		command = vim.lsp.buf.type_definition,
	}
	map.normal {
		description = 'Subtypes',
		category = 'lsp',
		buffer = bufnr,
		keys = 'grh',
		command = function() vim.lsp.buf.typehierarchy 'subtypes' end,
	}
	map.normal {
		description = 'Supertypes',
		category = 'lsp',
		buffer = bufnr,
		keys = 'grH',
		command = function() vim.lsp.buf.typehierarchy 'supertypes' end,
	}
	map.normal {
		description = 'Declaration',
		category = 'lsp',
		buffer = bufnr,
		keys = 'grD',
		command = vim.lsp.buf.declaration,
	}
	map.normal {
		description = 'Workspace symbol',
		category = 'lsp',
		buffer = bufnr,
		keys = 'grs',
		command = vim.lsp.buf.workspace_symbol,
	}

	if client:supports_method 'textDocument/codeLens' then
		map.normal {
			description = 'Run code lens',
			category = 'lsp',
			buffer = bufnr,
			keys = 'grl',
			command = vim.lsp.codelens.run,
		}
		vim.lsp.codelens.enable(true, { bufnr = bufnr })
	end

	if client:supports_method 'textDocument/documentHighlight' then
		auto( 'lsp_doc_highlight_' .. bufnr ) {
			events = { 'CursorHold', 'CursorHoldI' },
			buffer = bufnr,
			command = vim.lsp.buf.document_highlight,
		}
		auto( 'lsp_clear_references_' .. bufnr ) {
			events = { 'CursorMoved', 'InsertEnter' },
			buffer = bufnr,
			command = vim.lsp.buf.clear_references,
		}
	end
end

auto 'lsp' {
	description = 'lsp: client attached to buffer',
	events = 'LspAttach',
	command = lsp_attach,
}