Skip to content

Instantly share code, notes, and snippets.

@takeshy takeshy/read_ts.vim
Last active Jul 11, 2018

Embed
What would you like to do?
typescriptのimport先のファイルの読み込み
function! ReadFile() abort
let s:currentPos = col('.')
let s:colNum = s:currentPos - 1
let s:lastPos = len(getline('.'))
let s:fileName = ''
let s:ext = '.' . expand("%:e")
while s:colNum > -1
if getline('.')[s:colNum] =~ "\['\"\]"
break
end
let s:fileName = getline('.')[s:colNum] . s:fileName
let s:colNum = s:colNum - 1
endwhile
while s:currentPos < s:lastPos
if getline('.')[s:currentPos] =~ "\['\"\]"
break
end
let s:fileName = s:fileName . getline('.')[s:currentPos]
let s:currentPos = s:currentPos + 1
endwhile
let s:prevPath = ""
let s:dirs = split(expand("%:p:h"), '/')
if s:fileName =~ "^\\."
let s:fullName = simplify(expand("%:p:h") . '/' . s:fileName)
else
for name in s:dirs
let s:prevPath = s:prevPath . '/' . name
if (name == "ts" && (s:ext == ".ts" || s:ext == ".tsx")) || (name == "es6")
break
endif
endfor
let s:fullName = simplify(s:prevPath . '/' . s:fileName)
endif
if !filereadable(s:fullName)
if isdirectory(s:fullName)
let s:fullName = s:fullName . '/index' . s:ext
else
if filereadable(s:fullName. '.ts')
let s:fullName = s:fullName . '.ts'
else
if filereadable(s:fullName. '.tsx')
let s:fullName = s:fullName . '.tsx'
else
if filereadable(s:fullName. '.d.ts')
let s:fullName = s:fullName . '.d.ts'
else
let s:fullName = s:fullName . s:ext
endif
endif
endif
endif
endif
execute ':e ' . s:fullName
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.