Skip to content

Commit 6609cfa

Browse files
authored
feat: add open changes button to editor title (conwnet#270)
1 parent cd2122d commit 6609cfa

5 files changed

Lines changed: 102 additions & 30 deletions

File tree

‎extensions/github1s/package.json‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@
128128
"category": "GitHub1s",
129129
"icon": "$(globe)"
130130
},
131+
{
132+
"command": "github1s.editor-view-open-changes",
133+
"title": "Open Changes",
134+
"category": "GitHub1s",
135+
"icon": "$(compare-changes)"
136+
},
131137
{
132138
"command": "github1s.diff-view-open-left-file",
133139
"title": "Open Left File",
@@ -236,6 +242,10 @@
236242
"command": "github1s.commit-view-item-open-on-github",
237243
"when": "false"
238244
},
245+
{
246+
"command": "github1s.editor-view-open-changes",
247+
"when": "false"
248+
},
239249
{
240250
"command": "github1s.diff-view-open-left-file",
241251
"when": "false"
@@ -298,15 +308,20 @@
298308
}
299309
],
300310
"editor/title": [
311+
{
312+
"command": "github1s.editor-view-open-changes",
313+
"when": "!isInDiffEditor && github1s.context.showOpenChangesInEditorTitle",
314+
"group": "navigation@1"
315+
},
301316
{
302317
"command": "github1s.diff-view-open-left-file",
303318
"when": "isInDiffEditor",
304-
"group": "navigation@1"
319+
"group": "navigation@2"
305320
},
306321
{
307322
"command": "github1s.diff-view-open-right-file",
308323
"when": "isInDiffEditor",
309-
"group": "navigation@2"
324+
"group": "navigation@3"
310325
},
311326
{
312327
"command": "github1s.editor-view-open-prev-revision",

‎extensions/github1s/src/commands/editor.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,40 @@ import router from '@/router';
99
import repository from '@/repository';
1010
import { emptyFileUri } from '@/providers';
1111
import { basename } from '@/helpers/util';
12-
import { getChangedFileDiffTitle } from '@/source-control/changes';
12+
import {
13+
ChangedFile,
14+
getChangedFiles,
15+
getChangedFileCommand,
16+
getChangedFileDiffTitle,
17+
} from '@/source-control/changes';
1318
import { FileChangeType } from '@/repository/types';
1419

20+
export const getChangedFileFromSourceControl = async (
21+
fileUri: vscode.Uri
22+
): Promise<ChangedFile | undefined> => {
23+
// the file should belong to current workspace
24+
if (fileUri.authority) {
25+
return;
26+
}
27+
28+
return (await getChangedFiles()).find((changedFile) => {
29+
return changedFile.headFileUri.path === fileUri.path;
30+
});
31+
};
32+
33+
// open the diff editor of a file, such as click it in source-control-panel,
34+
// only work when we can found the corresponding file in source-control-panel
35+
export const commandEditorViewOpenChanges = async (fileUri: vscode.Uri) => {
36+
const changedFile = await getChangedFileFromSourceControl(fileUri);
37+
38+
if (!changedFile) {
39+
return;
40+
}
41+
42+
const command = await getChangedFileCommand(changedFile);
43+
vscode.commands.executeCommand(command.command, ...command.arguments);
44+
};
45+
1546
const openFileToEditor = async (fileUri) => {
1647
const isCurrentAuthority =
1748
fileUri.authority === (await router.getAuthority());

‎extensions/github1s/src/commands/index.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
} from './commit';
2525
import { commandOpenGitpod } from './gitpod';
2626
import {
27+
commandEditorViewOpenChanges,
2728
commandDiffViewOpenLeftFile,
2829
commandDiffViewOpenRightFile,
2930
commandEditorViewOpenNextRevision,
@@ -64,6 +65,8 @@ const commands: { id: string; callback: (...args: any[]) => any }[] = [
6465
// open current repository on gitpod
6566
{ id: 'github1s.open-gitpod', callback: commandOpenGitpod },
6667

68+
// open the changes of a file
69+
{ id: 'github1s.editor-view-open-changes', callback: commandEditorViewOpenChanges }, // prettier-ignore
6770
// open the left file in diff editor
6871
{ id: 'github1s.diff-view-open-left-file', callback: commandDiffViewOpenLeftFile }, // prettier-ignore
6972
// open the right file in diff editor

‎extensions/github1s/src/listeners/vscode.ts‎

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,58 @@
66
import * as vscode from 'vscode';
77
import router from '@/router';
88
import { PageType } from '@/router/types';
9+
import { getChangedFileFromSourceControl } from '@/commands/editor';
910
import { GitHub1sFileSystemProvider } from '@/providers/fileSystemProvider';
1011

11-
export const registerVSCodeEventListeners = () => {
12+
const handleRouterOnActiveEditorChange = async (
13+
editor: vscode.TextEditor | undefined
14+
) => {
1215
// replace current url when user change active editor
13-
vscode.window.onDidChangeActiveTextEditor(async (editor) => {
14-
const { owner, repo, ref, pageType } = await router.getState();
15-
const activeFileUri = editor?.document.uri;
16-
17-
// only `tree/blob` page will replace url with the active editor change
18-
if (![PageType.TREE, PageType.BLOB].includes(pageType)) {
19-
return;
20-
}
21-
22-
// if the file which not belong to current workspace is opened, or no file
23-
// is opened, only retain `owner` and `repo` (and `ref` if need) in browser url
24-
if (
25-
!activeFileUri ||
26-
activeFileUri?.authority ||
27-
activeFileUri?.scheme !== GitHub1sFileSystemProvider.scheme
28-
) {
29-
const browserPath =
30-
ref.toUpperCase() === 'HEAD'
31-
? `/${owner}/${repo}`
32-
: `/${owner}/${repo}/tree/${ref}`;
33-
router.history.replace(browserPath);
34-
return;
35-
}
36-
37-
const browserPath = `/${owner}/${repo}/blob/${ref}${activeFileUri.path}`;
16+
const { owner, repo, ref, pageType } = await router.getState();
17+
const activeFileUri = editor?.document.uri;
18+
19+
// only `tree/blob` page will replace url with the active editor change
20+
if (![PageType.TREE, PageType.BLOB].includes(pageType)) {
21+
return;
22+
}
23+
24+
// if the file which not belong to current workspace is opened, or no file
25+
// is opened, only retain `owner` and `repo` (and `ref` if need) in browser url
26+
if (
27+
!activeFileUri ||
28+
activeFileUri?.authority ||
29+
activeFileUri?.scheme !== GitHub1sFileSystemProvider.scheme
30+
) {
31+
const browserPath =
32+
ref.toUpperCase() === 'HEAD'
33+
? `/${owner}/${repo}`
34+
: `/${owner}/${repo}/tree/${ref}`;
3835
router.history.replace(browserPath);
36+
return;
37+
}
38+
39+
const browserPath = `/${owner}/${repo}/blob/${ref}${activeFileUri.path}`;
40+
router.history.replace(browserPath);
41+
};
42+
43+
// if the `Open Changes` Button should show in editor title
44+
const handleOpenChangesContextOnActiveEditorChange = async (
45+
editor: vscode.TextEditor | undefined
46+
) => {
47+
const changedFile = editor?.document.uri
48+
? await getChangedFileFromSourceControl(editor.document.uri)
49+
: undefined;
50+
51+
return vscode.commands.executeCommand(
52+
'setContext',
53+
'github1s.context.showOpenChangesInEditorTitle',
54+
!!changedFile
55+
);
56+
};
57+
58+
export const registerVSCodeEventListeners = () => {
59+
vscode.window.onDidChangeActiveTextEditor(async (editor) => {
60+
handleRouterOnActiveEditorChange(editor);
61+
handleOpenChangesContextOnActiveEditorChange(editor);
3962
});
4063
};

‎extensions/github1s/src/source-control/changes.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export const getCommitChangedFiles = async (commit: RepositoryCommit) => {
7979
});
8080
};
8181

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

8585
// github pull page

0 commit comments

Comments
 (0)