Skip to content

Commit c50b82c

Browse files
committed
Refactor
1 parent 8901dcc commit c50b82c

File tree

1 file changed

+32
-51
lines changed

1 file changed

+32
-51
lines changed

ftplugin/markdown.vim

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ if !exists("g:vmt_auto_update_on_save")
77
let g:vmt_auto_update_on_save = 1
88
endif
99

10-
if !exists("g:vmt_dont_insert_marker")
11-
let g:vmt_dont_insert_marker = 0
10+
if !exists("g:vmt_dont_insert_fence")
11+
let g:vmt_dont_insert_fence = 0
1212
endif
1313

1414
let g:GFMHeadingIds = {}
@@ -162,8 +162,8 @@ function! s:GenToc(markdownStyle)
162162

163163
let l:minLevel = min(l:levels)
164164

165-
if g:vmt_dont_insert_marker == 0
166-
put =<SID>GetBeginMarker(a:markdownStyle)
165+
if g:vmt_dont_insert_fence == 0
166+
put =<SID>GetBeginFence(a:markdownStyle)
167167
endif
168168

169169
let l:i = 0
@@ -185,36 +185,39 @@ function! s:GenToc(markdownStyle)
185185
" a blank line after toc to avoid effect typo of content below
186186
put =''
187187

188-
if g:vmt_dont_insert_marker == 0
189-
put =<SID>GetEndMarker()
188+
if g:vmt_dont_insert_fence == 0
189+
put =<SID>GetEndFence()
190190
endif
191191
endfunction
192192

193-
function! s:GetBeginMarker(markdownStyle)
193+
function! s:GetBeginFence(markdownStyle)
194194
return "<!-- vim-markdown-toc " . a:markdownStyle . " -->"
195195
endfunction
196196

197-
function! s:GetEndMarker()
197+
function! s:GetEndFence()
198198
return "<!-- vim-markdown-toc -->"
199199
endfunction
200200

201-
function! s:GetBeginMarkerPattern()
201+
function! s:GetBeginFencePattern()
202202
return "<!-- vim-markdown-toc \\([[:alpha:]]\\+\\) -->"
203203
endfunction
204204

205-
function! s:GetEndMarkerPattern()
206-
return <SID>GetEndMarker()
205+
function! s:GetEndFencePattern()
206+
return <SID>GetEndFence()
207207
endfunction
208208

209209
function! s:UpdateToc()
210210
let l:winview = winsaveview()
211211

212212
let l:totalLineNum = line("$")
213213

214-
let l:markdownStyle = <SID>GetExistsTocStyle()
214+
let [l:markdownStyle, l:beginLineNumber, l:endLineNumber] = <SID>DeleteExistingToc()
215215

216-
if l:markdownStyle != ""
217-
let [l:beginLineNumber,l:endLineNumber] = <SID>DeleteExistsToc()
216+
if l:markdownStyle ==# ""
217+
echoe "Cannot find existing toc"
218+
elseif l:markdownStyle ==# "Unknown"
219+
echoe "Find unsupported style toc"
220+
else
218221
let l:isFirstLine = (l:beginLineNumber == 1)
219222
if l:beginLineNumber > 1
220223
let l:beginLineNumber -= 1
@@ -239,68 +242,46 @@ function! s:UpdateToc()
239242
let l:winview['lnum'] += l:diff
240243
let l:winview['topline'] += l:diff
241244
endif
242-
else
243-
echoe "Cannot find existing toc"
244245
endif
245246

246247
call winrestview(l:winview)
247248
endfunction
248249

249-
function! s:DeleteExistsToc()
250+
function! s:DeleteExistingToc()
250251
let l:winview = winsaveview()
251252

252253
normal! gg
253254

254-
let l:tocBeginPattern = <SID>GetBeginMarkerPattern()
255-
let l:tocEndPattern = <SID>GetEndMarkerPattern()
255+
let l:tocBeginPattern = <SID>GetBeginFencePattern()
256+
let l:tocEndPattern = <SID>GetEndFencePattern()
256257

258+
let l:markdownStyle = ""
257259
let l:beginLineNumber = -1
258260
let l:endLineNumber= -1
259261

260262
let l:flags = "Wc"
261263
if search(l:tocBeginPattern, l:flags) != 0
264+
let l:beginLine = getline(".")
262265
let l:beginLineNumber = line(".")
263266

264267
if search(l:tocEndPattern, l:flags) != 0
265-
let l:endLineNumber = line(".")
266-
execute l:beginLineNumber. "," . l:endLineNumber. "delete_"
267-
else
268-
echoe "Cannot find toc end marker"
269-
let l:beginLineNumber = -1
270-
endif
271-
endif
272-
273-
call winrestview(l:winview)
274-
275-
return [l:beginLineNumber,l:endLineNumber]
276-
endfunction
277-
278-
function! s:GetExistsTocStyle()
279-
let l:winview = winsaveview()
280-
281-
normal! gg
282-
283-
let l:markdownStyle = ""
284-
285-
let l:tocBeginPattern = <SID>GetBeginMarkerPattern()
286-
let l:tocEndPattern = <SID>GetEndMarkerPattern()
287-
288-
let l:flags = "Wc"
289-
if search(l:tocBeginPattern, l:flags) != 0
290-
let l:line = getline(".")
291-
292-
if search(l:tocEndPattern, l:flags) != 0
293-
let l:markdownStyle = matchlist(l:line, l:tocBeginPattern)[1]
268+
let l:markdownStyle = matchlist(l:beginLine, l:tocBeginPattern)[1]
294269
if l:markdownStyle != "GFM" && l:markdownStyle != "Redcarpet"
295-
echoe "Find unsupported markdown style"
296-
let l:markdownStyle = ""
270+
let l:markdownStyle = "Unknown"
271+
else
272+
let l:endLineNumber = line(".")
273+
execute l:beginLineNumber. "," . l:endLineNumber. "delete_"
297274
end
275+
else
276+
echoe "Cannot find toc end fence"
298277
endif
278+
else
279+
echoe "Cannot find toc begin fence"
299280
endif
300281

301282
call winrestview(l:winview)
302283

303-
return l:markdownStyle
284+
return [l:markdownStyle, l:beginLineNumber, l:endLineNumber]
304285
endfunction
305286

306287
command! GenTocGFM :call <SID>GenToc("GFM")

0 commit comments

Comments
 (0)