Extract neovim configuration to a separate

...repository

https://github.com/opdavies/opdavies.nvim
This commit is contained in:
Oliver Davies 2024-01-24 08:32:38 +00:00
parent 16520cca16
commit 6d27e72b02
40 changed files with 70 additions and 1696 deletions

View file

@ -1,13 +0,0 @@
P = function(v)
print(vim.inspect(v))
return v
end
RELOAD = function(...)
return require("plenary.reload").reload_module(...)
end
R = function(name)
RELOAD(name)
return require(name)
end

View file

@ -1,22 +0,0 @@
pcall("require", impatient)
require "opdavies.globals"
require("opdavies.options").setup()
require "opdavies.lsp"
require "opdavies.telescope.setup"
require "opdavies.telescope.mappings"
vim.api.nvim_create_user_command("GoToFile", function()
require("opdavies.telescope").git_files()
end, {})
local imap = require("opdavies.keymap").imap
local nmap = require("opdavies.keymap").nmap
local xmap = require("opdavies.keymap").xmap
-- Quicker macro playback.
nmap { "Q", "@qj" }
xmap { "Q", ":norm @q<CR>" }

View file

@ -1,23 +0,0 @@
local M = {}
M.imap = function(tbl)
vim.keymap.set("i", tbl[1], tbl[2], tbl[3])
end
M.map = function(tbl)
vim.keymap.set(tbl[1], tbl[2], tbl[3], tbl[4] or {})
end
M.nmap = function(tbl)
vim.keymap.set("n", tbl[1], tbl[2], tbl[3])
end
M.vmap = function(tbl)
vim.keymap.set("v", tbl[1], tbl[2], tbl[3])
end
M.xmap = function(tbl)
vim.keymap.set("x", tbl[1], tbl[2], tbl[3])
end
return M

View file

@ -1,153 +0,0 @@
local has_lsp, lspconfig = pcall(require, "lspconfig")
if not has_lsp then
return
end
local nvim_status = require "lsp-status"
local imap = require("opdavies.keymap").imap
local nmap = require("opdavies.keymap").nmap
local telescope_mapper = require "opdavies.telescope.mappings"
local buf_nnoremap = function(opts)
opts.buffer = 0
nmap(opts)
end
local buf_inoremap = function(opts)
opts.buffer = 0
imap(opts)
end
local default_capabilities = vim.lsp.protocol.make_client_capabilities()
default_capabilities = require("cmp_nvim_lsp").default_capabilities(default_capabilities)
local custom_init = function(client)
client.config.flags = client.config.flags or {}
client.config.flags.allow_incremental_sync = true
end
local custom_attach = function(client)
local filetype = vim.api.nvim_buf_get_option(0, "filetype")
nvim_status.on_attach(client)
buf_inoremap { "<C-s>", vim.lsp.buf.signature_help }
buf_nnoremap { "<leader>ca", vim.lsp.buf.code_action }
buf_nnoremap { "<leader>d", vim.diagnostic.open_float }
buf_nnoremap { "<leader>rn", vim.lsp.buf.rename }
buf_nnoremap { "<leader>rr", "<cmd>LspRestart<cr>" }
buf_nnoremap { "[d", vim.diagnostic.goto_prev }
buf_nnoremap { "]d", vim.diagnostic.goto_next }
buf_nnoremap { "gD", vim.lsp.buf.declaration }
buf_nnoremap { "gd", vim.lsp.buf.definition }
buf_nnoremap { "gi", vim.lsp.buf.implementation }
buf_nnoremap { "gT", vim.lsp.buf.type_definition }
if filetype ~= "lua" then
buf_nnoremap { "K", vim.lsp.buf.hover }
end
telescope_mapper("<leader>dl", "diagnostics", nil, true)
-- Set autocommands conditional on server_capabilities
if client.server_capabilities.document_highlight then
vim.cmd [[
augroup lsp_document_highlight
autocmd! * <buffer>
autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
augroup END
]]
end
-- Attach any filetype specific options to the client
-- filetype_attach[filetype](client)
end
local servers = {
ansiblels = true,
astro = true,
bashls = true,
cssls = true,
gopls = true,
html = true,
rnix = true,
terraformls = true,
tsserver = true,
vuels = true,
intelephense = {
filetypes = { "php", "module", "test", "inc" },
},
lua_ls = {
settings = {
Lua = {
diagnostics = {
globals = { "vim" },
},
},
},
},
tailwindcss = {
filetypes = { "html", "html.twig", "javascript", "typescript", "vue" },
init_options = {
userLanguages = {
["html.twig"] = "html",
},
},
},
yamlls = {
settings = {
yaml = {
keyOrdering = false,
},
},
},
}
local setup_server = function(server, config)
if not config then
return
end
if type(config) ~= "table" then
config = {}
end
config = vim.tbl_deep_extend("force", {
on_init = custom_init,
on_attach = custom_attach,
capabilities = default_capabilities,
flags = {
debounce_text_changes = nil,
},
}, config)
lspconfig[server].setup(config)
end
for server, config in pairs(servers) do
setup_server(server, config)
end
vim.diagnostic.config {
float = {
source = true,
},
signs = true,
underline = false,
update_in_insert = false,
virtual_text = { spacing = 2 },
}
vim.keymap.set("n", "<leader>f", function()
vim.lsp.buf.format { async = true }
end)
require "opdavies.lsp.none-ls"
require "opdavies.lsp.signature"

