Skip to content

Commit

Permalink
Fixing indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
calldelegation committed Nov 7, 2023
1 parent 2465cd5 commit 06bcbb6
Showing 1 changed file with 31 additions and 51 deletions.
82 changes: 31 additions & 51 deletions src/lib/plugins/code-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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;
}
}
}
Expand All @@ -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,
};
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 06bcbb6

Please sign in to comment.