Skip to content

Commit e88e0c0

Browse files
Copilotjoshspicer
andcommitted
Refactor: Extract common TODO detection logic to reduce duplication
Co-authored-by: joshspicer <23246594+joshspicer@users.noreply.github.com>
1 parent 008c532 commit e88e0c0

File tree

1 file changed

+62
-60
lines changed

1 file changed

+62
-60
lines changed

src/issues/issueTodoProvider.ts

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ export class IssueTodoProvider implements vscode.CodeActionProvider, vscode.Code
3030
this.expression = triggers.length > 0 ? new RegExp(triggers.map(trigger => escapeRegExp(trigger)).join('|')) : undefined;
3131
}
3232

33+
private findTodoInLine(lineNumber: number, line: string): { match: RegExpMatchArray; search: number; insertIndex: number } | undefined {
34+
const truncatedLine = line.substring(0, MAX_LINE_LENGTH);
35+
const matches = truncatedLine.match(ISSUE_OR_URL_EXPRESSION);
36+
if (matches) {
37+
return undefined;
38+
}
39+
const match = truncatedLine.match(this.expression!);
40+
const search = match?.index ?? -1;
41+
if (search >= 0 && match) {
42+
const indexOfWhiteSpace = truncatedLine.substring(search).search(/\s/);
43+
const insertIndex =
44+
search +
45+
(indexOfWhiteSpace > 0 ? indexOfWhiteSpace : truncatedLine.match(this.expression!)![0].length);
46+
return { match, search, insertIndex };
47+
}
48+
return undefined;
49+
}
50+
3351
async provideCodeActions(
3452
document: vscode.TextDocument,
3553
range: vscode.Range | vscode.Selection,
@@ -43,45 +61,37 @@ export class IssueTodoProvider implements vscode.CodeActionProvider, vscode.Code
4361
let lineNumber = range.start.line;
4462
do {
4563
const line = document.lineAt(lineNumber).text;
46-
const truncatedLine = line.substring(0, MAX_LINE_LENGTH);
47-
const matches = truncatedLine.match(ISSUE_OR_URL_EXPRESSION);
48-
if (!matches) {
49-
const match = truncatedLine.match(this.expression);
50-
const search = match?.index ?? -1;
51-
if (search >= 0 && match) {
52-
// Create GitHub Issue action
53-
const createIssueAction: vscode.CodeAction = new vscode.CodeAction(
54-
vscode.l10n.t('Create GitHub Issue'),
64+
const todoInfo = this.findTodoInLine(lineNumber, line);
65+
if (todoInfo) {
66+
const { match, search, insertIndex } = todoInfo;
67+
// Create GitHub Issue action
68+
const createIssueAction: vscode.CodeAction = new vscode.CodeAction(
69+
vscode.l10n.t('Create GitHub Issue'),
70+
vscode.CodeActionKind.QuickFix,
71+
);
72+
createIssueAction.ranges = [new vscode.Range(lineNumber, search, lineNumber, search + match[0].length)];
73+
createIssueAction.command = {
74+
title: vscode.l10n.t('Create GitHub Issue'),
75+
command: 'issue.createIssueFromSelection',
76+
arguments: [{ document, lineNumber, line, insertIndex, range }],
77+
};
78+
codeActions.push(createIssueAction);
79+
80+
// Start Coding Agent Session action (if copilot manager is available)
81+
if (this.copilotRemoteAgentManager) {
82+
const startAgentAction: vscode.CodeAction = new vscode.CodeAction(
83+
vscode.l10n.t('Delegate to coding agent'),
5584
vscode.CodeActionKind.QuickFix,
5685
);
57-
createIssueAction.ranges = [new vscode.Range(lineNumber, search, lineNumber, search + match[0].length)];
58-
const indexOfWhiteSpace = truncatedLine.substring(search).search(/\s/);
59-
const insertIndex =
60-
search +
61-
(indexOfWhiteSpace > 0 ? indexOfWhiteSpace : truncatedLine.match(this.expression)![0].length);
62-
createIssueAction.command = {
63-
title: vscode.l10n.t('Create GitHub Issue'),
64-
command: 'issue.createIssueFromSelection',
86+
startAgentAction.ranges = [new vscode.Range(lineNumber, search, lineNumber, search + match[0].length)];
87+
startAgentAction.command = {
88+
title: vscode.l10n.t('Delegate to coding agent'),
89+
command: 'issue.startCodingAgentFromTodo',
6590
arguments: [{ document, lineNumber, line, insertIndex, range }],
6691
};
67-
codeActions.push(createIssueAction);
68-
69-
// Start Coding Agent Session action (if copilot manager is available)
70-
if (this.copilotRemoteAgentManager) {
71-
const startAgentAction: vscode.CodeAction = new vscode.CodeAction(
72-
vscode.l10n.t('Delegate to coding agent'),
73-
vscode.CodeActionKind.QuickFix,
74-
);
75-
startAgentAction.ranges = [new vscode.Range(lineNumber, search, lineNumber, search + match[0].length)];
76-
startAgentAction.command = {
77-
title: vscode.l10n.t('Delegate to coding agent'),
78-
command: 'issue.startCodingAgentFromTodo',
79-
arguments: [{ document, lineNumber, line, insertIndex, range }],
80-
};
81-
codeActions.push(startAgentAction);
82-
}
83-
break;
92+
codeActions.push(startAgentAction);
8493
}
94+
break;
8595
}
8696
lineNumber++;
8797
} while (range.end.line >= lineNumber);
@@ -99,35 +109,27 @@ export class IssueTodoProvider implements vscode.CodeActionProvider, vscode.Code
99109
const codeLenses: vscode.CodeLens[] = [];
100110
for (let lineNumber = 0; lineNumber < document.lineCount; lineNumber++) {
101111
const line = document.lineAt(lineNumber).text;
102-
const truncatedLine = line.substring(0, MAX_LINE_LENGTH);
103-
const matches = truncatedLine.match(ISSUE_OR_URL_EXPRESSION);
104-
if (!matches) {
105-
const match = truncatedLine.match(this.expression);
106-
const search = match?.index ?? -1;
107-
if (search >= 0 && match) {
108-
const range = new vscode.Range(lineNumber, search, lineNumber, search + match[0].length);
109-
const indexOfWhiteSpace = truncatedLine.substring(search).search(/\s/);
110-
const insertIndex =
111-
search +
112-
(indexOfWhiteSpace > 0 ? indexOfWhiteSpace : truncatedLine.match(this.expression)![0].length);
112+
const todoInfo = this.findTodoInLine(lineNumber, line);
113+
if (todoInfo) {
114+
const { match, search, insertIndex } = todoInfo;
115+
const range = new vscode.Range(lineNumber, search, lineNumber, search + match[0].length);
113116

114-
// Create GitHub Issue CodeLens
115-
const createIssueCodeLens = new vscode.CodeLens(range, {
116-
title: vscode.l10n.t('Create GitHub Issue'),
117-
command: 'issue.createIssueFromSelection',
117+
// Create GitHub Issue CodeLens
118+
const createIssueCodeLens = new vscode.CodeLens(range, {
119+
title: vscode.l10n.t('Create GitHub Issue'),
120+
command: 'issue.createIssueFromSelection',
121+
arguments: [{ document, lineNumber, line, insertIndex, range }],
122+
});
123+
codeLenses.push(createIssueCodeLens);
124+
125+
// Delegate to coding agent CodeLens (if copilot manager is available)
126+
if (this.copilotRemoteAgentManager) {
127+
const startAgentCodeLens = new vscode.CodeLens(range, {
128+
title: vscode.l10n.t('Delegate to coding agent'),
129+
command: 'issue.startCodingAgentFromTodo',
118130
arguments: [{ document, lineNumber, line, insertIndex, range }],
119131
});
120-
codeLenses.push(createIssueCodeLens);
121-
122-
// Delegate to coding agent CodeLens (if copilot manager is available)
123-
if (this.copilotRemoteAgentManager) {
124-
const startAgentCodeLens = new vscode.CodeLens(range, {
125-
title: vscode.l10n.t('Delegate to coding agent'),
126-
command: 'issue.startCodingAgentFromTodo',
127-
arguments: [{ document, lineNumber, line, insertIndex, range }],
128-
});
129-
codeLenses.push(startAgentCodeLens);
130-
}
132+
codeLenses.push(startAgentCodeLens);
131133
}
132134
}
133135
}

0 commit comments

Comments
 (0)