Rewrite vim plugin extensions in lua

pull/1/head
Ensar Sarajčić 2021-03-05 11:08:05 +01:00
parent 00efb1065b
commit 793e34f355
9 changed files with 260 additions and 224 deletions

View File

@ -0,0 +1,82 @@
-------------------------------------------------------------------------------
-- - Fugitive.vim extensions library -
-------------------------------------------------------------------------------
-- Shorcut to push directly to current branch on origin
-- Similar to `ggpush` in fish config
local function push_origin()
vim.cmd('Git push origin ' .. vim.fn.FugitiveHead())
end
-- Shorcut to pull directly from current branch on origin
-- Similar to `ggpull` in fish config
local function pull_origin()
vim.cmd('Git pull origin ' .. vim.fn.FugitiveHead())
end
-- Generates url for creating PR for current branch
-- Tested only with github.com
-- Works regardless of ssh or https for origin config
-- Hardcoded to use 'origin' remote
local function get_pr_url(...)
local origin_url = vim.fn['fugitive#RemoteUrl']('origin')
origin_url = string.gsub(origin_url, '.git$', '')
origin_url = string.gsub(origin_url, ':', '/')
origin_url = string.gsub(origin_url, 'git@', 'https://')
-- Remove prefix if it is available, for some of common git services
local common_services = {'github.com', 'bitbucket.org', 'gitlab.com'}
for k,service in pairs(common_services) do
if (string.find(origin_url, service, 1, true))
then
-- Common mechanism for managing multiple SSH keys
origin_url = string.gsub(origin_url, '://.*' .. service, '://' .. service)
end
end
-- This part probably only works on github
local pr_url
if (select('#', ...) == 0)
then
pr_url = origin_url .. '/compare/' .. vim.fn.FugitiveHead() .. '?expand=1'
else
pr_url = origin_url .. '/compare/' .. select(1, ...) .. '...' .. vim.fn.FugitiveHead() .. '?expand=1'
end
return pr_url
end
-------------------------------------------------------------------------------
-- - Public API -
-------------------------------------------------------------------------------
local M = {}
-- Prints current branches PR url (not saved to :messages)
-- Makes it easy to use terminal for opening url on click
M.print_pr_url = function(...)
vim.cmd('echo "' .. get_pr_url(...) .. '"')
end
-- Copies current branches PR url to system clipboard
M.copy_pr_url = function(...)
vim.cmd('let @+ = "' .. get_pr_url(...) .. '"')
end
-- Opens current banches PR url in default browser
-- Utilizes netrw browse, meaning it should behave same as netrw
M.open_new_pr = function(...)
vim.fn['netrw#BrowseX'](get_pr_url(...), 0)
end
-- Creates new branch and checks out to it
-- Similar to `gcb` in fish config
M.create_branch = function(branch)
vim.cmd('Git checkout -b ' .. branch)
end
-- Switches to branch
M.checkout_branch = function(branch)
vim.cmd('Git checkout ' .. branch)
end
return M

View File

@ -66,7 +66,7 @@ return require('packer').startup {
use 'nvim-treesitter/playground' -- TSPlaygroundToggle - access treesitter data
-- LSP --
-- use 'tjdevries/nlua.nvim' -- Built-in Lua integration with LSP
use 'tjdevries/nlua.nvim' -- Built-in Lua integration with LSP
use 'neovim/nvim-lspconfig' -- Easy LSP Config
use 'alexaandru/nvim-lspupdate' -- Easy install and update for many LSP servers
use 'nvim-lua/completion-nvim' -- LSP completion integration

View File

@ -38,10 +38,10 @@ for _, lsp in ipairs(servers) do
end
-- Lua bultin lsp
-- require('nlua.lsp.nvim').setup(lspconfig, {
-- on_attach = on_attach,
require('nlua.lsp.nvim').setup(lspconfig, {
on_attach = on_attach,
-- -- Include globals you want to tell the LSP are real :)
-- globals = {
-- }
-- })
-- Include globals you want to tell the LSP are real :)
globals = {
}
})

View File

