Skip to content

Commit f4e20de

Browse files
committed
Some jinja and python support
1 parent f50ce4b commit f4e20de

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

ftplugin/htmldjango/switch.vim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
if !exists("g:loaded_switch")
2+
finish
3+
endif
4+
5+
let b:switch_definitions =
6+
\ [
7+
\ g:switch_builtins.jinja_tag_type,
8+
\ ]

ftplugin/python/switch.vim

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
if !exists("g:loaded_switch")
2+
finish
3+
endif
4+
5+
let b:switch_definitions =
6+
\ [
7+
\ g:switch_builtins.python_dict_get,
8+
\ g:switch_builtins.python_string_style,
9+
\ ]

plugin/switch.vim

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,21 @@ let g:switch_builtins =
226226
\ 'markdown_task_item': {
227227
\ '^\(\s*\)- \[ \] \(.*\)': '\1- [x] \2',
228228
\ '^\(\s*\)- \[x\] \(.*\)': '\1- [ ] \2',
229-
\ }
229+
\ },
230+
\ 'jinja_tag_type': {
231+
\ '{{ \(.*\) }}': '{% \1 %}',
232+
\ '{% \(.*\) %}': '{# \1 #}',
233+
\ '{# \(.*\) #}': '{{ \1 }}',
234+
\ },
235+
\ 'python_dict_get': {
236+
\ '\v(%(\%@<!\k)+)\[(.{-})\]': '\1.get(\2)',
237+
\ '\v(\k+)\.get\((.{-})\)': '\1[\2]',
238+
\ },
239+
\ 'python_string_style': {
240+
\ 'f"\(\%([^"\\]\|\\.\)*\)"': '''\1''',
241+
\ '''\(\%([^''\\]\|\\.\)*\)''': '"\1"',
242+
\ 'f\@!"\(\%([^"\\]\|\\.\)*\)"': 'f"\1"',
243+
\ },
230244
\ }
231245

232246
let g:switch_definitions =

spec/plugin/python_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'spec_helper'
2+
3+
describe "python definitions" do
4+
let(:filename) { 'test.py' }
5+
6+
specify "string type" do
7+
set_file_contents 'foo = "bar?"'
8+
9+
vim.search('bar').switch
10+
assert_file_contents 'foo = f"bar?"'
11+
12+
vim.switch
13+
assert_file_contents "foo = 'bar?'"
14+
15+
vim.switch
16+
assert_file_contents 'foo = "bar?"'
17+
end
18+
19+
specify "fetch array access" do
20+
set_file_contents "foo['one']"
21+
22+
vim.search('foo').switch
23+
assert_file_contents "foo.get('one')"
24+
25+
vim.switch
26+
assert_file_contents "foo['one']"
27+
end
28+
end

0 commit comments

Comments
 (0)