Skip to content

Commit 2dcdd21

Browse files
anntzerblueyed
authored andcommitted
Add Cython support (#48)
Just making indents after cdef and cpdef similar to def. The variable must be made buffer-local in case one is editing both Python and Cython sources at the same time.
1 parent f4f95ee commit 2dcdd21

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.*.swp

indent/python.vim

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ let s:block_rules = {
4040
\ '^\s*finally\>': ['try', 'except', 'else']
4141
\ }
4242
let s:paren_pairs = ['()', '{}', '[]']
43-
let s:control_statement = '^\s*\(class\|def\|if\|while\|with\|for\|except\)\>'
43+
if &ft == 'pyrex' || &ft == 'cython'
44+
let b:control_statement = '\v^\s*(class|def|if|while|with|for|except|cdef|cpdef)>'
45+
else
46+
let b:control_statement = '\v^\s*(class|def|if|while|with|for|except)>'
47+
endif
4448
let s:stop_statement = '^\s*\(break\|continue\|raise\|return\|pass\)\>'
4549

4650
" Skip strings and comments. Return 1 for chars to skip.
@@ -198,7 +202,7 @@ function! s:indent_like_opening_paren(lnum)
198202
" If this line is the continuation of a control statement
199203
" indent further to distinguish the continuation line
200204
" from the next logical line.
201-
if text =~# s:control_statement && res == base + s:sw()
205+
if text =~# b:control_statement && res == base + s:sw()
202206
return base + s:sw() * 2
203207
else
204208
return res
@@ -259,7 +263,7 @@ function! s:indent_like_previous_line(lnum)
259263
" If this line is the continuation of a control statement
260264
" indent further to distinguish the continuation line
261265
" from the next logical line.
262-
if getline(start) =~# s:control_statement
266+
if getline(start) =~# b:control_statement
263267
return base + s:sw() * 2
264268
endif
265269

spec/indent/indent_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,3 +457,35 @@ def proposed_indent
457457

458458
it_behaves_like "vim"
459459
end
460+
461+
describe "vim for cython" do
462+
before {
463+
vim.command "enew"
464+
vim.command "set ft=cython"
465+
vim.command "runtime indent/python.vim"
466+
}
467+
468+
def shiftwidth
469+
@shiftwidth ||= vim.echo("exists('*shiftwidth') ? shiftwidth() : &sw").to_i
470+
end
471+
def tabstop
472+
@tabstop ||= vim.echo("&tabstop").to_i
473+
end
474+
def indent
475+
vim.echo("indent('.')").to_i
476+
end
477+
478+
describe "when using a cdef function definition" do
479+
it "indents shiftwidth spaces" do
480+
vim.feedkeys 'icdef long_function_name(\<CR>arg'
481+
indent.should == shiftwidth * 2
482+
end
483+
end
484+
485+
describe "when using a cpdef function definition" do
486+
it "indents shiftwidth spaces" do
487+
vim.feedkeys 'icpdef long_function_name(\<CR>arg'
488+
indent.should == shiftwidth * 2
489+
end
490+
end
491+
end

0 commit comments

Comments
 (0)