Skip to content

Commit fde168b

Browse files
committed
Various Fixes
- Ensure attribute region exists before getting string - reset indentation level during indenting if line is not cfscript - simplify format region selection - ensure method chains are in cfscript
1 parent ec6c74d commit fde168b

File tree

4 files changed

+37
-32
lines changed

4 files changed

+37
-32
lines changed

src/component_parser/cfml_functions.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ def find_script_functions(file_string, cfscript_range):
6161

6262
attributes = ''
6363
func_body_range = params_range.parent.next_child_range(params_range.end, ['curly_brackets', 'semicolon'])
64-
attributes = file_string[params_range.end:func_body_range.start]
65-
l.append(attributes)
64+
if func_body_range:
65+
attributes = file_string[params_range.end:func_body_range.start]
66+
l.append(attributes)
67+
else:
68+
l.append('')
6669

6770
functions.append(parse_script_function(regex.ScriptFunction._make(l)))
6871
return functions

src/formatting/alignment.py

+22-8
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
'punctuation.section.group.end.cfml'
1616
]
1717

18-
non_indent_regions = [
19-
'embedding.cfml -source.cfml.script',
18+
non_script_selector = 'embedding.cfml -source.cfml.script'
19+
20+
non_indent_selector = [
2021
'comment.block.cfml -punctuation.definition.comment.cfml',
2122
'meta.string -punctuation.definition.string.begin'
2223
]
@@ -29,11 +30,18 @@ def indent_region(cfml_format):
2930
end_indent_selector = ','.join(end_indent_selectors)
3031
accessor_selector = 'punctuation.accessor.cfml'
3132

32-
non_cfscript_regions = cfml_format.view.find_by_selector(','.join(non_indent_regions))
33-
non_cfscript_lines = []
33+
non_cfscript_regions = cfml_format.view.find_by_selector(non_script_selector)
34+
non_indent_regions = cfml_format.view.find_by_selector(','.join(non_indent_selector))
35+
non_cfscript_line_starts = set()
36+
non_indent_line_starts = set()
37+
3438
for r in non_cfscript_regions:
35-
non_cfscript_lines.extend(cfml_format.view.split_by_newlines(r))
36-
non_cfscript_line_starts = [r.begin() for r in non_cfscript_lines]
39+
for l in cfml_format.view.split_by_newlines(r):
40+
non_cfscript_line_starts.add(l.begin())
41+
42+
for r in non_indent_regions:
43+
for l in cfml_format.view.split_by_newlines(r):
44+
non_indent_line_starts.add(l.begin())
3745

3846
lines = cfml_format.view.lines(cfml_format.region_to_format)
3947
base_indent_column = cfml_format.line_indent_column(lines[0].begin())
@@ -52,13 +60,19 @@ def indent_region(cfml_format):
5260
indent_level += 1
5361
lines = lines[1:]
5462

55-
if lines[-1].end() > cfml_format.region_to_format.end():
63+
if len(lines) > 0 and lines[-1].end() > cfml_format.region_to_format.end():
5664
lines = lines[:-1]
5765

66+
if len(lines) == 0:
67+
return []
68+
5869
for l in lines:
5970
full_line_str = cfml_format.view.substr(l)
6071
stripped_line_str = full_line_str.strip()
6172

73+
if l.begin() in non_cfscript_line_starts:
74+
base_indent_column = cfml_format.line_indent_column(l.begin())
75+
6276
if len(stripped_line_str) == 0:
6377
replacements.append(stripped_line_str)
6478
continue
@@ -103,7 +117,7 @@ def indent_region(cfml_format):
103117

104118
indented_line = cfml_format.indent_to_column(indent_columns) + stripped_line_str
105119

106-
if l.begin() in non_cfscript_line_starts:
120+
if l.begin() in non_cfscript_line_starts or l.begin() in non_indent_line_starts:
107121
replacements.append(full_line_str)
108122
else:
109123
replacements.append(indented_line)

src/formatting/cfml_format.py

+9-21
Original file line numberDiff line numberDiff line change
@@ -92,27 +92,15 @@ def get_region_to_format(self, current_method):
9292
return self.view.sel()[0], True
9393
if current_method:
9494
pt = self.view.sel()[0].begin()
95-
cfc_selector = 'source.cfml.script meta.class'
96-
decl_selector = 'source.cfml.script meta.class.body.cfml meta.function.declaration.cfml'
97-
body_selector = 'source.cfml.script meta.class.body.cfml meta.function.body.cfml'
98-
99-
in_cfc = self.view.match_selector(pt, cfc_selector)
100-
in_funct_decl = self.view.match_selector(pt, decl_selector)
101-
in_funct_body = self.view.match_selector(pt, body_selector)
102-
103-
if in_funct_decl or in_funct_body:
104-
if in_funct_body:
105-
funct_body_region = utils.get_scope_region_containing_point(self.view, pt, body_selector)
106-
funct_decl_pt = funct_body_region.begin() - 1
107-
funct_decl_region = utils.get_scope_region_containing_point(self.view, funct_decl_pt, decl_selector)
108-
else:
109-
funct_decl_region = utils.get_scope_region_containing_point(self.view, pt, decl_selector)
110-
funct_body_pt = funct_decl_region.end()
111-
funct_body_region = utils.get_scope_region_containing_point(self.view, funct_body_pt, body_selector)
112-
113-
function_region = sublime.Region(funct_decl_region.begin(), funct_body_region.end())
114-
return function_region, False
115-
if in_cfc:
95+
cfc_selector = 'source.cfml meta.class'
96+
funct_selector = 'source.cfml meta.class meta.function'
97+
98+
if self.view.match_selector(pt, funct_selector):
99+
for function_region in self.view.find_by_selector(funct_selector):
100+
if function_region.contains(pt):
101+
return function_region, False
102+
103+
if self.view.match_selector(pt, cfc_selector):
116104
return None, False
117105

118106
return sublime.Region(0, self.view.size()), False

src/formatting/method_chains.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def format_method_chains(cfml_format):
4040

4141
def find_method_chains(cfml_format):
4242
method_chains = []
43-
regions = cfml_format.find_by_selector('meta.function-call.method -meta.function-call.method.static')
43+
regions = cfml_format.find_by_selector('source.cfml.script meta.function-call.method -meta.function-call.method.static')
4444

4545
if len(regions) == 0:
4646
return method_chains

0 commit comments

Comments
 (0)