Skip to content

Commit 6252270

Browse files
committed
Support open shelf files
1 parent 5319123 commit 6252270

2 files changed

Lines changed: 65 additions & 24 deletions

File tree

autoload/project.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ function! s:Prepare()
144144
\'push': 'p',
145145
\'pull_and_push': 'P',
146146
\'shelf': 's',
147-
\'unshelf': 'b',
147+
\'unshelf': 'S',
148148
\}
149149
let s:default.file_open_types = {
150150
\'': 'edit',
@@ -1372,12 +1372,12 @@ function! project#RunShellCmd(cmd, warning=1)
13721372

13731373
if v:shell_error
13741374
if !empty(output) && a:warning
1375+
call project#Warn('----------------------------------------')
13751376
call project#Warn(a:cmd)
13761377
for error in output
13771378
let formatted_error = substitute(error, ' ', ' ', 'g')
13781379
call project#Warn(formatted_error)
13791380
endfor
1380-
call project#Warn('')
13811381
redraw
13821382
execute (len(output) + 2).'messages'
13831383
endif

autoload/project/git.vim

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,11 @@ function! s:SwitchBuffer(search)
322322
endfunction
323323

324324
function! s:GetBufWinnr(search)
325-
return bufwinnr(escape(a:search, '[]'))
325+
return bufwinnr('^'.escape(a:search, '[]').'$')
326326
endfunction
327327

328328
function! s:GetBufnr(search)
329-
return bufnr(escape(a:search, '[]'))
329+
return bufnr('^'.escape(a:search, '[]').'$')
330330
endfunction
331331

332332
function! s:CloseBuffer(name)
@@ -469,20 +469,20 @@ function! s:IsStagedFile(file)
469469
return s:IsStagedFolder(folder)
470470
endfunction
471471

472+
function! s:IsShelfFile(file)
473+
return s:Match(a:file, '\.\(diff\|patch\)$')
474+
endfunction
475+
472476
function! s:TryGetDiffFile(file)
473-
let is_shelf = s:Match(a:file, '\.\(diff\|patch\)$')
474-
if !is_shelf
475-
return 0
477+
if !s:IsShelfFile(a:file)
478+
return ''
476479
endif
477-
let shelf_folder = s:GetShelfFolder()
478-
let folder = s:GetBelongFolder(line('.'))
479-
let shelf_file = shelf_folder.'/'.folder.name.'/'.a:file
480-
let has_shelf_file = filereadable(shelf_file)
481-
if has_shelf_file
482-
return shelf_file
480+
let diff_file = s:GetDiffFileByLine(line('.'))
481+
if filereadable(diff_file)
482+
return diff_file
483483
endif
484484

485-
return 0
485+
return ''
486486
endfunction
487487

488488
function! s:RunJob(cmd, exit_cb, buf_nr)
@@ -758,16 +758,22 @@ endfunction
758758

759759
function! s:OpenFolderOrFile()
760760
let lnum = line('.')
761-
let item = s:GetCurrentFolder(lnum)
762-
if empty(item)
761+
let folder = s:GetCurrentFolder(lnum)
762+
if empty(folder)
763763
let file = s:GetCurrentFile()
764764
if empty(file)
765765
return
766766
endif
767+
768+
let diff_file = s:TryGetDiffFile(file)
767769
wincmd k
768-
execute 'e '.s:GetAbsolutePath(file)
770+
if empty(diff_file)
771+
execute 'e '.s:GetAbsolutePath(file)
772+
else
773+
execute 'e '.diff_file
774+
endif
769775
else
770-
let item.expand = 1 - item.expand
776+
let folder.expand = 1 - folder.expand
771777
call s:ShowStatus()
772778
execute lnum
773779
endif
@@ -801,7 +807,11 @@ function! s:GetShelfFolder()
801807
let config_home = project#GetVariable('config_home')
802808
let project = project#GetVariable('project')
803809
let config = project#GetProjectConfigPath(config_home, project)
804-
return config.'/shelf/'
810+
return config.'/shelf'
811+
endfunction
812+
813+
function! s:GetDiffFilePath(dir, filename)
814+
return join(a:dir, s:shelf_path_spliter).s:shelf_path_file_spliter.a:filename
805815
endfunction
806816

807817
function! s:ShelfFile() range
@@ -835,8 +845,7 @@ function! s:ShelfFile() range
835845
else
836846
let dir = segments[:-2]
837847
let filename = segments[-1]
838-
let diff_file = join(dir, s:shelf_path_spliter)
839-
\.s:shelf_path_file_spliter.filename
848+
let diff_file = s:GetDiffFilePath(dir, filename)
840849
let cmd = diff_cmd.' > '.folder.'/'.diff_file.'.diff'
841850
endif
842851
call project#RunShellCmd(cmd)
@@ -849,6 +858,38 @@ function! s:ShelfFile() range
849858
call s:CloseBuffer(s:diff_buffer)
850859
endfunction
851860

861+
function! s:GetDiffFileByLine(lnum)
862+
let folder = s:GetBelongFolder(a:lnum)
863+
let folder_name = folder.name
864+
let dir = split(s:GetDirectoryByLine(a:lnum), '/\|\\')
865+
let filename = s:GetFilenameByLine(a:lnum)
866+
if len(dir) == 0
867+
let diff_file = filename
868+
else
869+
let diff_file = s:GetDiffFilePath(dir, filename)
870+
endif
871+
let shelf_folder = s:GetShelfFolder()
872+
return shelf_folder.'/'.folder_name.'/'.diff_file
873+
endfunction
874+
875+
function! s:UnshelfFile() range
876+
let lnum = line('.')
877+
let folder = s:GetCurrentFolder(lnum)
878+
if empty(folder)
879+
let lines = range(a:firstline, a:lastline)
880+
let files = map(lines, {idx, v -> s:GetDiffFileByLine(v)})
881+
let diff_files = join(files, ' ')
882+
else
883+
let folder_name = folder.name
884+
let files = folder.files
885+
let shelf_folder = s:GetShelfFolder()
886+
let diff_files = join(map(copy(files), {idx, v -> shelf_folder.'/'.folder_name.'/'.v}), ' ')
887+
endif
888+
let cmd = 'git apply '.diff_files
889+
call project#RunShellCmd(cmd)
890+
call s:ShowStatus(1)
891+
endfunction
892+
852893
function! s:RollbackFile() range
853894
let files = []
854895
for lnum in range(a:firstline, a:lastline)
@@ -1221,7 +1262,7 @@ function! s:UpdateShelfChangelist()
12211262
let cmd = 'ls '.shelf_folder
12221263
let folder_names = project#RunShellCmd(cmd)
12231264
for folder_name in folder_names
1224-
let folder_path = shelf_folder.folder_name
1265+
let folder_path = shelf_folder.'/'.folder_name
12251266
if isdirectory(folder_path)
12261267
let folder_path_cmd = 'ls '.folder_path
12271268
let files = project#RunShellCmd(folder_path_cmd)
@@ -1483,8 +1524,8 @@ function! s:SetupChangelistBuffer()
14831524
call s:AddVisualMapping(mappings.move_to_changelist, '<SID>MoveToFolder()')
14841525
call s:AddMapping(mappings.shelf, '<SID>ShelfFile()')
14851526
call s:AddVisualMapping(mappings.shelf, '<SID>ShelfFile()')
1486-
call s:AddMapping(mappings.unshelf, '<SID>ShelfFile()')
1487-
call s:AddVisualMapping(mappings.unshelf, '<SID>ShelfFile()')
1527+
call s:AddMapping(mappings.unshelf, '<SID>UnshelfFile()')
1528+
call s:AddVisualMapping(mappings.unshelf, '<SID>UnshelfFile()')
14881529

14891530
hi DiffBufferModify ctermfg=3 guifg=#b58900
14901531
hi DiffBufferAdd ctermfg=2 guifg=#719e07

0 commit comments

Comments
 (0)