Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,683 changes: 2,683 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
"activationEvents": [
"*"
],
"extensionDependencies": [
"vscode.git"
],
"main": "./out/extension",
"contributes": {
"configuration": {
Expand All @@ -52,6 +55,9 @@
"diffSelectionWithClipboard": {
"type": "boolean"
},
"diffWithBranch": {
"type": "boolean"
},
"diffVisibleEditors": {
"type": "boolean"
},
Expand All @@ -63,6 +69,7 @@
"markSection1": true,
"markSection2AndTakeDiff": true,
"diffSelectionWithClipboard": true,
"diffWithBranch": true,
"diffVisibleEditors": true,
"togglePreComparisonTextNormalizationRules": true
},
Expand Down Expand Up @@ -131,6 +138,11 @@
"title": "Compare Text with Previous Selection",
"category": "PartialDiff"
},
{
"command": "extension.partialDiff.diffWithBranch",
"title": "Compare Open File with Branch",
"category": "PartialDiff"
},
{
"command": "extension.partialDiff.diffSelectionWithClipboard",
"title": "Compare Text with Clipboard",
Expand Down Expand Up @@ -173,6 +185,11 @@
"command": "extension.partialDiff.togglePreComparisonTextNormalizationRules",
"group": "2_partialdiff@5",
"when": "editorTextFocus && !config.partialDiff.hideCommandsOnContextMenu && config.partialDiff.commandsOnContextMenu.togglePreComparisonTextNormalizationRules"
},
{
"command": "extension.partialDiff.diffWithBranch",
"group": "2_partialdiff@6",
"when": "editorTextFocus && !config.partialDiff.hideCommandsOnContextMenu && config.partialDiff.commandsOnContextMenu.diffWithBranch"
}
]
}
Expand Down
29 changes: 29 additions & 0 deletions src/lib/adaptors/git.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as vscode from 'vscode';
import { Repository, Ref, Branch } from '../types/git.d';

export default class GitAdapter {
repo: Repository;
constructor(private readonly extensions: typeof vscode.extensions) {
const ext = this.extensions.getExtension('vscode.git')?.exports;
const api = ext.getAPI(1);
this.repo = api.repositories[0];
}

isGitRepo(): boolean {
return !!this.repo;
}

async show(branchName: string, fileDir: string): Promise<string> {
return this.repo.show(branchName, fileDir);
}

async allBranches(): Promise<Ref[]> {
return await this.repo.getBranches({
pattern: ''
});
}

async getBranch(name: string): Promise<Branch> {
return await this.repo.getBranch(name);
}
}
8 changes: 8 additions & 0 deletions src/lib/adaptors/text-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ export default class TextEditor {
return basename(this.vsEditor.document.fileName);
}

get fileUri(): string {
return this.vsEditor.document.fileName;
}

get viewColumn(): ViewColumn {
return this.vsEditor.viewColumn!;
}

get getText(): string {
return this.vsEditor.document.getText();
}

