Skip to content

Commit

Permalink
feat: add open changes button to editor title (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
conwnet authored Apr 2, 2021
1 parent cd2122d commit 6609cfa
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 30 deletions.
19 changes: 17 additions & 2 deletions extensions/github1s/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@
"category": "GitHub1s",
"icon": "$(globe)"
},
{
"command": "github1s.editor-view-open-changes",
"title": "Open Changes",
"category": "GitHub1s",
"icon": "$(compare-changes)"
},
{
"command": "github1s.diff-view-open-left-file",
"title": "Open Left File",
Expand Down Expand Up @@ -236,6 +242,10 @@
"command": "github1s.commit-view-item-open-on-github",
"when": "false"
},
{
"command": "github1s.editor-view-open-changes",
"when": "false"
},
{
"command": "github1s.diff-view-open-left-file",
"when": "false"
Expand Down Expand Up @@ -298,15 +308,20 @@
}
],
"editor/title": [
{
"command": "github1s.editor-view-open-changes",
"when": "!isInDiffEditor && github1s.context.showOpenChangesInEditorTitle",
"group": "navigation@1"
},
{
"command": "github1s.diff-view-open-left-file",
"when": "isInDiffEditor",
"group": "navigation@1"
"group": "navigation@2"
},
{
"command": "github1s.diff-view-open-right-file",
"when": "isInDiffEditor",
"group": "navigation@2"
"group": "navigation@3"
},
{
"command": "github1s.editor-view-open-prev-revision",
Expand Down
33 changes: 32 additions & 1 deletion extensions/github1s/src/commands/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,40 @@ import router from '@/router';
import repository from '@/repository';
import { emptyFileUri } from '@/providers';
import { basename } from '@/helpers/util';
import { getChangedFileDiffTitle } from '@/source-control/changes';
import {
ChangedFile,
getChangedFiles,
getChangedFileCommand,
getChangedFileDiffTitle,
} from '@/source-control/changes';
import { FileChangeType } from '@/repository/types';

export const getChangedFileFromSourceControl = async (
fileUri: vscode.Uri
): Promise<ChangedFile | undefined> => {
// the file should belong to current workspace
if (fileUri.authority) {
return;
}

return (await getChangedFiles()).find((changedFile) => {
return changedFile.headFileUri.path === fileUri.path;
});
};

// open the diff editor of a file, such as click it in source-control-panel,
// only work when we can found the corresponding file in source-control-panel
export const commandEditorViewOpenChanges = async (fileUri: vscode.Uri) => {
const changedFile = await getChangedFileFromSourceControl(fileUri);

if (!changedFile) {
return;
}

const command = await getChangedFileCommand(changedFile);
vscode.commands.executeCommand(command.command, ...command.arguments);
};

const openFileToEditor = async (fileUri) => {
const isCurrentAuthority =
fileUri.authority === (await router.getAuthority());
Expand Down
3 changes: 3 additions & 0 deletions extensions/github1s/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from './commit';
import { commandOpenGitpod } from './gitpod';
import {
commandEditorViewOpenChanges,
commandDiffViewOpenLeftFile,
commandDiffViewOpenRightFile,
commandEditorViewOpenNextRevision,
Expand Down Expand Up @@ -64,6 +65,8 @@ const commands: { id: string; callback: (...args: any[]) => any }[] = [
// open current repository on gitpod
{ id: 'github1s.open-gitpod', callback: commandOpenGitpod },

// open the changes of a file
{ id: 'github1s.editor-view-open-changes', callback: commandEditorViewOpenChanges }, // prettier-ignore
// open the left file in diff editor
{ id: 'github1s.diff-view-open-left-file', callback: commandDiffViewOpenLeftFile }, // prettier-ignore
// open the right file in diff editor
Expand Down
75 changes: 49 additions & 26 deletions extensions/github1s/src/listeners/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,58 @@
import * as vscode from 'vscode';
import router from '@/router';
import { PageType } from '@/router/types';
import { getChangedFileFromSourceControl } from '@/commands/editor';
import { GitHub1sFileSystemProvider } from '@/providers/fileSystemProvider';

export const registerVSCodeEventListeners = () => {
const handleRouterOnActiveEditorChange = async (
editor: vscode.TextEditor | undefined
) => {
// replace current url when user change active editor
vscode.window.onDidChangeActiveTextEditor(async (editor) => {
const { owner, repo, ref, pageType } = await router.getState();
const activeFileUri = editor?.document.uri;

// only `tree/blob` page will replace url with the active editor change
if (![PageType.TREE, PageType.BLOB].includes(pageType)) {
return;
}

// if the file which not belong to current workspace is opened, or no file
// is opened, only retain `owner` and `repo` (and `ref` if need) in browser url
if (
!activeFileUri ||
activeFileUri?.authority ||
activeFileUri?.scheme !== GitHub1sFileSystemProvider.scheme
) {
const browserPath =
ref.toUpperCase() === 'HEAD'
? `/${owner}/${repo}`
: `/${owner}/${repo}/tree/${ref}`;
router.history.replace(browserPath);
return;
}

const browserPath = `/${owner}/${repo}/blob/${ref}${activeFileUri.path}`;
const { owner, repo, ref, pageType } = await router.getState();
const activeFileUri = editor?.document.uri;

// only `tree/blob` page will replace url with the active editor change
if (![PageType.TREE, PageType.BLOB].includes(pageType)) {
return;
}

// if the file which not belong to current workspace is opened, or no file
// is opened, only retain `owner` and `repo` (and `ref` if need) in browser url
if (
!activeFileUri ||
activeFileUri?.authority ||
activeFileUri?.scheme !== GitHub1sFileSystemProvider.scheme
) {
const browserPath =
ref.toUpperCase() === 'HEAD'
? `/${owner}/${repo}`
: `/${owner}/${repo}/tree/${ref}`;
router.history.replace(browserPath);
return;
}

const browserPath = `/${owner}/${repo}/blob/${ref}${activeFileUri.path}`;
router.history.replace(browserPath);
};

// if the `Open Changes` Button should show in editor title
const handleOpenChangesContextOnActiveEditorChange = async (
editor: vscode.TextEditor | undefined
) => {
const changedFile = editor?.document.uri
? await getChangedFileFromSourceControl(editor.document.uri)
: undefined;

return vscode.commands.executeCommand(
'setContext',
'github1s.context.showOpenChangesInEditorTitle',
!!changedFile
);
};

export const registerVSCodeEventListeners = () => {
vscode.window.onDidChangeActiveTextEditor(async (editor) => {
handleRouterOnActiveEditorChange(editor);
handleOpenChangesContextOnActiveEditorChange(editor);
});
};
2 changes: 1 addition & 1 deletion extensions/github1s/src/source-control/changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const getCommitChangedFiles = async (commit: RepositoryCommit) => {
});
};

const getChangedFiles = async (): Promise<ChangedFile[]> => {
export const getChangedFiles = async (): Promise<ChangedFile[]> => {
const routerState = await router.getState();

// github pull page
Expand Down

0 comments on commit 6609cfa

Please sign in to comment.