Skip to content

Fixes #1651 - Adds an option to open a file on click in Search & Compare #3583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ A big thanks to the people that have contributed to this project 🙏❤️:
- Jordon Kashanchi ([@jordonkash](https://github.com/JordonKash)) — [contributions](https://github.com/gitkraken/vscode-gitlens/commits?author=jordonkash)
- JounQin ([@JounQin](https://github.com/JounQin)) — [contributions](https://github.com/gitkraken/vscode-gitlens/commits?author=JounQin)
- Noritaka Kobayashi ([@noritaka1166](https://github.com/noritaka1166)) — [contributions](https://github.com/gitkraken/vscode-gitlens/commits?author=noritaka1166)
- 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:

Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3268,6 +3268,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",
Expand Down
13 changes: 7 additions & 6 deletions src/commands/openFileAtRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,18 @@ export class OpenFileAtRevisionCommand extends ActiveEditorCommand {
}

async execute(editor: TextEditor | undefined, uri?: Uri, args?: OpenFileAtRevisionCommandArgs): Promise<void> {
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(
Expand Down
4 changes: 3 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,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;
Expand Down
20 changes: 18 additions & 2 deletions src/views/nodes/commitFileNode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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';
import { Schemes } from '../../constants';
import type { TreeViewRefFileNodeTypes } from '../../constants.views';
import { StatusFileFormatter } from '../../git/formatters/statusFormatter';
Expand Down Expand Up @@ -166,6 +167,21 @@ 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<[CommitFileNodeBase<Type, TView>, OpenFileAtRevisionCommandArgs]>(
'gitlens.views.openFileRevision',
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this open the file at the revision or open current file? The context menu item has both "Open File" and "Open File at Revision" and same for the icons, though clicking the icon without [Alt] would open the file at the revision so I opted to make that the default for a single click

'Open File at Revision',
Comment on lines +174 to +176
Copy link
Author

@EhabY EhabY Jul 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This relies on reusing the logic from the context menu item "Open File at Revision". The alternative is to use the generic command gitlens.openFileRevision and copy the logic to get the revision URI:

const revisionUri =
	node.commit.file?.status === 'D'
		? this.container.git
				.getRepositoryService(node.commit.repoPath)
				.getRevisionUri(
					(await node.commit.getPreviousSha()) ?? deletedOrMissing,
					node.commit.file.path,
				)
		: this.container.git.getRevisionUriFromGitUri(node.uri);

However this would require making this method async which wouldn't work :/

this,
{
range: range,
showOptions: showOptions,
},
);
}

return createCommand<[undefined, DiffWithPreviousCommandArgs]>(
'gitlens.diffWithPrevious',
'Open Changes with Previous Revision',
Expand All @@ -174,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,
},
);
}
Expand Down
15 changes: 15 additions & 0 deletions src/webviews/apps/settings/partials/views.searchAndCompare.html
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ <h2>
<p class="setting__hint">Compacts (flattens) unnecessary nesting when using a tree layouts</p>
</div>

<div class="setting">
<div class="setting__input">
<input
id="views.searchAndCompare.files.openDiffOnClick"
name="views.searchAndCompare.files.openDiffOnClick"
type="checkbox"
data-setting
/>
<label for="views.searchAndCompare.files.openDiffOnClick">Open file diff on click</label>
</div>
<p class="setting__hint">
Opens the diff view when clicking on a file instead of opening the file itself
</p>
</div>

<div class="setting">
<div class="setting__input">
<input
Expand Down