|
1 | | -/*--------------------------------------------------------------------------------------------- |
2 | | - * Copyright (c) Microsoft Corporation. All rights reserved. |
3 | | - * Licensed under the MIT License. See License.txt in the project root for license information. |
4 | | - *--------------------------------------------------------------------------------------------*/ |
5 | | - |
6 | | -import * as vscode from 'vscode'; |
7 | | -import { CREATE_ISSUE_TRIGGERS, ISSUES_SETTINGS_NAMESPACE } from '../common/settingKeys'; |
8 | | -import { escapeRegExp } from '../common/utils'; |
9 | | -import { CopilotRemoteAgentManager } from '../github/copilotRemoteAgent'; |
10 | | -import { ISSUE_OR_URL_EXPRESSION } from '../github/utils'; |
11 | | -import { MAX_LINE_LENGTH } from './util'; |
12 | | - |
13 | | -export class IssueTodoProvider implements vscode.CodeActionProvider { |
14 | | - private expression: RegExp | undefined; |
15 | | - |
16 | | - constructor( |
17 | | - context: vscode.ExtensionContext, |
18 | | - private copilotRemoteAgentManager: CopilotRemoteAgentManager |
19 | | - ) { |
20 | | - context.subscriptions.push( |
21 | | - vscode.workspace.onDidChangeConfiguration(() => { |
22 | | - this.updateTriggers(); |
23 | | - }), |
24 | | - ); |
25 | | - this.updateTriggers(); |
26 | | - } |
27 | | - |
28 | | - private updateTriggers() { |
29 | | - const triggers = vscode.workspace.getConfiguration(ISSUES_SETTINGS_NAMESPACE).get(CREATE_ISSUE_TRIGGERS, []); |
30 | | - this.expression = triggers.length > 0 ? new RegExp(triggers.map(trigger => escapeRegExp(trigger)).join('|')) : undefined; |
31 | | - } |
32 | | - |
33 | | - async provideCodeActions( |
34 | | - document: vscode.TextDocument, |
35 | | - range: vscode.Range | vscode.Selection, |
36 | | - context: vscode.CodeActionContext, |
37 | | - _token: vscode.CancellationToken, |
38 | | - ): Promise<vscode.CodeAction[]> { |
39 | | - if (this.expression === undefined || (context.only && context.only !== vscode.CodeActionKind.QuickFix)) { |
40 | | - return []; |
41 | | - } |
42 | | - const codeActions: vscode.CodeAction[] = []; |
43 | | - let lineNumber = range.start.line; |
44 | | - do { |
45 | | - 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'), |
55 | | - vscode.CodeActionKind.QuickFix, |
56 | | - ); |
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', |
65 | | - arguments: [{ document, lineNumber, line, insertIndex, range }], |
66 | | - }; |
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('Start Coding Agent Session'), |
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('Start Coding Agent Session'), |
78 | | - command: 'issue.startCodingAgentFromTodo', |
79 | | - arguments: [{ document, lineNumber, line, insertIndex, range }], |
80 | | - }; |
81 | | - codeActions.push(startAgentAction); |
82 | | - } |
83 | | - break; |
84 | | - } |
85 | | - } |
86 | | - lineNumber++; |
87 | | - } while (range.end.line >= lineNumber); |
88 | | - return codeActions; |
89 | | - } |
90 | | -} |
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import { CREATE_ISSUE_TRIGGERS, ISSUES_SETTINGS_NAMESPACE } from '../common/settingKeys'; |
| 8 | +import { escapeRegExp } from '../common/utils'; |
| 9 | +import { CopilotRemoteAgentManager } from '../github/copilotRemoteAgent'; |
| 10 | +import { ISSUE_OR_URL_EXPRESSION } from '../github/utils'; |
| 11 | +import { MAX_LINE_LENGTH } from './util'; |
| 12 | + |
| 13 | +export class IssueTodoProvider implements vscode.CodeActionProvider, vscode.CodeLensProvider { |
| 14 | + private expression: RegExp | undefined; |
| 15 | + |
| 16 | + constructor( |
| 17 | + context: vscode.ExtensionContext, |
| 18 | + private copilotRemoteAgentManager: CopilotRemoteAgentManager |
| 19 | + ) { |
| 20 | + context.subscriptions.push( |
| 21 | + vscode.workspace.onDidChangeConfiguration(() => { |
| 22 | + this.updateTriggers(); |
| 23 | + }), |
| 24 | + ); |
| 25 | + this.updateTriggers(); |
| 26 | + } |
| 27 | + |
| 28 | + private updateTriggers() { |
| 29 | + const triggers = vscode.workspace.getConfiguration(ISSUES_SETTINGS_NAMESPACE).get(CREATE_ISSUE_TRIGGERS, []); |
| 30 | + this.expression = triggers.length > 0 ? new RegExp(triggers.map(trigger => escapeRegExp(trigger)).join('|')) : undefined; |
| 31 | + } |
| 32 | + |
| 33 | + async provideCodeActions( |
| 34 | + document: vscode.TextDocument, |
| 35 | + range: vscode.Range | vscode.Selection, |
| 36 | + context: vscode.CodeActionContext, |
| 37 | + _token: vscode.CancellationToken, |
| 38 | + ): Promise<vscode.CodeAction[]> { |
| 39 | + if (this.expression === undefined || (context.only && context.only !== vscode.CodeActionKind.QuickFix)) { |
| 40 | + return []; |
| 41 | + } |
| 42 | + const codeActions: vscode.CodeAction[] = []; |
| 43 | + let lineNumber = range.start.line; |
| 44 | + do { |
| 45 | + 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'), |
| 55 | + vscode.CodeActionKind.QuickFix, |
| 56 | + ); |
| 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', |
| 65 | + arguments: [{ document, lineNumber, line, insertIndex, range }], |
| 66 | + }; |
| 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('Start Coding Agent Session'), |
| 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('Start Coding Agent Session'), |
| 78 | + command: 'issue.startCodingAgentFromTodo', |
| 79 | + arguments: [{ document, lineNumber, line, insertIndex, range }], |
| 80 | + }; |
| 81 | + codeActions.push(startAgentAction); |
| 82 | + } |
| 83 | + break; |
| 84 | + } |
| 85 | + } |
| 86 | + lineNumber++; |
| 87 | + } while (range.end.line >= lineNumber); |
| 88 | + return codeActions; |
| 89 | + } |
| 90 | + |
| 91 | + async provideCodeLenses( |
| 92 | + document: vscode.TextDocument, |
| 93 | + _token: vscode.CancellationToken, |
| 94 | + ): Promise<vscode.CodeLens[]> { |
| 95 | + if (this.expression === undefined) { |
| 96 | + return []; |
| 97 | + } |
| 98 | + |
| 99 | + const codeLenses: vscode.CodeLens[] = []; |
| 100 | + for (let lineNumber = 0; lineNumber < document.lineCount; lineNumber++) { |
| 101 | + 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 | + |
| 105 | + if (!matches) { |
| 106 | + const match = truncatedLine.match(this.expression); |
| 107 | + const search = match?.index ?? -1; |
| 108 | + if (search >= 0 && match) { |
| 109 | + const indexOfWhiteSpace = truncatedLine.substring(search).search(/\s/); |
| 110 | + const insertIndex = |
| 111 | + search + |
| 112 | + (indexOfWhiteSpace > 0 ? indexOfWhiteSpace : truncatedLine.match(this.expression)![0].length); |
| 113 | + |
| 114 | + const range = new vscode.Range(lineNumber, search, lineNumber, search + match[0].length); |
| 115 | + |
| 116 | + // Only show "Start Coding Agent Session" code lens if copilot manager is available |
| 117 | + if (this.copilotRemoteAgentManager) { |
| 118 | + const startAgentCodeLens = new vscode.CodeLens(range, { |
| 119 | + title: vscode.l10n.t('Start Coding Agent Session'), |
| 120 | + command: 'issue.startCodingAgentFromTodo', |
| 121 | + arguments: [{ document, lineNumber, line, insertIndex, range }], |
| 122 | + }); |
| 123 | + codeLenses.push(startAgentCodeLens); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + return codeLenses; |
| 129 | + } |
| 130 | + |
| 131 | + resolveCodeLens( |
| 132 | + codeLens: vscode.CodeLens, |
| 133 | + _token: vscode.CancellationToken, |
| 134 | + ): vscode.ProviderResult<vscode.CodeLens> { |
| 135 | + // Code lens is already resolved in provideCodeLenses |
| 136 | + return codeLens; |
| 137 | + } |
| 138 | +} |
0 commit comments