View file

@ -1,46 +0,0 @@
local status_ok, null_ls = pcall(require, "null-ls")
if not status_ok then
return
end
local code_actions = null_ls.builtins.code_actions
local diagnostics = null_ls.builtins.diagnostics
local formatting = null_ls.builtins.formatting
null_ls.setup {
sources = {
code_actions.gitsigns,
formatting.black,
formatting.markdownlint,
formatting.phpcbf.with {
command = "./vendor/bin/phpcbf",
condition = function(utils)
return utils.root_has_file { "phpcs.xml.dist" }
end,
},
formatting.prettier,
formatting.rustywind,
formatting.stylua,
diagnostics.eslint.with {
condition = function(utils)
return utils.root_has_file { ".eslintrc.js" }
end,
},
diagnostics.markdownlint.with {
extra_args = { "--config", "~/.markdownlint.yaml" },
},
diagnostics.php,
diagnostics.phpcs.with {
command = "./vendor/bin/phpcs",
condition = function(utils)
return utils.root_has_file { "phpcs.xml.dist" }
end,
},
diagnostics.phpstan,
diagnostics.shellcheck,
},
temp_dir = "/tmp",
}

View file

@ -1,6 +0,0 @@
local status_ok, lsp_signature = pcall(require, "lsp_signature")
if not status_ok then
return
end
lsp_signature.setup {}

View file

