Skip to content

Commit c2dfb68

Browse files
committed
fix: [OCaml] Indentation on multi-line call containing functions
This commit uses the newly introduced "measuring scopes" to fix the indentation setback introduced in #745.
1 parent 4847bc1 commit c2dfb68

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

topiary-queries/queries/ocaml.scm

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,27 +1153,41 @@
11531153
; long_argument_3
11541154
; long_argument_4
11551155
;
1156-
; When the last argument is a (parenthesized) function application, end the scope
1156+
; When the last argument is a (parenthesized) function application, end the indentation
11571157
; _before_ the application. This allows the following to be formatted as such:
11581158
; let () =
11591159
; foo bar (fun x ->
1160-
; something horrible onto x
1160+
; something horrible onto x
1161+
; )
1162+
; But when the function application minus the last argument is multiline,
1163+
; the whole scope still must be indented:
1164+
; let () =
1165+
; foo
1166+
; bar
1167+
; (fun x ->
1168+
; x
11611169
; )
1170+
;
1171+
; Because of these constraints, we must use measuring scopes here: the position of the
1172+
; indent_end depends on the multi-line-ness of a subsection of the whole scope.
11621173
(application_expression
11631174
.
1164-
(_) @append_indent_start @prepend_begin_scope
1175+
(_) @append_indent_start @prepend_begin_scope @prepend_begin_measuring_scope
11651176
(#scope_id! "function_application")
1166-
(_) @append_indent_end
1177+
(_) @append_end_scope
11671178
.
11681179
)
1180+
; The end of the measuring scope depends on the last argument: if it's a function,
1181+
; end it before the function, otherwise end it after the last argument. In that case,
1182+
; it's the same as the regular scope.
11691183
(application_expression
11701184
(#scope_id! "function_application")
11711185
(_
11721186
[
11731187
(fun_expression)
11741188
(function_expression)
11751189
]? @do_nothing
1176-
) @append_end_scope
1190+
) @append_end_measuring_scope
11771191
.
11781192
)
11791193
(application_expression
@@ -1182,10 +1196,24 @@
11821196
[
11831197
(fun_expression)
11841198
(function_expression)
1185-
] @prepend_end_scope
1199+
] @prepend_end_measuring_scope
11861200
)
11871201
.
11881202
)
1203+
; If the measuring scope is single-line, end indentation _before_ the last node.
1204+
; Otherwise, end the indentation after the last node.
1205+
(application_expression
1206+
(#multi_line_scope_only! "function_application")
1207+
(_) @append_indent_end
1208+
.
1209+
)
1210+
(application_expression
1211+
(#single_line_scope_only! "function_application")
1212+
(_) @prepend_indent_end
1213+
.
1214+
)
1215+
; The node to which we apply append_spaced_scoped_softline will always
1216+
; be in both scopes, regular and measuring.
11891217
(application_expression
11901218
(_) @append_spaced_scoped_softline
11911219
.

0 commit comments

Comments
 (0)