Skip to content

Commit ec6c74d

Browse files
committed
Formatting fixes
- Don't reindent inside of multline strings - fixes #117 - Don't reindent inside multiline comments - better switch block indenting
1 parent ce666b6 commit ec6c74d

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

src/formatting/alignment.py

+30-3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@
1515
'punctuation.section.group.end.cfml'
1616
]
1717

18+
non_indent_regions = [
19+
'embedding.cfml -source.cfml.script',
20+
'comment.block.cfml -punctuation.definition.comment.cfml',
21+
'meta.string -punctuation.definition.string.begin'
22+
]
23+
24+
switch_block_end = 'meta.switch.cfml meta.block.cfml punctuation.section.block.end.cfml'
25+
1826

1927
def indent_region(cfml_format):
2028
start_indent_selector = ','.join(start_indent_selectors)
2129
end_indent_selector = ','.join(end_indent_selectors)
2230
accessor_selector = 'punctuation.accessor.cfml'
2331

24-
non_cfscript_regions = cfml_format.view.find_by_selector('embedding.cfml -source.cfml.script')
32+
non_cfscript_regions = cfml_format.view.find_by_selector(','.join(non_indent_regions))
2533
non_cfscript_lines = []
2634
for r in non_cfscript_regions:
2735
non_cfscript_lines.extend(cfml_format.view.split_by_newlines(r))
@@ -32,13 +40,14 @@ def indent_region(cfml_format):
3240

3341
indent_level = 0
3442
leading_comma_flag = False
43+
switch_case_flag = None
3544
replacements = []
3645

3746
if lines[0].begin() < cfml_format.region_to_format.begin():
3847
first_line_str = cfml_format.view.substr(lines[0])
3948
first_line_stripped = first_line_str.rstrip()
4049
if first_line_stripped[-1] in ['{', '(', '[']:
41-
last_char_pt = lines[0].begin() + len(first_line_stripped.rstrip()) - 1
50+
last_char_pt = lines[0].begin() + len(first_line_stripped) - 1
4251
if cfml_format.view.match_selector(last_char_pt, start_indent_selector):
4352
indent_level += 1
4453
lines = lines[1:]
@@ -57,11 +66,25 @@ def indent_region(cfml_format):
5766
first_line_char = stripped_line_str[0]
5867

5968
if first_line_char in ['}', ')', ']', ',', '.']:
60-
first_char_pt = l.begin() + full_line_str.index(stripped_line_str[0])
69+
first_char_pt = l.begin() + full_line_str.index(first_line_char)
6170

6271
if first_line_char in ['}', ')', ']'] and cfml_format.view.match_selector(first_char_pt, end_indent_selector):
6372
indent_level -= 1
6473

74+
if first_line_char == '}' and switch_case_flag == 'case_block':
75+
scope_name = cfml_format.view.scope_name(first_char_pt)
76+
if scope_name.strip().endswith(switch_block_end):
77+
switch_case_flag = None
78+
indent_level -= 1
79+
80+
if stripped_line_str[-1] == ':':
81+
last_char_pt = l.begin() + len(full_line_str.rstrip()) - 1
82+
scope_name = cfml_format.view.scope_name(last_char_pt)
83+
if scope_name.strip().endswith('meta.switch.cfml meta.block.cfml punctuation.separator.cfml'):
84+
if switch_case_flag == 'case_block':
85+
indent_level -= 1
86+
switch_case_flag = 'case_start'
87+
6588
indent_columns = base_indent_column + (cfml_format.tab_size * indent_level)
6689

6790
# leading comma alignment
@@ -90,6 +113,10 @@ def indent_region(cfml_format):
90113
if cfml_format.view.match_selector(last_char_pt, start_indent_selector):
91114
indent_level += 1
92115

116+
if switch_case_flag == 'case_start':
117+
indent_level += 1
118+
switch_case_flag = 'case_block'
119+
93120
replacement_str = '\n'.join(replacements)
94121

95122
return [(sublime.Region(lines[0].begin(), lines[-1].end()), replacement_str)]

src/formatting/delimited_scopes.py

-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ def render_delimited_scope(cfml_format, ds, indent_column, ds_start_column, anon
286286

287287
el_indent_col = indent_column + cfml_format.tab_size
288288
rendered_elements = [render_ds_element(cfml_format, e, el_indent_col, el_indent_col, anon_functs) for e in ds.elements]
289-
290289
leading_comma = cfml_format.get_setting(ds.scope_type + '.multiline.leading_comma')
291290
after_comma_spacing = cfml_format.get_setting(ds.scope_type + '.after_comma_spacing')
292291

src/formatting/keywords.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ def format_keywords(cfml_format):
9999

100100
if not next_char_is_block:
101101
next_char = cfml_format.view.substr(next_char_pt)
102-
if to_next_char.size() == 0 and next_char != ';':
102+
if to_next_char.size() != 1 and next_char not in [';', ':']:
103103
replacement_str = ' '
104104
substitutions.append((to_next_char, replacement_str))
105-
elif to_next_char.size() > 0 and next_char == ';':
105+
elif to_next_char.size() > 0 and next_char in [';', ':']:
106106
replacement_str = ''
107107
substitutions.append((to_next_char, replacement_str))
108108
continue

0 commit comments

Comments
 (0)