summaryrefslogtreecommitdiffstats
path: root/autoload/jsx_pretty/comment.vim
blob: 1569cd8285f69627e30311e94592fc7fe0debf13 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
if !exists('g:polyglot_disabled') || !(index(g:polyglot_disabled, 'typescript') != -1 || index(g:polyglot_disabled, 'typescript') != -1 || index(g:polyglot_disabled, 'jsx') != -1)

function! jsx_pretty#comment#update_commentstring(original)
  let syn_current = s:syn_name(line('.'), col('.'))
  let syn_start = s:syn_name(line('.'), 1)
  let save_view = winsaveview()

  if syn_start =~? '^jsx'
    let line = getline(".")
    let start = len(matchstr(line, '^\s*'))
    let syn_name = s:syn_name(line('.'), start + 1)

    if line =~ '^\s*//'
      let &l:commentstring = '// %s'
    elseif s:syn_contains(line('.'), col('.'), 'jsxTaggedRegion')
      let &l:commentstring = '<!-- %s -->'
    elseif syn_name =~? '^jsxAttrib'
      let &l:commentstring = '// %s'
    else
      let &l:commentstring = '{/* %s */}'
    endif
  else
    let &l:commentstring = a:original
  endif

  " Restore the cursor position
  call winrestview(save_view)
endfunction

function! s:syn_name(lnum, cnum)
  let syn_id = get(synstack(a:lnum, a:cnum), -1)
  return synIDattr(syn_id, "name")
endfunction

function! s:syn_contains(lnum, cnum, syn_name)
  let stack = synstack(a:lnum, a:cnum)
  let syn_names = map(stack, 'synIDattr(v:val, "name")')
  return index(syn_names, a:syn_name) >= 0
endfunction

endif