diff --git a/src/core/file/fileManipulate.ts b/src/core/file/fileManipulate.ts index f5384fd..9fc85a1 100644 --- a/src/core/file/fileManipulate.ts +++ b/src/core/file/fileManipulate.ts @@ -6,7 +6,12 @@ interface FileManipulator { removeEmptyLines(content: string): string; } -const rtrimLines = (content: string): string => content.replace(/[ \t]+$/gm, ''); +const rtrimLines = (content: string): string => + content + .split('\n') + .map((line) => line.trimEnd()) + .join('\n'); + class BaseManipulator implements FileManipulator { removeComments(content: string): string { @@ -40,15 +45,108 @@ class StripCommentsManipulator extends BaseManipulator { class PythonManipulator extends BaseManipulator { removeDocStrings(content: string): string { - const docstringRegex = /(?:^|\n)\s*(?:'{3}|"{3})[\s\S]*?(?:'{3}|"{3})/gm; - return content.replace(docstringRegex, ''); + if (!content) return ''; + const lines = content.split('\n'); + + let result = ''; + + let buffer = ''; + let quoteType: '' | "'" | '"' = ''; + let tripleQuotes = 0; + + const doubleQuoteRegex = /^\s*(? { + return pairs.some(([start, end]) => hashIndex > start && hashIndex < end); + }; + + let result = ''; + const pairs: [number, number][] = []; + let prevQuote = 0; + while (prevQuote < content.length) { + const openingQuote = content.slice(prevQuote + 1).search(/(? manipulator.removeComments(acc), content); + } +} + const manipulators: Record = { '.c': new StripCommentsManipulator('c'), '.cs': new StripCommentsManipulator('csharp'), @@ -96,15 +207,7 @@ const manipulators: Record = { ), }; -const getOrCreateManipulator = (ext: string): FileManipulator => { - if (!manipulators[ext]) { - manipulators[ext] = - ext === '.py' ? new PythonManipulator() : new StripCommentsManipulator('javascript'); - } - return manipulators[ext]; -}; - export const getFileManipulator = (filePath: string): FileManipulator | null => { const ext = path.extname(filePath); - return getOrCreateManipulator(ext) || null; + return manipulators[ext] || null; };