@ -0,0 +1,16 @@
-------------------------------------------------------------------------------
-- - Fugitive.vim setup and extra commands -
-------------------------------------------------------------------------------
vim.cmd[[command! -nargs=0 Ggpush :lua require('fugitive_extensions').push_origin()]]
vim.cmd[[command! -nargs=0 Ggpull :lua require('fugitive_extensions').pull_origin()]]
vim.cmd[[command! -nargs=? Gpropen :lua require('fugitive_extensions').open_new_pr(<f-args>)]]
vim.cmd[[command! -nargs=? Gpr Gpropen <args>]]
vim.cmd[[command! -nargs=? Gprprint :lua require('fugitive_extensions').print_pr_url(<f-args>)]]
vim.cmd[[command! -nargs=? Gprcopy :lua require('fugitive_extensions').copy_pr_url(<f-args>)]]
vim.cmd[[command! -nargs=1 Gcbranch :lua require('fugitive_extensions').create_branch(<f-args>)]]
vim.cmd[[command! -nargs=0 Gcmaster :lua require('fugitive_extensions').checkout_branch('main')]]
vim.cmd[[command! -nargs=0 Gcm Gcmaster]]
vim.cmd[[command! -nargs=0 Gcdev :lua require('fugitive_extensions').checkout_branch('develop')]]
vim.cmd[[command! -nargs=1 Gcheckout :lua require('fugitive_extensions').checkout_branch(<f-args>)]]
vim.cmd[[command! -nargs=1 Gc Gcheckout <args>]]

View File

@ -0,0 +1,23 @@
-------------------------------------------------------------------------------
-- - Vimwiki extensions for working with weekly/monthly/yearly reviews -
--
-- DEPENDS ON VIMWIKI PLUGIN
-------------------------------------------------------------------------------
vim.cmd[[command! -nargs=? VimwikiWeeklyReview :lua require('vimwiki_reviews').vimwiki_weekly_review(<f-args>)]]
vim.cmd[[command! -nargs=? VimwikiWeeklyTemplate :lua require('vimwiki_reviews').open_review_weekly_template(<f-args>)]]
vim.cmd[[command! -nargs=? VimwikiMonthlyReview :lua require('vimwiki_reviews').vimwiki_monthly_review(<f-args>)]]
vim.cmd[[command! -nargs=? VimwikiMonthlyTemplate :lua require('vimwiki_reviews').open_review_monthly_template(<f-args>)]]
vim.cmd[[command! -nargs=? VimwikiYearlyReview :lua require('vimwiki_reviews').vimwiki_yearly_review(<f-args>)]]
vim.cmd[[command! -nargs=? VimwikiYearlyTemplate :lua require('vimwiki_reviews').open_review_yearly_template(<f-args>)]]
vim.cmd[[command! -nargs=? VimwikiReviewIndex :lua require('vimwiki_reviews').vimwiki_review_index(<f-args>)]]
local default_opts = {noremap=true}
local function set_keymap(...) vim.api.nvim_set_keymap(...) end
set_keymap('n', '<Leader>wrw', '<cmd>VimwikiWeeklyReview<CR>', default_opts)
set_keymap('n', '<Leader>wrtw', '<cmd>VimwikiWeeklyTemplate<CR>', default_opts)
set_keymap('n', '<Leader>wrm', '<cmd>VimwikiMonthlyReview<CR>', default_opts)
set_keymap('n', '<Leader>wrtm', '<cmd>VimwikiMonthlyTemplate<CR>', default_opts)
set_keymap('n', '<Leader>wry', '<cmd>VimwikiYearlyReview<CR>', default_opts)
set_keymap('n', '<Leader>wrty', '<cmd>VimwikiYearlyTemplate<CR>', default_opts)
set_keymap('n', '<Leader>wri', '<cmd>VimwikiReviewIndex<CR>', default_opts)

View File

