Skip to content

Commit 2da5c62

Browse files
committed
Distinguish named and anonymous functions
1 parent 9909a31 commit 2da5c62

File tree

10 files changed

+327
-53
lines changed

10 files changed

+327
-53
lines changed

.cfformat.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
"brackets.padding": false,
1010
"comment.asterisks": "align",
1111
"for_loop_semicolons.padding": true,
12+
"function_anonymous.empty_padding": false,
13+
"function_anonymous.group_to_block_spacing": "spaced",
14+
"function_anonymous.multiline.element_count": 4,
15+
"function_anonymous.multiline.leading_comma": false,
16+
"function_anonymous.multiline.leading_comma.padding": true,
17+
"function_anonymous.multiline.min_length": 40,
18+
"function_anonymous.padding": false,
1219
"function_call.empty_padding": false,
1320
"function_call.multiline.element_count": 4,
1421
"function_call.multiline.leading_comma": false,
@@ -31,8 +38,8 @@
3138
"keywords.spacing_to_group": true,
3239
"max_columns": 120,
3340
"parentheses.padding": false,
34-
"strings.quote": "single",
3541
"strings.attributes.quote": "double",
42+
"strings.quote": "single",
3643
"struct.empty_padding": false,
3744
"struct.multiline.element_count": 4,
3845
"struct.multiline.leading_comma": false,

