Skip to content

Quick Neovim Extension Example

Published: at 02:30 PM

Problem to solve

Is there anything more annoying than submitting a perfectly crafted PR just to realize that you missed capitalizing some SQL keywords? This happened a couple of times to me, so I quickly build some custom functions in neovim to catch this before I submit a PR. There are some plugins that will automatically capitalize the keywords as you are typing, but somethimes they will capitalize SOME words that you don’t want to be capitalized. I also sometimes copy and paste queries between neovim and data grip, so I wanted a solution to check and then easily capitalize the words.

Highlight lowercase sql keywords

local M = {}

function M.highlight_words(words)
  if vim.g.my_highlight_id then
    vim.fn.matchdelete(vim.g.my_highlight_id)
  end

  vim.cmd 'highlight default MyHighlight ctermfg=Black ctermbg=Green guifg=Black guibg=Green'

  local pattern = '\\<\\(' .. table.concat(words, '\\|') .. '\\)\\>'

  vim.g.my_highlight_id = vim.fn.matchadd('MyHighlight', pattern)
end

function M.reset_highlight()
  if vim.g.my_highlight_id then
    vim.fn.matchdelete(vim.g.my_highlight_id)
    vim.g.my_highlight_id = nil
  end

  vim.cmd 'highlight clear MyHighlight'
end

-- List of SQL keywords
local sql_keywords = {
  'select',
  'insert',
  'update',
  'delete',
  'from',
  'where',
  'join',
  'inner',
  'left',
  'right',
  'full',
  'on',
  'group',
  'by',
  'order',
  'having',
  'limit',
  'offset',
  'distinct',
  'union',
  'all',
  'as',
  'in',
  'exists',
  'between',
  'like',
  'ilike',
  'not',
  'null',
  'is',
  'true',
  'false',
  'create',
  'alter',
  'drop',
  'table',
  'index',
  'view',
  'sequence',
  'trigger',
  'function',
  'procedure',
  'database',
  'schema',
  'grant',
  'revoke',
  'primary',
  'key',
  'foreign',
  'references',
  'check',
  'default',
  'constraint',
  'explain',
  'analyze',
  'vacuum',
  'reindex',
  'cluster',
  'comment',
  'copy',
  'with',
  'recursive',
  'cast',
  'type',
  'enum',
  'range',
  'collate',
  'extension',
  'language',
  'aggregate',
  'operator',
  'rule',
  'policy',
  'role',
  'user',
  'group',
  'login',
  'password',
  'inherit',
  'noinherit',
  'valid',
  'invalid',
  'rename',
  'owner',
  'set',
  'reset',
  'show',
  'start',
  'transaction',
  'commit',
  'rollback',
  'savepoint',
  'release',
  'prepare',
  'execute',
  'deallocate',
  'listen',
  'notify',
  'unlisten',
  'discard',
  'lock',
  'unlock',
  'disable',
  'enable',
  'reindex',
  'cluster',
  'analyze',
  'vacuum',
  'reindex',
  'cluster',
  'analyze',
  'vacuum',
}

vim.cmd(
  'command! HighlightLowercaseSqlKeywords lua require("user_functions.highlightLowercaseSqlKeywords").highlight_words(' .. vim.inspect(sql_keywords) .. ')'
)
vim.cmd 'command! ResetHighlight lua require("user_functions.highlightLowercaseSqlKeywords").reset_highlight()'

vim.keymap.set('n', '<leader>hi', function()
  M.highlight_words(sql_keywords)
end, { remap = true, silent = false })
vim.keymap.set('n', '<leader>hc', function()
  M.reset_highlight()
end, { remap = true, silent = false })

return M

Quickly capitalize words

local M = {}

function M.delete_and_paste_uppercase()
  vim.cmd 'normal! diw'

  local word = vim.fn.getreg '"'
  local upper_word = string.upper(word)

  vim.fn.setreg('a', upper_word)

  local cursor_pos = vim.api.nvim_win_get_cursor(0)

  vim.api.nvim_win_set_cursor(0, { cursor_pos[1], cursor_pos[2] - 1 })

  vim.cmd 'normal! "ap'
end

vim.cmd 'command! DeleteAndPasteUppercase lua require("user_functions.capitalizeWord").delete_and_paste_uppercase()'

vim.keymap.set('n', '<leader>u', M.delete_and_paste_uppercase, { remap = true, silent = false })

return M


Previous Post
Treekanga
Next Post
Aerospace