@ -0,0 +1,131 @@
-------------------------------------------------------------------------------
-- - Vimwiki reviews extension library -
-------------------------------------------------------------------------------
-- Gets path to reviews dir of provided vimwiki (by index)
local function get_reviews_dir(...)
local vimwiki = {}
if (select('#', ...) == 0)
then
vimwiki = vim.g.vimwiki_list[1]
else
vimwiki = vim.g.vimwiki_list[tonumber(select(1, ...))]
end
return vimwiki.path .. 'reviews/'
end
-- Finds review template path for provided review type
local function get_review_template_path(vimwiki_reviews_path, review_type)
return vimwiki_reviews_path .. 'template-' .. review_type .. '.md'
end
-- Reads template for provided review type into current buffer
-- Uses overrides in form of files in reviews directory
-- Looks for file named template-{review_type}.md:
-- - template-week.md
-- - template-month.md
-- - template-year.md
-- Templates can use variables using %variable% syntax
-- Currently supported variables are:
-- - %date% (Puts different date based on review type)
local function read_review_template_into_buffer(vimwiki_reviews_path, review_type)
local template_path = get_review_template_path(vimwiki_reviews_path, review_type)
if (vim.fn.filereadable(vim.fn.glob(template_path)))
then
vim.cmd('read ' .. template_path)
else
if (review_type == 'week')
then
vim.fn.setline(1, '# %date% Weekly Review')
elseif (review_type == 'month')
then
vim.fn.setline(1, '# %date% Monthly Review')
elseif (review_type == 'year')
then
vim.fn.setline(1, '# %date% Yearly Review')
end
end
end
-------------------------------------------------------------------------------
-- - Public API -
-------------------------------------------------------------------------------
local M = {}
-- Edits weekly review template
M.open_review_weekly_template = function(...)
local reviews_dir = get_reviews_dir(...)
vim.cmd('edit ' .. get_review_template_path(reviews_dir, 'week'))
end
-- Edits monthly review template
M.open_review_monthly_template = function(...)
local reviews_dir = get_reviews_dir(...)
vim.cmd('edit ' .. get_review_template_path(reviews_dir, 'month'))
end
-- Edits yearly review template
M.open_review_yearly_template = function(...)
local reviews_dir = get_reviews_dir(...)
vim.cmd('edit ' .. get_review_template_path(reviews_dir, 'year'))
end
-- Open current week weekly review file
-- Created buffer is dated to Sunday of current week
-- Opens current week because Sunday is good time to do this review
M.vimwiki_weekly_review = function(...)
local reviews_dir = get_reviews_dir(...)
local days_to_sunday = 7 - tonumber(os.date('%u'))
local week_date = os.date('%Y-%m-%d', os.time() + days_to_sunday * 24 * 60 * 60)
local file_name = reviews_dir .. week_date .. '-week.md'
local exists = vim.fn.filereadable(vim.fn.glob(file_name)) == 1
vim.cmd('edit ' .. file_name)
if (exists == false)
then
read_review_template_into_buffer(reviews_dir, 'week')
vim.cmd('%substitute/%date%/' .. week_date)
end
end
-- Open past month monthly review file
-- Created buffer is dated to previous month
-- Previous month is calculated in an erroneous way
-- 28 days are subtracted from current time to get previous month
M.vimwiki_monthly_review = function(...)
local reviews_dir = get_reviews_dir(...)
local month_time = os.time() - 28 * 24 * 60 * 60
local month_date = os.date('%Y-%m', month_time)
local file_name = reviews_dir .. month_date .. '-month.md'
local exists = vim.fn.filereadable(vim.fn.glob(file_name)) == 1
vim.cmd('edit ' .. file_name)
if (exists == false)
then
read_review_template_into_buffer(reviews_dir, 'month')
vim.cmd('%substitute/%date%/' .. os.date('%Y %B', month_time))
end
end
-- Open past year yearly review file
-- Created buffer is dated to previous year
M.vimwiki_yearly_review = function(...)
local reviews_dir = get_reviews_dir(...)
local year_date = (tonumber(os.date('%Y')) - 1)
local file_name = reviews_dir .. year_date .. '-year.md'
local exists = vim.fn.filereadable(vim.fn.glob(file_name)) == 1
vim.cmd('edit ' .. file_name)
if (exists == false)
then
read_review_template_into_buffer(reviews_dir, 'year')
vim.cmd('%substitute/%date%/' .. year_date)
end
end
-- Open reviews index file
M.vimwiki_review_index = function(...)
local reviews_dir = get_reviews_dir(...)
vim.cmd('edit ' .. reviews_dir .. 'reviews.md')
end
return M

View File

