Skip to content

Commit

Permalink
fix(vim_fn): check return values of vim.fn against 1 (ayamir#747)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jint-lzxy authored and singlemancombat committed May 16, 2023
1 parent 0a1e218 commit 37ecb17
Show file tree
Hide file tree
Showing 24 changed files with 1,047 additions and 1,062 deletions.
8 changes: 4 additions & 4 deletions lua/core/event.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ end

local mapping = require("keymap.completion")
vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup("UserLspConfig", {}),
callback = function(event)
mapping.lsp(event.buf)
end,
group = vim.api.nvim_create_augroup("UserLspConfig", {}),
callback = function(event)
mapping.lsp(event.buf)
end,
})

-- auto close NvimTree
Expand Down
4 changes: 2 additions & 2 deletions lua/core/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ end

local shell_config = function()
if global.is_windows then
if not (vim.fn.executable("pwsh") or vim.fn.executable("powershell")) then
if not (vim.fn.executable("pwsh") == 1 or vim.fn.executable("powershell") == 1) then
vim.notify(
[[
Failed to setup terminal config
Expand All @@ -134,7 +134,7 @@ You're recommended to install PowerShell for better experience.]],

local basecmd = "-NoLogo -MTA -ExecutionPolicy RemoteSigned"
local ctrlcmd = "-Command [console]::InputEncoding = [console]::OutputEncoding = [System.Text.Encoding]::UTF8"
vim.api.nvim_set_option_value("shell", vim.fn.executable("pwsh") and "pwsh" or "powershell", {})
vim.api.nvim_set_option_value("shell", vim.fn.executable("pwsh") == 1 and "pwsh" or "powershell", {})
vim.api.nvim_set_option_value("shellcmdflag", string.format("%s %s;", basecmd, ctrlcmd), {})
vim.api.nvim_set_option_value("shellredir", "-RedirectStandardOutput %s -NoNewWindow -Wait", {})
vim.api.nvim_set_option_value("shellpipe", "2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode", {})
Expand Down
18 changes: 9 additions & 9 deletions lua/core/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ local function load_options()
return s == nil or s == ""
end

-- custom python provider
local conda_prefix = os.getenv("CONDA_PREFIX")
if not isempty(conda_prefix) then
vim.g.python_host_prog = conda_prefix .. "/bin/python"
vim.g.python3_host_prog = conda_prefix .. "/bin/python"
else
vim.g.python_host_prog = "python"
vim.g.python3_host_prog = "python3"
end
-- custom python provider
local conda_prefix = os.getenv("CONDA_PREFIX")
if not isempty(conda_prefix) then
vim.g.python_host_prog = conda_prefix .. "/bin/python"
vim.g.python3_host_prog = conda_prefix .. "/bin/python"
else
vim.g.python_host_prog = "python"
vim.g.python3_host_prog = "python3"
end

for name, value in pairs(global_local) do
vim.o[name] = value
Expand Down
30 changes: 15 additions & 15 deletions lua/keymap/bind.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,21 @@ end

---@param mapping table<string, map_rhs>
function bind.nvim_load_mapping(mapping)
for key, value in pairs(mapping) do
local modes, keymap = key:match("([^|]*)|?(.*)")
if type(value) == "table" then
for _, mode in ipairs(vim.split(modes, "")) do
local rhs = value.cmd
local options = value.options
local buf = value.buffer
if buf and type(buf) == "number" then
vim.api.nvim_buf_set_keymap(buf, mode, keymap, rhs, options)
else
vim.api.nvim_set_keymap(mode, keymap, rhs, options)
end
end
end
end
for key, value in pairs(mapping) do
local modes, keymap = key:match("([^|]*)|?(.*)")
if type(value) == "table" then
for _, mode in ipairs(vim.split(modes, "")) do
local rhs = value.cmd
local options = value.options
local buf = value.buffer
if buf and type(buf) == "number" then
vim.api.nvim_buf_set_keymap(buf, mode, keymap, rhs, options)
else
vim.api.nvim_set_keymap(mode, keymap, rhs, options)
end
end
end
end
end

return bind
42 changes: 21 additions & 21 deletions lua/keymap/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@ local map_callback = bind.map_callback
local mapping = {}

function mapping.lsp(buf)
local map = {
-- LSP-related keymaps, work only when event = { "InsertEnter", "LspStart" }
["n|gli"] = map_cr("LspInfo"):with_buffer(buf):with_desc("lsp: Info"),
["n|glr"] = map_cr("LspRestart"):with_buffer(buf):with_nowait():with_desc("lsp: Restart"),
["n|go"] = map_cr("Lspsaga outline"):with_buffer(buf):with_desc("lsp: Toggle outline"),
["n|gjp"] = map_cr("Lspsaga diagnostic_jump_prev"):with_buffer(buf):with_desc("lsp: Prev diagnostic"),
["n|gjn"] = map_cr("Lspsaga diagnostic_jump_next"):with_buffer(buf):with_desc("lsp: Next diagnostic"),
["n|gsl"] = map_cr("Lspsaga show_line_diagnostics"):with_buffer(buf):with_desc("lsp: Line diagnostic"),
["n|gsc"] = map_cr("Lspsaga show_cursor_diagnostics"):with_buffer(buf):with_desc("lsp: Cursor diagnostic"),
["n|gsb"] = map_cr("Lspsaga show_buf_diagnostics"):with_buffer(buf):with_desc("lsp: Buffer diagnostic"),
["n|gr"] = map_cr("Lspsaga rename"):with_buffer(buf):with_desc("lsp: Rename in file range"),
["n|gR"] = map_cr("Lspsaga rename ++project"):with_buffer(buf):with_desc("lsp: Rename in project range"),
["n|K"] = map_cr("Lspsaga hover_doc"):with_buffer(buf):with_desc("lsp: Show doc"),
["nv|ga"] = map_cr("Lspsaga code_action"):with_buffer(buf):with_desc("lsp: Code action for cursor"),
["n|gd"] = map_cr("Lspsaga peek_definition"):with_buffer(buf):with_desc("lsp: Preview definition"),
["n|gD"] = map_cr("Lspsaga goto_definition"):with_buffer(buf):with_desc("lsp: Goto definition"),
local map = {
-- LSP-related keymaps, work only when event = { "InsertEnter", "LspStart" }
["n|gli"] = map_cr("LspInfo"):with_buffer(buf):with_desc("lsp: Info"),
["n|glr"] = map_cr("LspRestart"):with_buffer(buf):with_nowait():with_desc("lsp: Restart"),
["n|go"] = map_cr("Lspsaga outline"):with_buffer(buf):with_desc("lsp: Toggle outline"),
["n|gjp"] = map_cr("Lspsaga diagnostic_jump_prev"):with_buffer(buf):with_desc("lsp: Prev diagnostic"),
["n|gjn"] = map_cr("Lspsaga diagnostic_jump_next"):with_buffer(buf):with_desc("lsp: Next diagnostic"),
["n|gsl"] = map_cr("Lspsaga show_line_diagnostics"):with_buffer(buf):with_desc("lsp: Line diagnostic"),
["n|gsc"] = map_cr("Lspsaga show_cursor_diagnostics"):with_buffer(buf):with_desc("lsp: Cursor diagnostic"),
["n|gsb"] = map_cr("Lspsaga show_buf_diagnostics"):with_buffer(buf):with_desc("lsp: Buffer diagnostic"),
["n|gr"] = map_cr("Lspsaga rename"):with_buffer(buf):with_desc("lsp: Rename in file range"),
["n|gR"] = map_cr("Lspsaga rename ++project"):with_buffer(buf):with_desc("lsp: Rename in project range"),
["n|K"] = map_cr("Lspsaga hover_doc"):with_buffer(buf):with_desc("lsp: Show doc"),
["nv|ga"] = map_cr("Lspsaga code_action"):with_buffer(buf):with_desc("lsp: Code action for cursor"),
["n|gd"] = map_cr("Lspsaga peek_definition"):with_buffer(buf):with_desc("lsp: Preview definition"),
["n|gD"] = map_cr("Lspsaga goto_definition"):with_buffer(buf):with_desc("lsp: Goto definition"),
["n|gp"] = map_cr("Lspsaga preview_definition"):with_buffer(buf):with_desc("lsp: Preview definition"),
["n|gt"] = map_cr("Lspsaga term_toggle"):with_buffer(buf):with_desc("lsp: Toggle term"),
["n|gh"] = map_cr("Lspsaga lsp_finder"):with_buffer(buf):with_desc("lsp: Show reference"),
["n|gci"] = map_cr("Lspsaga incoming_calls"):with_buffer(buf):with_desc("lsp: Show incoming calls"),
["n|gco"] = map_cr("Lspsaga outgoing_calls"):with_buffer(buf):with_desc("lsp: Show outgoing calls"),
}
bind.nvim_load_mapping(map)
["n|gh"] = map_cr("Lspsaga lsp_finder"):with_buffer(buf):with_desc("lsp: Show reference"),
["n|gci"] = map_cr("Lspsaga incoming_calls"):with_buffer(buf):with_desc("lsp: Show incoming calls"),
["n|gco"] = map_cr("Lspsaga outgoing_calls"):with_buffer(buf):with_desc("lsp: Show outgoing calls"),
}
bind.nvim_load_mapping(map)
end

return mapping
16 changes: 8 additions & 8 deletions lua/keymap/editor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ local plug_map = {
return et("<Plug>(accelerated_jk_gk)")
end):with_expr(),

-- Plugin: auto_session
["n|<leader>ss"] = map_cu("SessionSave"):with_noremap():with_silent():with_desc("session: Save"),
["n|<leader>sr"] = map_cu("SessionRestore"):with_noremap():with_silent():with_desc("session: Restore"),
["n|<leader>sd"] = map_cu("SessionDelete"):with_noremap():with_silent():with_desc("session: Delete"),
-- Plugin: auto_session
["n|<leader>ss"] = map_cu("SessionSave"):with_noremap():with_silent():with_desc("session: Save"),
["n|<leader>sr"] = map_cu("SessionRestore"):with_noremap():with_silent():with_desc("session: Restore"),
["n|<leader>sd"] = map_cu("SessionDelete"):with_noremap():with_silent():with_desc("session: Delete"),

-- Plugin: nvim-bufdel
["n|<A-q>"] = map_cr("BufDel"):with_noremap():with_silent():with_desc("buffer: Close current"),
Expand Down Expand Up @@ -68,8 +68,8 @@ local plug_map = {
["n|<leader>D"] = map_cr("DiffviewOpen"):with_silent():with_noremap():with_desc("git: Show diff"),
["n|<leader><leader>D"] = map_cr("DiffviewClose"):with_silent():with_noremap():with_desc("git: Close diff"),

-- Plugin: vim-easy-align
["nx|gea"] = map_cr("EasyAlign"):with_desc("edit: Align with delimiter"),
-- Plugin: vim-easy-align
["nx|gea"] = map_cr("EasyAlign"):with_desc("edit: Align with delimiter"),

-- Plugin: hop
["n|<leader>w"] = map_cu("HopWord"):with_noremap():with_desc("jump: Goto word"),
Expand All @@ -85,8 +85,8 @@ local plug_map = {
["i|<A-k>"] = map_cmd("<Plug>(TaboutMulti)"):with_silent():with_noremap():with_desc("edit: Goto end of pair"),
["i|<A-j>"] = map_cmd("<Plug>(TaboutBackMulti)"):with_silent():with_noremap():with_desc("edit: Goto begin of pair"),

-- Plugin suda.vim
["n|<A-s>"] = map_cu("SudaWrite"):with_silent():with_noremap():with_desc("editn: Save file using sudo"),
-- Plugin suda.vim
["n|<A-s>"] = map_cu("SudaWrite"):with_silent():with_noremap():with_desc("editn: Save file using sudo"),
}

bind.nvim_load_mapping(plug_map)
26 changes: 13 additions & 13 deletions lua/keymap/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ end

local _lazygit = nil
_G._toggle_lazygit = function()
if vim.fn.executable("lazygit") then
if not _lazygit then
_lazygit = require("toggleterm.terminal").Terminal:new({
cmd = "lazygit",
direction = "float",
close_on_exit = true,
hidden = true,
})
end
_lazygit:toggle()
else
vim.notify("Command [lazygit] not found!", vim.log.levels.ERROR, { title = "toggleterm.nvim" })
end
if vim.fn.executable("lazygit") == 1 then
if not _lazygit then
_lazygit = require("toggleterm.terminal").Terminal:new({
cmd = "lazygit",
direction = "float",
close_on_exit = true,
hidden = true,
})
end
_lazygit:toggle()
else
vim.notify("Command [lazygit] not found!", vim.log.levels.ERROR, { title = "toggleterm.nvim" })
end
end
9 changes: 3 additions & 6 deletions lua/keymap/tool.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ local plug_map = {
["n|<leader>r"] = map_cu([[%SnipRun]]):with_noremap():with_silent():with_desc("tool: Run code by file"),

-- Plugin: toggleterm
["t|<Esc><Esc>"] = map_cmd([[<C-\><C-n>]]):with_noremap():with_silent(), -- switch to normal mode in terminal.
["t|jk"] = map_cmd([[<C-\><C-n>]]):with_noremap():with_silent(), -- switch to normal mode in terminal.
["t|<Esc><Esc>"] = map_cmd([[<C-\><C-n>]]):with_noremap():with_silent(), -- switch to normal mode in terminal.
["t|jk"] = map_cmd([[<C-\><C-n>]]):with_noremap():with_silent(), -- switch to normal mode in terminal.
["n|<C-\\>"] = map_cr([[execute v:count . "ToggleTerm direction=horizontal"]])
:with_noremap()
:with_silent()
Expand All @@ -31,10 +31,7 @@ local plug_map = {
:with_noremap()
:with_silent()
:with_desc("terminal: Toggle horizontal"),
["t|<C-\\>"] = map_cmd("<Cmd>ToggleTerm<CR>")
:with_noremap()
:with_silent()
:with_desc("terminal: Toggle horizontal"),
["t|<C-\\>"] = map_cmd("<Cmd>ToggleTerm<CR>"):with_noremap():with_silent():with_desc("terminal: Toggle horizontal"),
["n|<leader>tt"] = map_cr([[execute v:count . "ToggleTerm direction=horizontal"]])
:with_noremap()
:with_silent()
Expand Down
Loading

0 comments on commit 37ecb17

Please sign in to comment.