Skip to content

Commit 8d904f6

Browse files
authored
improve end of line trim (#662)
* improve end of line trim * Date
1 parent ba2245f commit 8d904f6

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

indent/javascript.vim

+20-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
" Language: Javascript
33
" Maintainer: Chris Paul ( https://github.com/bounceme )
44
" URL: https://github.com/pangloss/vim-javascript
5-
" Last Change: September 26, 2016
5+
" Last Change: October 8, 2016
66

77
" Only load this indent file when no other was loaded.
88
if exists('b:did_indent')
@@ -38,6 +38,8 @@ else
3838
endif
3939

4040
let s:line_pre = '^\s*\%(\%(\%(\/\*.\{-}\)\=\*\+\/\s*\)\=\)\@>'
41+
let s:line_term = '\s*\%(\%(\/\%(\%(\*.\{-}\*\/\)\|\%(\*\+\)\)\)\s*\)\=$'
42+
4143
let s:expr_case = '\<\%(\%(case\>\s*\S.\{-}\)\|default\)\s*:\C'
4244
" Regex of syntax group names that are or delimit string or are comments.
4345
let s:syng_strcom = '\%(s\%(tring\|pecial\)\|comment\|regex\|doc\|template\)'
@@ -64,10 +66,23 @@ else
6466
endfunction
6567
endif
6668

67-
let s:line_term = '\s*\%(\%(\/\%(\%(\*.\{-}\*\/\)\|\%(\*\+\)\)\)\s*\)\=$'
68-
69+
" indent/python.vim
6970
function s:Trimline(ln)
70-
return substitute(substitute(getline(a:ln),':\@<!\/\/.*','',''), s:line_term,'','')
71+
let pline = getline(a:ln)
72+
let min = match(pline,'\/[/*]') + 1
73+
if min && synIDattr(synID(a:ln, strlen(pline), 0), 'name') =~? '\%(comment\|doc\)'
74+
let max = match(pline,'.*\zs\/[/*]') + 1
75+
while min < max
76+
let col = (min + max) / 2
77+
if synIDattr(synID(a:ln, col, 0), 'name') =~? '\%(comment\|doc\)'
78+
let max = col
79+
else
80+
let min = match(pline,'\/[/*]',col) + 1
81+
endif
82+
endwhile
83+
let pline = strpart(pline, 0, min - 1)
84+
endif
85+
return substitute(pline,'\s*$','','')
7186
endfunction
7287

7388
" configurable regexes that define continuation lines, not including (, {, or [.
@@ -179,7 +194,7 @@ function GetJavascriptIndent()
179194
return 0
180195
endif
181196

182-
let l:line = substitute(l:line,s:line_pre,'','')
197+
let l:line = substitute(l:line,'^\s*\%(\/\*.\{-}\*\/\s*\)*','','')
183198

184199
if l:line =~# '^' . s:expr_case
185200
let cpo_switch = &cpo

0 commit comments

Comments
 (0)