Skip to content

Commit

Permalink
Clarify variables + skip isConsecutive
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Jun 20, 2022
1 parent cb966e3 commit fc4fcda
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions source/features/show-whitespace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ function showWhiteSpacesOn(line: Element): void {
const isLeading = nodeIndex === 0;
const isTrailing = nodeIndex === textNodesOnThisLine.length - 1;

const startingCharacter = shouldAvoidSurroundingSpaces && isLeading ? 1 : 0;
const startingCharacterIndex = shouldAvoidSurroundingSpaces && isLeading ? 1 : 0;
const skipLastCharacter = shouldAvoidSurroundingSpaces && isTrailing;
const endingCharacter = text.length - 1 - Number(skipLastCharacter);
const endingCharacterIndex = text.length - 1 - Number(skipLastCharacter);

// Loop goes in reverse otherwise `splitText`'s `index` parameter needs to keep track of the previous split
for (let i = endingCharacter; i >= startingCharacter; i--) {
for (let i = endingCharacterIndex; i >= startingCharacterIndex; i--) {
const thisCharacter = text[i];
const endingIndex = i;

Expand All @@ -39,15 +39,13 @@ function showWhiteSpacesOn(line: Element): void {
continue;
}

// Find the same character so they can be wrapped together, but stop at `startingCharacter`
let isConsecutive = false;
while (text[i - 1] === thisCharacter && !(i === startingCharacter)) {
// Find the same character so they can be wrapped together, but stop at `startingCharacterIndex`
while (text[i - 1] === thisCharacter && !(i === startingCharacterIndex)) {
i--;
isConsecutive = true;
}

// Skip non-boundary single spaces
if (!isLeading && !isTrailing && !isConsecutive && thisCharacter === ' ') {
if (!isLeading && !isTrailing && i === endingIndex && thisCharacter === ' ') {
continue;
}

Expand Down

0 comments on commit fc4fcda

Please sign in to comment.