Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 5.18 KB | None | 0 0
  1. set grepprg=LC_ALL=C\ grep\ -nrsH\ --exclude-dir={node_modules,.git,Session.vim} " External grep command to use
  2.  
  3. setlocal suffixesadd+=.ts,.tsx,.json,.js
  4.  
  5. setlocal include=\\%(\\<require\\s*(\\s*\\\|\\<import\\>[^;\"']*\\)[\"']\\zs[^\"']*
  6.  
  7. setlocal define=^\\s*\\(export\ \\)\\?\\<\\(type\\\|var\\\|interface\\\|const\\\|let\\\|\\(async\ \\)\\?function\\\|class\\)\\>
  8.  
  9. setlocal isfname+=@-@
  10.  
  11. setlocal sw=2
  12.  
  13. setlocal commentstring=//\ %s
  14.  
  15. setlocal includeexpr=TypeScriptIncludeExpression(v:fname)
  16.  
  17. nnoremap <leader>n :call NodeModulesIncsearchToggle()<CR>
  18.  
  19. let g:node = 0
  20.  
  21. function! NodeModulesIncsearchToggle()
  22.    if g:node
  23.        let g:node = 0
  24.    else
  25.        let g:node = 1
  26.    endif
  27. endfunction
  28.    
  29. function! TypeScriptIncludeExpression(fname) abort
  30.    " BUILT-IN NODE MODULES
  31.     " =====================
  32.     " they aren't accessible so we might as well give up early
  33.     if index([
  34.         \ 'assert', 'async_hooks',
  35.         \ 'child_process', 'cluster', 'crypto',
  36.         \ 'dgram', 'dns', 'domain',
  37.         \ 'events',
  38.         \ 'fs',
  39.         \ 'http', 'http2', 'https',
  40.         \ 'inspector',
  41.         \ 'net',
  42.         \ 'os',
  43.         \ 'path', 'perf_hooks', 'punycode',
  44.         \ 'querystring',
  45.         \ 'readline',
  46.         \ 'stream', 'string_decoder',
  47.         \ 'tls', 'tty',
  48.         \ 'url', 'util',
  49.         \ 'v8', 'vm',
  50.         \ 'zlib' ], a:fname) != -1
  51.         return 0
  52.     endif
  53.    
  54.     " ./foo
  55.     " ./foo/bar
  56.     " ../foo
  57.     " ../foo/bar
  58.     " simplify module name to find it more easily
  59.     let module_name = substitute(a:fname, '^\W*', '', '')
  60.  
  61.  
  62.     " LOCAL IMPORTS
  63.     " =============
  64.     " they are everywhere so we must get them right
  65.     if a:fname =~ '^\.'
  66.         " ./
  67.         if a:fname =~ '^\./$'
  68.             return 'index.ts'
  69.         endif
  70.  
  71.         " ../
  72.         if a:fname =~ '\.\./$'
  73.             return a:fname . 'index.ts'
  74.         endif
  75.  
  76.         " first, look for the module name only
  77.         " (findfile() uses 'suffixesadd')
  78.         let found_plain = findfile(module_name, '.;')
  79.         if len(found_plain)
  80.             return found_plain
  81.         endif
  82.  
  83.         " second, look for an index.ts file
  84.         let found_index = findfile(module_name . '/index.ts', '.;')
  85.         if len(found_index)
  86.             return found_index
  87.         endif
  88.  
  89.         " give up
  90.         return a:fname
  91.     endif
  92.        
  93.     " REQUIRE IMPORTS
  94.     " ===============
  95.     " first, look for the module name only
  96.     " (findfile() uses 'suffixesadd')
  97.     let found_plain = findfile(module_name, '.;')
  98.     if len(found_plain)
  99.         return found_plain
  100.     endif
  101.  
  102.     " second, look for an index.js file
  103.     let found_index = findfile(module_name . '/index.js', '.;')
  104.     if len(found_index)
  105.         return found_index
  106.     endif
  107.  
  108.     if !g:node
  109.         return 0
  110.     endif
  111.  
  112.     " NODE IMPORTS
  113.     " ============
  114.     " localize the closest node_modules/
  115.     let node_modules = finddir('node_modules', '.;', -1)
  116.  
  117.     " give up if there's none
  118.     if !len(node_modules)
  119.         return 0
  120.     endif
  121.  
  122.     " split the filename in meaningful parts:
  123.     " - a package name, used to search for the package in node_modules/
  124.     " - a subpath if applicable, used to reach the right module
  125.     "
  126.     " example:
  127.     " import bar from 'coolcat/foo/bar';
  128.     " - package_name = coolcat
  129.     " - sub_path     = foo/bar
  130.     "
  131.     " special case:
  132.     " import something from '@scope/something/else';
  133.     " - package_name = @scope/something
  134.     " - sub_path     = else
  135.     let parts = split(a:fname, '/')
  136.  
  137.     if parts[0] =~ '^@'
  138.         let package_name = join(parts[0:1], '/')
  139.         let sub_path = join(parts[2:-1], '/')
  140.     else
  141.         let package_name = parts[0]
  142.         let sub_path = join(parts[1:-1], '/')
  143.     endif
  144.  
  145.     " find the package.json for that package
  146.     let package_json = node_modules[0] . '/' . package_name . '/package.json'
  147.  
  148.     " give up if there's no package.json
  149.     if !filereadable(package_json)
  150.         return 0
  151.     endif
  152.  
  153.     if len(sub_path) == 0
  154.         " grab data from the package.json
  155.         let package = json_decode(join(readfile(package_json)))
  156.  
  157.         " build path from 'main' key
  158.         return fnamemodify(package_json, ':p:h') . "/" . substitute(get(package, "main", "index.js"), '^\.\{1,2}\/', '', '')
  159.     else
  160.         " build the path to the module
  161.         let common_path = fnamemodify(package_json, ':p:h') . '/' . sub_path
  162.  
  163.         " first, try with .ts
  164.         let found_dotts = glob(common_path . '.ts', 1)
  165.         if len(found_dotts)
  166.             return found_dotts
  167.         endif
  168.  
  169.         " second, try with /index.ts
  170.         let found_index = glob(common_path . '/index.ts', 1)
  171.         if len(found_index)
  172.             return found_index
  173.         endif
  174.  
  175.         " third, try with .js
  176.         let found_dotts = glob(common_path . '.js', 1)
  177.         if len(found_dotts)
  178.             return found_dotts
  179.         endif
  180.  
  181.         " fourth, try with /index.js
  182.         let found_index = glob(common_path . '/index.js', 1)
  183.         if len(found_index)
  184.             return found_index
  185.         endif
  186.  
  187.         " give up
  188.         return 0
  189.     endif
  190.  
  191.     " give up
  192.     return 0
  193. endfunction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement