Skip to content

Commit 65a9328

Browse files
committed
Refactor token processing in transformToMarkdown and CodeUtils for improved compound handling
1 parent 094fda6 commit 65a9328

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

workspaces/ballerina/ballerina-side-panel/src/components/editors/ExpandedEditor/utils/transformToMarkdown.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,8 @@ export const transformExpressionToMarkdown = (
9090
return expression;
9191
}
9292

93-
// Sort by position in reverse order to maintain position integrity
94-
const sortedTokens = renderableTokens.sort((a, b) => b.start - a.start);
95-
9693
let transformed = expression;
97-
for (const token of sortedTokens) {
94+
for (const token of renderableTokens) {
9895
transformed =
9996
transformed.slice(0, token.start) +
10097
token.chipTag +

workspaces/ballerina/ballerina-side-panel/src/components/editors/MultiModeExpressionEditor/ChipExpressionEditor/CodeUtils.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,28 +269,39 @@ export const iterateTokenStream = (
269269
) => {
270270
const docLength = content.length;
271271

272-
// Create a set of token indices that are part of compounds
272+
const compoundsByStartIndex = new Map<number, CompoundTokenSequence[]>();
273273
const compoundTokenIndices = new Set<number>();
274-
for (const compound of compounds) {
275-
for (let i = compound.startIndex; i <= compound.endIndex; i++) {
276-
compoundTokenIndices.add(i);
277-
}
278-
}
279274

280-
// Process compound tokens
281275
for (const compound of compounds) {
282276
// Validate compound range
283277
if (compound.start < 0 || compound.end > docLength || compound.start >= compound.end) {
284278
continue;
285279
}
286-
callbacks.onCompound(compound);
280+
281+
// Group compounds by their starting token index
282+
const existing = compoundsByStartIndex.get(compound.startIndex) || [];
283+
existing.push(compound);
284+
compoundsByStartIndex.set(compound.startIndex, existing);
285+
286+
// Mark all indices within this compound as consumed
287+
for (let i = compound.startIndex; i <= compound.endIndex; i++) {
288+
compoundTokenIndices.add(i);
289+
}
287290
}
288291

289-
// Process individual tokens that are not part of compounds
290292
for (let i = 0; i < tokens.length; i++) {
291293
const token = tokens[i];
292294

293-
// Skip tokens that are part of compound sequences
295+
// Check if any compounds begin at this token index
296+
const startingCompounds = compoundsByStartIndex.get(i);
297+
if (startingCompounds) {
298+
// Trigger callback for each compound starting here
299+
for (const compound of startingCompounds) {
300+
callbacks.onCompound(compound);
301+
}
302+
}
303+
304+
// Check if the individual token is consumed by a compound
294305
if (compoundTokenIndices.has(i)) {
295306
continue;
296307
}

0 commit comments

Comments
 (0)