From a2a7c605b82a03b1b067c157255a9e5984f19f4e Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Tue, 12 Nov 2019 01:47:59 -0500 Subject: [PATCH] Changes to re-use open editor if possible --- src/commands/common.ts | 52 +++++++++++++++++++++++++++++ src/commands/diffWithPrevious.ts | 4 +-- src/commands/openChangedFiles.ts | 4 +-- src/commands/openFileRevision.ts | 4 +-- src/commands/openFileRevisionFrom.ts | 4 +-- src/commands/openRevisionFile.ts | 4 +-- src/commands/openWorkingFile.ts | 4 +-- src/quickpicks/commitFileQuickPick.ts | 4 +-- src/quickpicks/commitQuickPick.ts | 6 ++-- src/quickpicks/repoStatusQuickPick.ts | 4 +-- src/views/nodes/searchResultsCommitsNode.ts | 3 +- src/views/viewCommands.ts | 6 ++-- 12 files changed, 75 insertions(+), 24 deletions(-) diff --git a/src/commands/common.ts b/src/commands/common.ts index aadd841..4b63f7c 100644 --- a/src/commands/common.ts +++ b/src/commands/common.ts @@ -537,6 +537,58 @@ export abstract class EditorCommand implements Disposable { abstract execute(editor: TextEditor, edit: TextEditorEdit, ...args: any[]): any; } +export function findEditor(uri: Uri, lastActive?: TextEditor): TextEditor | undefined { + const normalizedUri = uri.toString(false); + + let e = window.activeTextEditor; + if (e !== undefined && e.document.uri.toString(false) === normalizedUri) { + return e; + } + + let found; + for (e of window.visibleTextEditors) { + // Prioritize the last active window over other visible ones + if (e === lastActive && e.document.uri.toString(false) === normalizedUri) { + return e; + } + + if (e.document.uri.toString(false) === normalizedUri) { + found = e; + } + } + + return found; +} + +export async function findOrOpenEditor( + uri: Uri, + options: TextDocumentShowOptions & { rethrow?: boolean } = {}, + lastActive?: TextEditor +): Promise { + const e = findEditor(uri, lastActive); + if (e !== undefined) { + if (!options.preserveFocus) { + await window.showTextDocument(e.document, { ...options, viewColumn: e.viewColumn }); + } + + return e; + } + + let column = window.activeTextEditor?.viewColumn; + + // If we have a last active view column and it isn't the same as the webview's, then use it + if (lastActive !== undefined && lastActive.viewColumn !== undefined && lastActive.viewColumn !== column) { + column = lastActive.viewColumn; + } else if (column !== undefined) { + column--; + if (column <= 0) { + column = undefined; + } + } + + return openEditor(uri, { viewColumn: column, ...options }); +} + export async function openEditor( uri: Uri, options: TextDocumentShowOptions & { rethrow?: boolean } = {} diff --git a/src/commands/diffWithPrevious.ts b/src/commands/diffWithPrevious.ts index 80d8d0f..4794ef9 100644 --- a/src/commands/diffWithPrevious.ts +++ b/src/commands/diffWithPrevious.ts @@ -4,7 +4,7 @@ import { Container } from '../container'; import { GitCommit, GitService, GitUri } from '../git/gitService'; import { Logger } from '../logger'; import { Messages } from '../messages'; -import { ActiveEditorCommand, command, CommandContext, Commands, getCommandUri, openEditor } from './common'; +import { ActiveEditorCommand, command, CommandContext, Commands, findOrOpenEditor, getCommandUri } from './common'; import { DiffWithCommandArgs } from './diffWith'; export interface DiffWithPreviousCommandArgs { @@ -77,7 +77,7 @@ export class DiffWithPreviousCommand extends ActiveEditorCommand { // If we have no previous and the current is the working file, just open the working file if (diffUris.current.sha === undefined) { - return openEditor(diffUris.current, args.showOptions); + return findOrOpenEditor(diffUris.current, args.showOptions); } if (!diffUris.current.isUncommittedStaged) { diff --git a/src/commands/openChangedFiles.ts b/src/commands/openChangedFiles.ts index 1a8fd50..c758ce0 100644 --- a/src/commands/openChangedFiles.ts +++ b/src/commands/openChangedFiles.ts @@ -5,7 +5,7 @@ import { Container } from '../container'; import { Logger } from '../logger'; import { Messages } from '../messages'; import { Arrays } from '../system'; -import { Command, command, Commands, getRepoPathOrPrompt, openEditor } from './common'; +import { command, Command, Commands, findOrOpenEditor, getRepoPathOrPrompt } from './common'; export interface OpenChangedFilesCommandArgs { uris?: Uri[]; @@ -34,7 +34,7 @@ export class OpenChangedFilesCommand extends Command { } for (const uri of args.uris) { - await openEditor(uri, { preserveFocus: true, preview: false }); + await findOrOpenEditor(uri, { preserveFocus: true, preview: false }); } return undefined; diff --git a/src/commands/openFileRevision.ts b/src/commands/openFileRevision.ts index a65e0b3..ef96114 100644 --- a/src/commands/openFileRevision.ts +++ b/src/commands/openFileRevision.ts @@ -8,7 +8,7 @@ import { Logger } from '../logger'; import { Messages } from '../messages'; import { CommandQuickPickItem, FileHistoryQuickPick, ShowFileHistoryFromQuickPickItem } from '../quickpicks'; import { Iterables, Strings } from '../system'; -import { ActiveEditorCommand, command, Commands, getCommandUri, openEditor } from './common'; +import { ActiveEditorCommand, command, Commands, findOrOpenEditor, getCommandUri } from './common'; export interface OpenFileRevisionCommandArgs { reference?: GitBranch | GitTag | GitReference; @@ -180,7 +180,7 @@ export class OpenFileRevisionCommand extends ActiveEditorCommand { args.showOptions.selection = new Range(args.line, 0, args.line, 0); } - const e = await openEditor(args.uri, { ...args.showOptions, rethrow: true }); + const e = await findOrOpenEditor(args.uri, { ...args.showOptions, rethrow: true }); if (args.annotationType === undefined) return e; return Container.fileAnnotations.show(e, args.annotationType, args.line); diff --git a/src/commands/openFileRevisionFrom.ts b/src/commands/openFileRevisionFrom.ts index 2994e92..e91a0d7 100644 --- a/src/commands/openFileRevisionFrom.ts +++ b/src/commands/openFileRevisionFrom.ts @@ -4,7 +4,7 @@ import { GlyphChars } from '../constants'; import { GitBranch, GitReference, GitTag, GitUri } from '../git/gitService'; import { CommandQuickPickItem, ReferencesQuickPick } from '../quickpicks'; import { Strings } from '../system'; -import { ActiveEditorCommand, command, Commands, getCommandUri, openEditor } from './common'; +import { ActiveEditorCommand, command, Commands, findOrOpenEditor, getCommandUri } from './common'; export interface OpenFileRevisionFromCommandArgs { reference?: GitBranch | GitTag | GitReference; @@ -50,7 +50,7 @@ export class OpenFileRevisionFromCommand extends ActiveEditorCommand { args.showOptions.selection = new Range(args.line, 0, args.line, 0); } - return openEditor(GitUri.toRevisionUri(args.reference.ref, gitUri.fsPath, gitUri.repoPath), { + return findOrOpenEditor(GitUri.toRevisionUri(args.reference.ref, gitUri.fsPath, gitUri.repoPath), { ...args.showOptions, rethrow: true }); diff --git a/src/commands/openRevisionFile.ts b/src/commands/openRevisionFile.ts index 9029a9f..7e0045e 100644 --- a/src/commands/openRevisionFile.ts +++ b/src/commands/openRevisionFile.ts @@ -5,7 +5,7 @@ import { Container } from '../container'; import { GitUri } from '../git/gitService'; import { Logger } from '../logger'; import { Messages } from '../messages'; -import { ActiveEditorCommand, command, Commands, getCommandUri, openEditor } from './common'; +import { ActiveEditorCommand, command, Commands, findOrOpenEditor, getCommandUri } from './common'; export interface OpenRevisionFileCommandArgs { uri?: Uri; @@ -51,7 +51,7 @@ export class OpenRevisionFileCommand extends ActiveEditorCommand { args.showOptions.selection = new Range(args.line, 0, args.line, 0); } - const e = await openEditor(args.uri, { ...args.showOptions, rethrow: true }); + const e = await findOrOpenEditor(args.uri, { ...args.showOptions, rethrow: true }); if (args.annotationType === undefined) return e; return Container.fileAnnotations.show(e, args.annotationType, args.line); diff --git a/src/commands/openWorkingFile.ts b/src/commands/openWorkingFile.ts index 6dac9c2..0c958b8 100644 --- a/src/commands/openWorkingFile.ts +++ b/src/commands/openWorkingFile.ts @@ -5,7 +5,7 @@ import { Container } from '../container'; import { GitUri } from '../git/gitService'; import { Logger } from '../logger'; import { Messages } from '../messages'; -import { ActiveEditorCommand, command, Commands, getCommandUri, openEditor } from './common'; +import { ActiveEditorCommand, command, Commands, findOrOpenEditor, getCommandUri } from './common'; export interface OpenWorkingFileCommandArgs { uri?: Uri; @@ -53,7 +53,7 @@ export class OpenWorkingFileCommand extends ActiveEditorCommand { args.showOptions.selection = new Range(args.line, 0, args.line, 0); } - const e = await openEditor(args.uri, { ...args.showOptions, rethrow: true }); + const e = await findOrOpenEditor(args.uri, { ...args.showOptions, rethrow: true }); if (args.annotationType === undefined) return e; return Container.fileAnnotations.show(e, args.annotationType, args.line); diff --git a/src/quickpicks/commitFileQuickPick.ts b/src/quickpicks/commitFileQuickPick.ts index 42bf7a8..f69b47e 100644 --- a/src/quickpicks/commitFileQuickPick.ts +++ b/src/quickpicks/commitFileQuickPick.ts @@ -6,7 +6,7 @@ import { CopyRemoteFileUrlToClipboardCommandArgs, CopyShaToClipboardCommandArgs, DiffWithPreviousCommandArgs, - openEditor, + findOrOpenEditor, OpenWorkingFileCommandArgs, ShowQuickCommitDetailsCommandArgs, ShowQuickCommitFileDetailsCommandArgs, @@ -102,7 +102,7 @@ export class OpenCommitFileRevisionCommandQuickPickItem extends CommandQuickPick } execute(options?: TextDocumentShowOptions): Thenable { - return openEditor(this._uri, options); + return findOrOpenEditor(this._uri, options); } async onDidPressKey(key: Keys): Promise { diff --git a/src/quickpicks/commitQuickPick.ts b/src/quickpicks/commitQuickPick.ts index 9cb739a..5d929f4 100644 --- a/src/quickpicks/commitQuickPick.ts +++ b/src/quickpicks/commitQuickPick.ts @@ -8,7 +8,7 @@ import { CopyShaToClipboardCommandArgs, DiffDirectoryCommandArgs, DiffWithPreviousCommandArgs, - openEditor, + findOrOpenEditor, OpenWorkingFileCommandArgs, ShowQuickCommitDetailsCommandArgs, StashApplyCommandArgs, @@ -51,7 +51,7 @@ export class CommitWithFileStatusQuickPickItem extends CommandQuickPickItem { } execute(options?: TextDocumentShowOptions): Thenable { - return openEditor(GitUri.toRevisionUri(this.commit.sha, this._file, this.commit.repoPath), options); + return findOrOpenEditor(GitUri.toRevisionUri(this.commit.sha, this._file, this.commit.repoPath), options); } async onDidPressKey(key: Keys): Promise { @@ -129,7 +129,7 @@ export class OpenCommitFileRevisionsCommandQuickPickItem extends CommandQuickPic ); for (const uri of uris) { - await openEditor(uri, options); + await findOrOpenEditor(uri, options); } return undefined; } diff --git a/src/quickpicks/repoStatusQuickPick.ts b/src/quickpicks/repoStatusQuickPick.ts index 2d6f43c..b0c3af0 100644 --- a/src/quickpicks/repoStatusQuickPick.ts +++ b/src/quickpicks/repoStatusQuickPick.ts @@ -4,8 +4,8 @@ import { commands, QuickPickItem, TextDocumentShowOptions, TextEditor, window } import { Commands, DiffWithPreviousCommandArgs, + findOrOpenEditor, OpenChangedFilesCommandArgs, - openEditor, ShowQuickBranchHistoryCommandArgs, ShowQuickRepoStatusCommandArgs, ShowQuickStashListCommandArgs @@ -80,7 +80,7 @@ export class OpenStatusFileCommandQuickPickItem extends CommandQuickPickItem { } execute(options?: TextDocumentShowOptions): Thenable { - return openEditor(this._status.uri, options); + return findOrOpenEditor(this._status.uri, options); } async onDidPressKey(key: Keys): Promise { diff --git a/src/views/nodes/searchResultsCommitsNode.ts b/src/views/nodes/searchResultsCommitsNode.ts index a49dd1d..bbb9c01 100644 --- a/src/views/nodes/searchResultsCommitsNode.ts +++ b/src/views/nodes/searchResultsCommitsNode.ts @@ -1,7 +1,6 @@ 'use strict'; import { TreeItem, TreeItemCollapsibleState } from 'vscode'; -import { SearchCommitsCommandArgs } from '../../commands'; -import { Commands } from '../../commands/common'; +import { Commands, SearchCommitsCommandArgs } from '../../commands'; import { ViewWithFiles } from '../viewBase'; import { CommitsQueryResults, ResultsCommitsNode } from './resultsCommitsNode'; import { ResourceType, ViewNode } from './viewNode'; diff --git a/src/views/viewCommands.ts b/src/views/viewCommands.ts index f9ef68e..9a4db8f 100644 --- a/src/views/viewCommands.ts +++ b/src/views/viewCommands.ts @@ -5,8 +5,8 @@ import { DiffWithCommandArgs, DiffWithPreviousCommandArgs, DiffWithWorkingCommandArgs, + findOrOpenEditor, GitCommandsCommandArgs, - openEditor, OpenFileInRemoteCommandArgs, OpenFileRevisionCommandArgs, OpenWorkingFileCommandArgs @@ -752,7 +752,7 @@ export class ViewCommands { } } - return openEditor(uri, options.showOptions || { preserveFocus: true, preview: false }); + return findOrOpenEditor(uri, options.showOptions || { preserveFocus: true, preview: false }); } @debug() @@ -963,7 +963,7 @@ export class ViewCommands { for (const file of files) { const uri = GitUri.toRevisionUri(file.status === 'D' ? ref2 : ref1, file, repoPath); - await openEditor(uri, options); + await findOrOpenEditor(uri, options); } }