Skip to content

Commit

Permalink
Switch typescript provider, closes #428
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerun committed Sep 6, 2019
1 parent 66b7693 commit 84ec4ee
Show file tree
Hide file tree
Showing 69 changed files with 3,196 additions and 639 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -178,7 +178,7 @@ If you need full functionality of any plugin, please use it directly with your p
- [toml](https://github.com/cespare/vim-toml) (syntax, ftplugin)
- [tptp](https://github.com/c-cube/vim-tptp) (syntax)
- [twig](https://github.com/lumiliet/vim-twig) (syntax, indent, ftplugin)
- [typescript](https://github.com/leafgarland/typescript-vim) (syntax, indent, compiler, ftplugin)
- [typescript](https://github.com/HerringtonDarkholme/yats.vim) (syntax, indent, compiler, ftplugin)
- [vala](https://github.com/arrufat/vala.vim) (syntax, indent)
- [vbnet](https://github.com/vim-scripts/vbnet.vim) (syntax)
- [vcl](https://github.com/smerrill/vcl-vim-plugin) (syntax)
Expand Down
2 changes: 1 addition & 1 deletion build
Expand Up @@ -292,7 +292,7 @@ PACKS="
toml:cespare/vim-toml
tptp:c-cube/vim-tptp
twig:lumiliet/vim-twig
typescript:leafgarland/typescript-vim
typescript:HerringtonDarkholme/yats.vim
vala:arrufat/vala.vim
vbnet:vim-scripts/vbnet.vim
vcl:smerrill/vcl-vim-plugin
Expand Down
31 changes: 11 additions & 20 deletions compiler/typescript.vim
Expand Up @@ -2,33 +2,24 @@ if exists('g:polyglot_disabled') && index(g:polyglot_disabled, 'typescript') !=
finish
endif

if exists("current_compiler")
if exists('current_compiler')
finish
endif
let current_compiler = "typescript"

if !exists("g:typescript_compiler_binary")
let g:typescript_compiler_binary = "tsc"
endif
let current_compiler='typescript'

if !exists("g:typescript_compiler_options")
let g:typescript_compiler_options = ""
if !exists('g:typescript_compiler_binary')
let g:typescript_compiler_binary = 'tsc'
endif

if exists(":CompilerSet") != 2
command! -nargs=* CompilerSet setlocal <args>
if !exists('g:typescript_compiler_options')
if exists('g:syntastic_typescript_tsc_args')
let g:typescript_compiler_options = g:syntastic_typescript_tsc_args
else
let g:typescript_compiler_options = ''
endif
endif

let s:cpo_save = &cpo
set cpo-=C

execute 'CompilerSet makeprg='
\ . escape(g:typescript_compiler_binary, ' ')
\ . '\ '
\ . escape(g:typescript_compiler_options, ' ')
\ . '\ $*\ %'
let &l:makeprg = g:typescript_compiler_binary . ' ' . g:typescript_compiler_options . ' $* %'

CompilerSet errorformat=%+A\ %#%f\ %#(%l\\\,%c):\ %m,%C%m

let &cpo = s:cpo_save
unlet s:cpo_save
14 changes: 9 additions & 5 deletions ftdetect/polyglot.vim
Expand Up @@ -1425,11 +1425,15 @@ endif

if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'typescript') == -1
augroup filetypedetect
" typescript, from typescript.vim in leafgarland/typescript-vim
" use `set filetype` to override default filetype=xml for *.ts files
autocmd BufNewFile,BufRead *.ts set filetype=typescript
" use `setfiletype` to not override any other plugins like ianks/vim-tsx
autocmd BufNewFile,BufRead *.tsx setfiletype typescript
" typescript, from tsx.vim in HerringtonDarkholme/yats.vim
autocmd BufNewFile,BufRead *.tsx setlocal filetype=typescript.tsx
augroup end
endif

if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'typescript') == -1
augroup filetypedetect
" typescript, from typescript.vim in HerringtonDarkholme/yats.vim
autocmd BufNewFile,BufRead *.ts setlocal filetype=typescript
augroup end
endif

Expand Down
15 changes: 15 additions & 0 deletions ftplugin/tsx.vim
@@ -0,0 +1,15 @@
if exists('g:polyglot_disabled') && index(g:polyglot_disabled, 'typescript') != -1
finish
endif

" modified from mxw/vim-jsx from html.vim
if exists("loaded_matchit") && !exists('b:tsx_match_words')
let b:match_ignorecase = 0
let b:tsx_match_words = '(:),\[:\],{:},<:>,' .
\ '<\@<=\([^/][^ \t>]*\)[^>]*\%(/\@<!>\|$\):<\@<=/\1>'
let b:match_words = exists('b:match_words')
\ ? b:match_words . ',' . b:tsx_match_words
\ : b:tsx_match_words
endif

set suffixesadd+=.tsx
67 changes: 64 additions & 3 deletions ftplugin/typescript.vim
Expand Up @@ -2,6 +2,8 @@ if exists('g:polyglot_disabled') && index(g:polyglot_disabled, 'typescript') !=
finish
endif

" set Vi-incompatible, compiler and commentstring

if exists("b:did_ftplugin")
finish
endif
Expand All @@ -17,9 +19,68 @@ setlocal commentstring=//\ %s
" " and insert the comment leader when hitting <CR> or using "o".
setlocal formatoptions-=t formatoptions+=croql

setlocal suffixesadd+=.ts,.tsx

let b:undo_ftplugin = "setl fo< ofu< com< cms<"
" setlocal foldmethod=syntax

let &cpo = s:cpo_save
unlet s:cpo_save

function! TsIncludeExpr(file)
if (filereadable(a:file))
return l:file
else
let l:file2=substitute(a:file,'$','/index.ts','g')
return l:file2
endif
endfunction

set path+=./node_modules/**,node_modules/**
set include=import\_s.\\zs[^'\"]*\\ze
set includeexpr=TsIncludeExpr(v:fname)
set suffixesadd=.ts

"
" TagBar
"
let g:tagbar_type_typescript = {
\ 'ctagstype' : 'typescript',
\ 'kinds' : [
\ 'c:classes',
\ 'a:abstract classes',
\ 't:types',
\ 'n:modules',
\ 'f:functions',
\ 'v:variables',
\ 'l:varlambdas',
\ 'm:members',
\ 'i:interfaces',
\ 'e:enums'
\ ],
\ 'sro' : '.',
\ 'kind2scope' : {
\ 'c' : 'classes',
\ 'a' : 'abstract classes',
\ 't' : 'types',
\ 'f' : 'functions',
\ 'v' : 'variables',
\ 'l' : 'varlambdas',
\ 'm' : 'members',
\ 'i' : 'interfaces',
\ 'e' : 'enums'
\ },
\ 'scope2kind' : {
\ 'classes' : 'c',
\ 'abstract classes' : 'a',
\ 'types' : 't',
\ 'functions' : 'f',
\ 'variables' : 'v',
\ 'varlambdas' : 'l',
\ 'members' : 'm',
\ 'interfaces' : 'i',
\ 'enums' : 'e'
\ }
\ }

" In case you've updated/customized your ~/.ctags and prefer to use it.
if get(g:, 'typescript_use_builtin_tagbar_defs', 1)
let g:tagbar_type_typescript.deffile = expand('<sfile>:p:h:h') . '/ctags/typescript.ctags'
endif
114 changes: 114 additions & 0 deletions indent/tsx.vim
@@ -0,0 +1,114 @@
if exists('g:polyglot_disabled') && index(g:polyglot_disabled, 'typescript') != -1
finish
endif

" Save the current JavaScript indentexpr.
let b:tsx_ts_indentexpr = &indentexpr

" Prologue; load in XML indentation.
if exists('b:did_indent')
let s:did_indent=b:did_indent
unlet b:did_indent
endif
exe 'runtime! indent/xml.vim'
if exists('s:did_indent')
let b:did_indent=s:did_indent
endif

setlocal indentexpr=GetTsxIndent()

" JS indentkeys
setlocal indentkeys=0{,0},0),0],0\,,!^F,o,O,e
" XML indentkeys
setlocal indentkeys+=*<Return>,<>>,<<>,/

" Multiline end tag regex (line beginning with '>' or '/>')
let s:endtag = '^\s*\/\?>\s*;\='
let s:startexp = '[\{\(]\s*$'

" Get all syntax types at the beginning of a given line.
fu! SynSOL(lnum)
return map(synstack(a:lnum, 1), 'synIDattr(v:val, "name")')
endfu

" Get all syntax types at the end of a given line.
fu! SynEOL(lnum)
let lnum = prevnonblank(a:lnum)
let col = strlen(getline(lnum))
return map(synstack(lnum, col), 'synIDattr(v:val, "name")')
endfu

" Check if a syntax attribute is XMLish.
fu! SynAttrXMLish(synattr)
return a:synattr =~ "^xml" || a:synattr =~ "^tsx"
endfu

" Check if a synstack is XMLish (i.e., has an XMLish last attribute).
fu! SynXMLish(syns)
return SynAttrXMLish(get(a:syns, -1))
endfu

" Check if a synstack denotes the end of a TSX block.
fu! SynTSXBlockEnd(syns)
return get(a:syns, -1) =~ '\%(ts\|typescript\)Braces' &&
\ SynAttrXMLish(get(a:syns, -2))
endfu

" Determine how many tsxRegions deep a synstack is.
fu! SynTSXDepth(syns)
return len(filter(copy(a:syns), 'v:val ==# "tsxRegion"'))
endfu

" Check whether `cursyn' continues the same tsxRegion as `prevsyn'.
fu! SynTSXContinues(cursyn, prevsyn)
let curdepth = SynTSXDepth(a:cursyn)
let prevdepth = SynTSXDepth(a:prevsyn)

" In most places, we expect the nesting depths to be the same between any
" two consecutive positions within a tsxRegion (e.g., between a parent and
" child node, between two TSX attributes, etc.). The exception is between
" sibling nodes, where after a completed element (with depth N), we return
" to the parent's nesting (depth N - 1). This case is easily detected,
" since it is the only time when the top syntax element in the synstack is
" tsxRegion---specifically, the tsxRegion corresponding to the parent.
return prevdepth == curdepth ||
\ (prevdepth == curdepth + 1 && get(a:cursyn, -1) ==# 'tsxRegion')
endfu

" Cleverly mix JS and XML indentation.
fu! GetTsxIndent()
let cursyn = SynSOL(v:lnum)
let prevsyn = SynEOL(v:lnum - 1)

" Use XML indenting iff:
" - the syntax at the end of the previous line was either TSX or was the
" closing brace of a jsBlock whose parent syntax was TSX; and
" - the current line continues the same tsxRegion as the previous line.
if (SynXMLish(prevsyn) || SynTSXBlockEnd(prevsyn)) &&
\ SynTSXContinues(cursyn, prevsyn)
let ind = XmlIndentGet(v:lnum, 0)
let l:line = getline(v:lnum)
let l:pline = getline(v:lnum - 1)

" Align '/>' and '>' with '<' for multiline tags.
" Align end of expression ')' or '}'.
if l:line =~? s:endtag
let ind = ind - shiftwidth()
endif

" Then correct the indentation of any TSX following '/>' or '>'.
" Align start of expression '(' or '{'
if l:pline =~? s:endtag || l:pline =~? s:startexp
let ind = ind + shiftwidth()
endif
else
if len(b:tsx_ts_indentexpr)
" Invoke the base TS package's custom indenter
let ind = eval(b:tsx_ts_indentexpr)
else
let ind = cindent(v:lnum)
endif
endif

return ind
endfu

0 comments on commit 84ec4ee

Please sign in to comment.