Skip to content

Commit

Permalink
Add rules for Python-specific string literals
Browse files Browse the repository at this point in the history
  • Loading branch information
kana committed Apr 6, 2012
1 parent c0bee1f commit ddf0b92
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
12 changes: 12 additions & 0 deletions autoload/smartinput.vim
Expand Up @@ -148,6 +148,15 @@ function! smartinput#define_default_rules() "{{{2
\ {'at': '\%#', 'char': '''', 'input': '''''<Left>',
\ 'syntax': ['Constant']},
\ ])
" Unfortunately, the space beyond the end of a comment line is not
" highlighted as 'Comment'. So that it is necessary to define one more rule
" to cover the edge case with only 'at'.
call urules.add('Python string', [
\ {'at': '\v\c<([bu]|[bu]?r)>%#', 'char': '''', 'input': '''''<Left>'},
\ {'at': '\v\c<([bu]|[bu]?r)>%#', 'char': '''', 'input': '''',
\ 'syntax': ['Comment', 'Constant']},
\ {'at': '\v\c\#.*<([bu]|[bu]?r)>%#$', 'char': '''', 'input': ''''},
\ ])
call urules.add('Vim script comment', [
\ {'at': '^\s*\%#', 'char': '"', 'input': '"'},
\ ])
Expand Down Expand Up @@ -179,6 +188,9 @@ function! smartinput#define_default_rules() "{{{2
\ 'perl': [
\ urules.table[''''' as strong quote'],
\ ],
\ 'python': [
\ urules.table['Python string'],
\ ],
\ 'ruby': [
\ urules.table[''''' as strong quote'],
\ ],
Expand Down
40 changes: 40 additions & 0 deletions t/api.vim
Expand Up @@ -1122,4 +1122,44 @@ describe 'The default configuration'
endfor
endfor
end

it 'should have rules for Python-specific string literals'
setfiletype python

let test_set_table = {}
for prefix in ['r', 'R',
\ 'b', 'B', 'br', 'bR', 'Br', 'BR',
\ 'u', 'U', 'ur', 'uR', 'Ur', 'UR']
let test_set_table[printf('%s''...'' in code', prefix)] = [
\ [prefix . "", [prefix . ''], 1, len(prefix) + 1 - 1],
\ ["'", [prefix . ''''''], 1, len(prefix) + 2 - 1],
\ ["x", [prefix . '''x'''], 1, len(prefix) + 3 - 1],
\ ]
let test_set_table[printf('%s''...'' in string', prefix)] = [
\ ["\"", ['""'], 1, 1 + 1 - 1],
\ [prefix . "", ['"' . prefix . '"'], 1, 1 + len(prefix) + 1 - 1],
\ ["'", ['"' . prefix . '''"'], 1, 1 + len(prefix) + 1 + 1 - 1],
\ ["x", ['"' . prefix . '''x"'], 1, 1 + len(prefix) + 2 + 1 - 1],
\ ]
let test_set_table[printf('%s''...'' in comment (middle)', prefix)] = [
\ ["# $\<Left>", ['# $'], 1, 2 + 1 - 1],
\ [prefix . "", ['# ' . prefix . '$'], 1, 2 + len(prefix) + 1 - 1],
\ ["'", ['# ' . prefix . '''$'], 1, 2 + len(prefix) + 1 + 1 - 1],
\ ["x", ['# ' . prefix . '''x$'], 1, 2 + len(prefix) + 2 + 1 - 1],
\ ]
let test_set_table[printf('%s''...'' in comment (EOL)', prefix)] = [
\ ["foo # ", ['foo # '], 1, 6 + 1 - 1],
\ [prefix . "", ['foo # ' . prefix . ''], 1, 6 + len(prefix) + 1 - 1],
\ ["'", ['foo # ' . prefix . ''''], 1, 6 + len(prefix) + 1 + 1 - 1],
\ ["x", ['foo # ' . prefix . '''x'], 1, 6 + len(prefix) + 2 + 1 - 1],
\ ]
endfor
let test_set_table['Typical comment'] = [
\ ["# You", ['# You'], 1, 6 - 1],
\ ["'", ['# You'''], 1, 7 - 1],
\ ["re", ['# You''re'], 1, 9 - 1],
\ ]

call b:.test_rules(test_set_table)
end
end

0 comments on commit ddf0b92

Please sign in to comment.