Skip to content

Commit 60defd0

Browse files
committed
Refine
1 parent 8231fef commit 60defd0

2 files changed

Lines changed: 40 additions & 43 deletions

File tree

indent/python.vim

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
" Vim indent file
22
" Language: Python
33
" Author: Akinori Hattori <hattya@gmail.com>
4-
" Last Change: 2017-04-22
4+
" Last Change: 2019-03-17
55
" License: MIT License
66

77
if exists('b:did_indent')
@@ -82,8 +82,8 @@ function! GetPEP8PythonIndent() abort
8282
if l =~# stmt
8383
let lnum = s:prevstmt(v:lnum - 1, s:syn_skip)
8484
let ind = indent(v:lnum) + 1
85-
while 0 < lnum
86-
while 1 < lnum && getline(lnum - 1) =~# s:lcont
85+
while lnum > 0
86+
while lnum > 1 && getline(lnum - 1) =~# s:lcont
8787
let lnum -= 1
8888
endwhile
8989
let pind = indent(lnum)
@@ -102,7 +102,7 @@ function! GetPEP8PythonIndent() abort
102102
" indent for line
103103
let lnum = s:prevstmt(v:lnum - 1, s:syn_cmt)
104104
let buf = []
105-
while 0 < lnum
105+
while lnum > 0
106106
call insert(buf, matchlist(getline(lnum), '\v^(.{-})\\=$')[1])
107107
if getline(lnum - 1) =~# s:lcont
108108
let lnum -= 1
@@ -158,7 +158,7 @@ endfunction
158158

159159
function! s:prevstmt(lnum, pat) abort
160160
let lnum = prevnonblank(a:lnum)
161-
while 0 < lnum && s:synmatch(lnum, indent(lnum) + 1, a:pat) != -1
161+
while lnum > 0 && s:synmatch(lnum, indent(lnum) + 1, a:pat) != -1
162162
let lnum = prevnonblank(lnum - 1)
163163
endwhile
164164
return lnum
@@ -168,11 +168,8 @@ function! s:synmatch(lnum, col, pat) abort
168168
return match(map(synstack(a:lnum, a:col), "synIDattr(v:val, 'name')"), a:pat)
169169
endfunction
170170