@ -1,181 +0,0 @@
local M = {}
local function set_autocmd()
vim.cmd [[
autocmd BufRead,BufNewFile *.test set filetype=php
autocmd FileType gitcommit highlight ColorColumn ctermbg=8
autocmd FileType gitcommit setlocal colorcolumn=50,72
autocmd FileType gitcommit setlocal spell
autocmd FileType gitcommit setlocal textwidth=72
]]
-- Cursorline highlighting control.
-- Only have it on in the current buffer.
local group = vim.api.nvim_create_augroup("CursorLineControl", { clear = true })
local set_cursorline = function(event, value, pattern)
vim.api.nvim_create_autocmd(event, {
group = group,
pattern = pattern,
callback = function()
vim.opt_local.cursorline = value
end,
})
end
set_cursorline("WinLeave", false)
set_cursorline("WinEnter", true)
set_cursorline("FileType", false, "TelescopePrompt")
vim.api.nvim_create_autocmd("TermOpen", {
callback = function()
vim.cmd.set "filetype=term"
end,
})
end
local function set_filetypes()
vim.cmd [[
filetype indent on
filetype on
filetype plugin on
]]
end
local function set_key_mappings()
local map = vim.api.nvim_set_keymap
local options = { noremap = true }
map("n", "<Leader>so", ":call opdavies#save_and_exec()<CR>", options)
-- Format paragraphs to an 80 character line length.
map("n", "<Leader>g", "gqap", options)
map("x", "<Leader>g", "gqa", options)
-- Make the current file executable
map("n", "<Leader>x", ":!chmod +x %<Cr>", options)
-- Yank from the current column to the end of the line
map("n", "Y", "yg$", options)
-- Keep things centred
map("n", "n", "nzzzv", options)
map("n", "N", "Nzzzv", options)
-- Remove arrow keys
map("v", "<down>", "<nop>", options)
map("v", "<left>", "<nop>", options)
map("v", "<right>", "<nop>", options)
map("v", "<up>", "<nop>", options)
-- Clears hlsearch after doing a search, otherwise just does normal <CR> stuff
vim.cmd [[ nnoremap <expr> <CR> {-> v:hlsearch ? ":nohl\<CR>" : "\<CR>"}() ]]
map("n", "<C-f>", ":silent !tmux neww tmux-sessioniser<CR>", { noremap = true, silent = true })
local keymap = require "opdavies.keymap"
local imap = keymap.imap
local nmap = keymap.nmap
local vmap = keymap.vmap
local xmap = keymap.xmap
-- Easy insertion of a trailing ; or , from insert mode
imap { ",,", "<Esc>A,<Esc>" }
imap { ";;", "<Esc>A;<Esc>" }
nmap { "ga", "<Plug>(EasyAlign)" }
xmap { "ga", "<Plug>(EasyAlign)" }
-- Focus on the current buffer.
nmap { "<leader>-", ":wincmd _<cr>:wincmd |<cr>", { noremap = true, silent = true } }
-- Automatically resize buffers.
nmap { "<leader>=", ":wincmd =<cr>", { noremap = true, silent = true } }
-- Move line(s) up and down.
local opts = { noremap = true, silent = true }
imap { "<M-j>", "<Esc>:m .+1<CR>==gi", opts }
imap { "<M-k>", "<Esc>:m .-2<CR>==gi", opts }
nmap { "<M-j>", ":m .+1<CR>==", opts }
nmap { "<M-k>", ":m .-2<CR>==", opts }
vmap { "<M-j>", ":m '>+1<CR>gv=gv", opts }
vmap { "<M-k>", ":m '<-2<CR>gv=gv", opts }
-- Move half a screen up or down and re-center.
nmap { "<C-d>", "<C-d>zz" }
nmap { "<C-u>", "<C-u>zz" }
end
local function set_highlights()
vim.cmd [[highlight Comment cterm=italic gui=italic]]
end
local function set_vim_g()
vim.g.mapleader = " "
end
local function set_vim_o()
local settings = {
autoindent = true,
backup = false,
breakindent = true,
conceallevel = 0,
cursorline = true,
expandtab = true,
foldlevel = 1,
foldlevelstart = 99,
foldmethod = "indent",
formatoptions = "lm",
hidden = false,
inccommand = "split",
linebreak = true,
list = true,
mouse = "n",
number = true,
pumblend = 10,
pumheight = 10,
relativenumber = true,
scrolloff = 10,
shiftwidth = 2,
showmode = false,
signcolumn = "yes:1",
smartindent = true,
softtabstop = 2,
splitbelow = false,
splitright = true,
swapfile = false,
syntax = "on",
tabstop = 2,
termguicolors = true,
textwidth = 0,
undodir = os.getenv("HOME") .. "/.vim/undodir",
undofile = true,
updatetime = 1000,
wrap = false,
}
for key, value in pairs(settings) do
vim.o[key] = value
end
vim.opt.backupdir:remove "." -- keep backups out of the current directory
vim.opt.clipboard:append "unnamedplus"
vim.opt.listchars:append {
trail = "·",
}
vim.opt.completeopt = { "menu", "menuone", "noselect" }
vim.opt.laststatus = 3
end
M.setup = function()
set_vim_g()
set_vim_o()
set_key_mappings()
set_autocmd()
set_filetypes()
set_highlights()
vim.g.snippets = "luasnip"
end
return M

