Skip to content
This repository was archived by the owner on Jul 16, 2024. It is now read-only.

Commit 82c0f6d

Browse files
committed
feat: add tcd cwd lock feature
When tcd cwd is locked, the current tab will always use the tab cwd as default cwd
1 parent 7678f82 commit 82c0f6d

File tree

10 files changed

+80
-8
lines changed

10 files changed

+80
-8
lines changed

lazy-lock.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,19 @@
6767
"nvim-ts-context-commentstring": { "branch": "main", "commit": "7ab799a9792f7cf3883cf28c6a00ad431f3d382a" },
6868
"nvim-web-devicons": { "branch": "master", "commit": "6662f059bf78e6092ba90cd1929599625f448f55" },
6969
"nvim-window-picker": { "branch": "main", "commit": "41cfaa428577c53552200a404ae9b3a0b5719706" },
70-
"obsidian.nvim": { "branch": "main", "commit": "df0c5cce68a02481c0d40d1b25433e368fd12a5a" },
7170
"oil.nvim": { "branch": "master", "commit": "e462a3446505185adf063566f5007771b69027a1" },
7271
"overseer.nvim": { "branch": "master", "commit": "b04b0b105c07b4f02b3073ea3a98d6eca90bf152" },
7372
"plenary.nvim": { "branch": "master", "commit": "8aad4396840be7fc42896e3011751b7609ca4119" },
7473
"popfix": { "branch": "master", "commit": "bf3cc436df63cd535350d5ef1b951c91554d4b01" },
7574
"popup.nvim": { "branch": "master", "commit": "b7404d35d5d3548a82149238289fa71f7f6de4ac" },
76-
"project_nvim": { "branch": "main", "commit": "8d1d178d58b9b83cbc878d775cb356727c668dd7" },
75+
"project_nvim": { "branch": "main", "commit": "8f9c196bd159f85f949388a982770120cce1dcb7" },
7776
"range-highlight.nvim": { "branch": "master", "commit": "8b5e8ccb3460b2c3675f4639b9f54e64eaab36d9" },
7877
"registers": { "branch": "main", "commit": "22bb98f93a423252fffeb3531f7bc12a3e07b63f" },
7978
"rgflow.nvim": { "branch": "master", "commit": "e092b984d2ec244d67bedf6fea1b6ad5f91fbaba" },
8079
"smart-splits.nvim": { "branch": "master", "commit": "958d385f5f791ee4f8d63b9999b13d5f26eaff56" },
8180
"sqlite.lua": { "branch": "master", "commit": "40701b6151f8883980c1548647116de39b763540" },
8281
"ssr.nvim": { "branch": "main", "commit": "bb323ba621ac647b4ac5638b47666e3ef3c279e1" },
8382
"stay-in-place.nvim": { "branch": "main", "commit": "0628b6db8970fc731abf9608d6f80659b58932c9" },
84-
"stickybuf.nvim": { "branch": "master", "commit": "2160fcd536d81f5fa43f7167dba6634e814e3154" },
8583
"switch.vim": { "branch": "main", "commit": "68d269301181835788dcdcb6d5bca337fb954395" },
8684
"telescope-file-browser.nvim": { "branch": "master", "commit": "61271263618f5dbff9d7c35a161565d158e77f64" },
8785
"telescope-fzf-native.nvim": { "branch": "main", "commit": "9ef21b2e6bb6ebeaf349a0781745549bbb870d27" },

lua/user/config/autocmd.lua

+3
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,9 @@ function M.load_on_startup()
370370
if not new_cwd then
371371
new_cwd = safe_cwd()
372372
end
373+
if not vim.t.cwd_locked then
374+
vim.cmd.tcd(new_cwd)
375+
end
373376
local buf_cwd, buf_cwd_short = vim.b[ctx.buf].project_nvim_cwd, vim.b[ctx.buf].project_nvim_cwd_short
374377
local cwd, cwd_short = require('userlib.runtime.utils').update_cwd_env(buf_cwd, buf_cwd_short)
375378
if vim.b[ctx.buf].did_set_cwd_short == cwd then

lua/user/config/commands.lua

+11
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,14 @@ end, {
291291
nargs = 0,
292292
desc = 'Write all and save session and exit',
293293
})
294+
295+
create_cmd('LockTcd', function()
296+
require('userlib.runtime.utils').lock_tcd()
297+
end, {
298+
desc = 'Lock tcd',
299+
})
300+
create_cmd('UnlockTcd', function()
301+
require('userlib.runtime.utils').unlock_tcd()
302+
end, {
303+
desc = 'Unlock tcd',
304+
})

lua/user/plugins/finder.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ plug({
243243
pattern = 'oil:///*',
244244
callback = function(ctx)
245245
local cwd = require('oil').get_current_dir()
246-
require('userlib.runtime.utils').change_cwd(cwd, 'tcd', true)
246+
require('userlib.runtime.utils').change_cwd(cwd, 'lcd', true)
247247
end,
248248
})
249249
end,

