Skip to content

Commit

Permalink
🚧 Refactor: Move dashboard plugin to core
Browse files Browse the repository at this point in the history
  • Loading branch information
Nguyen-Hoang-Nam committed Jul 17, 2022
1 parent 19e0be5 commit a4dbcbf
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 69 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,6 @@ Please store below plugins in `~/.local/share/nvim/site/pack/*/start/`.
| [nvim-jdtls](https://github.com/mfussenegger/nvim-jdtls) | Jun 30 2022 |
| [nvim-lspconfig](https://github.com/neovim/nvim-lspconfig) | Jul 14 2022 |
| [nvim-mini-file-icons](https://github.com/Nguyen-Hoang-Nam/nvim-mini-file-icons) | Jul 09 2022 |
| [nvim-minimal-dashboard](https://github.com/Nguyen-Hoang-Nam/nvim-minimal-dashboard) | Jul 03 2022 |
| [nvim-navic](https://github.com/SmiteshP/nvim-navic) | Jul 13 2022 |
| [nvim-scrollbar](https://github.com/petertriho/nvim-scrollbar) | Jul 11 2022 |
| [nvim-scrollview](https://github.com/dstein64/nvim-scrollview) | Jun 07 2022 |
Expand Down
34 changes: 34 additions & 0 deletions after/plugin/dashboard.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- Credit https://github.com/glepnir/dashboard-nvim/blob/master/lua/dashboard/init.lua
local api = vim.api

local autogroup = api.nvim_create_augroup("Dashboard", {})

api.nvim_create_autocmd("Vimenter", {
group = autogroup,
pattern = "*",
nested = true,
callback = function()
if vim.fn.argc() == 0 and vim.api.nvim_buf_get_name(0) == "" then
require("dashboard").instance()
end
end,
})

api.nvim_create_autocmd({ "BufLeave" }, {
group = autogroup,
pattern = "*",
callback = function()
local pos = api.nvim_win_get_cursor(0)
api.nvim_win_set_var(0, "dashboard_prev_pos", pos)
end,
})

api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, {
group = autogroup,
pattern = "*",
callback = function()
if vim.bo.filetype == "dashboard" then
return
end
end,
})
4 changes: 3 additions & 1 deletion init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require("options")
require("luasnip.loaders.from_vscode").lazy_load()
require("setup.aerial")
require("setup.autopairs")
require("setup.dashboard")
require("setup.autosession")
require("setup.blankline")
require("setup.biscuits")
Expand Down Expand Up @@ -35,4 +34,7 @@ require("tree")
require("mappings")
require("highlights")
require("setup.dap")

require("autocommands")
require("autocommands.statusline")
require("autocommands.winbar")
65 changes: 0 additions & 65 deletions lua/autocommands.lua

This file was deleted.

46 changes: 46 additions & 0 deletions lua/autocommands/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
local augroup = vim.api.nvim_exec
local cmd = vim.api.nvim_command
local utils = require("utils.core")

-- Auto format
utils.autocommand_by_filetypes(
require("settings").autoformat.filetypes,
"BufWritePre",
[[lua require('format').format()]]
)

-- Code action
utils.autocommand_by_filetypes(
require("settings").codeaction.filetypes,
"CursorHold,CursorHoldI",
[[lua require('utils.lightbulb').code_action()]]
)

-- Toggle comment
cmd("command! -range CommentToggle lua require('utils.comment').comment_toggle(<line1>, <line2>)")

augroup(
[[
augroup Config
autocmd!
autocmd InsertEnter * set nocursorline
autocmd InsertLeave * set cursorline
autocmd! BufEnter * if &ft ==# 'help' | wincmd L | endif
autocmd bufenter * if (winnr("$") == 2 && &filetype == "Yanil") | q | endif
autocmd BufNewFile,BufRead *.sol set filetype=solidity
augroup END
]],
true
)

augroup(
[[
augroup Highlight
autocmd!
autocmd FileType dapui* setlocal winhighlight=Normal:DapUINormal
autocmd FileType aerial setlocal winhighlight=Normal:AerialNormal
autocmd FileType DiffviewFiles setlocal winhighlight=Normal:DiffviewFilesNormal
augroup END
]],
true
)
18 changes: 18 additions & 0 deletions lua/autocommands/statusline.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
local api = vim.api
local autogroup = api.nvim_create_augroup("StatusLine", {})

api.nvim_create_autocmd({ "Bufenter", "BufWritePost" }, {
group = autogroup,
pattern = "*",
callback = function()
require("statusline").load()
end,
})

api.nvim_create_autocmd("FileType", {
group = autogroup,
pattern = "dapui*,dap-repl,aerial,sqls_output",
callback = function()
require("statusline").load()
end,
})
18 changes: 18 additions & 0 deletions lua/autocommands/winbar.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
local api = vim.api
local autogroup = api.nvim_create_augroup("Winbar", {})

api.nvim_create_autocmd({ "Bufenter", "BufWritePost" }, {
group = autogroup,
pattern = "*",
callback = function()
require("winbar").load()
end,
})

api.nvim_create_autocmd("FileType", {
group = autogroup,
pattern = "dapui*,dap-repl,aerial,sqls_output",
callback = function()
require("statusline").load()
end,
})
119 changes: 119 additions & 0 deletions lua/dashboard.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
-- Credit https://github.com/glepnir/dashboard-nvim/blob/master/lua/dashboard/init.lua
local api, fn = vim.api, vim.fn
local opt_local = vim.opt_local
local M = {}

local dashboard_loaded = false

local default_banner = {
"⠀⠀⠀⠀⠀⠀⠀⢀⣴⣷⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣦⡀⠀⠀⠀⠀⠀⠀⠀",
"⠀⠀⠀⠀⠀⢀⣴⣿⣿⣿⣿⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣦⡀⠀⠀⠀⠀⠀",
"⠀⠀⠀⢀⣴⣿⣿⣿⣿⣿⣿⣿⣦⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣦⡀⠀⠀⠀",
"⠀⢀⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣦⡀⠀",
"⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣦",
"⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿",
"⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿",
"⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣄⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿",
"⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿",
"⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⡀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿",
"⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠙⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡄⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿",
"⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠘⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣆⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿",
"⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠈⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿",
"⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿",
"⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠘⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿",
"⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿",
"⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿",
"⠻⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟",
"⠀⠈⠻⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⠁⠀",
"⠀⠀⠀⠈⠻⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠻⣿⣿⣿⣿⣿⣿⣿⠟⠁⠀⠀⠀",
"⠀⠀⠀⠀⠀⠈⠻⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣿⣿⣿⣿⠟⠁⠀⠀⠀⠀⠀",
"⠀⠀⠀⠀⠀⠀⠀⠈⠻⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢿⠟⠁⠀⠀⠀⠀⠀⠀⠀",
}

local set_line_with_highlight = function(bufnr, tbl)
api.nvim_buf_set_lines(bufnr, 1, #tbl, false, tbl)

local dashboard_namespace = api.nvim_create_namespace("Dashboard")
for i = 1, #tbl do
api.nvim_buf_add_highlight(bufnr, dashboard_namespace, "DashboardHeader", i, 1, -1)
end
end

local cache_header = {}

local render_header = coroutine.create(function(bufnr)
local centered_lines = {}

local top = math.floor((fn.winheight(0) - #default_banner) / 2)
for _ = 1, top do
table.insert(centered_lines, "")
end

local width = math.floor((fn.winwidth(0) - fn.strwidth(default_banner[1])) / 2)
local space = vim.fn["repeat"](" ", width)
for i = 1, #default_banner do
table.insert(centered_lines, space .. default_banner[i])
end

cache_header = centered_lines
set_line_with_highlight(bufnr, centered_lines)
end)

function M.instance()
if api.nvim_get_mode().mode == "i" or not vim.bo.modifiable then
return
end

if not vim.o.hidden and vim.bo.modfied then
return
end

local bufnr

if vim.fn.line2byte("$") ~= -1 then
vim.cmd("noautocmd")
bufnr = api.nvim_create_buf(false, true)
else
bufnr = api.nvim_get_current_buf()
end

local window = api.nvim_get_current_win()
api.nvim_win_set_buf(window, bufnr)

opt_local.wrap = false
opt_local.signcolumn = "no"
opt_local.buftype = "nofile"
opt_local.swapfile = false
opt_local.spell = false
opt_local.relativenumber = false
opt_local.filetype = "dashboard"
opt_local.number = false
opt_local.list = false
opt_local.cursorline = false
opt_local.cursorcolumn = false
opt_local.buflisted = false
opt_local.matchpairs = ""
opt_local.foldcolumn = "0"
opt_local.colorcolumn = ""
opt_local.bufhidden = "wipe"

if dashboard_loaded then
set_line_with_highlight(bufnr, cache_header)
else
coroutine.resume(render_header, bufnr)
dashboard_loaded = true
end

local opts = { noremap = true, silent = true, nowait = true }
api.nvim_buf_set_keymap(bufnr, "n", "h", "", opts)
api.nvim_buf_set_keymap(bufnr, "n", "l", "", opts)
api.nvim_buf_set_keymap(bufnr, "n", "w", "", opts)
api.nvim_buf_set_keymap(bufnr, "n", "b", "", opts)
api.nvim_buf_set_keymap(bufnr, "n", "<BS>", "", opts)

api.nvim_exec_autocmds("User DashboardReady", {
modeline = false,
})
end

return M
1 change: 0 additions & 1 deletion lua/setup/dashboard.lua

This file was deleted.

2 changes: 1 addition & 1 deletion lua/utils/bufdelete.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function M.bufdelete()
cmd("silent! ScrollViewDisable | bd " .. bufnr .. " | silent! ScrollViewEnable")

-- NOTE: Show Dashboard when closing the last buffer
require("dashboard").instance(true)
require("dashboard").instance()
else
if bufnr ~= buffers[#buffers] then
cmd("bnext")
Expand Down

0 comments on commit a4dbcbf

Please sign in to comment.