@ -16,6 +16,7 @@ let g:ale_fixers = {}
let g:ale_fixers['*'] = ['remove_trailing_lines', 'trim_whitespace']
let g:ale_fixers.python = ['autopep8', 'isort']
let g:ale_fixers.dart = ['dartfmt']
let g:ale_fixers.lua = ['luafmt']
" Warnings navigation
nmap <silent> [W <Plug>(ale_first)

View File

@ -1,84 +0,0 @@
" -----------------------------------------------------------------------------
" - Fugitive.vim setup and extra mappings -
" -----------------------------------------------------------------------------
" Shorcut to push directly to current branch on origin
" Similar to `ggpush` in fish config
function! s:PushOrigin()
execute 'Git push origin ' . FugitiveHead()
endfunction
" Shorcut to pull directly from current branch on origin
" Similar to `ggpull` in fish config
function! s:PullOrigin()
execute 'Git pull origin ' . FugitiveHead()
endfunction
" Generates url for creating PR for current branch
" Tested only with github.com
" Works regardless of ssh or https for origin config
" Hardcoded to use 'origin' remote
function! s:GetPrUrl(...)
let origin_url = fugitive#RemoteUrl('origin')
let l:origin_url = substitute(l:origin_url, '\.git$', '', '')
let l:origin_url = substitute(l:origin_url, ':', '/', '')
let l:origin_url = substitute(l:origin_url, 'git@', 'https://', '')
" Remove prefix if it is available, for some of common git services
let common_services = ['github.com', 'bitbucket.org', 'gitlab.com']
for service in l:common_services
if (l:origin_url =~ l:service)
" Common mechanism for managing multiple SSH keys
let l:origin_url = substitute(l:origin_url, '://.*' . l:service, '://' . l:service, '')
endif
endfor
" This part probably only works on github
if a:0 == 0
let pr_url = l:origin_url . '/compare/' . FugitiveHead() . '?expand=1'
else
let pr_url = l:origin_url . '/compare/' . a:1 . '...' . FugitiveHead() . '?expand=1'
endif
return l:pr_url
endfunction
" Prints current branches PR url (not saved to :messages)
" Makes it easy to use terminal for opening url on click
function! s:PrintPrUrl(...)
echo call('s:GetPrUrl', a:000)
endfunction
" Copies current branches PR url to system clipboard
function! s:CopyPrUrl(...)
let @+ = call('s:GetPrUrl', a:000)
endfunction
" Opens current banches PR url in default browser
" Utilizes netrw browse, meaning it should behave same as netrw
function! s:OpenNewPr(...)
call netrw#BrowseX(call('s:GetPrUrl', a:000), 0)
endfunction
" Creates new branch and checks out to it
" Similar to `gcb` in fish config
function! s:CreateBranch(branch)
execute 'Git checkout -b ' . a:branch
endfunction
" Switches to branch
function! s:CheckoutBranch(branch)
execute 'Git checkout ' . a:branch
endfunction
command! -nargs=0 Ggpush :call s:PushOrigin()
command! -nargs=0 Ggpull :call s:PullOrigin()
command! -nargs=? Gpropen :call s:OpenNewPr(<f-args>)
command! -nargs=? Gpr Gpropen <args>
command! -nargs=? Gprprint :call s:PrintPrUrl(<f-args>)
command! -nargs=? Gprcopy :call s:CopyPrUrl(<f-args>)
command! -nargs=1 Gcbranch :call s:CreateBranch(<f-args>)
command! -nargs=0 Gcmaster :call s:CheckoutBranch('main')
command! -nargs=0 Gcm Gcmaster
command! -nargs=0 Gcdev :call s:CheckoutBranch('develop')
command! -nargs=1 Gcheckout :call s:CheckoutBranch(<f-args>)
command! -nargs=1 Gc Gcheckout <args>

View File

@ -1,133 +0,0 @@
" -----------------------------------------------------------------------------
" - Vimwiki extensions for working with weekly/monthly/yearly reviews -
"
" DEPENDS ON VIMWIKI PLUGIN
" -----------------------------------------------------------------------------
" Gets path to reviews dir of provided vimwiki (by index)
function! s:GetReviewsDir(...)
if a:0 == 0
let l:vimwiki = g:vimwiki_list[0]
else
let l:vimwiki = g:vimwiki_list[str2nr(a:1) - 1]
endif
return l:vimwiki.path . 'reviews/'
endfunction
" Finds review template path for provided review type
function! s:GetReviewTemplatePath(vimwiki_reviews_path, review_type)
return a:vimwiki_reviews_path . 'template-' . a:review_type . '.md'
endfunction
" Edits weekly review template
function! s:OpenReviewWeeklyTemplate(...)
let reviews_dir = call('s:GetReviewsDir', a:000)
execute 'edit ' . s:GetReviewTemplatePath(l:reviews_dir, 'week')
endfunction
" Edits monthly review template
function! s:OpenReviewMonthlyTemplate(...)
let reviews_dir = call('s:GetReviewsDir', a:000)
execute 'edit ' . s:GetReviewTemplatePath(l:reviews_dir, 'month')
endfunction
" Edits yearly review template
function! s:OpenReviewYearlyTemplate(...)
let reviews_dir = call('s:GetReviewsDir', a:000)
execute 'edit ' . s:GetReviewTemplatePath(l:reviews_dir, 'year')
endfunction
" Reads template for provided review type into current buffer
" Uses overrides in form of files in reviews directory
" Looks for file named template-{review_type}.md:
" - template-week.md
" - template-month.md
" - template-year.md
" Templates can use variables using %variable% syntax
" Currently supported variables are:
" - %date% (Puts different date based on review type)
function! s:ReadReviewTemplateIntoBuffer(vimwiki_reviews_path, review_type)
let template_path = s:GetReviewTemplatePath(a:vimwiki_reviews_path, a:review_type)
if filereadable(glob(l:template_path))
execute 'read ' . l:template_path
else
if a:review_type == 'week'
call setline(1, '# %date% Weekly Review')
elseif a:review_type == 'month'
call setline(1, '# %date% Monthly Review')
elseif a:review_type == 'year'
call setline(1, '# %date% Yearly Review')
endif
endif
endfunction
" Open current week weekly review file
" Created buffer is dated to Sunday of current week
" Opens current week because Sunday is good time to do this review
function! s:VimwikiWeeklyReview(...)
let reviews_dir = call('s:GetReviewsDir', a:000)
let days_to_sunday = 7 - str2nr(strftime('%u'))
let week_date = strftime('%Y-%m-%d', localtime() + l:days_to_sunday * 24 * 60 * 60)
let file_name = l:reviews_dir . l:week_date . '-week.md'
let exists = filereadable(glob(l:file_name))
execute 'edit ' . l:file_name
if exists == v:false
call s:ReadReviewTemplateIntoBuffer(l:reviews_dir, 'week')
execute '%substitute/%date%/' . l:week_date
endif
endfunction
" Open past month monthly review file
" Created buffer is dated to previous month
" Previous month is calculated in an erroneous way
" 28 days are subtracted from current time to get previous month
function! s:VimwikiMonthlyReview(...)
let reviews_dir = call('s:GetReviewsDir', a:000)
let month_time = localtime() - 28 * 24 * 60 * 60
let month_date = strftime('%Y-%m', l:month_time)
let file_name = l:reviews_dir . l:month_date .'-month.md'
let exists = filereadable(glob(l:file_name))
execute 'edit ' . l:file_name
if exists == v:false
call s:ReadReviewTemplateIntoBuffer(l:reviews_dir, 'month')
execute '%substitute/%date%/' . strftime('%Y %B', l:month_time)
endif
endfunction
" Open past year yearly review file
" Created buffer is dated to previous year
function! s:VimwikiYearlyReview(...)
let reviews_dir = call('s:GetReviewsDir', a:000)
let year_date = (str2nr(strftime('%Y')) - 1)
let file_name = l:reviews_dir . l:year_date .'-year.md'
let exists = filereadable(glob(l:file_name))
execute 'edit ' . l:file_name
if exists == v:false
call s:ReadReviewTemplateIntoBuffer(l:reviews_dir, 'year')
execute '%substitute/%date%/' . l:year_date
endif
endfunction
" Open reviews index file
function! s:VimwikiReviewIndex(...)
let reviews_dir = call('s:GetReviewsDir', a:000)
execute 'edit ' . l:reviews_dir . 'reviews.md'
endfunction
command! -nargs=? VimwikiWeeklyReview :call s:VimwikiWeeklyReview(<f-args>)
command! -nargs=? VimwikiWeeklyTemplate :call s:OpenReviewWeeklyTemplate(<f-args>)
command! -nargs=? VimwikiMonthlyReview :call s:VimwikiMonthlyReview(<f-args>)
command! -nargs=? VimwikiMonthlyTemplate :call s:OpenReviewMonthlyTemplate(<f-args>)
command! -nargs=? VimwikiYearlyReview :call s:VimwikiYearlyReview(<f-args>)
command! -nargs=? VimwikiYearlyTemplate :call s:OpenReviewYearlyTemplate(<f-args>)
command! -nargs=? VimwikiReviewIndex :call s:VimwikiReviewIndex(<f-args>)
nnoremap <Leader>wrw :VimwikiWeeklyReview<CR>
nnoremap <Leader>wrtw :VimwikiWeeklyTemplate<CR>
nnoremap <Leader>wrm :VimwikiMonthlyReview<CR>
nnoremap <Leader>wrtm :VimwikiMonthlyTemplate<CR>
nnoremap <Leader>wry :VimwikiYearlyReview<CR>
nnoremap <Leader>wrty :VimwikiYearlyTemplate<CR>
nnoremap <Leader>wri :VimwikiReviewIndex<CR>