From 0948fd66dd690c0614b4a6df7a0fa052b145929d Mon Sep 17 00:00:00 2001 From: Ehab Younes Date: Mon, 16 Sep 2024 01:03:32 +0300 Subject: [PATCH 1/3] Adds the option to open a file on click in Search & Compare view --- CHANGELOG.md | 4 +++ README.md | 1 + package.json | 7 +++++ src/commands/openFileAtRevision.ts | 10 +++---- src/config.ts | 4 ++- src/views/nodes/commitFileNode.ts | 27 +++++++++++++++---- .../partials/views.searchAndCompare.html | 15 +++++++++++ 7 files changed, 57 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9111bc60079b4..3a8f190a35a7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p ## [Unreleased] +### Added + +- Adds the option to open a file instead of showing a diff when clicking in _Search & Compare_ — closes [#1651](https://github.com/gitkraken/vscode-gitlens/issues/1651) + ### Changed - Adds vscode-test to run unit-tests — closes [#3570](https://github.com/gitkraken/vscode-gitlens/issues/3570) diff --git a/README.md b/README.md index ec0f4f53a1d8f..4d89730874009 100644 --- a/README.md +++ b/README.md @@ -449,6 +449,7 @@ A big thanks to the people that have contributed to this project 🙏❤️: - may ([@m4rch3n1ng](https://github.com/m4rch3n1ng)) — [contributions](https://github.com/gitkraken/vscode-gitlens/commits?author=m4rch3n1ng) - bm-w ([@bm-w](https://github.com/bm-w)) — [contributions](https://github.com/gitkraken/vscode-gitlens/commits?author=bm-w) - Tyler Johnson ([@TJohnsonSE](https://github.com/TJohnsonSE)) — [contributions](https://github.com/gitkraken/vscode-gitlens/commits?author=TJohnsonSE) +- Ehab Younes ([@EhabY](https://github.com/EhabY)) — [contributions](https://github.com/gitkraken/vscode-gitlens/commits?author=EhabY) Also special thanks to the people that have provided support, testing, brainstorming, etc: diff --git a/package.json b/package.json index fd20013a40cc7..4718aee18d27d 100644 --- a/package.json +++ b/package.json @@ -2863,6 +2863,13 @@ "scope": "window", "order": 22 }, + "gitlens.views.searchAndCompare.files.openDiffOnClick": { + "type": "boolean", + "default": "true", + "markdownDescription": "Specifies whether to open the diff view or the file itself when clicking on a file in the _Search & Compare_ view", + "scope": "window", + "order": 23 + }, "gitlens.views.searchAndCompare.files.icon": { "type": "string", "default": "type", diff --git a/src/commands/openFileAtRevision.ts b/src/commands/openFileAtRevision.ts index 2796824a457b2..70b51c9e4e9f9 100644 --- a/src/commands/openFileAtRevision.ts +++ b/src/commands/openFileAtRevision.ts @@ -101,11 +101,6 @@ export class OpenFileAtRevisionCommand extends ActiveEditorCommand { } async execute(editor: TextEditor | undefined, uri?: Uri, args?: OpenFileAtRevisionCommandArgs) { - uri = getCommandUri(uri, editor); - if (uri == null) return; - - const gitUri = await GitUri.fromUri(uri); - args = { ...args }; if (args.line == null) { args.line = editor?.selection.active.line ?? 0; @@ -113,6 +108,11 @@ export class OpenFileAtRevisionCommand extends ActiveEditorCommand { try { if (args.revisionUri == null) { + uri = getCommandUri(uri, editor); + if (uri == null) return; + + const gitUri = await GitUri.fromUri(uri); + const log = this.container.git.getLogForFile(gitUri.repoPath, gitUri.fsPath).then( log => log ?? diff --git a/src/config.ts b/src/config.ts index af834850bd4df..a2018947da5bd 100644 --- a/src/config.ts +++ b/src/config.ts @@ -817,7 +817,9 @@ export interface RepositoriesViewConfig { export interface SearchAndCompareViewConfig { readonly avatars: boolean; - readonly files: ViewsFilesConfig; + readonly files: ViewsFilesConfig & { + readonly openDiffOnClick: boolean; + }; readonly pullRequests: { readonly enabled: boolean; readonly showForCommits: boolean; diff --git a/src/views/nodes/commitFileNode.ts b/src/views/nodes/commitFileNode.ts index b344b0bb4e8dd..e07ac070bf012 100644 --- a/src/views/nodes/commitFileNode.ts +++ b/src/views/nodes/commitFileNode.ts @@ -1,6 +1,7 @@ -import type { Command, Selection } from 'vscode'; +import type { Command, Selection, TextDocumentShowOptions } from 'vscode'; import { MarkdownString, TreeItem, TreeItemCollapsibleState, Uri } from 'vscode'; import type { DiffWithPreviousCommandArgs } from '../../commands/diffWithPrevious'; +import type { OpenFileAtRevisionCommandArgs } from '../../commands/openFileAtRevision'; import { Schemes } from '../../constants'; import { Commands } from '../../constants.commands'; import type { TreeViewRefFileNodeTypes } from '../../constants.views'; @@ -176,14 +177,30 @@ export abstract class CommitFileNodeBase< line = this.options?.selection?.active.line ?? 0; } + const showOptions: TextDocumentShowOptions = { + preserveFocus: true, + preview: true, + }; + + const filesConfig = this.view.config.files; + if ('openDiffOnClick' in filesConfig && filesConfig.openDiffOnClick === false) { + const commandArgs: OpenFileAtRevisionCommandArgs = { + revisionUri: GitUri.fromFile(this.file, this.commit.repoPath, this.ref.ref), + line: line, + showOptions: showOptions, + }; + return { + title: 'Open File', + command: Commands.OpenFileAtRevision, + arguments: [commandArgs], + }; + } + const commandArgs: DiffWithPreviousCommandArgs = { commit: this.commit, uri: GitUri.fromFile(this.file, this.commit.repoPath), line: line, - showOptions: { - preserveFocus: true, - preview: true, - }, + showOptions: showOptions, }; return { title: 'Open Changes with Previous Revision', diff --git a/src/webviews/apps/settings/partials/views.searchAndCompare.html b/src/webviews/apps/settings/partials/views.searchAndCompare.html index 5574ae12d5f11..79ec133efdb04 100644 --- a/src/webviews/apps/settings/partials/views.searchAndCompare.html +++ b/src/webviews/apps/settings/partials/views.searchAndCompare.html @@ -114,6 +114,21 @@

