Skip to content

Commit

Permalink
refact: Partially implement new comment/docstring removal algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
thecurz committed Aug 30, 2024
1 parent 4abd583 commit 56f88a3
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions src/core/file/fileManipulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,50 @@ class StripCommentsManipulator extends BaseManipulator {

class PythonManipulator extends BaseManipulator {
removeComments(content: string): string {
// Remove single-line comments
const result = content.replace(/(?<!\\)#.*$/gm, '');

if (!content)
return '';
const lines = content.split('\n');

let result = '';
let buffer = '';
let quoteType = "";
let tripleQuotes = 0;

const doubleQuoteRegex = /^\s*(?:""")\s*(?:\n)?[\s\S]*?(?<!("""))(?:""")/gm;
const singleQuoteRegex = /^\s*(?:''')\s*(?:\n)?[\s\S]*?(?<!('''))(?:''')/gm;

const sz = lines.length;
for (let i = 0; i < sz; i++) {
const line = (lines[i] + (i != sz - 1 ? '\n' : ''));
buffer += line;
if (quoteType === "") {
const indexSingle = line.indexOf("'''");
const indexDouble = line.indexOf('"""');
if (indexSingle !== -1 && (indexDouble === -1 || indexSingle < indexDouble)) {
quoteType = "'''";
} else if (indexDouble !== -1 && (indexSingle === -1 || indexDouble < indexSingle)) {
quoteType = '"""';
}
}
if (quoteType === "'''") {
tripleQuotes += (line.match(/'''/g) || []).length;
}
if (quoteType === '"""') {
tripleQuotes += (line.match(/"""/g) || []).length;
}

if (tripleQuotes % 2 === 0) {
const docstringRegex = quoteType === '"""' ? doubleQuoteRegex : singleQuoteRegex;
buffer = buffer.replace(docstringRegex, '');
result += buffer;
buffer = '';
tripleQuotes = 0;
quoteType = "";
}
}

result += buffer;
result = result.replace(/(?<!\\)#.*$/gm, '');
return rtrimLines(result);
}
}
Expand Down

0 comments on commit 56f88a3

Please sign in to comment.