Skip to content

Commit b254d7f

Browse files
committed
Closes #236 - adds open working file
1 parent 5391b2e commit b254d7f

File tree

10 files changed

+104
-7
lines changed

10 files changed

+104
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [Unreleased]
8+
### Added
9+
- Adds `Open Working File` command (`gitlens.openWorkingFile`) - opens the working file for the active file revision -- closes [#236](https://github.com/eamodio/vscode-gitlens/issues/236)
10+
711
## [7.1.0-beta] - 2017-12-20
812
### Changed
913
- Improves startup performance by ~65% (on my very fast PC) and reduces package size by over 75%

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ While GitLens is highly customizable and provides many [configuration settings](
330330

331331
- Adds a `Copy Commit Message to Clipboard` command (`gitlens.copyMessageToClipboard`) to copy the commit message of the active line to the clipboard or from the most recent commit to the current branch, if there is no active editor
332332

333+
- Adds a `Open Working File"` command (`gitlens.openWorkingFile`) to open the working file for the active file revision
334+
333335
- Adds a `Open Changes (with difftool)` command (`gitlens.externalDiff`) to the source control group and source control resource context menus to open the changes of a file or set of files with the configured git difftool
334336

335337
- Adds a `Open All Changes (with difftool)` command (`gitlens.externalDiffAll`) to open all working changes with the configured git difftool

images/dark/open-file.svg

Lines changed: 4 additions & 0 deletions
Loading

images/light/open-file.svg

Lines changed: 4 additions & 0 deletions
Loading

package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,15 @@
13271327
"title": "Open Repository in Remote",
13281328
"category": "GitLens"
13291329
},
1330+
{
1331+
"command": "gitlens.openWorkingFile",
1332+
"title": "Open Working File",
1333+
"category": "GitLens",
1334+
"icon": {
1335+
"dark": "images/dark/open-file.svg",
1336+
"light": "images/light/open-file.svg"
1337+
}
1338+
},
13301339
{
13311340
"command": "gitlens.stashApply",
13321341
"title": "Apply Stashed Changes",
@@ -1732,6 +1741,10 @@
17321741
"command": "gitlens.openRepoInRemote",
17331742
"when": "gitlens:activeHasRemote"
17341743
},
1744+
{
1745+
"command": "gitlens.openWorkingFile",
1746+
"when": "gitlens:activeIsRevision"
1747+
},
17351748
{
17361749
"command": "gitlens.stashApply",
17371750
"when": "gitlens:enabled"
@@ -1958,6 +1971,11 @@
19581971
}
19591972
],
19601973
"editor/title": [
1974+
{
1975+
"command": "gitlens.openWorkingFile",
1976+
"when": "gitlens:activeIsRevision",
1977+
"group": "navigation@1"
1978+
},
19611979
{
19621980
"command": "gitlens.toggleFileBlame",
19631981
"alt": "gitlens.toggleFileRecentChanges",

src/commands.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export * from './commands/openFileInRemote';
2929
export * from './commands/openFileRevision';
3030
export * from './commands/openInRemote';
3131
export * from './commands/openRepoInRemote';
32+
export * from './commands/openWorkingFile';
3233
export * from './commands/resetSuppressedWarnings';
3334
export * from './commands/showCommitSearch';
3435
export * from './commands/showFileBlame';
@@ -81,6 +82,7 @@ export function configureCommands(
8182
context.subscriptions.push(new Commands.OpenFileRevisionCommand(annotationController));
8283
context.subscriptions.push(new Commands.OpenInRemoteCommand());
8384
context.subscriptions.push(new Commands.OpenRepoInRemoteCommand(git));
85+
context.subscriptions.push(new Commands.OpenWorkingFileCommand(annotationController, git));
8486
context.subscriptions.push(new Commands.ClearFileAnnotationsCommand(annotationController));
8587
context.subscriptions.push(new Commands.ShowFileBlameCommand(annotationController));
8688
context.subscriptions.push(new Commands.ShowLineBlameCommand(currentLineController));

src/commands/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export enum Commands {
3131
OpenFileRevision = 'gitlens.openFileRevision',
3232
OpenInRemote = 'gitlens.openInRemote',
3333
OpenRepoInRemote = 'gitlens.openRepoInRemote',
34+
OpenWorkingFile = 'gitlens.openWorkingFile',
3435
ResetSuppressedWarnings = 'gitlens.resetSuppressedWarnings',
3536
ShowCommitSearch = 'gitlens.showCommitSearch',
3637
ShowFileBlame = 'gitlens.showFileBlame',

src/commands/openWorkingFile.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
import { Range, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode';
3+
import { AnnotationController, FileAnnotationType } from '../annotations/annotationController';
4+
import { ActiveEditorCommand, Commands, getCommandUri, openEditor } from './common';
5+
import { GitService, GitUri } from '../gitService';
6+
import { Logger } from '../logger';
7+
8+
export interface OpenWorkingFileCommandArgs {
9+
uri?: Uri;
10+
line?: number;
11+
showOptions?: TextDocumentShowOptions;
12+
annotationType?: FileAnnotationType;
13+
}
14+
15+
export class OpenWorkingFileCommand extends ActiveEditorCommand {
16+
17+
constructor(
18+
private readonly annotationController: AnnotationController,
19+
private readonly git: GitService
20+
) {
21+
super(Commands.OpenWorkingFile);
22+
}
23+
24+
async execute(editor: TextEditor, uri?: Uri, args: OpenWorkingFileCommandArgs = {}) {
25+
args = { ...args };
26+
if (args.line === undefined) {
27+
args.line = editor === undefined ? 0 : editor.selection.active.line;
28+
}
29+
30+
try {
31+
if (args.uri === undefined) {
32+
uri = getCommandUri(uri, editor);
33+
if (uri === undefined) return undefined;
34+
35+
args.uri = await GitUri.fromUri(uri, this.git);
36+
}
37+
38+
if (args.line !== undefined && args.line !== 0) {
39+
if (args.showOptions === undefined) {
40+
args.showOptions = {};
41+
}
42+
args.showOptions.selection = new Range(args.line, 0, args.line, 0);
43+
}
44+
45+
const e = await openEditor(args.uri, args.showOptions);
46+
if (args.annotationType === undefined) return e;
47+
48+
return this.annotationController.showAnnotations(e!, args.annotationType, args.line);
49+
}
50+
catch (ex) {
51+
Logger.error(ex, 'OpenWorkingFileCommand');
52+
return window.showErrorMessage(`Unable to open working file. See output channel for more details`);
53+
}
54+
}
55+
}

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export enum CommandContext {
2929
ActiveHasRemote = 'gitlens:activeHasRemote',
3030
ActiveIsBlameable = 'gitlens:activeIsBlameable',
3131
ActiveFileIsTracked = 'gitlens:activeIsTracked',
32+
ActiveIsRevision = 'gitlens:activeIsRevision',
3233
AnnotationStatus = 'gitlens:annotationStatus',
3334
CanToggleCodeLens = 'gitlens:canToggleCodeLens',
3435
Enabled = 'gitlens:enabled',

src/git/gitContextTracker.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ interface Context {
3131
interface ContextState {
3232
blameable?: boolean;
3333
dirty: boolean;
34+
revision?: boolean;
3435
tracked?: boolean;
3536
}
3637

@@ -147,7 +148,8 @@ export class GitContextTracker extends Disposable {
147148

148149
private async updateContext(reason: BlameabilityChangeReason, editor: TextEditor | undefined, force: boolean = false) {
149150
try {
150-
let tracked: boolean;
151+
let revision = false;
152+
let tracked = false;
151153
if (force || this._context.editor !== editor) {
152154
this._context.editor = editor;
153155
this._context.repo = undefined;
@@ -166,20 +168,24 @@ export class GitContextTracker extends Disposable {
166168
}
167169

168170
this._context.state.dirty = editor.document.isDirty;
171+
revision = !!this._context.uri.sha;
169172
tracked = await this.git.isTracked(this._context.uri);
170173
}
171174
else {
172175
this._context.uri = undefined;
173176
this._context.state.dirty = false;
174177
this._context.state.blameable = false;
175-
tracked = false;
176178
}
177179
}
178-
else {
179-
// Since the tracked state could have changed, update it
180-
tracked = this._context.uri !== undefined
181-
? await this.git.isTracked(this._context.uri!)
182-
: false;
180+
// Since the revision or tracked state could have changed, update it
181+
else if (this._context.uri !== undefined) {
182+
revision = !!this._context.uri.sha;
183+
tracked = await this.git.isTracked(this._context.uri);
184+
}
185+
186+
if (this._context.state.revision !== revision) {
187+
this._context.state.revision = revision;
188+
setCommandContext(CommandContext.ActiveIsRevision, revision);
183189
}
184190

185191
if (this._context.state.tracked !== tracked) {

0 commit comments

Comments
 (0)