get selectedText(): string {
const validSelections = this.collectNonEmptySelections(this.vsEditor.selections);
return this.extractText(validSelections);
Expand Down
7 changes: 5 additions & 2 deletions src/lib/adaptors/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ export default class WindowAdaptor {
get visibleTextEditors(): TextEditor[] {
return this.window.visibleTextEditors.map((editor: VsTextEditor) => new TextEditor(editor));
}
get activeTextEditor(): any {
return this.window.activeTextEditor;
}

async showQuickPick<T extends QuickPickItem>(items: T[]): Promise<T[] | undefined> {
async showQuickPick<T extends QuickPickItem>(items: T[], canPickMany: boolean = true): Promise<T[] | undefined> {
// @ts-ignore
return this.window.showQuickPick(items, {canPickMany: true});
return this.window.showQuickPick(items, {canPickMany});
}

async showInformationMessage(message: string): Promise<string | undefined> {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/bootstrapper-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import SelectionInfoRegistry from './selection-info-registry';
import * as vscode from 'vscode';
import CommandAdaptor from './adaptors/command';
import WindowAdaptor from './adaptors/window';
import GitAdapter from './adaptors/git';
import {NullVsTelemetryReporter, VsTelemetryReporterCreator} from './telemetry-reporter';
import VsTelemetryReporter from 'vscode-extension-telemetry';

Expand All @@ -24,6 +25,7 @@ export default class BootstrapperFactory {
normalisationRuleStore,
commandAdaptor,
new WindowAdaptor(vscode.window),
new GitAdapter(vscode.extensions),
vscode.env.clipboard,
() => new Date()
);
Expand Down
5 changes: 5 additions & 0 deletions src/lib/bootstrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export default class Bootstrapper {
type: 'GENERAL',
command: this.commandFactory.createCompareVisibleEditorsCommand()
},
{
name: `${EXTENSION_NAMESPACE}.diffWithBranch`,
type: 'TEXT_EDITOR',
command: this.commandFactory.createCompareWithBranchCommand()
},
{
name: `${EXTENSION_NAMESPACE}.markSection1`,
type: 'TEXT_EDITOR',
Expand Down
40 changes: 40 additions & 0 deletions src/lib/branch-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import WindowAdaptor from './adaptors/window';
import { Ref } from './types/git.d';
import GitAdaptor from './adaptors/git';
import { QuickPickItem } from 'vscode';

export default class BranchManager {
constructor(private readonly gitAdaptor: GitAdaptor,
private readonly windowAdaptor: WindowAdaptor) {}

private toQuickPickItems(list: string[]): QuickPickItem[] {
return list.map((name, i)=> ({
label: name,
picked: false,
ruleIndex: i,
description: ''
}));
}

async getFileContent(branchName: string, fileUri: string): Promise<string> {
try {
return await this.gitAdaptor.show(branchName, fileUri);
} catch(e) {
this.windowAdaptor.showInformationMessage(`file does not exist on ${branchName} branch`);
return '';
}
}

async selectViaQuickPick(branchNames: string[]): Promise<string> {
const branch: QuickPickItem = await <any>this.windowAdaptor.showQuickPick(this.toQuickPickItems(branchNames), false);
return branch.label;
}

async getLocalBranchNames(): Promise<string[]> {
if (!this.gitAdaptor.isGitRepo()) {
this.windowAdaptor.showInformationMessage('Git repo not found!');
}
const branches: Ref[] = await this.gitAdaptor.allBranches();
return branches.map((i:Ref) => i.name || '');
}
}
25 changes: 25 additions & 0 deletions src/lib/command-factory.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import SaveText1Command from './commands/save-text-1';
import CompareSelectionWithText1Command from './commands/compare-selection-with-text1';
import CompareSelectionWithClipboardCommand from './commands/compare-selection-with-clipboard';
import CompareWithGitBranchCommand from './commands/compare-with-git-branch';
import CompareVisibleEditorsCommand from './commands/compare-visible-editors';
import DiffPresenter from './diff-presenter';
import BranchManager from './branch-manager';
import ToggleNormalisationRulesCommand from './commands/toggle-normalisation-rules';
import NormalisationRuleStore from './normalisation-rule-store';
import SelectionInfoRegistry from './selection-info-registry';
import CommandAdaptor from './adaptors/command';
import WindowAdaptor from './adaptors/window';
import GitAdaptor from './adaptors/git';
import {Command} from './commands/command';
import * as vscode from 'vscode';

export default class CommandFactory {
private diffPresenter?: DiffPresenter;
private branchManager?: BranchManager;

constructor(private readonly selectionInfoRegistry: SelectionInfoRegistry,
private readonly normalisationRuleStore: NormalisationRuleStore,
private readonly commandAdaptor: CommandAdaptor,
private readonly windowAdaptor: WindowAdaptor,
private readonly gitAdaptor: GitAdaptor,
private readonly clipboard: typeof vscode.env.clipboard,
private readonly getCurrentDate: () => Date) {
}
Expand All @@ -33,6 +38,14 @@ export default class CommandFactory {
);
}

createCompareWithBranchCommand(): Command {
return new CompareWithGitBranchCommand(
this.getDiffPresenter(),
this.selectionInfoRegistry,
this.getBranchManager()
);
}

createCompareSelectionWithClipboardCommand(): Command {
return new CompareSelectionWithClipboardCommand(
this.getDiffPresenter(),
Expand Down Expand Up @@ -61,6 +74,18 @@ export default class CommandFactory {
return this.diffPresenter;
}

private getBranchManager(): BranchManager {
this.branchManager = this.branchManager || this.createBranchManager();
return this.branchManager;
}

private createBranchManager(): BranchManager {
return new BranchManager(
this.gitAdaptor,
this.windowAdaptor
);
}

private createDiffPresenter(): DiffPresenter {
return new DiffPresenter(
this.selectionInfoRegistry,
Expand Down
33 changes: 33 additions & 0 deletions src/lib/commands/compare-with-git-branch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import DiffPresenter from '../diff-presenter';
import BranchManager from '../branch-manager';
import SelectionInfoRegistry from '../selection-info-registry';
import {TextKey} from '../const';
import {Command} from './command';
import TextEditor from '../adaptors/text-editor';

export default class CompareWithGitBranchCommand implements Command {
constructor(private readonly diffPresenter: DiffPresenter,
private readonly selectionInfoRegistry: SelectionInfoRegistry,
private readonly branchManager: BranchManager) {}

async execute(editor: TextEditor) {
const branchNames = await this.branchManager.getLocalBranchNames();
const branchName = await this.branchManager.selectViaQuickPick(branchNames);
const branchText = await this.branchManager.getFileContent(branchName, editor.fileUri);

this.selectionInfoRegistry.set(TextKey.VISIBLE_EDITOR1, {
text: editor.getText,
fileName: `${editor.fileName} (local)`,
lineRanges: []
});

this.selectionInfoRegistry.set(TextKey.GIT_BRANCH, {
text: branchText,
fileName: `${editor.fileName} (${branchName})`,
lineRanges: []
});

await 'HACK'; // HACK: Avoid "TextEditor has been disposed" error
await this.diffPresenter.takeDiff(TextKey.GIT_BRANCH, TextKey.VISIBLE_EDITOR1);
}
}
1 change: 1 addition & 0 deletions src/lib/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const TextKey = {
REGISTER1: 'reg1',
REGISTER2: 'reg2',
CLIPBOARD: 'clipboard',
GIT_BRANCH: 'gitbranch',
VISIBLE_EDITOR1: 'visible1',
VISIBLE_EDITOR2: 'visible2'
};
Loading