Browse Source

Changes to re-use open editor if possible

main
Eric Amodio 5 years ago
parent
commit
a2a7c605b8
12 changed files with 75 additions and 24 deletions
  1. +52
    -0
      src/commands/common.ts
  2. +2
    -2
      src/commands/diffWithPrevious.ts
  3. +2
    -2
      src/commands/openChangedFiles.ts
  4. +2
    -2
      src/commands/openFileRevision.ts
  5. +2
    -2
      src/commands/openFileRevisionFrom.ts
  6. +2
    -2
      src/commands/openRevisionFile.ts
  7. +2
    -2
      src/commands/openWorkingFile.ts
  8. +2
    -2
      src/quickpicks/commitFileQuickPick.ts
  9. +3
    -3
      src/quickpicks/commitQuickPick.ts
  10. +2
    -2
      src/quickpicks/repoStatusQuickPick.ts
  11. +1
    -2
      src/views/nodes/searchResultsCommitsNode.ts
  12. +3
    -3
      src/views/viewCommands.ts

+ 52
- 0
src/commands/common.ts View File

@ -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<TextEditor | undefined> {
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 } = {}

+ 2
- 2
src/commands/diffWithPrevious.ts View File

@ -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) {

+ 2
- 2
src/commands/openChangedFiles.ts View File

@ -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;

+ 2
- 2
src/commands/openFileRevision.ts View File

@ -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);

+ 2
- 2
src/commands/openFileRevisionFrom.ts View File

@ -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
});

+ 2
- 2
src/commands/openRevisionFile.ts View File

@ -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);

+ 2
- 2
src/commands/openWorkingFile.ts View File

@ -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);

+ 2
- 2
src/quickpicks/commitFileQuickPick.ts View File

@ -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<TextEditor | undefined> {
return openEditor(this._uri, options);
return findOrOpenEditor(this._uri, options);
}
async onDidPressKey(key: Keys): Promise<void> {

+ 3
- 3
src/quickpicks/commitQuickPick.ts View File

@ -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<TextEditor | undefined> {
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<void> {
@ -129,7 +129,7 @@ export class OpenCommitFileRevisionsCommandQuickPickItem extends CommandQuickPic
);
for (const uri of uris) {
await openEditor(uri, options);
await findOrOpenEditor(uri, options);
}
return undefined;
}

+ 2
- 2
src/quickpicks/repoStatusQuickPick.ts View File

@ -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<TextEditor | undefined> {
return openEditor(this._status.uri, options);
return findOrOpenEditor(this._status.uri, options);
}
async onDidPressKey(key: Keys): Promise<void> {

+ 1
- 2
src/views/nodes/searchResultsCommitsNode.ts View File

@ -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';

+ 3
- 3
src/views/viewCommands.ts View File

@ -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);
}
}

Loading…
Cancel
Save