Skip to content

Commit 52f67bc

Browse files
committed
feat(new_class,ts): to not overwrite existing file, cancel when file already exists
1 parent 590ca5b commit 52f67bc

File tree

7 files changed

+64
-6
lines changed

7 files changed

+64
-6
lines changed

autoload/refactoring_toolbox/new_class/adapters/spy_filesystem.vim

+9
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@ let s:constructed = v:false
2020
function s:constuct()
2121
let s:self.writtenFiles = []
2222
let s:self.writtenLines = []
23+
let s:self.filesystemList = []
2324

2425
let s:constructed = v:true
2526

2627
return s:self
2728
endfunction
2829

30+
function s:self.filepathExists(filepath)
31+
return s:LIST_ITEM_NOT_FOUND != index(s:self.filesystemList, a:filepath)
32+
endfunction
33+
2934
function s:self.writeFileWithLines(filepath, lines)
3035
call add(s:self.writtenFiles, a:filepath)
3136
call add(s:self.writtenLines, a:lines)
@@ -39,6 +44,10 @@ function s:self.writtenLinesOnFilepath(filepath)
3944
return s:self.writtenLines[s:findFileIndex(a:filepath)]
4045
endfunction
4146

47+
function s:self.setFilesystemList(list)
48+
let s:self.filesystemList = a:list
49+
endfunction
50+
4251
function s:findFileIndex(filepath)
4352
return index(s:self.writtenFiles, a:filepath)
4453
endfunction

autoload/refactoring_toolbox/new_class/adapters/vim_filesystem.vim

+4
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ function s:self.writeFileWithLines(filepath, lines)
1010
call writefile(a:lines, a:filepath)
1111
endfunction
1212

13+
function s:self.filepathExists(filepath)
14+
return filereadable(a:filepath) || isdirectory(a:filepath)
15+
endfunction
16+
1317
call refactoring_toolbox#adapters#vim#end_script()

autoload/refactoring_toolbox/new_class/class_maker.vim

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
call refactoring_toolbox#adapters#vim#begin_script()
22

3-
function refactoring_toolbox#new_class#class_maker#execute(filesystem, texteditor, language)
3+
function refactoring_toolbox#new_class#class_maker#execute(filesystem, texteditor, language, output)
44
let s:filesystem = a:filesystem
55
let s:texteditor = a:texteditor
66
let s:language = a:language
7+
let s:output = a:output
78

89
let l:newClassName = s:texteditor.getWordOnCursor()
910
let l:newFilepath = s:getFilePathOfNewClassNamed(l:newClassName, s:language.getFileExtension())
1011

11-
call s:filesystem.writeFileWithLines(l:newFilepath, s:language.makeClassFileLines(l:newClassName))
12+
if s:filesystem.filepathExists(l:newFilepath)
13+
call s:output.echoWarning('Going to create new class "'.l:newClassName.'" on file "'.l:newFilepath.'" but that file already exists.')
14+
else
15+
call s:filesystem.writeFileWithLines(l:newFilepath, s:language.makeClassFileLines(l:newClassName))
1216

13-
call s:texteditor.openFileAside(l:newFilepath)
17+
call s:texteditor.openFileAside(l:newFilepath)
18+
endif
1419
endfunction
1520

1621
function s:getFilePathOfNewClassNamed(className, extension)

autoload/refactoring_toolbox/new_class/main.vim

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function refactoring_toolbox#new_class#main#executeForTypescript()
1616
\ refactoring_toolbox#new_class#adapters#filesystem_factory#make(),
1717
\ refactoring_toolbox#new_class#adapters#texteditor_factory#make(),
1818
\ refactoring_toolbox#new_class#adapters#ts_language#make(),
19+
\ refactoring_toolbox#adapters#output#make(),
1920
\ )
2021
endfunction
2122

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Before:
2+
call mkdir(g:test_tmpdir, 'p')
3+
4+
After:
5+
call delete(g:test_tmpdir, 'rf')
6+
7+
Execute (filepathExists(not_exists_file) == v:false):
8+
Assert ! refactoring_toolbox#new_class#adapters#vim_filesystem#make().filepathExists(g:test_tmpdir.'/not_existing')
9+
10+
Execute (filepathExists(file) == v:true):
11+
call writefile([], g:test_tmpdir.'/file')
12+
Assert refactoring_toolbox#new_class#adapters#vim_filesystem#make().filepathExists(g:test_tmpdir.'/file')
13+
14+
Execute (filepathExists(directory) == v:true):
15+
Assert refactoring_toolbox#new_class#adapters#vim_filesystem#make().filepathExists(g:test_tmpdir)
16+
17+
Execute (filepathExists(symlink) == v:true):
18+
call writefile([], g:test_tmpdir.'/file')
19+
call system('ln -sr '.shellescape(g:test_tmpdir.'/file').' '.shellescape(g:test_tmpdir.'/symlink'))
20+
21+
Assert refactoring_toolbox#new_class#adapters#vim_filesystem#make().filepathExists(g:test_tmpdir.'/symlink')
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
Before:
22
call mkdir(g:test_tmpdir, 'p')
33

4+
After:
5+
call delete(g:test_tmpdir, 'rf')
6+
47
Execute:
58
call refactoring_toolbox#new_class#adapters#vim_filesystem#make().writeFileWithLines(g:test_tmpdir.'/new_class.ext', ['some line'])
6-
7-
Then:
89
AssertEqual ['some line'], readfile(g:test_tmpdir.'/new_class.ext')
9-
call delete(g:test_tmpdir, 'rf')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Given typescript:
2+
NewClass
3+
4+
Before:
5+
call refactoring_toolbox#new_class#adapters#texteditor_factory#setTexteditor(
6+
\ refactoring_toolbox#new_class#adapters#stub_texteditor#makeWithCurrentFileDirectory('some/current')
7+
\ )
8+
call refactoring_toolbox#new_class#adapters#filesystem_factory#setFilesystem(
9+
\ refactoring_toolbox#new_class#adapters#spy_filesystem#make()
10+
\ )
11+
call refactoring_toolbox#new_class#adapters#spy_filesystem#get().setFilesystemList(['some/current/NewClass.ts'])
12+
13+
Do:
14+
/NewClass\<Enter>
15+
;nc
16+
17+
Then:
18+
AssertEqual [], refactoring_toolbox#new_class#adapters#spy_filesystem#get().writtenFiles

0 commit comments

Comments
 (0)