Skip to content

Commit 2019921

Browse files
committed
Correctly insert content block closing bracket
1 parent a4d3bd6 commit 2019921

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

core/katvan_codemodel.cpp

+24-10
Original file line numberDiff line numberDiff line change
@@ -282,28 +282,41 @@ QTextBlock CodeModel::findMatchingIndentBlock(QTextBlock block) const
282282
return block;
283283
}
284284

285-
parsing::ParserState::Kind CodeModel::getStateForBracketInsertion(QTextCursor cursor) const
285+
std::tuple<State, State> CodeModel::getStatesForBracketInsertion(QTextCursor cursor) const
286286
{
287+
State prevState = State::INVALID;
288+
State currState = State::INVALID;
289+
287290
// First, there might be an interesting state that the parser may have
288291
// 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.
290294
if (!cursor.atBlockStart()) {
291295
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+
}
294301
}
295302
}
296303

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+
}
300312
}
301-
return State::CONTENT;
313+
314+
return std::make_tuple(prevState, currState);
302315
}
303316

304317
std::optional<QChar> CodeModel::getMatchingCloseBracket(QTextCursor cursor, QChar openBracket) const
305318
{
306-
State state = getStateForBracketInsertion(cursor);
319+
auto [prevState, state] = getStatesForBracketInsertion(cursor);
307320

308321
QChar prevChar;
309322
if (!cursor.atBlockStart()) {
@@ -340,7 +353,8 @@ std::optional<QChar> CodeModel::getMatchingCloseBracket(QTextCursor cursor, QCha
340353
}
341354
}
342355
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) {
344358
return QLatin1Char(']');
345359
}
346360
}

core/katvan_codemodel.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ class CodeModel : public QObject
111111
// Find the inner most state span still in effect at the given global position
112112
std::optional<StateSpan> spanAtPosition(QTextBlock block, int globalPos) const;
113113

114-
// Find the relevant "current" state for the cursor to consider which brackets
115-
// can be auto-inserted.
116-
parsing::ParserState::Kind getStateForBracketInsertion(QTextCursor cursor) const;
114+
// Find the relevant "previous" and current" states for the cursor to consider
115+
// which brackets can be auto-inserted.
116+
std::tuple<parsing::ParserState::Kind, parsing::ParserState::Kind> getStatesForBracketInsertion(QTextCursor cursor) const;
117117

118118
QTextDocument* d_document;
119119
};

tests/katvan_codemodel.t.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ Q_GLOBAL_STATIC(QStringList, GET_MATCHING_CLOSE_BRACKET_TEST_DOC, {
301301
/* 3 */ "$\"AB\" = ln(1 + x)$",
302302
/* 4 */ "// a comment",
303303
/* 5 */ "`raw content`",
304-
/* 6 */ "#par $ x = #rect $"
304+
/* 6 */ "#par $ x = #rect $",
305+
/* 7 */ "#text()"
305306
})
306307

307308
class CodeModel_GetMatchingCloseBracketTests : public ::testing::Test {
@@ -418,6 +419,8 @@ TEST_F(CodeModel_GetMatchingCloseBracketTests, SquareBrackets)
418419

419420
EXPECT_THAT(model.getMatchingCloseBracket(cursorAt(6, 4), QLatin1Char('[')), ::testing::Eq(QLatin1Char(']')));
420421
EXPECT_THAT(model.getMatchingCloseBracket(cursorAt(6, 16), QLatin1Char('[')), ::testing::Eq(QLatin1Char(']')));
422+
423+
EXPECT_THAT(model.getMatchingCloseBracket(cursorAt(7, 7), QLatin1Char('[')), ::testing::Eq(QLatin1Char(']')));
421424
}
422425

423426
TEST_F(CodeModel_GetMatchingCloseBracketTests, MathDelimiters)

0 commit comments

Comments
 (0)