@@ -93,6 +93,7 @@ endfunction
9393function ! s: TokeniseLine (line_num)
9494 let tokens = []
9595 let ln = getline(a:line_num)
96+ let possible_comment = 0
9697
9798 while 1
9899 " We perform searches within the buffer (and move the cusor)
@@ -108,10 +109,16 @@ function! s:TokeniseLine(line_num)
108109 if s: IsEscaped (ln , t_idx) | continue | endif
109110
110111 " Add token to the list.
111- call add (tokens, [ln [t_idx], token_pos])
112+ let token = ln [t_idx]
113+ call add (tokens, [token, token_pos])
114+
115+ " Early "possible comment" detection to reduce copying later.
116+ if token == # ' ;' | let possible_comment = 1 | endif
112117 endwhile
113118
114- return tokens
119+ " echom 'Tokens: ' string(tokens)
120+
121+ return [tokens, possible_comment]
115122endfunction
116123
117124let s: pairs = {' (' : ' )' , ' [' : ' ]' , ' {' : ' }' }
@@ -131,8 +138,15 @@ function! s:InsideForm(lnum)
131138
132139 let lnum = a: lnum - 1
133140 while lnum > 0
141+ let [line_tokens, possible_comment] = s: TokeniseLine (lnum)
142+
143+ " In case of comments, copy "tokens" so we can undo alterations.
144+ if possible_comment
145+ let prev_tokens = copy (tokens)
146+ endif
147+
134148 " Reduce tokens from line "lnum" into "tokens".
135- for tk in s: TokeniseLine (lnum)
149+ for tk in line_tokens
136150 if tk[0 ] == # ' "'
137151 if in_string
138152 let in_string = 0
@@ -150,9 +164,10 @@ function! s:InsideForm(lnum)
150164 endif
151165 elseif in_string
152166 " In string: ignore other tokens.
153- elseif tk[0 ] == # ' ;'
154- " Comment: break loop.
155- break
167+ elseif possible_comment && tk[0 ] == # ' ;'
168+ " Comment: undo previous token applications on
169+ " this line.
170+ let tokens = copy (prev_tokens)
156171 elseif ! empty (tokens) && get (s: pairs , tk[0 ], ' ' ) == # tokens[-1 ][0 ]
157172 " Matching pair: drop the last item in tokens.
158173 call remove (tokens, -1 )
0 commit comments