mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-04-14 15:19:12 +08:00
refactor(file): refactor the file
api
This commit is contained in:
parent
9b65c22c73
commit
d5f7ef3332
@ -6,16 +6,43 @@
|
||||
" License: GPLv3
|
||||
"=============================================================================
|
||||
scriptencoding utf-8
|
||||
let s:file = {}
|
||||
let s:system = SpaceVim#api#import('system')
|
||||
let s:vim_comp = SpaceVim#api#import('vim#compatible')
|
||||
if exists('s:self')
|
||||
finish
|
||||
endif
|
||||
|
||||
if s:system.isWindows
|
||||
let s:file['separator'] = '\'
|
||||
let s:file['pathSeparator'] = ';'
|
||||
""
|
||||
" @section file, api-file
|
||||
" @parentsection api
|
||||
" The `file` api provides basic functions to manage file. The following
|
||||
" functions can be used:
|
||||
"
|
||||
" - `fticon(path)`: get the filetype icon of path
|
||||
" - `write(msg, fname)`: append msg to fname.
|
||||
" - `override(msg, fname)`: override fname with msg.
|
||||
" - `read(fname)`: read the context of fname.
|
||||
" - `ls(dir, if_file_only)`: list files and directories in dir
|
||||
" - `updatefiles(files)`: update all files
|
||||
" - `unify_path(path, ...)`: unify the format of path
|
||||
" - `path_to_fname(path)`: get unify string of a path.
|
||||
" - `findfile(pattern, dir)`: find path match pattern in dir.
|
||||
" - `finddir(pattern, dir)`: find directory match pattern in dir
|
||||
"
|
||||
" Example:
|
||||
" >
|
||||
" let s:FILE = SpaceVim#api#import('file')
|
||||
" <
|
||||
|
||||
|
||||
let s:self = {}
|
||||
let s:self.__system = SpaceVim#api#import('system')
|
||||
let s:self.__cmp = SpaceVim#api#import('vim#compatible')
|
||||
|
||||
if s:self.__system.isWindows
|
||||
let s:self.separator = '\'
|
||||
let s:self.pathSeparator = ';'
|
||||
else
|
||||
let s:file['separator'] = '/'
|
||||
let s:file['pathSeparator'] = ':'
|
||||
let s:self.separator = '/'
|
||||
let s:self.pathSeparator = ':'
|
||||
endif
|
||||
|
||||
let s:file_node_extensions = {
|
||||
@ -145,7 +172,7 @@ let s:file_node_pattern_matches = {
|
||||
\}
|
||||
|
||||
|
||||
function! s:filetypeIcon(path) abort
|
||||
function! s:self.fticon(path) abort
|
||||
let file = fnamemodify(a:path, ':t')
|
||||
if has_key(s:file_node_exact_matches, file)
|
||||
return s:file_node_exact_matches[file]
|
||||
@ -165,23 +192,17 @@ function! s:filetypeIcon(path) abort
|
||||
|
||||
endfunction
|
||||
|
||||
let s:file['fticon'] = function('s:filetypeIcon')
|
||||
|
||||
function! s:write(msg, fname) abort
|
||||
function! s:self.write(msg, fname) abort
|
||||
let flags = filewritable(a:fname) ? 'a' : ''
|
||||
call writefile([a:msg], a:fname, flags)
|
||||
endfunction
|
||||
|
||||
let s:file['write'] = function('s:write')
|
||||
|
||||
function! s:override(msg, fname) abort
|
||||
function! s:self.override(msg, fname) abort
|
||||
let flags = filewritable(a:fname) ? 'b' : ''
|
||||
call writefile([a:msg], a:fname, flags)
|
||||
endfunction
|
||||
|
||||
let s:file['override'] = function('s:override')
|
||||
|
||||
function! s:read(fname) abort
|
||||
function! s:self.read(fname) abort
|
||||
if filereadable(a:fname)
|
||||
return readfile(a:fname, '')
|
||||
else
|
||||
@ -189,9 +210,7 @@ function! s:read(fname) abort
|
||||
endif
|
||||
endfunction
|
||||
|
||||
let s:file['read'] = function('s:read')
|
||||
|
||||
function! s:ls(dir, if_file_only) abort
|
||||
function! s:self.ls(dir, if_file_only) abort
|
||||
let items = s:vim_comp.globpath(a:dir, '*')
|
||||
if a:if_file_only
|
||||
let items = filter(items, '!isdirectory(v:val)')
|
||||
@ -199,8 +218,6 @@ function! s:ls(dir, if_file_only) abort
|
||||
return map(items, "fnamemodify(v:val, ':t')")
|
||||
endfunction
|
||||
|
||||
let s:file['ls'] = function('s:ls')
|
||||
|
||||
"
|
||||
" {
|
||||
" 'filename' : {
|
||||
@ -208,7 +225,7 @@ let s:file['ls'] = function('s:ls')
|
||||
" line2 : content,
|
||||
" }
|
||||
" }
|
||||
function! s:updatefiles(files) abort
|
||||
function! s:self.updatefiles(files) abort
|
||||
let failed = []
|
||||
for fname in keys(a:files)
|
||||
let buffer = readfile(fname)
|
||||
@ -225,14 +242,12 @@ function! s:updatefiles(files) abort
|
||||
endfunction
|
||||
|
||||
|
||||
let s:file['updateFiles'] = function('s:updatefiles')
|
||||
|
||||
" this function should return a unify path
|
||||
" CHANGED: This function will not run resolve
|
||||
" 1. the sep is /
|
||||
" 2. if it is a dir, end with /
|
||||
" 3. if a:path end with /, then return path also end with /
|
||||
function! s:unify_path(path, ...) abort
|
||||
function! s:self.unify_path(path, ...) abort
|
||||
if empty(a:path)
|
||||
return ''
|
||||
endif
|
||||
@ -247,22 +262,18 @@ function! s:unify_path(path, ...) abort
|
||||
endif
|
||||
endfunction
|
||||
|
||||
let s:file['unify_path'] = function('s:unify_path')
|
||||
|
||||
function! s:path_to_fname(path, ...) abort
|
||||
function! s:self.path_to_fname(path, ...) abort
|
||||
let sep = get(a:000, 0, '_')
|
||||
return substitute(s:unify_path(a:path), '[\\/:;.]', sep, 'g')
|
||||
return substitute(self.unify_path(a:path), '[\\/:;.]', sep, 'g')
|
||||
endfunction
|
||||
|
||||
let s:file['path_to_fname'] = function('s:path_to_fname')
|
||||
|
||||
|
||||
" Both findfile() and finddir() do not has same logic between latest
|
||||
" version of vim and vim7.4.629. I do not know which pathch cause this
|
||||
" issue. But I have change the logic of these functions.
|
||||
" Now it should works same as in vim8 and old vim.
|
||||
|
||||
function! s:findfile(what, where, ...) abort
|
||||
function! s:self.findfile(what, where, ...) abort
|
||||
let old_suffixesadd = &suffixesadd
|
||||
let &suffixesadd = ''
|
||||
let l:count = get(a:000, 0, 0)
|
||||
@ -285,9 +296,7 @@ function! s:findfile(what, where, ...) abort
|
||||
return file
|
||||
endfunction
|
||||
|
||||
let s:file['findfile'] = function('s:findfile')
|
||||
|
||||
function! s:finddir(what, where, ...) abort
|
||||
function! s:self.finddir(what, where, ...) abort
|
||||
let old_suffixesadd = &suffixesadd
|
||||
let &suffixesadd = ''
|
||||
let l:count = get(a:000, 0, 0)
|
||||
@ -308,10 +317,8 @@ function! s:finddir(what, where, ...) abort
|
||||
let &suffixesadd = old_suffixesadd
|
||||
return file
|
||||
endfunction
|
||||
|
||||
let s:file['finddir'] = function('s:finddir')
|
||||
function! SpaceVim#api#file#get() abort
|
||||
return deepcopy(s:file)
|
||||
return deepcopy(s:self)
|
||||
endfunction
|
||||
|
||||
" vim:set et sw=2:
|
||||
|
@ -246,20 +246,21 @@ CONTENTS *SpaceVim-contents*
|
||||
4. data#list....................................|SpaceVim-api-data-list|
|
||||
5. data#number................................|SpaceVim-api-data-number|
|
||||
6. data#string................................|SpaceVim-api-data-string|
|
||||
7. job................................................|SpaceVim-api-job|
|
||||
8. logger..........................................|SpaceVim-api-logger|
|
||||
9. notify..........................................|SpaceVim-api-notify|
|
||||
10. password.....................................|SpaceVim-api-password|
|
||||
11. prompt.........................................|SpaceVim-api-prompt|
|
||||
12. sid...........................................|SpaceVim-api-vim-sid|
|
||||
13. system.........................................|SpaceVim-api-system|
|
||||
14. time.............................................|SpaceVim-api-time|
|
||||
15. unicode#box...............................|SpaceVim-api-unicode-box|
|
||||
16. vim#buffer.................................|SpaceVim-api-vim-buffer|
|
||||
17. vim#command...............................|SpaceVim-api-vim-command|
|
||||
18. vim#compatible.........................|SpaceVim-api-vim-compatible|
|
||||
19. vim#message...............................|SpaceVim-api-vim-message|
|
||||
20. vim#window.................................|SpaceVim-api-vim-window|
|
||||
7. file..............................................|SpaceVim-api-file|
|
||||
8. job................................................|SpaceVim-api-job|
|
||||
9. logger..........................................|SpaceVim-api-logger|
|
||||
10. notify.........................................|SpaceVim-api-notify|
|
||||
11. password.....................................|SpaceVim-api-password|
|
||||
12. prompt.........................................|SpaceVim-api-prompt|
|
||||
13. sid...........................................|SpaceVim-api-vim-sid|
|
||||
14. system.........................................|SpaceVim-api-system|
|
||||
15. time.............................................|SpaceVim-api-time|
|
||||
16. unicode#box...............................|SpaceVim-api-unicode-box|
|
||||
17. vim#buffer.................................|SpaceVim-api-vim-buffer|
|
||||
18. vim#command...............................|SpaceVim-api-vim-command|
|
||||
19. vim#compatible.........................|SpaceVim-api-vim-compatible|
|
||||
20. vim#message...............................|SpaceVim-api-vim-message|
|
||||
21. vim#window.................................|SpaceVim-api-vim-window|
|
||||
10. Development...............................................|SpaceVim-dev|
|
||||
1. commit-style-guide..................|SpaceVim-dev-commit-style-guide|
|
||||
11. FAQ.......................................................|SpaceVim-faq|
|
||||
@ -6025,6 +6026,28 @@ fill(str, length[, char])
|
||||
|
||||
fill string to length with {char}, if {char} is omnit, a space is used.
|
||||
|
||||
==============================================================================
|
||||
FILE *SpaceVim-api-file*
|
||||
|
||||
The `file` api provides basic functions to manage file. The following
|
||||
functions can be used:
|
||||
|
||||
`fticon(path)`: get the filetype icon of path
|
||||
`write(msg, fname)`: append msg to fname.
|
||||
`override(msg, fname)`: override fname with msg.
|
||||
`read(fname)`: read the context of fname.
|
||||
`ls(dir, if_file_only)`: list files and directories in dir
|
||||
`updatefiles(files)`: update all files
|
||||
`unify_path(path, ...)`: unify the format of path
|
||||
`path_to_fname(path)`: get unify string of a path.
|
||||
`findfile(pattern, dir)`: find path match pattern in dir.
|
||||
`finddir(pattern, dir)`: find directory match pattern in dir
|
||||
|
||||
Example:
|
||||
>
|
||||
let s:FILE = SpaceVim#api#import('file')
|
||||
<
|
||||
|
||||
==============================================================================
|
||||
JOB *SpaceVim-api-job*
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user