Skip to content

Commit d37dac6

Browse files
joshspicerCopilot
andauthored
only show TODO code lens on lines that start with a comment token (#7962)
* Change default value for codingAgent.codeLens setting While #7962 is under discussion * [WIP] Fix test for codingAgent.codeLens setting (#7982) * Initial plan * Fix IssueTodoProvider test by enabling codeLens setting Co-authored-by: joshspicer <23246594+joshspicer@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: joshspicer <23246594+joshspicer@users.noreply.github.com> * check isComment first * swap order of checks * workaround for test --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
1 parent 11ac6f4 commit d37dac6

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

src/issues/issueTodoProvider.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as vscode from 'vscode';
7-
import { MAX_LINE_LENGTH } from './util';
7+
import { isComment, MAX_LINE_LENGTH } from './util';
88
import { CODING_AGENT, CREATE_ISSUE_TRIGGERS, ISSUES_SETTINGS_NAMESPACE, SHOW_CODE_LENS } from '../common/settingKeys';
99
import { escapeRegExp } from '../common/utils';
1010
import { CopilotRemoteAgentManager } from '../github/copilotRemoteAgent';
@@ -114,19 +114,24 @@ export class IssueTodoProvider implements vscode.CodeActionProvider, vscode.Code
114114

115115
const codeLenses: vscode.CodeLens[] = [];
116116
for (let lineNumber = 0; lineNumber < document.lineCount; lineNumber++) {
117-
const line = document.lineAt(lineNumber).text;
117+
const textLine = document.lineAt(lineNumber);
118+
const { text: line, firstNonWhitespaceCharacterIndex } = textLine;
118119
const todoInfo = this.findTodoInLine(line);
119-
if (todoInfo) {
120-
const { match, search, insertIndex } = todoInfo;
121-
const range = new vscode.Range(lineNumber, search, lineNumber, search + match[0].length);
122-
if (this.copilotRemoteAgentManager && (await this.copilotRemoteAgentManager.isAvailable())) {
123-
const startAgentCodeLens = new vscode.CodeLens(range, {
124-
title: vscode.l10n.t('Delegate to coding agent'),
125-
command: 'issue.startCodingAgentFromTodo',
126-
arguments: [{ document, lineNumber, line, insertIndex, range }],
127-
});
128-
codeLenses.push(startAgentCodeLens);
129-
}
120+
if (!todoInfo) {
121+
continue;
122+
}
123+
if (!(await isComment(document, new vscode.Position(lineNumber, firstNonWhitespaceCharacterIndex)))) {
124+
continue;
125+
}
126+
const { match, search, insertIndex } = todoInfo;
127+
const range = new vscode.Range(lineNumber, search, lineNumber, search + match[0].length);
128+
if (this.copilotRemoteAgentManager && (await this.copilotRemoteAgentManager.isAvailable())) {
129+
const startAgentCodeLens = new vscode.CodeLens(range, {
130+
title: vscode.l10n.t('Delegate to coding agent'),
131+
command: 'issue.startCodingAgentFromTodo',
132+
arguments: [{ document, lineNumber, line, insertIndex, range }],
133+
});
134+
codeLenses.push(startAgentCodeLens);
130135
}
131136
}
132137
return codeLenses;

src/test/issues/issueTodoProvider.test.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,31 @@ import * as vscode from 'vscode';
88
import { IssueTodoProvider } from '../../issues/issueTodoProvider';
99
import { CopilotRemoteAgentManager } from '../../github/copilotRemoteAgent';
1010
import { CODING_AGENT, CREATE_ISSUE_TRIGGERS, ISSUES_SETTINGS_NAMESPACE, SHOW_CODE_LENS } from '../../common/settingKeys';
11+
import * as issueUtil from '../../issues/util';
1112

1213
const mockCopilotManager: Partial<CopilotRemoteAgentManager> = {
1314
isAvailable: () => Promise.resolve(true)
1415
}
1516

1617
describe('IssueTodoProvider', function () {
18+
// Mock isComment
19+
// We don't have a real 'vscode.TextDocument' in these tests, which
20+
// causes 'vscode.languages.getTokenInformationAtPosition' to throw.
21+
const originalIsComment = issueUtil.isComment;
22+
before(() => {
23+
(issueUtil as any).isComment = async (document: vscode.TextDocument, position: vscode.Position) => {
24+
try {
25+
const lineText = document.lineAt(position.line).text;
26+
return lineText.trim().startsWith('//');
27+
} catch {
28+
return false;
29+
}
30+
};
31+
});
32+
after(() => {
33+
(issueUtil as any).isComment = originalIsComment;
34+
});
35+
1736
it('should provide both actions when CopilotRemoteAgentManager is available', async function () {
1837
const mockContext = {
1938
subscriptions: []
@@ -119,10 +138,12 @@ describe('IssueTodoProvider', function () {
119138

120139
// Create a mock document with TODO comment
121140
const document = {
122-
lineAt: (line: number) => ({
123-
text: line === 1 ? ' // TODO: Fix this' : 'function test() {}'
124-
}),
125-
lineCount: 4
141+
lineAt: (lineNo: number) => ({
142+
text: lineNo === 1 ? ' // TODO: Fix this' : 'function test() {}',
143+
firstNonWhitespaceCharacterIndex: lineNo === 1 ? 2 : 0,
144+
} as vscode.TextLine),
145+
lineCount: 4,
146+
languageId: 'javascript'
126147
} as vscode.TextDocument;
127148

128149
const originalGetConfiguration = vscode.workspace.getConfiguration;

0 commit comments

Comments
 (0)