diff options
author | Adam Stankiewicz <sheerun@sher.pl> | 2017-03-23 11:28:19 +0100 |
---|---|---|
committer | Adam Stankiewicz <sheerun@sher.pl> | 2017-03-23 11:28:28 +0100 |
commit | 0801eac01aab5940fc1e4409ba749383cc353bc2 (patch) | |
tree | 9034c9f6cd0c7592a09e6c65521c6948c3a983af /autoload/dart.vim | |
parent | 9f735b1fe77072e001a593f7f6660703bf4a8c9c (diff) | |
download | vim-polyglot-0801eac01aab5940fc1e4409ba749383cc353bc2.tar.gz vim-polyglot-0801eac01aab5940fc1e4409ba749383cc353bc2.zip |
Update
Diffstat (limited to 'autoload/dart.vim')
-rw-r--r-- | autoload/dart.vim | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/autoload/dart.vim b/autoload/dart.vim index df341b48..9f04fcc2 100644 --- a/autoload/dart.vim +++ b/autoload/dart.vim @@ -68,5 +68,86 @@ function! dart#tojs(q_args) abort endif endfunction +" Finds the path to `uri`. +" +" If the file is a package: uri, looks for a .packages file to resolve the path. +" If the path cannot be resolved, or is not a package: uri, returns the +" original. +function! dart#resolveUri(uri) abort + if a:uri !~ 'package:' + return a:uri + endif + let package_name = substitute(a:uri, 'package:\(\w\+\)\/.*', '\1', '') + let [found, package_map] = s:PackageMap() + if !found + call s:error('cannot find .packages file') + return a:uri + endif + if !has_key(package_map, package_name) + call s:error('no package mapping for '.package_name) + return a:uri + endif + let package_lib = package_map[package_name] + return substitute(a:uri, + \ 'package:'.package_name, + \ escape(package_map[package_name], '\'), + \ '') +endfunction + +" A map from package name to lib directory parse from a '.packages' file. +" +" Returns [found, package_map] +function! s:PackageMap() abort + let [found, dot_packages] = s:DotPackagesFile() + if !found + return [v:false, {}] + endif + let dot_packages_dir = fnamemodify(dot_packages, ':p:h') + let lines = readfile(dot_packages) + let map = {} + for line in lines + if line =~ '\s*#' + continue + endif + let package = substitute(line, ':.*$', '', '') + let lib_dir = substitute(line, '^[^:]*:', '', '') + if lib_dir =~ 'file:/' + let lib_dir = substitute(lib_dir, 'file://', '', '') + if lib_dir =~ '/[A-Z]:/' + let lib_dir = lib_dir[1:] + endif + else + let lib_dir = resolve(dot_packages_dir.'/'.lib_dir) + endif + if lib_dir =~ '/$' + let lib_dir = lib_dir[:len(lib_dir) - 2] + endif + let map[package] = lib_dir + endfor + return [v:true, map] +endfunction + +" Finds a file name '.packages' in the cwd, or in any directory above the open +" file. +" +" Returns [found, file]. +function! s:DotPackagesFile() abort + if filereadable('.packages') + return [v:true, '.packages'] + endif + let dir_path = expand('%:p:h') + while v:true + let file_path = dir_path.'/.packages' + if filereadable(file_path) + return [v:true, file_path] + endif + let parent = fnamemodify(dir_path, ':h') + if dir_path == parent + break + endif + let dir_path = parent + endwhile + return [v:false, ''] +endfunction endif |