diff --git a/symlinks/config/nvim/lua/fugitive_extensions.lua b/symlinks/config/nvim/lua/fugitive_extensions.lua new file mode 100644 index 0000000..2cfec45 --- /dev/null +++ b/symlinks/config/nvim/lua/fugitive_extensions.lua @@ -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 diff --git a/symlinks/config/nvim/lua/init/plugins.lua b/symlinks/config/nvim/lua/init/plugins.lua index 19ccaca..49bd430 100644 --- a/symlinks/config/nvim/lua/init/plugins.lua +++ b/symlinks/config/nvim/lua/init/plugins.lua @@ -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 diff --git a/symlinks/config/nvim/lua/lsp/servers.lua b/symlinks/config/nvim/lua/lsp/servers.lua index ae79091..07a30f8 100644 --- a/symlinks/config/nvim/lua/lsp/servers.lua +++ b/symlinks/config/nvim/lua/lsp/servers.lua @@ -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 = { + } +}) diff --git a/symlinks/config/nvim/lua/plugin/fugitive.lua b/symlinks/config/nvim/lua/plugin/fugitive.lua new file mode 100644 index 0000000..dcfb4f1 --- /dev/null +++ b/symlinks/config/nvim/lua/plugin/fugitive.lua @@ -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()]] +vim.cmd[[command! -nargs=? Gpr Gpropen ]] +vim.cmd[[command! -nargs=? Gprprint :lua require('fugitive_extensions').print_pr_url()]] +vim.cmd[[command! -nargs=? Gprcopy :lua require('fugitive_extensions').copy_pr_url()]] +vim.cmd[[command! -nargs=1 Gcbranch :lua require('fugitive_extensions').create_branch()]] +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()]] +vim.cmd[[command! -nargs=1 Gc Gcheckout ]] diff --git a/symlinks/config/nvim/lua/plugin/vimwiki_reviews.lua b/symlinks/config/nvim/lua/plugin/vimwiki_reviews.lua new file mode 100644 index 0000000..8ab74b0 --- /dev/null +++ b/symlinks/config/nvim/lua/plugin/vimwiki_reviews.lua @@ -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()]] +vim.cmd[[command! -nargs=? VimwikiWeeklyTemplate :lua require('vimwiki_reviews').open_review_weekly_template()]] +vim.cmd[[command! -nargs=? VimwikiMonthlyReview :lua require('vimwiki_reviews').vimwiki_monthly_review()]] +vim.cmd[[command! -nargs=? VimwikiMonthlyTemplate :lua require('vimwiki_reviews').open_review_monthly_template()]] +vim.cmd[[command! -nargs=? VimwikiYearlyReview :lua require('vimwiki_reviews').vimwiki_yearly_review()]] +vim.cmd[[command! -nargs=? VimwikiYearlyTemplate :lua require('vimwiki_reviews').open_review_yearly_template()]] +vim.cmd[[command! -nargs=? VimwikiReviewIndex :lua require('vimwiki_reviews').vimwiki_review_index()]] + +local default_opts = {noremap=true} +local function set_keymap(...) vim.api.nvim_set_keymap(...) end +set_keymap('n', 'wrw', 'VimwikiWeeklyReview', default_opts) +set_keymap('n', 'wrtw', 'VimwikiWeeklyTemplate', default_opts) +set_keymap('n', 'wrm', 'VimwikiMonthlyReview', default_opts) +set_keymap('n', 'wrtm', 'VimwikiMonthlyTemplate', default_opts) +set_keymap('n', 'wry', 'VimwikiYearlyReview', default_opts) +set_keymap('n', 'wrty', 'VimwikiYearlyTemplate', default_opts) +set_keymap('n', 'wri', 'VimwikiReviewIndex', default_opts) diff --git a/symlinks/config/nvim/lua/vimwiki_reviews.lua b/symlinks/config/nvim/lua/vimwiki_reviews.lua new file mode 100644 index 0000000..c6e41d7 --- /dev/null +++ b/symlinks/config/nvim/lua/vimwiki_reviews.lua @@ -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 diff --git a/symlinks/config/nvim/plugin/ale.vim b/symlinks/config/nvim/plugin/ale.vim index 5a2f363..122c929 100644 --- a/symlinks/config/nvim/plugin/ale.vim +++ b/symlinks/config/nvim/plugin/ale.vim @@ -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 [W (ale_first) diff --git a/symlinks/config/nvim/plugin/fugitive.vim b/symlinks/config/nvim/plugin/fugitive.vim deleted file mode 100644 index 0d71aa3..0000000 --- a/symlinks/config/nvim/plugin/fugitive.vim +++ /dev/null @@ -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() -command! -nargs=? Gpr Gpropen -command! -nargs=? Gprprint :call s:PrintPrUrl() -command! -nargs=? Gprcopy :call s:CopyPrUrl() -command! -nargs=1 Gcbranch :call s:CreateBranch() -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() -command! -nargs=1 Gc Gcheckout diff --git a/symlinks/config/nvim/plugin/vimwiki_reviews.vim b/symlinks/config/nvim/plugin/vimwiki_reviews.vim deleted file mode 100644 index 6322e30..0000000 --- a/symlinks/config/nvim/plugin/vimwiki_reviews.vim +++ /dev/null @@ -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() -command! -nargs=? VimwikiWeeklyTemplate :call s:OpenReviewWeeklyTemplate() -command! -nargs=? VimwikiMonthlyReview :call s:VimwikiMonthlyReview() -command! -nargs=? VimwikiMonthlyTemplate :call s:OpenReviewMonthlyTemplate() -command! -nargs=? VimwikiYearlyReview :call s:VimwikiYearlyReview() -command! -nargs=? VimwikiYearlyTemplate :call s:OpenReviewYearlyTemplate() -command! -nargs=? VimwikiReviewIndex :call s:VimwikiReviewIndex() - -nnoremap wrw :VimwikiWeeklyReview -nnoremap wrtw :VimwikiWeeklyTemplate -nnoremap wrm :VimwikiMonthlyReview -nnoremap wrtm :VimwikiMonthlyTemplate -nnoremap wry :VimwikiYearlyReview -nnoremap wrty :VimwikiYearlyTemplate -nnoremap wri :VimwikiReviewIndex