data/examples.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@
77
"brackets.padding":"// brackets.padding: true\na[ 'mykey' ][ 1 ] = 7;\n\n// brackets.padding: false\na['mykey'][1] = 7;",
88
"comment.asterisks":"// comment.asterisks: \"align\"\n{\n /**\n * a comment\n */\n}\n\n// comment.asterisks: \"indent\"\n{\n /**\n * a comment\n */\n}\n\n// comment.asterisks: \"\"\n{\n /**\n * a comment\n */\n}",
99
"for_loop_semicolons.padding":"// for_loop_semicolons.padding: true\nfor (var i = 0; i < 10; i++) {\n}\n\n// for_loop_semicolons.padding: false\nfor (var i = 0;i < 10;i++) {\n}",
10+
"function_anonymous.empty_padding":"// function_anonymous.empty_padding: true\nfunction( ) {\n}\n\n// function_anonymous.empty_padding: false\nfunction() {\n}",
11+
"function_anonymous.group_to_block_spacing":"// function_anonymous.group_to_block_spacing: \"spaced\"\nfunction() {\n}\n\n// function_anonymous.group_to_block_spacing: \"compact\"\nfunction(){\n}\n\n// function_anonymous.group_to_block_spacing: \"newline\"\nfunction()\n{\n}",
12+
"function_anonymous.multiline.leading_comma":"// function_anonymous.multiline.leading_comma: true\nfunction(\n a\n , b\n , c\n , d\n) {\n}\n\n// function_anonymous.multiline.leading_comma: false\nfunction(\n a,\n b,\n c,\n d\n) {\n}",
13+
"function_anonymous.multiline.leading_comma.padding":"// function_anonymous.multiline.leading_comma.padding: true\nfunction(\n a\n , b\n , c\n , d\n) {\n}\n\n// function_anonymous.multiline.leading_comma.padding: false\nfunction(\n a\n ,b\n ,c\n ,d\n) {\n}",
14+
"function_anonymous.padding":"// function_anonymous.padding: true\nfunction( a, b ) {\n}\n\n// function_anonymous.padding: false\nfunction(a, b) {\n}",
1015
"function_call.empty_padding":"// function_call.empty_padding: true\nmyFunc( );\n\n// function_call.empty_padding: false\nmyFunc();",
1116
"function_call.multiline.leading_comma":"// function_call.multiline.leading_comma: true\nmyFunc(\n 1\n , 2\n , 3\n , 4\n);\n\n// function_call.multiline.leading_comma: false\nmyFunc(\n 1,\n 2,\n 3,\n 4\n);",
1217
"function_call.multiline.leading_comma.padding":"// function_call.multiline.leading_comma.padding: true\nmyFunc(\n 1\n , 2\n , 3\n , 4\n);\n\n// function_call.multiline.leading_comma.padding: false\nmyFunc(\n 1\n ,2\n ,3\n ,4\n);",
1318
"function_call.padding":"// function_call.padding: true\nmyFunc( 1, 2 );\n\n// function_call.padding: false\nmyFunc(1, 2);",
14-
"function_declaration.empty_padding":"// function_declaration.empty_padding: true\nfunction( ) {\n}\n\n// function_declaration.empty_padding: false\nfunction() {\n}",
15-
"function_declaration.group_to_block_spacing":"// function_declaration.group_to_block_spacing: \"spaced\"\nfunction() {\n}\n\n// function_declaration.group_to_block_spacing: \"compact\"\nfunction(){\n}\n\n// function_declaration.group_to_block_spacing: \"newline\"\nfunction()\n{\n}",
16-
"function_declaration.multiline.leading_comma":"// function_declaration.multiline.leading_comma: true\nfunction(\n a\n , b\n , c\n , d\n) {\n}\n\n// function_declaration.multiline.leading_comma: false\nfunction(\n a,\n b,\n c,\n d\n) {\n}",
17-
"function_declaration.multiline.leading_comma.padding":"// function_declaration.multiline.leading_comma.padding: true\nfunction(\n a\n , b\n , c\n , d\n) {\n}\n\n// function_declaration.multiline.leading_comma.padding: false\nfunction(\n a\n ,b\n ,c\n ,d\n) {\n}",
18-
"function_declaration.padding":"// function_declaration.padding: true\nfunction( a, b ) {\n}\n\n// function_declaration.padding: false\nfunction(a, b) {\n}",
19+
"function_declaration.empty_padding":"// function_declaration.empty_padding: true\nfunction example( ) {\n}\n\n// function_declaration.empty_padding: false\nfunction example() {\n}",
20+
"function_declaration.group_to_block_spacing":"// function_declaration.group_to_block_spacing: \"spaced\"\nfunction example() {\n}\n\n// function_declaration.group_to_block_spacing: \"compact\"\nfunction example(){\n}\n\n// function_declaration.group_to_block_spacing: \"newline\"\nfunction example()\n{\n}",
21+
"function_declaration.multiline.leading_comma":"// function_declaration.multiline.leading_comma: true\nfunction example(\n a\n , b\n , c\n , d\n) {\n}\n\n// function_declaration.multiline.leading_comma: false\nfunction example(\n a,\n b,\n c,\n d\n) {\n}",
22+
"function_declaration.multiline.leading_comma.padding":"// function_declaration.multiline.leading_comma.padding: true\nfunction example(\n a\n , b\n , c\n , d\n) {\n}\n\n// function_declaration.multiline.leading_comma.padding: false\nfunction example(\n a\n ,b\n ,c\n ,d\n) {\n}",
23+
"function_declaration.padding":"// function_declaration.padding: true\nfunction example( a, b ) {\n}\n\n// function_declaration.padding: false\nfunction example(a, b) {\n}",
1924
"indent_size":"// indent_size: 4\ndo {\n myFunc();\n}\n\n// indent_size: 2\ndo {\n myFunc();\n}",
2025
"keywords.block_to_keyword_spacing":"// keywords.block_to_keyword_spacing: \"spaced\"\nif (true) {\n} else {\n}\n\n// keywords.block_to_keyword_spacing: \"compact\"\nif (true) {\n}else {\n}\n\n// keywords.block_to_keyword_spacing: \"newline\"\nif (true) {\n}\nelse {\n}",
2126
"keywords.empty_group_spacing":"// keywords.empty_group_spacing: true\nif ( ) {\n}\n\n// keywords.empty_group_spacing: false\nif () {\n}",

