Skip to content

Commit b6bab56

Browse files
committed
Python dictionaries turned into dict(...)
1 parent 0de9b27 commit b6bab56

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

doc/switch.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,11 @@ String style (g:switch_builtins.python_string_style)
800800
"Ham"
801801
f"Ham"
802802
<
803+
Dictionary style (g:switch_builtins.python_dict_style)
804+
>
805+
{'foo': 'bar', 'bar': 'baz'}
806+
dict(foo='bar', bar='baz')
807+
<
803808
Jinja ~
804809

805810
Tag type (g:switch_builtins.jinja_tag_type)

ftplugin/python/switch.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ let b:switch_definitions =
66
\ [
77
\ g:switch_builtins.python_dict_get,
88
\ g:switch_builtins.python_string_style,
9+
\ g:switch_builtins.python_dict_style,
910
\ ]

plugin/switch.vim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,18 @@ let g:switch_builtins =
236236
\ '\v(%(\%@<!\k)+)\[(.{-})\]': '\1.get(\2)',
237237
\ '\v(\k+)\.get\((.{-})\)': '\1[\2]',
238238
\ },
239+
\ 'python_dict_style': {
240+
\ '{\(\s*[''"]\k\+[''"]:\s*.\+,\?\)\+}': {
241+
\ '{': 'dict(',
242+
\ '[''"]\(\k\+\)[''"]:\s*': '\1=',
243+
\ '}': ')',
244+
\ },
245+
\ 'dict(\(\s*\k\+=\s*.\+,\?\)\+)': {
246+
\ 'dict(': '{',
247+
\ '\(\k\+\)=': '''\1'': ',
248+
\ ')': '}',
249+
\ },
250+
\ },
239251
\ 'python_string_style': {
240252
\ 'f"\(\%([^"\\]\|\\.\)*\)"': '''\1''',
241253
\ '''\(\%([^''\\]\|\\.\)*\)''': '"\1"',

spec/plugin/python_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,14 @@
2525
vim.switch
2626
assert_file_contents "foo['one']"
2727
end
28+
29+
specify "dict style" do
30+
set_file_contents "data = {'foo': 'bar', 'bar': 'baz'}"
31+
32+
vim.search('{').switch
33+
assert_file_contents "data = dict(foo='bar', bar='baz')"
34+
35+
vim.switch
36+
assert_file_contents "data = {'foo': 'bar', 'bar': 'baz'}"
37+
end
2838
end

0 commit comments

Comments
 (0)