Repository navigation
Expand file tree
/
Copy patheditor.ts
More file actions
174 lines (152 loc) · 6.13 KB
/
Copy patheditor.ts
File metadata and controls
174 lines (152 loc) · 6.13 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* @file Editor Related Commands
* @author netcon
*/
import * as vscode from 'vscode';
import queryString from 'query-string';
import router from '@/router';
import { emptyFileUri } from '@/providers';
import { supportsCommitFeatures } from '@/adapters';
import { FileChangeStatus } from '@/adapters/types';
import { Repository } from '@/repository';
import { getChangedFiles, getChangedFileDiffCommand, getChangedFileDiffTitle } from '@/changes/files';
import { omit } from '@/helpers/util';
export const getChangedFileFromSourceControl = async (fileUri: vscode.Uri) => {
// 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
const commandDiffChangedFile = async (fileUri: vscode.Uri) => {
const changedFile = await getChangedFileFromSourceControl(fileUri);
if (!changedFile) {
return;
}
const command = await getChangedFileDiffCommand(changedFile);
vscode.commands.executeCommand(command.command, ...(command.arguments || []));
};
const isRepositoryFileUri = async (uri: vscode.Uri | undefined): Promise<boolean> => {
return !!uri && supportsCommitFeatures(uri.scheme);
};
const getActiveDiffInput = (resource?: vscode.Uri): vscode.TabInputTextDiff | undefined => {
const input = vscode.window.tabGroups.activeTabGroup.activeTab?.input;
if (!(input instanceof vscode.TabInputTextDiff)) {
return;
}
// Title actions receive the modified URI. Ignore actions targeting another diff.
if (resource && resource.toString() !== input.modified.toString()) {
return;
}
return input;
};
const createCommandDiffViewOpenFile = (side: 'original' | 'modified') => async (resource?: vscode.Uri) => {
const fileUri = getActiveDiffInput(resource)?.[side];
if (fileUri && fileUri?.scheme !== emptyFileUri.scheme) {
await vscode.commands.executeCommand('workbench.action.keepEditor');
return vscode.commands.executeCommand('vscode.open', fileUri, {});
}
};
const resolveOpenFileRevisionArgs = async (
fileUri: vscode.Uri | undefined,
direction: 'previous' | 'next',
): Promise<[vscode.Uri, string]> => {
let baseUri: vscode.Uri | undefined, from: string | undefined;
const getQueryFrom = (uri: vscode.Uri): string | undefined => {
return queryString.parse(uri.query).from as string | undefined;
};
const textDiffInput = getActiveDiffInput(fileUri);
if (textDiffInput) {
// this is a diff editor
const { original, modified } = textDiffInput;
const [hasLeftFile, hasRightFile] = await Promise.all([
isRepositoryFileUri(original),
isRepositoryFileUri(modified),
]);
if (direction === 'previous' && hasLeftFile) {
baseUri = original;
}
if (direction === 'next' && hasRightFile) {
baseUri = modified;
}
if (hasRightFile) {
from = getQueryFrom(modified);
}
} else if (fileUri && (await isRepositoryFileUri(fileUri))) {
// this is a single file editor
from = getQueryFrom(fileUri);
baseUri = fileUri;
}
if (!baseUri) {
throw new Error('Unable to resolve the target file.');
}
if (!from) {
// If 'from' cannot be obtained in the query, use the ref of baseUri as 'from'
const { scheme, repo, ref, path } = router.parseUri(baseUri);
const repository = Repository.getInstance(scheme, repo);
from = (await repository.getFileLatestCommit(ref, path))?.sha;
if (!from) {
throw new Error('Unable to resolve the latest commit for this file.');
}
baseUri = router.buildUri({ ref: from }, baseUri);
}
return [baseUri, from];
};
const createCommandOpenFileRevision = (direction: 'previous' | 'next') => async (fileUri?: vscode.Uri) => {
try {
const [baseUri, from] = await resolveOpenFileRevisionArgs(fileUri, direction);
const { scheme, repo, ref, path } = router.parseUri(baseUri);
const repository = Repository.getInstance(scheme, repo);
const baseSha = (await repository.getCommitItem(ref))?.sha;
if (!baseSha) {
throw new Error('Unable to resolve the commit for this file.');
}
let leftFileUri: vscode.Uri | undefined, rightFileUri: vscode.Uri | undefined;
if (direction === 'previous') {
const prevCommit = await repository.getPreviousCommit(baseSha, path, from);
leftFileUri = prevCommit ? router.buildUri({ ref: prevCommit.sha }, baseUri) : emptyFileUri;
rightFileUri = baseUri;
} else {
const nextCommit = await repository.getNextCommit(baseSha, path, from);
if (!nextCommit) throw new Error('Unable to find next commit for this file.');
leftFileUri = baseUri;
rightFileUri = router.buildUri({ ref: nextCommit.sha }, baseUri);
}
const hasNext = router.parseUri(rightFileUri).ref !== from || undefined;
const leftQuery = queryString.stringify(omit(queryString.parse(baseUri.query), ['from']));
const rightQuery = queryString.stringify({ ...queryString.parse(baseUri.query), from, hasNext });
if (fileUri && !queryString.parse(fileUri.query).from) {
await vscode.commands.executeCommand('workbench.action.keepEditor');
}
return await vscode.commands.executeCommand(
'vscode.diff',
leftFileUri.with({ query: leftQuery }),
rightFileUri.with({ query: rightQuery }),
getChangedFileDiffTitle(leftFileUri, rightFileUri, FileChangeStatus.Modified),
);
} catch (error) {
return vscode.window.showErrorMessage(`Unable to open file revision: ${error.message}`);
}
};
export const registerEditorCommands = (context: vscode.ExtensionContext) => {
return context.subscriptions.push(
vscode.commands.registerCommand('github1s.commands.diffChangedFile', commandDiffChangedFile),
vscode.commands.registerCommand(
'github1s.commands.diffViewOpenLeftFile',
createCommandDiffViewOpenFile('original'),
),
vscode.commands.registerCommand(
'github1s.commands.diffViewOpenRightFile',
createCommandDiffViewOpenFile('modified'),
),
vscode.commands.registerCommand(
'github1s.commands.openFilePreviousRevision',
createCommandOpenFileRevision('previous'),
),
vscode.commands.registerCommand('github1s.commands.openFileNextRevision', createCommandOpenFileRevision('next')),
);
};