From aa6a38114c3487606f6f3f8c234b4aae89e9ee57 Mon Sep 17 00:00:00 2001 From: Ladislau Szomoru <3372902+lszomoru@users.noreply.github.com> Date: Sat, 4 Jan 2025 19:15:35 +0100 Subject: [PATCH] Git - add stage/unstage commands to editor title (#237257) --- extensions/git/package.json | 12 +++++++++++- extensions/git/src/model.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/extensions/git/package.json b/extensions/git/package.json index 748ac40f1ae..cdb7c77a03a 100644 --- a/extensions/git/package.json +++ b/extensions/git/package.json @@ -2050,9 +2050,19 @@ "group": "navigation", "when": "config.git.enabled && !git.missing && gitOpenRepositoryCount != 0 && isInNotebookTextDiffEditor && resourceScheme =~ /^git$|^file$/" }, + { + "command": "git.stage", + "group": "navigation@1", + "when": "config.git.enabled && !git.missing && gitOpenRepositoryCount != 0 && !isInDiffEditor && !isMergeEditor && resourceScheme == file && git.activeResourceHasUnstagedChanges" + }, + { + "command": "git.unstage", + "group": "navigation@1", + "when": "config.git.enabled && !git.missing && gitOpenRepositoryCount != 0 && !isInDiffEditor && !isMergeEditor && resourceScheme == file && git.activeResourceHasStagedChanges" + }, { "command": "git.openChange", - "group": "navigation", + "group": "navigation@2", "when": "config.git.enabled && !git.missing && gitOpenRepositoryCount != 0 && !isInDiffEditor && !isMergeEditor && resourceScheme == file && scmActiveResourceHasChanges" }, { diff --git a/extensions/git/src/model.ts b/extensions/git/src/model.ts index 48e66d5e9ff..142d073914f 100644 --- a/extensions/git/src/model.ts +++ b/extensions/git/src/model.ts @@ -272,6 +272,7 @@ export class Model implements IRepositoryResolver, IBranchProtectionProviderRegi workspace.onDidChangeWorkspaceFolders(this.onDidChangeWorkspaceFolders, this, this.disposables); window.onDidChangeVisibleTextEditors(this.onDidChangeVisibleTextEditors, this, this.disposables); + window.onDidChangeActiveTextEditor(this.onDidChangeActiveTextEditor, this, this.disposables); workspace.onDidChangeConfiguration(this.onDidChangeConfiguration, this, this.disposables); const fsWatcher = workspace.createFileSystemWatcher('**'); @@ -519,6 +520,31 @@ export class Model implements IRepositoryResolver, IBranchProtectionProviderRegi } } + private onDidChangeActiveTextEditor(): void { + const textEditor = window.activeTextEditor; + + if (textEditor === undefined) { + commands.executeCommand('setContext', 'git.activeResourceHasUnstagedChanges', false); + commands.executeCommand('setContext', 'git.activeResourceHasStagedChanges', false); + return; + } + + const repository = this.getRepository(textEditor.document.uri); + if (!repository) { + commands.executeCommand('setContext', 'git.activeResourceHasUnstagedChanges', false); + commands.executeCommand('setContext', 'git.activeResourceHasStagedChanges', false); + return; + } + + const indexResource = repository.indexGroup.resourceStates + .find(resource => pathEquals(resource.resourceUri.fsPath, textEditor.document.uri.fsPath)); + const workingTreeResource = repository.workingTreeGroup.resourceStates + .find(resource => pathEquals(resource.resourceUri.fsPath, textEditor.document.uri.fsPath)); + + commands.executeCommand('setContext', 'git.activeResourceHasStagedChanges', indexResource !== undefined); + commands.executeCommand('setContext', 'git.activeResourceHasUnstagedChanges', workingTreeResource !== undefined); + } + @sequentialize async openRepository(repoPath: string, openIfClosed = false): Promise { this.logger.trace(`[Model][openRepository] Repository: ${repoPath}`); @@ -727,8 +753,10 @@ export class Model implements IRepositoryResolver, IBranchProtectionProviderRegi const statusListener = repository.onDidRunGitStatus(() => { checkForSubmodules(); updateMergeChanges(); + this.onDidChangeActiveTextEditor(); }); checkForSubmodules(); + this.onDidChangeActiveTextEditor(); const updateOperationInProgressContext = () => { let operationInProgress = false;