lua/user/plugins/statusline.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ plug({
4444
comp.Tabs,
4545
comp.lpad({ comp.Branch, comp.GitStatus }),
4646
comp.lpad(comp.Overseer),
47-
-- comp.lpad(require('userlib.statusline.heirline.component_diagnostic')),
47+
{ provider = '%=' },
48+
comp.TabCwdLock,
4849
{ provider = '%=' },
4950
comp.rpad({
5051
provider = '%c,%l',

lua/user/plugins/workflow.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ plug({
369369
-- * global (default)
370370
-- * tab
371371
-- * win
372-
scope_chdir = 'tab',
372+
scope_chdir = 'custom',
373373
},
374374
},
375375
{

lua/userlib/legendary/commands/migrate.lua

+8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
return {
2+
{
3+
':LockTcd',
4+
description = "Lock current tab's cwd | Pin tab cwd",
5+
},
6+
{
7+
':UnlockTcd',
8+
description = "Unlock current tab's cwd | Unpin tab cwd",
9+
},
210
{
311
':ToggleSpider',
412
description = 'Toggle spider motion',

lua/userlib/mini/clue/folder-action.lua

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ M.open = function(new_cwd)
77

88
require('userlib.mini.clue').shortly_open(function(set, unset)
99
set('n', '1', '<cmd>echo expand("%")<cr>', { desc = ' :' .. nicely_cwd })
10+
11+
set('n', 'l', function()
12+
require('userlib.runtime.utils').lock_tcd(new_cwd)
13+
end, {
14+
desc = 'Lock cwd to current tab',
15+
})
16+
1017
set('n', 'f', function()
1118
require('userlib.fzflua').files({
1219
cwd = new_cwd,

lua/userlib/runtime/utils.lua

+22-1
Original file line numberDiff line numberDiff line change
@@ -314,16 +314,37 @@ function M.change_cwd(cwd, cmd, silent)
314314
return
315315
end
316316
cwd = require('userlib.runtime.path').remove_path_last_separator(cwd)
317-
vim.cmd((cmd or 'cd') .. ' ' .. cwd)
317+
if cmd ~= 'tcd' or vim.t.cwd_locked ~= true then
318+
vim.cmd((cmd or 'cd') .. ' ' .. cwd)
319+
end
318320
M.update_cwd_env(cwd)
319321
if not silent then
320322
vim.notify(('New cwd: %s'):format(vim.t.cwd_short), vim.log.levels.INFO)
321323
end
322324
end
323325

326+
--- make tab stick to a cwd
327+
-- @param cwd? string
328+
function M.lock_tcd(cwd)
329+
vim.t.cwd_locked = false
330+
M.change_cwd(cwd or safe_cwd(), 'tcd', false)
331+
--- must put at last
332+
vim.t.cwd_locked = true
333+
end
334+
335+
function M.unlock_tcd()
336+
vim.t.cwd_locked = false
337+
end
338+
324339
---@param cwd string
325340
---@param cwd_short? string
326341
function M.update_cwd_env(cwd, cwd_short)
342+
-- if current tab have locked cwd
343+
if vim.t.cwd_locked and vim.t.cwd then
344+
cwd = vim.t.cwd
345+
cwd_short = vim.t.cwd_short
346+
end
347+
327348
cwd = require('userlib.runtime.path').remove_path_last_separator(cwd)
328349
vim.t.cwd = cwd
329350
-- only show last part of path.

lua/userlib/statusline/heirline/components.lua

+24-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,28 @@ local ShortFileName = {
100100
},
101101
}
102102

103+
local TabCwdLock = {
104+
condition = function()
105+
return vim.t.cwd_locked and vim.t.cwd_short ~= ''
106+
end,
107+
{
108+
{
109+
provider = 'TCD:%-2.30(',
110+
},
111+
{
112+
init = function(self)
113+
self.tabnr = vim.api.nvim_get_current_tabpage()
114+
end,
115+
provider = function(self)
116+
return vim.t[self.tabnr].cwd_short
117+
end,
118+
},
119+
{
120+
provider = '%) ',
121+
},
122+
},
123+
}
124+
103125
local ViMode = {
104126
init = function(self)
105127
self.mode = vim.fn.mode() -- :h mode()
@@ -645,7 +667,7 @@ local Copilot = {
645667
if not self.is_running() then
646668
return '󰚩 '
647669
end
648-
return '󰆄 '
670+
return '������������� '
649671
end,
650672
hl = function(self)
651673
local fg = vim.g.copilot_auto_mode == true and 'orange' or ''
@@ -974,4 +996,5 @@ return {
974996
UnsavedBufCount = UnsavedBufCount,
975997
CocStl = CocStl,
976998
ShortFileName = ShortFileName,
999+
TabCwdLock = TabCwdLock,
9771000
}

0 commit comments

Comments
 (0)