Skip to content

Commit c269bde

Browse files
committed
Enhancement to directionality heuristic for nuetral lines
For lines that don't have a strong character and don't close a scope, but open a math/code/raw scope - set them to LTR.
1 parent c1cf92e commit c269bde

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

core/katvan_codemodel.cpp

+32-8
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,23 @@ static bool isDelimitedState(State state)
148148
|| state == State::CODE_ARGUMENTS;
149149
}
150150

151+
static bool isIndentingState(State state)
152+
{
153+
return state == State::CONTENT_BLOCK
154+
|| state == State::CODE_BLOCK
155+
|| state == State::CODE_ARGUMENTS
156+
|| state == State::MATH_ARGUMENTS;
157+
}
158+
159+
static bool isLeftLeaningState(State state)
160+
{
161+
return state == State::CODE_BLOCK
162+
|| state == State::CODE_LINE
163+
|| state == State::MATH
164+
|| state == State::CONTENT_RAW
165+
|| state == State::CONTENT_RAW_BLOCK;
166+
}
167+
151168
std::optional<int> CodeModel::findMatchingBracket(int pos) const
152169
{
153170
QTextBlock block = d_document->findBlock(pos);
@@ -184,14 +201,6 @@ std::optional<int> CodeModel::findMatchingBracket(int pos) const
184201
return std::nullopt;
185202
}
186203

187-
static bool isIndentingState(State state)
188-
{
189-
return state == State::CONTENT_BLOCK
190-
|| state == State::CODE_BLOCK
191-
|| state == State::CODE_ARGUMENTS
192-
|| state == State::MATH_ARGUMENTS;
193-
}
194-
195204
std::optional<StateSpan> CodeModel::spanAtPosition(QTextBlock block, int globalPos) const
196205
{
197206
Q_ASSERT(d_document->findBlock(globalPos) == block);
@@ -282,6 +291,21 @@ QTextBlock CodeModel::findMatchingIndentBlock(QTextBlock block) const
282291
return block;
283292
}
284293

294+
bool CodeModel::startsLeftLeaningSpan(QTextBlock block) const
295+
{
296+
auto* blockData = BlockData::get<StateSpansBlockData>(block);
297+
if (!blockData) {
298+
return false;
299+
}
300+
301+
for (const StateSpan& span : blockData->stateSpans()) {
302+
if (span.startPos >= 0 && isLeftLeaningState(span.state)) {
303+
return true;
304+
}
305+
}
306+
return false;
307+
}
308+
285309
std::tuple<State, State> CodeModel::getStatesForBracketInsertion(QTextCursor cursor) const
286310
{
287311
State prevState = State::INVALID;

core/katvan_codemodel.h

+5
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ class CodeModel : public QObject
103103
// level. If none, returns the position's own block.
104104
QTextBlock findMatchingIndentBlock(QTextBlock block) const;
105105

106+
// For the given block, check if a "left leaning" scope starts in it. "Left
107+
// leaning" means a code scope whose a-priori directionality (without knowing
108+
// the content) is Left-to-Right.
109+
bool startsLeftLeaningSpan(QTextBlock block) const;
110+
106111
// Find closing bracket character that should be automatically appended
107112
// if _openBracket_ is inserted at the given cursor's position.
108113
std::optional<QChar> getMatchingCloseBracket(QTextCursor cursor, QChar openBracket) const;

core/katvan_editorlayout.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,10 @@ Qt::LayoutDirection EditorLayout::getBlockDirection(const QTextBlock& block)
503503
return matchingBlock.layout()->textOption().textDirection();
504504
}
505505

506+
if (d_codeModel->startsLeftLeaningSpan(block)) {
507+
return Qt::LeftToRight;
508+
}
509+
506510
QTextBlock prevBlock = block.previous();
507511
if (prevBlock.isValid()) {
508512
return prevBlock.layout()->textOption().textDirection();

0 commit comments

Comments
 (0)