From 06bcbb6f540d710bc241ed86d36c9838e83e04b6 Mon Sep 17 00:00:00 2001 From: Call Delegation Date: Mon, 6 Nov 2023 23:05:07 -0500 Subject: [PATCH] Fixing indentation --- src/lib/plugins/code-import.ts | 82 +++++++++++++--------------------- 1 file changed, 31 insertions(+), 51 deletions(-) diff --git a/src/lib/plugins/code-import.ts b/src/lib/plugins/code-import.ts index 4dbfed741..75f99718e 100644 --- a/src/lib/plugins/code-import.ts +++ b/src/lib/plugins/code-import.ts @@ -10,14 +10,9 @@ import type { Root } from 'remark-gfm'; import { visit } from 'unist-util-visit'; import { FUEL_TESTNET } from '~/src/config/constants'; +import { getEndCommentType } from './text-import'; import type { CommentTypes } from './text-import'; -interface Block { - content: string; - lineStart: number; - lineEnd: number; -} - function toAST(content: string) { return acorn.parse(content, { ecmaVersion: 'latest', @@ -57,35 +52,26 @@ function extractCommentBlock( comment: string, commentType: CommentTypes, trim: string -): Block { +) { const lines = content.split(EOL); - let lineStart = -1; - let lineEnd = -1; - const anchorStack: string[] = []; + let lineStart = 1; + let lineEnd = 1; - const endCommentType = - commentType === '' - : commentType === '{/*' - ? ' */}' - : commentType === '/*' - ? ' */' - : ''; - - const startAnchor = `${commentType} ANCHOR: ${comment}${endCommentType}`; - const endAnchor = `${commentType} ANCHOR_END: ${comment}${endCommentType}`; + const endCommentType = getEndCommentType(commentType); for (let i = 0; i < lines.length; i++) { - if (lines[i].includes(startAnchor)) { - if (lineStart === -1) { - lineStart = i + 1; - } - anchorStack.push('anchor'); - } else if (lines[i].includes(endAnchor)) { - anchorStack.pop(); - if (anchorStack.length === 0) { + const startLineA = `${commentType}ANCHOR:${comment}${endCommentType}`; + const endLineA = `${commentType}ANCHOR_END:${comment}${endCommentType}`; + const startLineB = `${commentType}${comment}:example:start${endCommentType}`; + const endLineB = `${commentType}${comment}:example:end${endCommentType}`; + const cleanLine = lines[i].replace(/\s+/g, ''); + const start = cleanLine === startLineA || cleanLine === startLineB; + if (start) { + lineStart = i + 1; + } else { + const end = cleanLine === endLineA || cleanLine === endLineB; + if (end) { lineEnd = i; - break; } } } @@ -104,19 +90,25 @@ function extractCommentBlock( lineEnd = lines.length; } - let newLines = lines.slice(lineStart, lineEnd); - newLines = newLines.filter((line) => !line.includes('ANCHOR')); + const newLines = lines.slice(lineStart, lineEnd); - // Dedent the lines here: - const toDedent = minWhitespace(newLines); - if (toDedent > 0) { - newLines = dedent(newLines, toDedent); - } + const minIndentation = newLines.reduce((min, line) => { + const currentIndent = line.search(/\S|$/); + if (currentIndent > 0) { + return Math.min(min, currentIndent); + } + return min; + }, Infinity); - const linesContent = newLines.join('\n').replace(/\n{3,}/g, '\n\n'); + const linesContent = newLines + .filter((line) => !line.includes('ANCHOR')) + .map((line) => + minIndentation < Infinity ? line.substring(minIndentation) : line + ) + .join('\n'); return { - content: linesContent.trim(), + content: linesContent, lineStart, lineEnd, }; @@ -165,18 +157,6 @@ function extractTestCase(source: string, testCase: string) { }; } -function minWhitespace(lines: string[]): number { - return lines - .filter((line) => line.trim() !== '') // ignore blank lines - .map((line) => line.match(/^(\s*)/)[0].length) - .reduce((min, curr) => Math.min(min, curr), Infinity); -} - -function dedent(lines: string[], amount: number): string[] { - const regex = new RegExp(`^\\s{${amount}}`); - return lines.map((line) => line.replace(regex, '')); -} - export function codeImport() { return function transformer(tree: Root, file: any) { const rootDir = process.cwd();