~whynothugo/dotfiles

ref: 38b900dc12ae810eda071e6fd1e4f25db6bb3a2a dotfiles/home/.config/nvim/init.lua -rw-r--r-- 8.0 KiB
38b900dc — Hugo Osvaldo Barrera nvim: Add borders to hover float windows 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
--  _ __   ___  _____   _(_)_ __ ___
-- | '_ \ / _ \/ _ \ \ / / | '_ ` _ \
-- | | | |  __/ (_) \ V /| | | | | | |
-- |_| |_|\___|\___/ \_/ |_|_| |_| |_|
--
-- Brief overview of how it all works:
--
-- * Plugins and language servers are installed along with nix as part of a
--   single flake which bundles everything..
--
-- * nvim-lspconfig sets up language servers which do a lot of heavy lifting.
--
--   * Lanaguge servers handle diagnostics, autoformatting, and provide
--     completion results.
--   * null-ls uses non-LSP tools as backends, which allows integrating other
--     tools using the same interface (e.g.: black, eslint, flake8, prettier,
--     shellcheck, etc.)
--
-- * nvim-cmp handles autocomplete. It uses LSPs, TS and other sources.
--
-- * lualine.nvim renderes the statusline. It's very lightweight and
--   configurable in lua. The statusline shows LSP diagnostics and a few other
--   details.
--
-- * The gutter refelcts git changes and LSP diagnostics.

-- Language server configuration.
require("lsp")

-- Various plugins
require("_trouble")

-- TODO: Make this wider, enough to fit everything, to a max of 50 chars.
require("symbols-outline").setup({})

-- TODO: This does centering without an extra plugin:
-- https://www.reddit.com/r/neovim/comments/u9ihdt/comment/i5v41e6/?utm_source=reddit&utm_medium=web2x&context=3

-- TODO: Implement support for commenting on Vue
-- See https://github.com/tomtom/tcomment_vim/issues/284#issuecomment-1107845552

-- Autocompletion (nvim-cmp) ==================================================
local cmp = require("cmp")

vim.opt.completeopt = { "menu", "menuone", "noselect" }

cmp.setup({
  sources = {
    { name = "nvim_lsp", priority = 100 }, -- Keep LSP results on top.
    { name = "emoji", option = { insert = true } },
    { name = "path" },
    { name = "spell" },
    { name = "treesitter" },
    { name = "nvim_lua" },
    { name = "buffer", priority = 1 }, -- Keep buffer words last
  },
  mapping = {
    -- TODO: See these other mappings: https://github.com/hrsh7th/nvim-cmp#recommended-configuration
    --       The mapping to scroll the doc overlay might be handy.
    ["<Tab>"] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }),
    ["<S-Tab>"] = cmp.mapping.select_prev_item({
      behavior = cmp.SelectBehavior.Insert,
    }),
  },
  formatting = {
    format = function(entry, vim_item)
      -- set a name for each source
      vim_item.menu = ({
        buffer = "[Buffer]",
        emoji = "[Emoji]",
        nvim_lsp = "[LSP]",
        path = "[Path]",
        spell = "[Spell]",
        treesitter = "[Treesitter]",
        nvim_lua = "[Neovim]",
      })[entry.source.name]
      return vim_item
    end,
  },
})

-- Spelling ===================================================================
vim.opt.spell = true
require("spellsitter").setup() -- Spell checking only on comments.
-- TODO: Disable for files that have no tree sitter suppot.
-- TODO: Disable for comments on code block in markdown files.

-- Fuzzy finder ===============================================================
require("fzf-lua").setup({
  winopts = {
    split = "belowright new",
    preview = {
      hidden = "hidden",
    },
  },
  fzf_opts = {
    ["--layout"] = "default",
  },
})

-- Make CursorHold trigger after 10ms, so git-blame renders immediately:
vim.opt.updatetime = 10

-- Mappings ===================================================================
require("mappings")

vim.api.nvim_set_keymap(
  "n",
  "<Leader>v",
  "<Cmd>SymbolsOutline<CR>",
  { noremap = true, silent = false, expr = false }
)

-- Show a nice pop-up for mappings.
require("which-key").setup()
vim.opt.timeoutlen = 0

-- Appearance =================================================================
local util = require("tokyonight.util")

-- Override tokyonight's colour inversion function to return my own hand-picked
-- background colours. This only replaces the colours when on light mode. To
-- switch to it, use `set background=light`.
local orig = util.invertColor
util.invertColor = function(color)
  if color == "#1a1b26" then
    return "#e9dda7"
  elseif color == "#15161E" then
    return "#e7d99b"
  else
    return orig(color)
  end
end

vim.opt.background = "dark"
vim.g.tokyonight_style = "night"
vim.g.tokyonight_italic_comments = true
vim.g.tokyonight_italic_keywords = false

vim.api.nvim_set_hl(0, "LightBulbVirtualText", { fg = "#1ABC9C" })

vim.cmd("colorscheme tokyonight")

-- colorise hex colours like #558817.
require("colorizer").setup()

-- Show a ruler on the 80th and 120th columns.
vim.opt.colorcolumn = { 80, 120 }

-- Show placeholders for these non-visible-characters.
vim.opt.listchars = {
  tab = ">-",
  trail = "·",
  extends = ">",
  precedes = "<",
}
vim.opt.list = true

-- Show line number for the current line...
vim.opt.number = true
-- ... and relative line numbers for the rest.
vim.opt.relativenumber = true

-- When a line doesn't fit on-screen, it's rendered wrapped, but not actually
-- wrapped. This is very confusing, and reduces readability.
vim.opt.wrap = false

-- Show thin vertical lines on each indentation level.
vim.g.indent_blankline_show_first_indent_level = false
vim.g.indent_blankline_space_char = "·"
vim.g.indent_blankline_space_char_blankline = " "
-- FIXME: Tabs are rendered as eight spaces (e.g.: for Makefiles)

-- git gutter signs and git blame =============================================
require("gitsigns").setup({
  signs = {
    add = { hl = "GitSignsAdd", text = "▋" },
    change = { hl = "GitSignsChange", text = "▋" },
    delete = { hl = "GitSignsDelete", text = "▁ " },
    topdelete = { hl = "GitSignsDelete", text = "▔" },
    changedelete = { hl = "GitSignsChange", text = "▎" },
  },
  current_line_blame = true,
  current_line_blame_opts = {
    delay = 0,
  },
})

-- Show borders for hover floats.
--
-- Neovim shows "hover" information on a DOS style popups. I'd prefer these to
-- be on splits, but that doesn't seem trivial, so at least add borders to make
-- slightly less visually confusing.
--
-- See: https://github.com/jdhao/nvim-config/blob/95b7ee5/lua/config/lsp.lua#L209-L212
vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, {
  border = "rounded",
})
-- TODO: Would be super nice to close floats with <Esc>

-- Language parsing and highlighting ==========================================
-- nvim-treesitter configures tree sitters which, amongst other things, provide
-- highlighting and syntax navigation.

-- Enable highlighting for all installed tree sitters.
require("nvim-treesitter.configs").setup({
  highlight = {
    enable = true,
  },
  -- Playground is basically an inspector.
  playground = {
    enable = true,
  },
  -- Allows using motions around functions and alike.
  textobjects = {
    select = {
      enable = true,

      -- If outside an object, jump into the next one:
      lookahead = true,

      keymaps = {
        ["af"] = "@function.outer",
        ["if"] = "@function.inner",
        ["ac"] = "@class.outer",
        ["ic"] = "@class.inner",
      },
    },
  },
})

-- Status line (lualine) ======================================================
require("lualine").setup({
  options = {
    component_separators = { left = "|", right = "|" },
    section_separators = { left = "", right = "" },
  },
  sections = {
    lualine_b = {
      "branch",
      { "filename", path = 1 },
      "diff",
    },
    -- TODO: indicator that LSP is working.
    lualine_c = { "diagnostics" },
    lualine_x = { "encoding", "fileformat", "filetype" },
  },
})

-- Indentation ================================================================

-- Number of spaces to use for each step of (auto)indent.
-- Note that this is just the default; many filetypes have their own preference.
vim.opt.shiftwidth = 2

-- Use the appropriate number of spaces to insert a <Tab>.
-- To insert a real tab when 'expandtab' is on, use CTRL-v <Tab>.
vim.opt.expandtab = true

-- Break lines longer than 79 characters.
vim.opt.textwidth = 79

-- Completely disable folding
vim.opt.foldenable = false

-- ========== EOF =============================================================