UNPKG

refakts

Version:

TypeScript refactoring tool built for AI coding agents to perform precise refactoring operations via command line instead of requiring complete code regeneration.

134 lines 5.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SelectPatternMatcher = void 0; class SelectPatternMatcher { findMatches(content, pattern) { const lines = content.split('\n'); if (this.isMultilinePattern(pattern)) { return this.findMultilineMatches(content, pattern, lines); } return this.findRegexMatches(lines, pattern); } isMultilinePattern(pattern) { return pattern.source.includes('\\s') || pattern.source.includes('\\S'); } findMultilineMatches(content, pattern, lines) { const matches = []; let match; while ((match = pattern.exec(content)) !== null) { this.processMultilineMatch(match, content, lines, matches); } return matches; } processMultilineMatch(match, content, lines, matches) { const selectMatch = this.createMultilineMatch(match, content, lines); if (selectMatch && !this.isMatchInComment(selectMatch, content)) { matches.push(selectMatch); } } isMatchInComment(selectMatch, content) { const beforeMatch = content.substring(0, this.getIndexFromLineColumn(content, selectMatch.line, selectMatch.column)); const commentStartIndex = beforeMatch.lastIndexOf('/**'); const commentEndIndex = beforeMatch.lastIndexOf('*/'); return commentStartIndex > commentEndIndex; } getIndexFromLineColumn(content, line, column) { const lines = content.split('\n'); let index = 0; for (let i = 0; i < line - 1; i++) { index += lines[i].length + 1; } return index + column - 1; } createMultilineMatch(match, content, lines) { const positions = this.getMatchPositions(match, content); if (!positions) return null; const { textToUse, adjustedStartPos } = this.extractMultilineMatchDetails(match, positions.startPos, content); return this.buildMultilineSelectMatch(adjustedStartPos, positions.endPos, textToUse, lines); } getMatchPositions(match, content) { const startIndex = match.index; const endIndex = match.index + match[0].length; const startPos = this.getLineColumnFromIndex(content, startIndex); const endPos = this.getLineColumnFromIndex(content, endIndex); return startPos && endPos ? { startPos, endPos } : null; } buildMultilineSelectMatch(adjustedStartPos, endPos, textToUse, lines) { return { line: adjustedStartPos.line, column: adjustedStartPos.column, endLine: endPos.line, endColumn: endPos.column, text: textToUse, fullLine: lines[adjustedStartPos.line - 1] || '' }; } getLineColumnFromIndex(content, index) { const beforeIndex = content.substring(0, index); const lines = beforeIndex.split('\n'); const line = lines.length; const column = lines[lines.length - 1].length + 1; return { line, column }; } extractMultilineMatchDetails(match, startPos, content) { const hasCapture = match.length > 1 && match[1] !== undefined; const textToUse = hasCapture ? match[1] : match[0]; const adjustedStartPos = this.calculateAdjustedStartPos(match, hasCapture, startPos, content); return { textToUse, adjustedStartPos }; } calculateAdjustedStartPos(match, hasCapture, startPos, content) { if (hasCapture) { const captureStart = match.index + match[0].indexOf(match[1]); const adjustedStartPos = this.getLineColumnFromIndex(content, captureStart); return adjustedStartPos || startPos; } return startPos; } findRegexMatches(lines, pattern) { const matches = []; for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) { this.processLineForMatches(lines[lineIndex], lineIndex, pattern, matches); } return matches; } processLineForMatches(line, lineIndex, pattern, matches) { if (this.isCommentLine(line)) { return; } this.extractMatchesFromLine(line, lineIndex, pattern, matches); } isCommentLine(line) { const trimmedLine = line.trim(); return trimmedLine.startsWith('//') || trimmedLine.startsWith('*') || trimmedLine.startsWith('/*'); } extractMatchesFromLine(line, lineIndex, pattern, matches) { let match; pattern.lastIndex = 0; while ((match = pattern.exec(line)) !== null) { matches.push(this.createSelectMatch(match, lineIndex, line)); } } createSelectMatch(match, lineIndex, line) { const { textToUse, startIndex } = this.extractMatchDetails(match); return this.buildSelectMatch(textToUse, startIndex, lineIndex, line); } extractMatchDetails(match) { const hasCapture = match.length > 1 && match[1] !== undefined; const textToUse = hasCapture ? match[1] : match[0]; const startIndex = hasCapture ? match.index + match[0].indexOf(match[1]) : match.index; return { textToUse, startIndex }; } buildSelectMatch(textToUse, startIndex, lineIndex, line) { return { line: lineIndex + 1, column: startIndex + 1, endLine: lineIndex + 1, endColumn: startIndex + textToUse.length + 1, text: textToUse, fullLine: line }; } } exports.SelectPatternMatcher = SelectPatternMatcher; //# sourceMappingURL=pattern-matcher.js.map