Skip to content

Commit

Permalink
✨ Rename in popup
Browse files Browse the repository at this point in the history
  • Loading branch information
Nguyen-Hoang-Nam committed Mar 4, 2022
1 parent 96eabc1 commit 5c9f8af
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 89 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ cooperation.
This configuration works properly with
JavaScript, Python, Go, Rust, Lua, PHP and Java.

Up coming languages, Haskell, Zig, Solidity, Scala, Dart (Flutter).

![Main](https://raw.githubusercontent.com/Nguyen-Hoang-Nam/readme-image/main/nvim-dotfiles/main.png)
_Kitty with Fira, Cascadia, and Mini-File-Icons_

Expand Down
65 changes: 65 additions & 0 deletions lua/dap/go.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
-- https://github.com/go-delve/delve/blob/master/Documentation/usage/dlv_dap.md
--
local M = {}

function M.adapters(callback, _)
local stdout = vim.loop.new_pipe(false)
local handle
local pid_or_err
local port = 38697
local opts = {
stdio = { nil, stdout },
args = { 'dap', '-l', '127.0.0.1:' .. port },
detached = true,
}

handle, pid_or_err = vim.loop.spawn('dlv', opts, function(code)
stdout:close()
handle:close()
if code ~= 0 then
print('dlv exited with code', code)
end
end)

assert(handle, 'Error running dlv: ' .. tostring(pid_or_err))
stdout:read_start(function(err, chunk)
assert(not err, err)
if chunk then
vim.schedule(function()
require('dap.repl').append(chunk)
end)
end
end)

-- Wait for delve to start
vim.defer_fn(function()
callback({ type = 'server', host = '127.0.0.1', port = port })
end, 100)
end

M.configuration = {
{
type = 'go',
name = 'Debug',
request = 'launch',
program = '${file}',
},

{
type = 'go',
name = 'Debug test', -- configuration for debugging test files
request = 'launch',
mode = 'test',
program = '${file}',
},

{
type = 'go',
name = 'Debug test (go.mod)',
request = 'launch',
mode = 'test',
program = './${relativeFileDirname}',
},
}

return M
27 changes: 27 additions & 0 deletions lua/dap/haskell.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
local M = {}

M.adapters = {
type = 'executable',
command = 'haskell-debug-adapter',
args = { '--hackage-version=0.0.35.0' },
}

M.configuration = {
{
type = 'haskell',
request = 'launch',
name = 'Debug',
workspace = '${workspaceFolder}',
startup = '${file}',
stopOnEntry = true,
logFile = vim.fn.stdpath('data') .. '/haskell-dap.log',
logLevel = 'WARNING',
ghciEnv = vim.empty_dict(),
ghciPrompt = 'λ: ',
-- Adjust the prompt to the prompt you see when you invoke the stack ghci command below
ghciInitialPrompt = 'λ: ',
ghciCmd = 'stack ghci --test --no-load --no-build --main-is TARGET --ghci-options -fprint-evld-with-show',
},
}

return M
2 changes: 1 addition & 1 deletion lua/languages/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function M.on_attach(client, bufnr)

buf_set_keymap('n', '<Leader>d', '<Cmd>lua vim.lsp.buf.definition()<CR>')
buf_set_keymap('n', '<Leader><Leader>', '<Cmd>lua vim.lsp.buf.hover()<CR>')
buf_set_keymap('n', '<Leader>r', '<cmd>lua vim.lsp.buf.rename()<CR>')
buf_set_keymap('n', '<Leader>r', '<cmd>lua require("utils.core").rename_popup()<CR>')
buf_set_keymap('n', '[d', '<cmd>lua vim.diagnostic.goto_prev()<CR>')
buf_set_keymap('n', ']d', '<cmd>lua vim.diagnostic.goto_next()<CR>')

Expand Down
2 changes: 1 addition & 1 deletion lua/languages/lua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ M.default_format = 'efm'
M.lsp_server = 'sumneko_lua'

local sumneko_root_path = '/home/nguyenhoangnam/.local/share/lsp/lua-language-server'
local sumneko_binary = sumneko_root_path .. '/bin/Linux/lua-language-server'
local sumneko_binary = sumneko_root_path .. '/bin/lua-language-server'

local runtime_path = vim.split(package.path, ';')
table.insert(runtime_path, 'lua/?.lua')
Expand Down
92 changes: 7 additions & 85 deletions lua/setup/dap.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
local dap = require('dap')
local sidebar = require('sidebar')

local go = require('dap.go')
local haskell = require('dap.haskell')

dap.defaults.fallback.terminal_win_cmd = ':belowright new | resize 10 | setlocal bt=nofile bh=wipe nobl noswapfile nu'

-- TODO: Find bigger icons
Expand Down Expand Up @@ -39,67 +42,8 @@ require('dapui').setup({
windows = { indent = 1 },
})

-- NOTE: Go dap
dap.adapters.go = function(callback, _)
local stdout = vim.loop.new_pipe(false)
local handle
local pid_or_err
local port = 38697
local opts = {
stdio = { nil, stdout },
args = { 'dap', '-l', '127.0.0.1:' .. port },
detached = true,
}

handle, pid_or_err = vim.loop.spawn('dlv', opts, function(code)
stdout:close()
handle:close()
if code ~= 0 then
print('dlv exited with code', code)
end
end)

assert(handle, 'Error running dlv: ' .. tostring(pid_or_err))
stdout:read_start(function(err, chunk)
assert(not err, err)
if chunk then
vim.schedule(function()
require('dap.repl').append(chunk)
end)
end
end)

-- Wait for delve to start
vim.defer_fn(function()
callback({ type = 'server', host = '127.0.0.1', port = port })
end, 100)
end

-- https://github.com/go-delve/delve/blob/master/Documentation/usage/dlv_dap.md
dap.configurations.go = {
{
type = 'go',
name = 'Debug',
request = 'launch',
program = '${file}',
},

{
type = 'go',
name = 'Debug test', -- configuration for debugging test files
request = 'launch',
mode = 'test',
program = '${file}',
},

{
type = 'go',
name = 'Debug test (go.mod)',
request = 'launch',
mode = 'test',
program = './${relativeFileDirname}',
},
}
dap.adapters.go = go.adapters
dap.configurations.go = go.configuration

-- NOTE: Nodejs dap
dap.adapters.node2 = {
Expand Down Expand Up @@ -128,29 +72,7 @@ dap.configurations.javascript = {
},
}

-- NOTE: Haskell dap
dap.adapters.haskell = {
type = 'executable',
command = 'haskell-debug-adapter',
args = { '--hackage-version=0.0.35.0' },
}

dap.configurations.haskell = {
{
type = 'haskell',
request = 'launch',
name = 'Debug',
workspace = '${workspaceFolder}',
startup = '${file}',
stopOnEntry = true,
logFile = vim.fn.stdpath('data') .. '/haskell-dap.log',
logLevel = 'WARNING',
ghciEnv = vim.empty_dict(),
ghciPrompt = 'λ: ',
-- Adjust the prompt to the prompt you see when you invoke the stack ghci command below
ghciInitialPrompt = 'λ: ',
ghciCmd = 'stack ghci --test --no-load --no-build --main-is TARGET --ghci-options -fprint-evld-with-show',
},
}
dap.adapters.haskell = haskell.adapters
dap.configurations.haskell = haskell.configuration

require('nvim-dap-virtual-text').setup()
49 changes: 49 additions & 0 deletions lua/utils/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,53 @@ function M.autocomment_by_filetypes(setting_filetypes, events, command)
cmd(autocmd)
end

local function cmd_option(callback)
return { noremap = true, silent = true, callback = callback }
end

function M.rename_popup()
local texts = { '' }

local width = 50
local buf = api.nvim_create_buf(false, true)
local opts = {
relative = 'cursor',
width = width,
height = 1,
col = 0,
row = 1,
anchor = 'NW',
style = 'minimal',
}

local win_handle = api.nvim_open_win(buf, 1, opts)

api.nvim_buf_set_lines(buf, 0, 1, false, texts)

api.nvim_buf_set_keymap(buf, 'n', 'q', ':close<CR>', { silent = true, nowait = true, noremap = true })
api.nvim_buf_set_keymap(
buf,
'i',
'<CR>',
'',
cmd_option(function()
local name = api.nvim_buf_get_lines(buf, 0, 1, false)[1]
api.nvim_win_close(win_handle, true)

if name ~= '' then
vim.lsp.buf.rename(name)
end

local keys = vim.api.nvim_replace_termcodes('<ESC>l', true, false, true)
api.nvim_feedkeys(keys, 'i', true)
end)
)

api.nvim_buf_set_option(buf, 'buftype', 'nofile')
api.nvim_buf_set_option(buf, 'bufhidden', 'delete')

-- Normal to insert
api.nvim_command('startinsert')
end

return M

0 comments on commit 5c9f8af

Please sign in to comment.