Skip to content

Commit 5f87ab2

Browse files
committed
Fixed indentation feature
If a line contained a semicolon in quotes or expressions, the indentation offset could be wrong
1 parent 4d21fa5 commit 5f87ab2

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/utils/display.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,29 @@ export function displayTime(value: number | null | undefined, showTrailingZeroes
197197
* @returns Indented file content
198198
*/
199199
export function indent(content: string): string {
200-
const lines = content.split('\n');
200+
function findSemicolon(line: string): number {
201+
for (let i = 0; i < line.length; i++) {
202+
if (inQuotes) {
203+
inQuotes = (line[i] !== '"');
204+
} else if (line[i] === '"') {
205+
inQuotes = true;
206+
} else if (inExpression) {
207+
inExpression = (line[i] !== '}');
208+
} else if (line[i] === '{') {
209+
inExpression = true;
210+
} else if (line[i] === ';') {
211+
return i;
212+
}
213+
}
214+
return -1;
215+
}
201216

202217
// Find out how long the maximum command is
203-
let maxCommandLength = 0;
204-
for (const line of lines) {
205-
const commentIndex = line.indexOf(';');
218+
const lines = content.split('\n');
219+
let maxCommandLength = 0;
220+
let inQuotes = false, inExpression = false;
221+
for (const line of lines) {
222+
const commentIndex = findSemicolon(line);
206223
if (commentIndex > 0) {
207224
const commandLength = line.substring(0, commentIndex).trimEnd().length;
208225
if (commandLength > maxCommandLength) {
@@ -214,7 +231,7 @@ export function indent(content: string): string {
214231
// Align line comments
215232
let newResult = "";
216233
for (const line of lines) {
217-
const commentIndex = line.indexOf(';');
234+
const commentIndex = findSemicolon(line);
218235
if (commentIndex <= 0) {
219236
newResult += line + '\n';
220237
} else {

0 commit comments

Comments
 (0)