aboutsummaryrefslogtreecommitdiff
path: root/plugin/commenter.vim
blob: f903fce5b48e9d0c8bced9d22e464a19b96252bd (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
" Function for commenting out blocks of text.

noremap cd <cmd>set operatorfunc=<sid>comment<cr>g@
nnoremap cdd <cmd>set operatorfunc=<sid>comment<cr>g@_
nnoremap cD <cmd>set operatorfunc=<sid>comment<cr>g@$

" Comment out the rest of the line and put the cursor into insert mode
nnoremap cdC  <cmd>call <sid>change_comment_obj('i')<cr>
nnoremap cdcc  <cmd>call <sid>change_comment_obj('I')<cr>

onoremap i/ <cmd>call <sid>comment_obj(v:operator, 'i')<cr>
vnoremap i/ <cmd>call <sid>comment_obj('', 'i')<cr>
onoremap a/ <cmd>call <sid>comment_obj(v:operator, 'a')<cr>
vnoremap a/ <cmd>call <sid>comment_obj('', 'a')<cr>

" Objects for paragraphs excluding any comment blocks immediately preceeding or
" Succeeding
onoremap acp <cmd>call <sid>uncommented_paragraph('a')<cr>
onoremap icp <cmd>call <sid>uncommented_paragraph('i')<cr>

noremap czd <cmd>set operatorfunc=<sid>uncomment<cr>g@
nnoremap czdd <cmd>set operatorfunc=<sid>uncomment<cr>g@_

function! s:single_character_style(c)
  return {
      \         'block_start_regex': '\(^\s*\(\(\S\)\(' . a:c . '\)\@<!.*\|\n\)\_s\+\|\%^\)${as}' . a:c . '\s*${is}\S${ie}',
      \         'block_end_regex': a:c . '.*${is}\(\n\s*\)*${as}\n\_s*\S' . a:c . '\@<!',
      \         'line': a:c
      \     }
endfunction
let s:default_style = s:single_character_style('#')

" Save the motion postions, for use with g@.
function s:save_object(t) abort
  let s:object = {
        \ "type": a:t,
        \ "start": getpos("'["),
        \ "end": getpos("']")
        \ }
endfunction

function! s:maxpos(cur, oth)
  let [a, l1, c1, b] = a:cur
  let [_, l2, c2, _] = a:oth

  if l1 > l2
    return a:cur
  elseif l2 > l1
    return a:oth
  elseif c2 > c1
    return a:oth
  else
    return a:cur
  endif

endfunction

function! s:minpos(cur, oth)
  let [a, l1, c1, b] = a:cur
  let [_, l2, c2, _] = a:oth

  if l1 < l2
    return a:cur
  elseif l2 < l1
    return a:oth
  elseif c2 < c1
    return a:oth
  else
    return a:cur
  endif

endfunction

function! s:uncommented_paragraph(t) abort
  let savepos = getpos('.')
  set operatorfunc=s:save_object
  exec "normal! g@". a:t . "pv\<esc>"
  call setpos('.', savepos)

  let [start_regex, end_regex] = g:GetCommentRegex('a')

  call search(end_regex . '\|\%^', 'cb')
  exec "normal! j"
  call setpos('.', s:maxpos(getpos('.'), s:object.start))
  normal! m<
  call setpos('.', savepos)

  call search(start_regex . '\|\%$', 'c')
  call setpos('.', s:minpos(getpos('.'), s:object.end))
  exec "normal! k"

  normal! m>gvV
endfunction

function! s:regex_combine(s1, s2) abort
  if a:s1 == ""
    return a:s2
  endif

  if a:s2 == ""
    return a:s1
  endif

  return printf('\(%s\)\|\(%s\)', a:s1, a:s2)
endfunction

function! g:GetCommentRegex(ia) abort
  let style = get(s:comment_style, &ft, s:default_style)

  let start_regex = s:regex_combine(
        \ get(style, 'multi_start_regex', ''),
        \ get(style, 'block_start_regex', ''))
  let end_regex = s:regex_combine(
        \ get(style, 'multi_end_regex', ''),
        \ get(style, 'block_end_regex', ''))
        \
  if a:ia == 'i'
    let start_regex =
          \ substitute(
          \   substitute(start_regex, '\V${i\(\[se]\)}', '\\z\1', 'g'),
          \   '\V${a\(\[se]\)}', '', 'g')
    let end_regex =
          \ substitute(
          \   substitute(end_regex, '\V${i\(\[se]\)}', '\\z\1', 'g'),
          \   '\V${a\(\[se]\)}', '', 'g')
  else
    let start_regex =
          \ substitute(
          \   substitute(start_regex, '\V${a\(\[se]\)}', '\\z\1', 'g'),
          \   '\V${i\(\[se]\)}', '', 'g')
    let end_regex =
          \ substitute(
          \   substitute(end_regex, '\V${a\(\[se]\)}', '\\z\1', 'g'),
          \   '\V${i\(\[se]\)}', '', 'g')
  endif
  return [start_regex, end_regex]
endfunction

function! s:comment_obj(op, ia)
  let [start_regex, end_regex] = g:GetCommentRegex(a:ia)

  call search(end_regex, 'c')
  exec "normal! v\<esc>m>"
  call search(start_regex, 'bW')
  exec "normal! m<gv"

  let [_, sl, sc, _] = getpos("'<")
  let [_, el, ec, _] = getpos("'>")

  if getline(sl)[:sc - 2] =~ '^\s*$' && a:ia == 'a' && ec >= len(getline(el))
    exec "normal V"
  endif
endfunction

function! s:change_comment_obj(mv) abort
  let s = get(s:comment_style, &ft, s:default_style)
  " <C-g>U<Left> allows <Left>,<Right>,etc. to show up in the redo buff.
  call feedkeys(printf(
        \ "%s %s %s",
        \ a:mv,
        \ s.line,
        \ repeat("\<C-g>U\<left>", len(s.line) + 2)), 'n')
endfunction

let s:comment_style = {
      \ 'java,c,cpp,rust,go': {
      \         'multi_start_regex': '${as}\/\*\([*]\|\_s\)*${is}\S',
      \         'multi_end_regex': '${is}\S\_s*\*\/${as}',
      \         'block_start_regex': '\(^\s*\(\(\S\S\)\(\/\/\)\@<!.*\|\S\n\)\n*${as}\_s\+\|\%^${as}\)\/\/\+\s*${is}\S${ie}',
      \         'block_end_regex': '\/\/.*${is}.${ie}${as}\n\_s*\S\/\@<!',
      \         'line': '//',
      \         'multi_start': '/*',
      \         'multi_cont': ' *',
      \         'multi_end': ' */'
      \     },
      \ 'vim': s:single_character_style('"')
      \ }

let s:comment_style_new = {}
for [langs, i] in items(s:comment_style)
  for l in split(langs, ',')
    let s:comment_style_new[l] = i
  endfor
endfor
let s:comment_style = s:comment_style_new

function! s:uncomment(arg, ...) abort

  let s = get(s:comment_style, &ft, s:default_style)
  let l1 = line("'[")
  let l2 = line("']")

  if has_key(s, 'multi_start')
    silent exec printf(
          \ 'silent! %s,%s s/\V\zs%s\s\?\ze//', l1, l2, escape(s.multi_start, '/'))
    silent exec printf('silent! %s,%s s/\V\zs\s\?%s\ze//', l1, l2, escape(s.multi_end, '/'))
    silent exec printf('silent! %s,%s s/\V\^\s\*\zs%s\s\?\ze//', l1, l2, escape(s.multi_cont, '/'))
  endif

  silent exec printf('silent! %s,%s s/\V\^\s\*\zs%s\s\?\ze//', l1, l2, escape(s.line, '/'))
endfunction

function! s:comment(arg, ...) abort
  let cb = { 'arg': a:arg }
  normal! m`

  function! cb.operate(ls, t) dict abort
    let s = get(s:comment_style, &ft, s:default_style)
    let smallest = 100
    let ls = a:ls

    if self.arg == 'line' || len(ls) > 1
      for l in ls
        if ! (l =~ '^\s*$')
          let sz = match(l, '^\(\s\+\)\zs\S\ze') + 1
          if sz <= 0
            let sz = 1
          endif

          if sz < smallest && sz > 0
            let smallest = sz
          endif
        endif
      endfor

      if self.arg == 'line'
        let i = 0
        while i < len(ls)
          let ls[i] = substitute(ls[i], printf('\%%%dc', smallest), s.line . ' ', '')
          let i += 1
        endwhile
      else
        if self.arg != 'line' && !has_key(s, 'multi_start')
          throw "Inline comments not allowed"
        endif

        let ls[0] = substitute(ls[0], '^', s.multi_start . ' ', '')
        let ls[-1] = substitute(ls[-1], '$', ' ' . s.multi_end, '')
      endif
    else
      let l = getline('.')
      if l[len(l) - len(ls[0]):] ==# ls[0]
        let ls[0] = printf("%s %s", s.line, ls[0])
      else
        if !has_key(s, 'multi_start')
          throw "Inline comments not allowed"
        endif
        let ls[0] = printf("%s %s %s", s.multi_start, ls[0], s.multi_end)
      endif
    endif

    return ls
  endfunction

  call fieldmarshal#modifytext(a:arg, cb)
  norm ``
endfunction