View file

@ -1,10 +0,0 @@
local fmta = require("luasnip.extras.fmt").fmta
local ls = require "luasnip"
local i = ls.insert_node
local M = {
log = fmta("console.log(<>);", { i(1, "value") }),
}
return M

View file

@ -1,27 +0,0 @@
local ls = require "luasnip"
local fmt = require("luasnip.extras.fmt").fmt
local rep = require("luasnip.extras").rep
local f, i = ls.function_node, ls.insert_node
return {
pcall = fmt(
[[
local status_ok, {} = pcall(require, "{}")
if not status_ok then
return
end
]],
{ i(1), rep(1) }
),
req = fmt([[local {} = require "{}"]], {
f(function(import_name)
local parts = vim.split(import_name[1][1], ".", true)
return parts[#parts] or ""
end, { 1 }),
i(1),
}),
}

View file

@ -1,20 +0,0 @@
local fmt = require("luasnip.extras.fmt").fmt
local ls = require "luasnip"
local i = ls.insert_node
local M = {
frontmatter = fmt(
[[
---
title: {}
---
{}
]],
{ i(1), i(0) }
),
link = fmt([[[{}]({}){} ]], { i(1), i(2), i(0) }),
}
return M

View file

@ -1,23 +0,0 @@
local fmta = require("luasnip.extras.fmt").fmta
local ls = require "luasnip"
local c = ls.choice_node
local i = ls.insert_node
local t = ls.text_node
local M = {
vimplugin = fmta(
[[
{
plugin = <>.<>;
type = "lua";
config = ''
<>
'';
}<>
]],
{ c(1, { t "vimPlugins", t "customVim" }), i(2), i(3), i(0) }
),
}
return M

View file

@ -1,61 +0,0 @@
local fmta = require("luasnip.extras.fmt").fmta
local ls = require "luasnip"
local c = ls.choice_node
local f = ls.function_node
local i = ls.insert_node
local t = ls.text_node
local M = {
__construct = fmta(
[[
public function __construct(<>) {
<>
}
]],
{ i(1), i(0) }
),
__invoke = fmta(
[[
public function __invoke(<>) {
<>
}
]],
{ i(1), i(0) }
),
func = fmta("function <>(<>)<> {\n <>\n}<>", { i(1), i(2), i(3), i(4), i(0) }),
met = fmta(
[[
<> function <>(<>)<> {
<>
}<>
]],
{ c(1, { t "public", t "protected", t "private" }), i(2), i(3), i(4), i(5), i(0) }
),
pest = fmta("<>('<>', function() {\n <>\n});", { c(1, { t "it", t "test" }), i(2), i(0) }),
test = fmta(
[[
public function test<>(): void {
<>
}<>
]],
{ i(1), i(2), i(0) }
),
testa = fmta(
[[
/** @test */
public function <>(): void {
<>
}<>
]],
{ i(1), i(2), i(0) }
),
}
return M

View file

@ -1,49 +0,0 @@
local fmta = require("luasnip.extras.fmt").fmta
local ls = require "luasnip"
local i = ls.insert_node
local f = ls.function_node
local fill_line = function(char)
return function()
local row = vim.api.nvim_win_get_cursor(0)[1]
local lines = vim.api.nvim_buf_get_lines(0, row - 2, row, false)
return string.rep(char, #lines[1])
end
end
local M = {
class = { ".. class:: ", i(1) },
footer = { ".. footer:: ", i(1) },
link = { ".. _", i(1), ":" },
raw = { ".. raw:: ", i(1) },
-- TODO: add an optional new line and ":width" property.
image = { ".. image:: ", i(1) },
head = f(fill_line "=", {}),
sub = f(fill_line "-", {}),
subsub = f(fill_line "^", {}),
-- Add a page break with an optional page template.
pb = fmta(
[[
.. raw:: pdf
PageBreak<>
]],
{ i(0) }
),
-- Add a new speaker note.
ta = fmta(
[[
.. raw:: pdf
TextAnnotation "<>"
]],
{ i(0) }
),
}
return M

View file

@ -1,161 +0,0 @@
SHOULD_RELOAD_TELESCOPE = true
local reloader = function()
if SHOULD_RELOAD_TELESCOPE then
RELOAD "plenary"
RELOAD "telescope"
RELOAD "opdavies.telescope.setup"
end
end
local themes = require "telescope.themes"
local M = {}
function M.current_buf()
local opts = {
sorting_strategy = "ascending",
previewer = false,
}
require("telescope.builtin").current_buffer_fuzzy_find(opts)
end
M.diagnostics = function()
local theme = require("telescope.themes").get_dropdown {
previewer = false,
}
require("telescope.builtin").diagnostics(theme)
end
function M.lsp_document_symbols()
local theme = require("telescope.themes").get_dropdown {
previewer = false,
}
require("telescope.builtin").lsp_document_symbols(theme)
end
function M.edit_neovim()
local opts = {
cwd = "~/Code/github.com/opdavies/dotfiles/config/neovim",
find_command = { "rg", "--no-ignore", "--files", "--follow" },
path_display = { "shorten" },
prompt_title = "~ dotfiles ~",
no_ignore = true,
layout_strategy = "flex",
layout_config = {
height = 0.8,
prompt_position = "top",
width = 0.9,
horizontal = {
width = { padding = 0.15 },
},
vertical = {
preview_height = 0.75,
},
},
}
require("telescope.builtin").find_files(opts)
end
function M.edit_zsh()
local opts = {
cwd = "~/.config/zsh",
path_display = { "shorten" },
prompt_title = "~ zsh ~",
no_ignore = true,
layout_strategy = "flex",
layout_config = {
height = 0.8,
prompt_position = "top",
width = 0.9,
horizontal = {
width = { padding = 0.15 },
},
vertical = {
preview_height = 0.75,
},
},
}
require("telescope.builtin").find_files(opts)
end
function M.file_browser()
local opts = {
cwd = vim.fn.expand "%:p:h",
sorting_strategy = "ascending",
}
require("telescope").extensions.file_browser.file_browser(opts)
end
function M.find_files()
local opts = {
file_ignore_patterns = { ".git/", "**/{core,contrib}", "vendor" },
}
require("telescope.builtin").find_files(opts)
end
function M.find_all_files()
local opts = {
no_ignore = true,
prompt_title = "All Files",
}
require("telescope.builtin").find_files(opts)
end
function M.git_files()
local opts = {
file_ignore_patterns = { ".git/", "**/{core,contrib}", "vendor" },
hidden = true,
no_ignore = true,
}
require("telescope.builtin").git_files(opts)
end
function M.grep_prompt()
require("telescope.builtin").grep_string {
path_display = { "shorten" },
search = vim.fn.input "Grep String > ",
}
end
function M.live_grep()
require("telescope").extensions.live_grep_args.live_grep_args {
file_ignore_patterns = { ".git/" },
hidden = true,
no_ignore = true,
sorting_strategy = "ascending",
}
end
function M.oldfiles()
local opts = {
prompt_title = "History",
}
require("telescope").extensions.frecency.frecency(opts)
end
return setmetatable({}, {
__index = function(_, k)
reloader()
if M[k] then
return M[k]
else
return require("telescope.builtin")[k]
end
end,
})

View file

@ -1,62 +0,0 @@
TelescopeMapArgs = TelescopeMapArgs or {}
local telescope = require "telescope"
local telescope_mapper = function(key, f, options, buffer)
local map_key = vim.api.nvim_replace_termcodes(key .. f, true, true, true)
TelescopeMapArgs[map_key] = options or {}
local mode = "n"
local rhs = string.format("<cmd>lua R('opdavies.telescope')['%s'](TelescopeMapArgs['%s'])<CR>", f, map_key)
local map_options = {
noremap = true,
silent = true,
}
if not buffer then
vim.api.nvim_set_keymap(mode, key, rhs, map_options)
else
vim.api.nvim_buf_set_keymap(0, mode, key, rhs, map_options)
end
end
telescope_mapper("<leader>fb", "buffers")
telescope_mapper("<leader>fd", "find_files")
telescope_mapper("<leader>fD", "find_all_files")
telescope_mapper("<leader>fe", "file_browser")
telescope_mapper("<leader>ff", "current_buf")
telescope_mapper("<leader>fg", "git_files")
telescope_mapper("<leader>fh", "help_tags")
telescope_mapper("<leader>fl", "live_grep")
telescope_mapper("<leader>fo", "oldfiles")
local builtin = require "telescope.builtin"
local nmap = require("opdavies.keymap").nmap
nmap {
"<leader>fw",
function()
local word = vim.fn.expand "<cword>"
builtin.grep_string { search = word }
end,
}
nmap {
"<leader>fW",
function()
local word = vim.fn.expand "<cWORD>"
builtin.grep_string { search = word }
end,
}
telescope_mapper("<leader>gp", "grep_prompt")
telescope_mapper("<leader>ds", "lsp_document_symbols")
telescope_mapper("<leader>dl", "diagnostics")
telescope_mapper("<leader>en", "edit_neovim")
telescope_mapper("<leader>ez", "edit_zsh")
return telescope_mapper

View file

@ -1,101 +0,0 @@
local status_ok, telescope = pcall(require, "telescope")
if not status_ok then
return
end
local Job = require "plenary.job"
local previewers = require "telescope.previewers"
-- Create a new maker that won't preview binary files
-- https://github.com/nvim-telescope/telescope.nvim/wiki/Configuration-Recipes#dont-preview-binaries
local new_maker = function(filepath, bufnr, opts)
filepath = vim.fn.expand(filepath)
Job:new({
command = "file",
args = { "--mime-type", "-b", filepath },
on_exit = function(j)
local mime_type = vim.split(j:result()[1], "/")[1]
if mime_type == "text" then
previewers.buffer_previewer_maker(filepath, bufnr, opts)
else
vim.schedule(function()
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { "BINARY" })
end)
end
end,
}):sync()
end
local action_layout = require "telescope.actions.layout"
local actions = require "telescope.actions"
local lga_actions = require "telescope-live-grep-args.actions"
telescope.setup {
defaults = {
buffer_previewer_maker = new_maker,
file_ignore_patterns = { ".direnv/", ".git/" },
layout_config = {
prompt_position = "top",
},
preview = {
hide_on_startup = true,
},
mappings = {
i = {
["<C-d>"] = actions.delete_buffer,
["<C-h>"] = actions.which_key,
["<M-p>"] = action_layout.toggle_preview,
["<M-m>"] = action_layout.toggle_mirror,
},
},
no_ignore = true,
path_display = { truncate = 1 },
prompt_prefix = "$ ",
sorting_strategy = "ascending",
},
pickers = {
find_files = {
hidden = true,
},
buffers = {
previewer = false,
layout_config = {
width = 80,
},
},
lsp_references = {
previewer = false,
},
},
extensions = {
file_browser = {
theme = "ivy",
},
live_grep_args = {
auto_quoting = true,
mappings = {
i = {
["<C-k>"] = lga_actions.quote_prompt {},
["<C-i>"] = lga_actions.quote_prompt { postfix = " --iglob " },
},
},
},
["ui-select"] = {
require("telescope.themes").get_dropdown {},
},
},
}
telescope.load_extension "file_browser"
telescope.load_extension "fzf"
telescope.load_extension "refactoring"
telescope.load_extension "ui-select"