@@ -282,28 +282,41 @@ QTextBlock CodeModel::findMatchingIndentBlock(QTextBlock block) const
282
282
return block;
283
283
}
284
284
285
- parsing::ParserState::Kind CodeModel::getStateForBracketInsertion (QTextCursor cursor) const
285
+ std::tuple<State, State> CodeModel::getStatesForBracketInsertion (QTextCursor cursor) const
286
286
{
287
+ State prevState = State::INVALID;
288
+ State currState = State::INVALID;
289
+
287
290
// First, there might be an interesting state that the parser may have
288
291
// already implicitly closed; e.g. block scoped states closed by reaching
289
- // end of line, or certain "instant" states. These take precedence.
292
+ // end of line, or certain "instant" states. These take precedence in
293
+ // being the "current" state - while they are also the "previous" state.
290
294
if (!cursor.atBlockStart ()) {
291
295
auto span = spanAtPosition (cursor.block (), cursor.position () - 1 );
292
- if (span && span->implicitlyClosed ) {
293
- return span->state ;
296
+ if (span) {
297
+ prevState = span->state ;
298
+ if (span->implicitlyClosed ) {
299
+ currState = span->state ;
300
+ }
294
301
}
295
302
}
296
303
297
- auto span = spanAtPosition (cursor.block (), cursor.position ());
298
- if (span) {
299
- return span->state ;
304
+ if (currState == State::INVALID) {
305
+ auto span = spanAtPosition (cursor.block (), cursor.position ());
306
+ if (span) {
307
+ currState = span->state ;
308
+ }
309
+ else {
310
+ currState = State::CONTENT;
311
+ }
300
312
}
301
- return State::CONTENT;
313
+
314
+ return std::make_tuple (prevState, currState);
302
315
}
303
316
304
317
std::optional<QChar> CodeModel::getMatchingCloseBracket (QTextCursor cursor, QChar openBracket) const
305
318
{
306
- State state = getStateForBracketInsertion (cursor);
319
+ auto [prevState, state] = getStatesForBracketInsertion (cursor);
307
320
308
321
QChar prevChar;
309
322
if (!cursor.atBlockStart ()) {
@@ -340,7 +353,8 @@ std::optional<QChar> CodeModel::getMatchingCloseBracket(QTextCursor cursor, QCha
340
353
}
341
354
}
342
355
else if (openBracket == QLatin1Char (' [' )) {
343
- if (isInCode || isCodeFunctionCall || ((isInContent || isInMath) && prevChar == QLatin1Char (' #' ))) {
356
+ if (isInCode || isCodeFunctionCall || ((isInContent || isInMath) && prevChar == QLatin1Char (' #' )) ||
357
+ prevState == State::CODE_ARGUMENTS || prevState == State::CONTENT_BLOCK) {
344
358
return QLatin1Char (' ]' );
345
359
}
346
360
}
0 commit comments