Path: blob/main/extensions/merge-conflict/src/codelensProvider.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as vscode from 'vscode';6import * as interfaces from './interfaces';78export default class MergeConflictCodeLensProvider implements vscode.CodeLensProvider, vscode.Disposable {9private codeLensRegistrationHandle?: vscode.Disposable | null;10private config?: interfaces.IExtensionConfiguration;11private tracker: interfaces.IDocumentMergeConflictTracker;1213constructor(trackerService: interfaces.IDocumentMergeConflictTrackerService) {14this.tracker = trackerService.createTracker('codelens');15}1617begin(config: interfaces.IExtensionConfiguration) {18this.config = config;1920if (this.config.enableCodeLens) {21this.registerCodeLensProvider();22}23}2425configurationUpdated(updatedConfig: interfaces.IExtensionConfiguration) {2627if (updatedConfig.enableCodeLens === false && this.codeLensRegistrationHandle) {28this.codeLensRegistrationHandle.dispose();29this.codeLensRegistrationHandle = null;30}31else if (updatedConfig.enableCodeLens === true && !this.codeLensRegistrationHandle) {32this.registerCodeLensProvider();33}3435this.config = updatedConfig;36}373839dispose() {40if (this.codeLensRegistrationHandle) {41this.codeLensRegistrationHandle.dispose();42this.codeLensRegistrationHandle = null;43}44}4546async provideCodeLenses(document: vscode.TextDocument, _token: vscode.CancellationToken): Promise<vscode.CodeLens[] | null> {4748if (!this.config || !this.config.enableCodeLens) {49return null;50}5152const conflicts = await this.tracker.getConflicts(document);53const conflictsCount = conflicts?.length ?? 0;54vscode.commands.executeCommand('setContext', 'mergeConflictsCount', conflictsCount);5556if (!conflictsCount) {57return null;58}5960const items: vscode.CodeLens[] = [];6162conflicts.forEach(conflict => {63const acceptCurrentCommand: vscode.Command = {64command: 'merge-conflict.accept.current',65title: vscode.l10n.t("Accept Current Change"),66arguments: ['known-conflict', conflict]67};6869const acceptIncomingCommand: vscode.Command = {70command: 'merge-conflict.accept.incoming',71title: vscode.l10n.t("Accept Incoming Change"),72arguments: ['known-conflict', conflict]73};7475const acceptBothCommand: vscode.Command = {76command: 'merge-conflict.accept.both',77title: vscode.l10n.t("Accept Both Changes"),78arguments: ['known-conflict', conflict]79};8081const diffCommand: vscode.Command = {82command: 'merge-conflict.compare',83title: vscode.l10n.t("Compare Changes"),84arguments: [conflict]85};8687const range = document.lineAt(conflict.range.start.line).range;88items.push(89new vscode.CodeLens(range, acceptCurrentCommand),90new vscode.CodeLens(range, acceptIncomingCommand),91new vscode.CodeLens(range, acceptBothCommand),92new vscode.CodeLens(range, diffCommand)93);94});9596return items;97}9899private registerCodeLensProvider() {100this.codeLensRegistrationHandle = vscode.languages.registerCodeLensProvider([101{ scheme: 'file' },102{ scheme: 'vscode-vfs' },103{ scheme: 'untitled' },104{ scheme: 'vscode-userdata' },105], this);106}107}108109110