171-
function! s:is_compound_stmt(string, ...) abort
172-
if get(a:000, 0) && a:string =~# '\v^\s*<%(class|def)>'
173-
return 1
174-
endif
175-
return a:string =~# '\v^\s*<%(if|elif|while|for|except|with)>'
171+
function! s:is_compound_stmt(str, ...) abort
172+
return (get(a:000, 0) && a:str =~# '\v^\s*<%(class|def)>') || a:str =~# '\v^\s*<%(if|elif|while|for|except|with)>'
176173
endfunction
177174

178175
function! s:cont() abort
@@ -188,7 +185,7 @@ function! s:ml_stmt() abort
188185
endfunction
189186

190187
function! s:getvar(name, ...) abort
191-
return get(b:, a:name, get(g:, a:name, 0 < a:0 ? a:1 : 0))
188+
return get(b:, a:name, get(g:, a:name, a:0 > 0 ? a:1 : 0))
192189
endfunction
193190

194191
if exists('*shiftwidth')

test/indent.vimspec

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Describe filetype indent
2626
End
2727

2828
Describe string
29-
It keeps current indent
29+
It keeps the current indent
3030
let in = "'\<CR>'"
3131
let out = [
3232
\ "'",
@@ -35,7 +35,7 @@ Describe filetype indent
3535
Assert Equals(Insert(in), Buffer(out))
3636
End
3737

38-
It keeps current indent (triple-quoted string)
38+
It keeps the current indent (triple-quoted string)
3939
let in = "'''\<CR>'''"
4040
let out = [
4141
\ "'''",
@@ -44,7 +44,7 @@ Describe filetype indent
4444
Assert Equals(Insert(in), Buffer(out))
4545
End
4646

47-
It increments indentation level
47+
It increases the indent level
4848
let in = "d = {'k': 'v'}\<Esc>%a\<CR>\<Esc>$i,\<CR>"
4949
let out = [
5050
\ 'd = {',
@@ -56,7 +56,7 @@ Describe filetype indent
5656
End
5757

5858
Describe :
59-
It keeps current indent when it is not at EOL
59+
It keeps the current indent when it is not at the EOL
6060
let in = "with open('spam.txt') as fp:\<CR>"
6161
let in .= "data = fp.read()\<CR>"
6262
let in .= "0\<C-D>print(data)\<Esc>i[3:]"
@@ -68,7 +68,7 @@ Describe filetype indent
6868
Assert Equals(Insert(in), Buffer(out))
6969
End
7070

71-
It keeps current indent when compound statement is not found
71+
It keeps the current indent when compound statement is not found
7272
let in = "with open('spam.txt') as fp:\<CR>"
7373
let in .= "data = fp.read()\<CR>"
7474
let in .= "0\<C-D>\<Esc>iprint(data[3:])"
@@ -80,7 +80,7 @@ Describe filetype indent
8080
Assert Equals(Insert(in), Buffer(out))
8181
End
8282

83-
It increments indentation level
83+
It increases the indent level
8484
let in = "if s == '''\<CR>\<CR>''':\<CR>"
8585
let in .= "# comment\<CR>"
8686
let in .= "v ="
@@ -96,7 +96,7 @@ Describe filetype indent
9696
End
9797

9898
Describe line continuation
99-
It increments indentation level
99+
It increases the indent level
100100
let in = "i = 1 + \\\<CR>2 + \\\<CR>3"
101101
let out = [
102102
\ 'i = 1 + \',
@@ -106,7 +106,7 @@ Describe filetype indent
106106
Assert Equals(Insert(in), Buffer(out))
107107
End
108108

109-
It indents by option value
109+
It indents by the option value
110110
let b:python_indent_continue = '&sw / 2'
111111
let in = "i = 1 + \\\<CR>2 + \\\<CR>3"
112112
let out = [
@@ -119,7 +119,7 @@ Describe filetype indent
119119
End
120120

121121
Describe logical line
122-
It indents like previous line (line continuation)
122+
It indents like the previous line (line continuation)
123123
let in = "def func():\<CR>"
124124
let in .= "i = 1 + \\\<CR>2 + \\\<CR>3\<CR>"
125125
let in .= "v ="
@@ -133,7 +133,7 @@ Describe filetype indent
133133
Assert Equals(Insert(in), Buffer(out))
134134
End
135135

136-
It indents like previous line (bracket)
136+
It indents like the previous line (bracket)
137137
let in = "def func():\<CR>"
138138
let in .= "i = (1 +\<CR>2 +\<CR>3)\<CR>"
139139
let in .= "v ="
@@ -147,7 +147,7 @@ Describe filetype indent
147147
Assert Equals(Insert(in), Buffer(out))
148148
End
149149

150-
It indents like previous line (triple-quoted string)
150+
It indents like the previous line (triple-quoted string)
151151
let in = "def func():\<CR>"
152152
let in .= "s = '''\\\<CR>0\<C-D>spam\<CR>ham\<CR>eggs\<CR>'''\<CR>"
153153
let in .= "v ="
@@ -163,7 +163,7 @@ Describe filetype indent
163163
Assert Equals(Insert(in), Buffer(out))
164164
End
165165

166-
It indents like previous line (complex)
166+
It indents like the previous line (complex)
167167
let in = "def func():\<CR>"
168168
let in .= "s = 'a' + \\\<CR>'b' +"
169169
let in .= " ('c'\<CR>'d') + \\\<CR>"
@@ -192,7 +192,7 @@ Describe filetype indent
192192
End
193193

194194
Describe list
195-
It aligns with left bracket
195+
It aligns with left brackets
196196
let in = "l = [0,\<CR># [\<CR>1,\<CR># ]\<CR>2]"
197197
let out = [
198198
\ 'l = [0,',
@@ -204,7 +204,7 @@ Describe filetype indent
204204
Assert Equals(Insert(in), Buffer(out))
205205
End
206206

207-
It increments indentation level (hanging indent)
207+
It increases the indent level (hanging indent)
208208
let in = "l = [\<CR># [\<CR>0,\<CR>1,\<CR>2,\<CR># ]\<CR>]"
209209
let out = [
210210
\ 'l = [',
@@ -218,7 +218,7 @@ Describe filetype indent
218218
Assert Equals(Insert(in), Buffer(out))
219219
End
220220

221-
It indents by option value (hanging indent)
221+
It indents by the option value (hanging indent)
222222
let b:python_indent_continue = '&sw / 2'
223223
let in = "l = [\<CR>0,\<CR>1,\<CR>2,\<CR>]"
224224
let out = [
@@ -231,7 +231,7 @@ Describe filetype indent
231231
Assert Equals(Insert(in), Buffer(out))
232232
End
233233

234-
It increments indentaion level for right bracket (hanging indent)
234+
It increases the indent level for right brackets (hanging indent)
235235
let b:python_indent_right_bracket = 1
236236
let in = "l = [\<CR>0,\<CR>1,\<CR>2,\<CR>]"
237237
let out = [
@@ -246,7 +246,7 @@ Describe filetype indent
246246
End
247247

248248
Describe call
249-
It aligns with left bracket
249+
It aligns with left brackets
250250
let in = "print('pi = {}, e = {}').format(3.14,\<CR>2.718))"
251251
let out = [
252252
\ "print('pi = {}, e = {}').format(3.14,",
@@ -255,7 +255,7 @@ Describe filetype indent
255255
Assert Equals(Insert(in), Buffer(out))
256256
End
257257

258-
It aligns with left bracket (multi-byte characters)
258+
It aligns with left brackets (multi-byte characters)
259259
let in = "print('π = {}, e = {}').format(3.14,\<CR>2.718))"
260260
let out = [
261261
\ "print('π = {}, e = {}').format(3.14,",
@@ -266,7 +266,7 @@ Describe filetype indent
266266
End
267267

268268
Describe function
269-
It aligns with left bracket
269+
It aligns with left brackets
270270
let in = "def func(*args,\<CR>**kwargs):"
271271
let out = [
272272
\ 'def func(*args,',
@@ -275,7 +275,7 @@ Describe filetype indent
275275
Assert Equals(Insert(in), Buffer(out))
276276
End
277277

278-
It increments indentation level (hanging indent)
278+
It increases the indent level (hanging indent)
279279
let in = "def func(\<CR>*args,\<CR>**kwargs):"
280280
let out = [
281281
\ 'def func(',
@@ -287,7 +287,7 @@ Describe filetype indent
287287
End
288288

289289
Describe if statement
290-
It aligns with left bracket
290+
It aligns with left brackets
291291
let in = "if (True and\<CR>True):"
292292
let out = [
293293
\ 'if (True and',
@@ -296,7 +296,7 @@ Describe filetype indent
296296
Assert Equals(Insert(in), Buffer(out))
297297
End
298298

299-
It increments indentation level by 2
299+
It increases the indent level by 2
300300
let b:python_indent_multiline_statement = 1
301301
let in = "if (True and\<CR>True):"
302302
let out = [
@@ -306,7 +306,7 @@ Describe filetype indent
306306
Assert Equals(Insert(in), Buffer(out))
307307
End
308308

309-
It increments indentaion level (hanging indent)
309+
It increases the indent level (hanging indent)
310310
let in = "if (\<CR>True and\<CR>True\<CR>):"
311311
let out = [
312312
\ 'if (',
@@ -317,7 +317,7 @@ Describe filetype indent
317317
Assert Equals(Insert(in), Buffer(out))
318318
End
319319

320-
It increments indentation level by 2 (hanging indent)
320+
It increases the indent level by 2 (hanging indent)
321321
let b:python_indent_multiline_statement = 1
322322
let in = "if (\<CR>True and\<CR>True\<CR>):"
323323
let out = [
@@ -329,7 +329,7 @@ Describe filetype indent
329329
Assert Equals(Insert(in), Buffer(out))
330330
End
331331

332-
It increments indentation level (line continueation)
332+
It increases the indent level (line continueation)
333333
let in = "if True and \\\<CR>True:"
334334
let out = [
335335
\ 'if True and \',
@@ -338,7 +338,7 @@ Describe filetype indent
338338
Assert Equals(Insert(in), Buffer(out))
339339
End
340340

341-
It increments indentation level by 2 (line continuation)
341+
It increases the indent level by 2 (line continuation)
342342
let b:python_indent_multiline_statement = 1
343343
let in = "if True and \\\<CR>True:"
344344
let out = [
@@ -350,7 +350,7 @@ Describe filetype indent
350350
End
351351

352352
Describe compound statement
353-
It indents elif statement
353+
It indents the elif statement
354354
let in = "if 1:\<CR>i = 1\<CR>elif 2:\<CR>"
355355
let in .= "if 3:\<CR>i = 3\<CR>elif 4:\<CR>i = 4\<CR>else:\<CR>pass\<CR>"
356356
let in .= "elif 5:"
@@ -369,7 +369,7 @@ Describe filetype indent
369369
Assert Equals(Insert(in), Buffer(out))
370370
End
371371

372-
It indents else statement
372+
It indents the else statement
373373
let in = "if 1:\<CR>i = 1\<CR>else:\<CR>"
374374
let in .= "if 2:\<CR>i = 2\<CR>elif 3:\<CR>pass\<CR>else:\<CR>"
375375
let in .= "for _ in []:\<CR>continue\<CR>else:\<CR>"
@@ -399,7 +399,7 @@ Describe filetype indent
399399
Assert Equals(Insert(in), Buffer(out))
400400
End
401401

402-
It indents except statement
402+
It indents the except statement
403403
let in = "try:\<CR>i = 1\<CR>except OSError:\<CR>"
404404
let in .= "try:\<CR>i = 2\<CR>else:\<CR>pass\<CR>"
405405
let in .= "except:"
@@ -416,7 +416,7 @@ Describe filetype indent
416416
Assert Equals(Insert(in), Buffer(out))
417417
End
418418

419-
It indents finally statement
419+
It indents the finally statement
420420
let in = "try:\<CR>i = 1\<CR>finally:\<CR>"
421421
let in .= "try:\<CR>i = 2\<CR>except:\<CR>raise\<CR>finally:\<CR>"
422422
let in .= "try:\<CR>i = 3\<CR>else:\<CR>pass\<CR>finally:"
@@ -438,7 +438,7 @@ Describe filetype indent
438438
Assert Equals(Insert(in), Buffer(out))
439439
End
440440

441-
It ignores compound statement in string
441+
It ignores compound statements in strings
442442
let in = "if 1:\<CR>"
443443
let in .= "if 2:\<CR>s = '''\<CR>"
444444
let in .= "0\<C-D>if 3:\<CR>\<C-T>pass'''\<CR>elif 4:"
@@ -468,7 +468,7 @@ Describe filetype indent
468468
Assert Equals(Insert(in), Buffer(out))
469469
End
470470

471-
It indents compound statement (line continuation)
471+
It indents compound statements (line continuation)
472472
let in = "def func():\<CR>"
473473
let in .= "\\\<CR>0\<C-D>if 1:\<CR>pass\<CR>"
474474
let in .= "elif 2:"

0 commit comments

Comments
 (0)