Repository navigation
Expand file tree
/
Copy pathchanges.ts
More file actions
117 lines (105 loc) · 3.49 KB
/
Copy pathchanges.ts
File metadata and controls
117 lines (105 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* @file source control change file list
* @author netcon
*/
import * as vscode from 'vscode';
import repository, { FileChangeType } from '@/repository';
import router from '@/router';
import { basename } from '@/helpers/util';
import { GitHub1sFileSystemProvider } from '@/providers/fileSystemProvider';
import { PageType } from '@/router/types';
interface ChangedFile {
previousFileUri: vscode.Uri;
currentFileUri: vscode.Uri;
status: FileChangeType;
}
const getChangedFiles = async (): Promise<ChangedFile[]> => {
const routerState = await router.getState();
// current workspace root directory uri, the `ref` in uri authority should be `pull.head.sha`
let currentRootUri = vscode.Uri.parse('').with({
scheme: GitHub1sFileSystemProvider.scheme,
authority: await router.getAuthority(),
path: '/',
});
// github pull page
if (routerState.pageType === PageType.PULL) {
// record that current editor is opened by pull
currentRootUri = currentRootUri.with({
query: `pull=${routerState.pullNumber}`,
});
const { owner, repo } = await router.getState();
const baseRef = (await repository.getPull(routerState.pullNumber)).base.sha;
const baseRootUri = currentRootUri.with({
authority: `${owner}+${repo}+${baseRef}`,
});
const pullFiles = await repository.getPullFiles(routerState.pullNumber);
return pullFiles.map((pullFile) => {
// the `previous_filename` field only exists in `RENAMED` file,
// fallback to `filename` otherwise
const previousFilePath = pullFile.previous_filename || pullFile.filename;
const currentFilePath = pullFile.filename;
return {
previousFileUri: vscode.Uri.joinPath(baseRootUri, previousFilePath),
currentFileUri: vscode.Uri.joinPath(currentRootUri, currentFilePath),
status: pullFile.status,
};
});
}
return [];
};
const getChangedFileCommand = (changedFile: ChangedFile) => {
const previousFileUri = changedFile.previousFileUri;
const currentFileUri = changedFile.currentFileUri;
if (changedFile.status === FileChangeType.ADDED) {
const title = `${basename(currentFileUri.path)} (Added)`;
return {
title: 'Open',
command: 'vscode.open',
arguments: [currentFileUri, {}, title],
};
}
if (changedFile.status === FileChangeType.REMOVED) {
const title = `${basename(previousFileUri.path)} (Deleted)`;
return {
title: 'Open',
command: 'vscode.open',
arguments: [previousFileUri, {}, title],
};
}
if (changedFile.status === FileChangeType.MODIFIED) {
const title = `${basename(currentFileUri.path)} (Modified)`;
return {
title: 'Diff',
command: 'vscode.diff',
arguments: [previousFileUri, currentFileUri, title],
};
}
if (changedFile.status === FileChangeType.RENAMED) {
const title = `${basename(previousFileUri.path)} -> ${basename(
currentFileUri.path
)}`;
return {
title: 'Diff',
command: 'vscode.diff',
arguments: [previousFileUri, currentFileUri, title],
};
}
return undefined;
};
export const updateSourceControlChanges = (() => {
const sourceControl = vscode.scm.createSourceControl('github1s', 'GitHub1s');
const changesGroup = sourceControl.createResourceGroup('changes', 'Changes');
return async () => {
const changedFiles = await getChangedFiles();
changesGroup.resourceStates = changedFiles.map((changedFile) => {
return {
resourceUri: changedFile.currentFileUri,
decorations: {
strikeThrough: changedFile.status === FileChangeType.REMOVED,
tooltip: changedFile.status,
},
command: getChangedFileCommand(changedFile),
};
});
};
})();