Compacts (flattens) unnecessary nesting when using a tree layouts

+
+
+ + +
+

+ Opens the diff view when clicking on a file instead of opening the file itself +

+
+
Date: Sat, 12 Jul 2025 17:52:07 +0300 Subject: [PATCH 2/3] Fix merge conflicts --- src/commands/openFileAtRevision.ts | 13 +++++++------ src/views/nodes/commitFileNode.ts | 17 +++++++++-------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/commands/openFileAtRevision.ts b/src/commands/openFileAtRevision.ts index be5d6961aa998..194f539b0be81 100644 --- a/src/commands/openFileAtRevision.ts +++ b/src/commands/openFileAtRevision.ts @@ -105,17 +105,18 @@ export class OpenFileAtRevisionCommand extends ActiveEditorCommand { } async execute(editor: TextEditor | undefined, uri?: Uri, args?: OpenFileAtRevisionCommandArgs): Promise { - uri = getCommandUri(uri, editor); - if (uri == null) return; - - const gitUri = await GitUri.fromUri(uri); - args = { ...args }; args.range ??= selectionToDiffRange(editor?.selection); - const svc = this.container.git.getRepositoryService(gitUri.repoPath!); try { if (args.revisionUri == null) { + uri = getCommandUri(uri, editor); + if (uri == null) return; + + const gitUri = await GitUri.fromUri(uri); + + const svc = this.container.git.getRepositoryService(gitUri.repoPath!); + const log = svc.commits .getLogForPath(gitUri.fsPath, undefined, { isFolder: false }) .then( diff --git a/src/views/nodes/commitFileNode.ts b/src/views/nodes/commitFileNode.ts index a370dbea1f1ca..8d8147ba19a3d 100644 --- a/src/views/nodes/commitFileNode.ts +++ b/src/views/nodes/commitFileNode.ts @@ -1,4 +1,4 @@ -import type { Command, Selection } from 'vscode'; +import type { Command, Selection, TextDocumentShowOptions } from 'vscode'; import { TreeItem, TreeItemCollapsibleState, Uri } from 'vscode'; import type { DiffWithPreviousCommandArgs } from '../../commands/diffWithPrevious'; import type { OpenFileAtRevisionCommandArgs } from '../../commands/openFileAtRevision'; @@ -167,16 +167,17 @@ export abstract class CommitFileNodeBase< range = this.commit.file?.range ?? selectionToDiffRange(this.options?.selection); } + const showOptions: TextDocumentShowOptions = { preserveFocus: true, preview: true }; + const filesConfig = this.view.config.files; if ('openDiffOnClick' in filesConfig && filesConfig.openDiffOnClick === false) { - return createCommand<[undefined, OpenFileAtRevisionCommandArgs]>( - 'gitlens.openFileRevision', - 'Open File', - undefined, + return createCommand<[CommitFileNodeBase, OpenFileAtRevisionCommandArgs]>( + 'gitlens.views.openFileRevision', + 'Open File at Revision', + this, { - revisionUri: GitUri.fromFile(this.file, this.commit.repoPath, this.ref.ref), range: range, - showOptions: { preserveFocus: true, preview: true }, + showOptions: showOptions, }, ); } @@ -189,7 +190,7 @@ export abstract class CommitFileNodeBase< commit: this.commit, uri: GitUri.fromFile(this.file, this.commit.repoPath), range: range, - showOptions: { preserveFocus: true, preview: true }, + showOptions: showOptions, }, ); } From 650e81655fd95d24409726f306fb08608e48fd83 Mon Sep 17 00:00:00 2001 From: Ehab Younes Date: Sat, 12 Jul 2025 18:26:18 +0300 Subject: [PATCH 3/3] Fix changelog --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a36fcfe8d78a..5ea03d2b390d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p ## [Unreleased] +### Added + +- Adds the option to open a file instead of showing a diff when clicking in _Search & Compare_ ([#1651](https://github.com/gitkraken/vscode-gitlens/issues/1651)) + ### Changed - Changes branch creation to avoid setting an upstream branch if the new branch name and remote branch name don't match ([#4477](https://github.com/gitkraken/vscode-gitlens/issues/4477)) @@ -18,7 +22,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p - Adds support for time-based commit searches on the _Commit Graph_, _Search & Compare_ view, and in the _Search Commits_ command - Adds 👍 "Helpful" and 👎 "Unhelpful" feedback buttons to AI-generated markdown previews such as Commit Composer and Explain Changes ([#4449](https://github.com/gitkraken/vscode-gitlens/issues/4449)) - Adds a _Commit with AI (Preview)_ button to the _Inspect Overview_ tab of the _Commit Graph_ and _Inspect_ views -- Adds the option to open a file instead of showing a diff when clicking in _Search & Compare_ ([#1651](https://github.com/gitkraken/vscode-gitlens/issues/1651)) ### Changed