@@ -422,6 +422,17 @@ static bool isCodeHolderState(const ParserState& state)
422
422
|| state.kind == ParserState::Kind::CODE_ARGUMENTS;
423
423
}
424
424
425
+ static bool isCodeState (const ParserState& state)
426
+ {
427
+ return isCodeHolderState (state)
428
+ || state.kind == ParserState::Kind::CODE_VARIABLE_NAME
429
+ || state.kind == ParserState::Kind::CODE_FUNCTION_NAME
430
+ || state.kind == ParserState::Kind::CODE_NUMERIC_LITERAL
431
+ || state.kind == ParserState::Kind::CODE_KEYWORD
432
+ || state.kind == ParserState::Kind::CODE_EXPRESSION_CHAIN
433
+ || state.kind == ParserState::Kind::CODE_STRING_EXPRESSION;
434
+ }
435
+
425
436
void Parser::parse ()
426
437
{
427
438
namespace m = matchers;
@@ -1125,8 +1136,6 @@ void IsolatesListener::initializeState(const ParserState& state, size_t endMarke
1125
1136
1126
1137
void IsolatesListener::finalizeState (const ParserState& state, size_t endMarker, bool implicit)
1127
1138
{
1128
- Q_UNUSED (implicit);
1129
-
1130
1139
// What do we want to isolate the directionality of? Ideally short and
1131
1140
// _continuous_ bits of text associated with a state that typically involves
1132
1141
// characters with weak or no directionality. Math, inline code, content bits
@@ -1141,55 +1150,67 @@ void IsolatesListener::finalizeState(const ParserState& state, size_t endMarker,
1141
1150
d_codeSequenceRangesForLevel.takeLast ();
1142
1151
}
1143
1152
1144
- if (state.kind == ParserState::Kind::CONTENT_REFERENCE) {
1145
- d_ranges.append (IsolateRange{ Qt::LayoutDirectionAuto, state.startPos , endMarker });
1146
- }
1147
- else if (state.kind == ParserState::Kind::CONTENT_BLOCK) {
1148
- // Don't include the square brackets in the isolate
1149
- d_ranges.append (IsolateRange{ Qt::LayoutDirectionAuto, state.startPos + 1 , endMarker - 1 });
1150
- }
1151
- else if (state.kind == ParserState::Kind::MATH) {
1152
- d_ranges.append (IsolateRange{ Qt::LeftToRight, state.startPos , endMarker });
1153
+ if (!implicit) {
1154
+ if (state.kind == ParserState::Kind::CONTENT_REFERENCE) {
1155
+ d_ranges.append (IsolateRange{ Qt::LayoutDirectionAuto, state.startPos , endMarker });
1156
+ }
1157
+ else if (state.kind == ParserState::Kind::CONTENT_BLOCK) {
1158
+ // Don't include the square brackets in the isolate
1159
+ d_ranges.append (IsolateRange{ Qt::LayoutDirectionAuto, state.startPos + 1 , endMarker - 1 });
1160
+ }
1161
+ else if (state.kind == ParserState::Kind::MATH) {
1162
+ d_ranges.append (IsolateRange{ Qt::LeftToRight, state.startPos , endMarker });
1163
+ }
1153
1164
}
1154
1165
1155
- bool isIsolatableCode = state.kind == ParserState::Kind::CODE_VARIABLE_NAME
1156
- || state.kind == ParserState::Kind::CODE_FUNCTION_NAME
1157
- || state.kind == ParserState::Kind::CODE_ARGUMENTS
1158
- || state.kind == ParserState::Kind::CODE_EXPRESSION_CHAIN
1159
- || state.kind == ParserState::Kind::CODE_NUMERIC_LITERAL
1160
- || state.kind == ParserState::Kind::CODE_STRING_EXPRESSION
1161
- || state.kind == ParserState::Kind::CONTENT_BLOCK;
1166
+ // Basically we want ANY code state to be considered so we have maximally
1167
+ // long isolate ranges; but if any of it is something we don't want to
1168
+ // isolate (code blocks, full lines, and parameter lists that spill to the
1169
+ // next line), we can discard the whole thing.
1170
+ if (isCodeState (state) || state.kind == ParserState::Kind::CONTENT_BLOCK) {
1171
+ IsolateRange* codeRange = createOrUpdateCodeRange (state.kind , state.startPos , endMarker);
1172
+ if (!codeRange) {
1173
+ return ;
1174
+ }
1162
1175
1163
- if (isIsolatableCode) {
1164
- if (!d_codeSequenceRangesForLevel.last ().isEmpty ()) {
1165
- auto & existingRange = d_ranges[d_codeSequenceRangesForLevel.last ().last ()];
1176
+ if (implicit || state.kind == ParserState::Kind::CODE_BLOCK || state.kind == ParserState::Kind::CODE_LINE) {
1177
+ codeRange->discard = true ;
1178
+ }
1179
+ }
1180
+ }
1166
1181
1167
- if (state.startPos == existingRange.endPos + 1 ) {
1168
- // Extends existing isolated code range
1169
- existingRange.endPos = endMarker;
1170
- return ;
1171
- }
1172
- else if (existingRange.startPos <= endMarker && existingRange.endPos >= state.startPos ) {
1173
- // Intersects with existing isolated code range
1174
- size_t origStartPos = existingRange.startPos ;
1175
- existingRange.startPos = qMin (existingRange.startPos , state.startPos );
1176
- existingRange.endPos = qMax (existingRange.endPos , endMarker);
1177
-
1178
- if (existingRange.startPos < origStartPos) {
1179
- discardRedundantCodeRanges ();
1180
- }
1181
- return ;
1182
+ IsolateRange* IsolatesListener::createOrUpdateCodeRange (ParserState::Kind state, size_t startPos, size_t endPos)
1183
+ {
1184
+ if (!d_codeSequenceRangesForLevel.last ().isEmpty ()) {
1185
+ auto & existingRange = d_ranges[d_codeSequenceRangesForLevel.last ().last ()];
1186
+
1187
+ if (startPos == existingRange.endPos + 1 ) {
1188
+ // Extends existing isolated code range
1189
+ existingRange.endPos = endPos;
1190
+ return &existingRange;
1191
+ }
1192
+ else if (existingRange.startPos <= endPos && existingRange.endPos >= startPos) {
1193
+ // Intersects with existing isolated code range
1194
+ size_t origStartPos = existingRange.startPos ;
1195
+ existingRange.startPos = qMin (existingRange.startPos , startPos);
1196
+ existingRange.endPos = qMax (existingRange.endPos , endPos);
1197
+
1198
+ if (existingRange.startPos < origStartPos) {
1199
+ discardRedundantCodeRanges ();
1182
1200
}
1201
+ return &existingRange;
1183
1202
}
1203
+ }
1184
1204
1185
- // No existing isolated code range in this nesting level, or does not
1186
- // intersect. Start a new one, but not for content blocks as they have
1187
- // special handling above .
1188
- if (state. kind != ParserState::Kind::CONTENT_BLOCK) {
1189
- d_codeSequenceRangesForLevel.last ().append (d_ranges.size ());
1190
- d_ranges.append (IsolateRange{ Qt::LeftToRight, state. startPos , endMarker });
1191
- }
1205
+ // No existing isolated code range in this nesting level, or does not
1206
+ // intersect. Start a new one, but not for content blocks as they have
1207
+ // special handling in finalizeState .
1208
+ if (state != ParserState::Kind::CONTENT_BLOCK) {
1209
+ d_codeSequenceRangesForLevel.last ().append (d_ranges.size ());
1210
+ d_ranges.append (IsolateRange{ Qt::LeftToRight, startPos, endPos });
1211
+ return &d_ranges. last ();
1192
1212
}
1213
+ return nullptr ;
1193
1214
}
1194
1215
1195
1216
void IsolatesListener::discardRedundantCodeRanges ()
0 commit comments