data/reference.json

Lines changed: 106 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@
6262
"code": "{/**\n * a comment\n */}"
6363
},
6464
"type": "string",
65-
"values": ["align", "indent", ""]
65+
"values": [
66+
"align",
67+
"indent",
68+
""
69+
]
6670
},
6771
"for_loop_semicolons.padding": {
6872
"description": "When true, insert a space after for loop semicolons.",
@@ -71,6 +75,61 @@
7175
},
7276
"type": "boolean"
7377
},
78+
"function_anonymous.empty_padding": {
79+
"description": "When true, pad anonymous function declarations that have no parameters with a space.",
80+
"example": {
81+
"code": "function() {}"
82+
},
83+
"type": "boolean"
84+
},
85+
"function_anonymous.group_to_block_spacing": {
86+
"description": "How to space from the anonymous function parameters to the function block.",
87+
"example": {
88+
"code": "function() {}"
89+
},
90+
"type": "string",
91+
"values": [
92+
"spaced",
93+
"compact",
94+
"newline"
95+
]
96+
},
97+
"function_anonymous.multiline.element_count": {
98+
"description": "When an anonymous function declaration has this number of parameters, split them onto multiple lines.",
99+
"type": "integer"
100+
},
101+
"function_anonymous.multiline.leading_comma": {
102+
"description": "Whether to use a leading comma when anonymous function declaration parameters are printed on multiple lines.",
103+
"example": {
104+
"code": "function(a,b,c,d) {}",
105+
"settings": {
106+
"function_anonymous.multiline.min_length": 1
107+
}
108+
},
109+
"type": "boolean"
110+
},
111+
"function_anonymous.multiline.leading_comma.padding": {
112+
"description": "Whether to insert a space after leading commas when anonymous function declaration parameters are printed on multiple lines.",
113+
"example": {
114+
"code": "function(a,b,c,d) {}",
115+
"settings": {
116+
"function_anonymous.multiline.leading_comma": true,
117+
"function_anonymous.multiline.min_length": 1
118+
}
119+
},
120+
"type": "boolean"
121+
},
122+
"function_anonymous.multiline.min_length": {
123+
"description": "No matter how many parameters an anonymous function declaration has, if they can be printed in this many columns or less, keep them on one line.",
124+
"type": "integer"
125+
},
126+
"function_anonymous.padding": {
127+
"description": "Whether to pad non-empty anonymous function declarations with spaces.",
128+
"example": {
129+
"code": "function(a,b) {}"
130+
},
131+
"type": "boolean"
132+
},
74133
"function_call.empty_padding": {
75134
"description": "When true, function calls with no arguments are padded with a space.",
76135
"example": {
@@ -117,17 +176,21 @@
117176
"function_declaration.empty_padding": {
118177
"description": "When true, pad function declarations that have no parameters with a space.",
119178
"example": {
120-
"code": "function() {}"
179+
"code": "function example() {}"
121180
},
122181
"type": "boolean"
123182
},
124183
"function_declaration.group_to_block_spacing": {
125184
"description": "How to space from the function parameters to the function block.",
126185
"example": {
127-
"code": "function() {}"
186+
"code": "function example() {}"
128187
},
129188
"type": "string",
130-
"values": ["spaced", "compact", "newline"]
189+
"values": [
190+
"spaced",
191+
"compact",
192+
"newline"
193+
]
131194
},
132195
"function_declaration.multiline.element_count": {
133196
"description": "When a function declaration has this number of parameters, split them onto multiple lines.",
@@ -136,7 +199,7 @@
136199
"function_declaration.multiline.leading_comma": {
137200
"description": "Whether to use a leading comma when function declaration parameters are printed on multiple lines.",
138201
"example": {
139-
"code": "function(a,b,c,d) {}",
202+
"code": "function example(a,b,c,d) {}",
140203
"settings": {
141204
"function_declaration.multiline.min_length": 1
142205
}
@@ -146,7 +209,7 @@
146209
"function_declaration.multiline.leading_comma.padding": {
147210
"description": "Whether to insert a space after leading commas when function declaration parameters are printed on multiple lines.",
148211
"example": {
149-
"code": "function(a,b,c,d) {}",
212+
"code": "function example(a,b,c,d) {}",
150213
"settings": {
151214
"function_declaration.multiline.leading_comma": true,
152215
"function_declaration.multiline.min_length": 1
@@ -159,17 +222,20 @@
159222
"type": "integer"
160223
},
161224
"function_declaration.padding": {
162-
"description": "Whether to pad non-empty function calls with spaces.",
225+
"description": "Whether to pad non-empty function declarations with spaces.",
163226
"example": {
164-
"code": "function(a,b) {}"
227+
"code": "function example(a,b) {}"
165228
},
166229
"type": "boolean"
167230
},
168231
"indent_size": {
169232
"description": "Each indent level or tab is equivalent to this many spaces.",
170233
"example": {
171234
"code": "do {myFunc();}",
172-
"values": [4, 2]
235+
"values": [
236+
4,
237+
2
238+
]
173239
},
174240
"type": "integer"
175241
},
@@ -179,7 +245,11 @@
179245
"code": "if(true){}else{}"
180246
},
181247
"type": "string",
182-
"values": ["spaced", "compact", "newline"]
248+
"values": [
249+
"spaced",
250+
"compact",
251+
"newline"
252+
]
183253
},
184254
"keywords.empty_group_spacing": {
185255
"description": "Whether to pad empty keyword groups.",
@@ -194,7 +264,11 @@
194264
"code": "if(true){}"
195265
},
196266
"type": "string",
197-
"values": ["spaced", "compact", "newline"]
267+
"values": [
268+
"spaced",
269+
"compact",
270+
"newline"
271+
]
198272
},
199273
"keywords.padding_inside_group": {
200274
"description": "Whether to pad inside non-empty keyword groups.",
@@ -209,7 +283,11 @@
209283
"code": "do{}"
210284
},
211285
"type": "string",
212-
"values": ["spaced", "compact", "newline"]
286+
"values": [
287+
"spaced",
288+
"compact",
289+
"newline"
290+
]
213291
},
214292
"keywords.spacing_to_group": {
215293
"description": "Whether to space a keyword from following group.",
@@ -235,15 +313,23 @@
235313
"code": "http url='www.google.com';\nparam name=\"key\";"
236314
},
237315
"type": "string",
238-
"values": ["single", "double", ""]
316+
"values": [
317+
"single",
318+
"double",
319+
""
320+
]
239321
},
240322
"strings.quote": {
241323
"description": "Whether to use a single or double quote for strings. If set to an empty string, leaves string quotes as they are found.",
242324
"example": {
243325
"code": "a=\"One\";\nb='Two';"
244326
},
245327
"type": "string",
246-
"values": ["single", "double", ""]
328+
"values": [
329+
"single",
330+
"double",
331+
""
332+
]
247333
},
248334
"struct.empty_padding": {
249335
"description": "When true, non-empty structs are padded with spaces.",
@@ -292,7 +378,12 @@
292378
"description": "The key value separator to use in structs - it must contain either a single `:` or `=` and be no more than 3 characters in length.",
293379
"example": {
294380
"code": "myStruct={a:1,b:2};",
295-
"values": [": ", " = ", " : ", "="]
381+
"values": [
382+
": ",
383+
" = ",
384+
" : ",
385+
"="
386+
]
296387
},
297388
"type": "struct-key-value"
298389
},

models/cfscript/Functions.cfc

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,23 @@ component {
1919
columnOffset
2020
) {
2121
if (cftokens.peekScopes(functionStart)) {
22-
var nextToken = cftokens.next(false);
23-
var isComponentMethod = nextToken[2][nextToken[2].len() - 2] == 'meta.class.body.cfml';
24-
25-
var formattedText = nextToken[1];
22+
var words = [];
2623

2724
while (!cftokens.nextIsElement()) {
28-
formattedText &= ' ' & cftokens.next(false)[1];
25+
var token = cftokens.next(false)
26+
words.append(token[1]);
2927
}
3028

29+
var formattedText = words.toList(' ');
30+
var rootSettingKey = token[2].last() == 'entity.name.function.cfml' ? 'function_declaration' : 'function_anonymous';
31+
3132
// parameters
3233
var parametersTxt = printFunctionParameters(
3334
cftokens.next(false),
3435
settings,
3536
indent,
36-
columnOffset + formattedText.len()
37+
columnOffset + formattedText.len(),
38+
rootSettingKey
3739
);
3840

3941
columnOffset += parametersTxt.len();
@@ -60,11 +62,11 @@ component {
6062
}
6163

6264
if (
63-
settings['function_declaration.group_to_block_spacing'] == 'spaced' &&
65+
settings['#rootSettingKey#.group_to_block_spacing'] == 'spaced' &&
6466
!attributesTxt.find(chr(10))
6567
) {
6668
formattedText = formattedText & ' ';
67-
} else if (settings['function_declaration.group_to_block_spacing'] == 'newline') {
69+
} else if (settings['#rootSettingKey#.group_to_block_spacing'] == 'newline') {
6870
formattedText &= settings.lf & cfformat.indentTo(indent, settings);
6971
}
7072

@@ -76,16 +78,19 @@ component {
7678
}
7779

7880
if (cftokens.peekElement('function-parameters')) {
81+
var parametersElement = cftokens.next(false);
82+
var arrowToken = cftokens.next(false);
83+
7984
// arrow function parameters
8085
var formattedText = printFunctionParameters(
81-
cftokens.next(false),
86+
parametersElement,
8287
settings,
8388
indent,
84-
columnOffset
89+
columnOffset,
90+
'function_anonymous'
8591
);
8692

87-
// collect the arrow `=>`
88-
var arrowToken = cftokens.next(false);
93+
// add the arrow `=>`
8994
formattedText &= ' => ';
9095

9196
columnOffset += formattedText.len();
@@ -126,22 +131,23 @@ component {
126131
element,
127132
settings,
128133
indent,
129-
columnOffset
134+
columnOffset,
135+
rootSettingKey
130136
) {
131137
var printedElements = cfformat.delimited.printElements(element, settings, indent);
132138

133139
if (printedElements.printed.len() == 1 && printedElements.printed[1].trim() == '') {
134-
return settings['function_declaration.empty_padding'] ? '( )' : '()';
140+
return settings['#rootSettingKey#.empty_padding'] ? '( )' : '()';
135141
}
136142

137-
var spacer = settings['function_declaration.padding'] ? ' ' : '';
143+
var spacer = settings['#rootSettingKey#.padding'] ? ' ' : '';
138144
var delimiter = ', ';
139145
var formatted = '(' & spacer & printedElements.printed.tolist(delimiter) & spacer & ')';
140146

141147
if (
142148
(
143-
printedElements.printed.len() < settings['function_declaration.multiline.element_count'] ||
144-
formatted.len() <= settings['function_declaration.multiline.min_length']
149+
printedElements.printed.len() < settings['#rootSettingKey#.multiline.element_count'] ||
150+
formatted.len() <= settings['#rootSettingKey#.multiline.min_length']
145151
) &&
146152
!formatted.find(chr(10)) &&
147153
columnOffset + formatted.len() <= settings.max_columns
@@ -151,7 +157,7 @@ component {
151157

152158
var formattedText = '(';
153159
formattedText &= cfformat.delimited.joinElements(
154-
'function_declaration',
160+
rootSettingKey,
155161
printedElements,
156162
settings,
157163
indent

0 commit comments

Comments
 (0)