Skip to content

Commit

Permalink
Adds the option to open a file on click in Search & Compare view
Browse files Browse the repository at this point in the history
  • Loading branch information
EhabY committed Sep 22, 2024
1 parent d3b4012 commit 0948fd6
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 11 deletions.
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_ — 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)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 5 additions & 5 deletions src/commands/openFileAtRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,18 @@ 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;
}

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 ??
Expand Down
4 changes: 3 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 22 additions & 5 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 { 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';
Expand Down Expand Up @@ -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',
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

0 comments on commit 0948fd6

Please sign in to comment.