|
| 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 { StateManager } from './stateManager'; |
| 8 | +import { getIssue } from './util'; |
| 9 | +import { CREATE_ISSUE_TRIGGERS, ISSUES_SETTINGS_NAMESPACE } from '../common/settingKeys'; |
| 10 | +import { escapeRegExp } from '../common/utils'; |
| 11 | +import { RepositoriesManager } from '../github/repositoriesManager'; |
| 12 | +import { ISSUE_OR_URL_EXPRESSION, parseIssueExpressionOutput } from '../github/utils'; |
| 13 | + |
| 14 | +export class IssueTodoDiagnosticProvider { |
| 15 | + private diagnosticCollection: vscode.DiagnosticCollection; |
| 16 | + private expression: RegExp | undefined; |
| 17 | + |
| 18 | + constructor( |
| 19 | + context: vscode.ExtensionContext, |
| 20 | + private manager: RepositoriesManager, |
| 21 | + private stateManager: StateManager, |
| 22 | + ) { |
| 23 | + this.diagnosticCollection = vscode.languages.createDiagnosticCollection('github-issues-todo'); |
| 24 | + context.subscriptions.push(this.diagnosticCollection); |
| 25 | + |
| 26 | + context.subscriptions.push( |
| 27 | + vscode.workspace.onDidChangeConfiguration(e => { |
| 28 | + if (e.affectsConfiguration(ISSUES_SETTINGS_NAMESPACE)) { |
| 29 | + this.updateTriggers(); |
| 30 | + } |
| 31 | + }), |
| 32 | + ); |
| 33 | + |
| 34 | + context.subscriptions.push( |
| 35 | + vscode.workspace.onDidOpenTextDocument(document => { |
| 36 | + this.validateDocument(document); |
| 37 | + }), |
| 38 | + ); |
| 39 | + |
| 40 | + context.subscriptions.push( |
| 41 | + vscode.workspace.onDidChangeTextDocument(e => { |
| 42 | + this.validateDocument(e.document); |
| 43 | + }), |
| 44 | + ); |
| 45 | + |
| 46 | + context.subscriptions.push( |
| 47 | + vscode.workspace.onDidCloseTextDocument(document => { |
| 48 | + this.diagnosticCollection.delete(document.uri); |
| 49 | + }), |
| 50 | + ); |
| 51 | + |
| 52 | + this.updateTriggers(); |
| 53 | + |
| 54 | + // Validate all currently open documents |
| 55 | + vscode.workspace.textDocuments.forEach(document => { |
| 56 | + this.validateDocument(document); |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + private updateTriggers() { |
| 61 | + const triggers = vscode.workspace.getConfiguration(ISSUES_SETTINGS_NAMESPACE).get(CREATE_ISSUE_TRIGGERS, []); |
| 62 | + this.expression = triggers.length > 0 ? new RegExp(triggers.map(trigger => escapeRegExp(trigger)).join('|')) : undefined; |
| 63 | + } |
| 64 | + |
| 65 | + private async validateDocument(document: vscode.TextDocument): Promise<void> { |
| 66 | + if (!this.expression) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + const folderManager = this.manager.getManagerForFile(document.uri); |
| 71 | + if (!folderManager) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + const diagnostics: vscode.Diagnostic[] = []; |
| 76 | + |
| 77 | + for (let lineNumber = 0; lineNumber < document.lineCount; lineNumber++) { |
| 78 | + const line = document.lineAt(lineNumber); |
| 79 | + const lineText = line.text; |
| 80 | + |
| 81 | + // Check if line contains a TODO trigger |
| 82 | + if (!this.expression.test(lineText)) { |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + // Look for issue references on this line |
| 87 | + const match = lineText.match(ISSUE_OR_URL_EXPRESSION); |
| 88 | + if (!match) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + const parsed = parseIssueExpressionOutput(match); |
| 93 | + if (!parsed) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + // Get the issue |
| 98 | + try { |
| 99 | + const issue = await getIssue(this.stateManager, folderManager, match[0], parsed); |
| 100 | + if (issue && issue.isClosed) { |
| 101 | + // Find the position of the issue reference |
| 102 | + const issueIndex = lineText.indexOf(match[0]); |
| 103 | + if (issueIndex !== -1) { |
| 104 | + const range = new vscode.Range( |
| 105 | + new vscode.Position(lineNumber, issueIndex), |
| 106 | + new vscode.Position(lineNumber, issueIndex + match[0].length) |
| 107 | + ); |
| 108 | + |
| 109 | + const diagnostic = new vscode.Diagnostic( |
| 110 | + range, |
| 111 | + vscode.l10n.t('Issue #{0} is closed. Consider removing this TODO comment.', issue.number), |
| 112 | + vscode.DiagnosticSeverity.Warning |
| 113 | + ); |
| 114 | + diagnostic.source = 'GitHub Issues'; |
| 115 | + diagnostics.push(diagnostic); |
| 116 | + } |
| 117 | + } |
| 118 | + } catch (error) { |
| 119 | + // Silently ignore errors fetching issues |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + this.diagnosticCollection.set(document.uri, diagnostics); |
| 124 | + } |
| 125 | +} |
0 commit comments