@@ -92,6 +92,34 @@ module BracketPair = {
92
92
let endsWithOpenPair = ({openPair, _}, str) => {
93
93
StringEx . endsWith(~postfix= openPair, str);
94
94
};
95
+
96
+ let isJustClosingPair = ({closePair, _}, str) => {
97
+ let len = String . length(str);
98
+
99
+ let rec loop = (foundPair, idx) =>
100
+ if (idx >= len) {
101
+ foundPair;
102
+ } else if (foundPair) {
103
+ false ;
104
+ // We found the closing pair... but there's other stuff after
105
+ } else {
106
+ let c = str. [idx];
107
+
108
+ if (c == ' ' || c == '\t' ) {
109
+ loop(foundPair, idx + 1 );
110
+ } else if (c == closePair. [0]) {
111
+ loop(true , idx + 1 );
112
+ } else {
113
+ false ;
114
+ };
115
+ };
116
+
117
+ if (String . length(closePair) == 1 ) {
118
+ loop(false , 0 );
119
+ } else {
120
+ false ;
121
+ };
122
+ };
95
123
};
96
124
97
125
let defaultBrackets: list(BracketPair . t) =
@@ -226,36 +254,45 @@ let toVimAutoClosingPairs = (syntaxScope: SyntaxScope.t, configuration: t) => {
226
254
);
227
255
};
228
256
229
- let toAutoIndent =
257
+ let shouldIncreaseIndent =
230
258
(
231
- {increaseIndentPattern, decreaseIndentPattern, brackets, _},
232
259
~previousLine as str,
233
260
~beforePreviousLine as _,
261
+ {increaseIndentPattern, brackets, _},
234
262
) => {
235
- let increase =
236
- increaseIndentPattern
237
- |> Option . map(regex => OnigRegExp . test(str, regex))
238
- // If no indentation pattern, fall-back to bracket pair
239
- |> OptionEx . or_lazy(() =>
240
- Some (
241
- List . exists(
242
- bracket => BracketPair . endsWithOpenPair(bracket, str),
243
- brackets,
244
- ),
245
- )
263
+ increaseIndentPattern
264
+ |> Option . map(regex => OnigRegExp . test(str, regex))
265
+ // If no indentation pattern, fall-back to bracket pair
266
+ |> OptionEx . or_lazy(() =>
267
+ Some (
268
+ List . exists(
269
+ bracket => BracketPair . endsWithOpenPair(bracket, str),
270
+ brackets,
271
+ ),
246
272
)
247
- |> Option . value(~default= false );
248
-
249
- let decrease =
250
- decreaseIndentPattern
251
- |> Option . map(regex => OnigRegExp . test(str, regex))
252
- |> Option . value(~default= false );
253
-
254
- if (increase) {
255
- Vim . AutoIndent . IncreaseIndent ;
256
- } else if (decrease) {
257
- Vim . AutoIndent . DecreaseIndent ;
258
- } else {
259
- Vim . AutoIndent . KeepIndent ;
273
+ )
274
+ |> Option . value(~default= false );
275
+ };
276
+
277
+ let shouldDecreaseIndent = (~line, {decreaseIndentPattern, brackets, _}) => {
278
+ decreaseIndentPattern
279
+ |> Option . map(regex => OnigRegExp . test(line, regex))
280
+ |> OptionEx . or_lazy(() => {
281
+ Some (
282
+ List . exists(
283
+ bracket => BracketPair . isJustClosingPair(bracket, line),
284
+ brackets,
285
+ ),
286
+ )
287
+ })
288
+ |> Option . value(~default= false );
289
+ };
290
+
291
+ let toAutoIndent = (languageConfig, ~previousLine, ~beforePreviousLine) => {
292
+ let increase =
293
+ shouldIncreaseIndent(~previousLine, ~beforePreviousLine, languageConfig);
294
+
295
+ if (increase) {Vim . AutoIndent . IncreaseIndent } else {
296
+ Vim . AutoIndent . KeepIndent
260
297
};
261
298
};
0 commit comments