This repository has been archived by the owner on Apr 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfour.js
496 lines (416 loc) · 14.7 KB
/
four.js
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// this is four
import * as paths from "path";
import { CodemarkType, CSMarkerIdentifier } from "@codestream/protocols/api";
import { Editor } from "extensions/editor";
import { commands, Disposable, env, Range, Uri, ViewColumn, window, workspace } from "vscode";
import { SessionSignedOutReason, StreamThread } from "./api/session";
import { TokenManager } from "./api/tokenManager";
import { WorkspaceState } from "./common";
import { BuiltInCommands } from "./constants";
import { Container } from "./container";
import { Logger } from "./logger";
import { Command, createCommandDecorator, Strings } from "./system";
const commandRegistry: Command[] = [];
const command = createCommandDecorator(commandRegistry);
export interface InsertTextCommandArgs {
text: string;
marker: CSMarkerIdentifier;
indentAfterInsert?: boolean;
}
export interface ApplyMarkerCommandArgs {
marker: CSMarkerIdentifier;
}
export interface ShowMarkerDiffCommandArgs {
marker: CSMarkerIdentifier;
}
export interface ShowReviewDiffCommandArgs {
reviewId: string;
repoId: string;
path: string;
}
export interface ShowReviewLocalDiffCommandArgs {
repoId: string;
path: string;
includeSaved: boolean;
includeStaged: boolean;
baseSha: string;
}
export interface CloseReviewDiffCommandArgs {}
export interface GotoCodemarkCommandArgs {
source?: string;
index: number;
}
export interface NewCodemarkCommandArgs {
source?: string;
}
export interface NewReviewCommandArgs {
source?: string;
}
export interface OpenCodemarkCommandArgs {
codemarkId: string;
onlyWhenVisible?: boolean;
sourceUri?: Uri;
}
export interface OpenReviewCommandArgs {
reviewId: string;
onlyWhenVisible?: boolean;
sourceUri?: Uri;
}
export interface OpenStreamCommandArgs {
streamThread: StreamThread;
}
export class Commands implements Disposable {
private readonly _disposable: Disposable;
constructor() {
this._disposable = Disposable.from(
...commandRegistry.map(({ name, method }) =>
commands.registerCommand(name, (...args: any[]) => method.apply(this, args))
),
commands.registerCommand("workbench.view.extension.codestream", () =>
Container.webview.show()
)
);
}
dispose() {
this._disposable && this._disposable.dispose();
}
@command("goOffline")
goOffline() {
return Container.session.goOffline();
}
@command("insertText", { showErrorMessage: "Unable to insertText" })
async insertText(args: InsertTextCommandArgs): Promise<boolean> {
const editor = await this.openWorkingFileForMarkerCore(args.marker);
if (editor === undefined) return false;
const resp = await Container.agent.documentMarkers.getDocumentFromMarker(args.marker);
if (resp === undefined) return false;
const line = resp.range.start.line;
await editor.edit(builder => {
builder.replace(new Range(line, 0, line, 0), args.text);
});
if (args.indentAfterInsert) {
await Editor.selectRange(editor.document.uri, new Range(line, 0, line + 10, 0), undefined, {
preserveFocus: false
});
await commands.executeCommand(BuiltInCommands.IndentSelection);
await commands.executeCommand(BuiltInCommands.FormatSelection);
}
return true;
}
@command("applyMarker", { showErrorMessage: "Unable to open comment" })
async applyMarker(args: ApplyMarkerCommandArgs): Promise<boolean> {
const editor = await this.openWorkingFileForMarkerCore(args.marker);
if (editor === undefined) return false;
const resp = await Container.agent.documentMarkers.getDocumentFromMarker(args.marker);
if (resp === undefined) return false;
return editor.edit(builder => {
builder.replace(
new Range(
resp.range.start.line,
resp.range.start.character,
resp.range.end.line,
resp.range.end.character
),
resp.marker.code
);
});
}
@command("showMarkerDiff", { showErrorMessage: "Unable to open comment" })
async showMarkerDiff(args: ShowMarkerDiffCommandArgs): Promise<boolean> {
const resp = await Container.agent.documentMarkers.getDocumentFromMarker(args.marker);
if (resp === undefined) return false;
const originalUri = Uri.parse(resp.textDocument.uri);
const markerId: CSMarkerIdentifier = {
id: args.marker.id,
file: args.marker.file,
repoId: args.marker.repoId
};
const patchedUri = originalUri.with({
scheme: "codestream-patch",
query: encodeURIComponent(JSON.stringify(markerId))
});
const fileName = paths.basename(originalUri.fsPath);
// Try to designate the diff view in the column to the left the webview
// FYI, this doesn't always work, see https://github.com/Microsoft/vscode/issues/56097
let column = Container.webview.viewColumn as number | undefined;
if (column !== undefined) {
column--;
if (column <= 0) {
column = undefined;
}
}
await commands.executeCommand(
BuiltInCommands.Diff,
originalUri,
patchedUri,
`${fileName} \u27f7 ${fileName} (patched)`,
{
preserveFocus: false,
preview: true,
viewColumn: column || ViewColumn.Beside
}
);
return true;
}
@command("showReviewDiff", { showErrorMessage: "Unable to display review diff" })
async showReviewDiff(args: ShowReviewDiffCommandArgs): Promise<boolean> {
await Container.diffContents.loadContents(args.reviewId, args.repoId, args.path);
const { review } = await Container.agent.reviews.get(args.reviewId);
// FYI, see showMarkerDiff() above
let column = Container.webview.viewColumn as number | undefined;
if (column !== undefined) {
column--;
if (column <= 0) {
column = undefined;
}
}
await commands.executeCommand(
BuiltInCommands.Diff,
Uri.parse(`codestream-diff://${args.reviewId}/${args.repoId}/left/${args.path}`),
Uri.parse(`codestream-diff://${args.reviewId}/${args.repoId}/right/${args.path}`),
`${paths.basename(args.path)} @ ${Strings.truncate(review.title, 25)}`,
{ preserveFocus: false, preview: true, viewColumn: column || ViewColumn.Beside }
);
return true;
}
@command("showReviewLocalDiff", { showErrorMessage: "Unable to display review local diff" })
async showReviewLocalDiff(args: ShowReviewLocalDiffCommandArgs): Promise<boolean> {
// FYI, see showMarkerDiff() above
let column = Container.webview.viewColumn as number | undefined;
if (column !== undefined) {
column--;
if (column <= 0) {
column = undefined;
}
}
const rightVersion = args.includeSaved ? "saved" : args.includeStaged ? "staged" : "head";
await Container.diffContents.loadContentsLocal(
args.repoId,
args.path,
args.baseSha,
rightVersion
);
await commands.executeCommand(
BuiltInCommands.Diff,
Uri.parse(`codestream-diff://local/${args.repoId}/left/${args.path}`),
Uri.parse(`codestream-diff://local/${args.repoId}/right/${args.path}`),
`${paths.basename(args.path)} review changes`,
{ preserveFocus: false, preview: true, viewColumn: column || ViewColumn.Beside }
);
return true;
}
@command("closeReviewDiff", { showErrorMessage: "Unable to close review diff" })
async closeReviewDiff(_args: CloseReviewDiffCommandArgs): Promise<boolean> {
for (const e of window.visibleTextEditors) {
const uri = Uri.parse(e.document.uri.toString(false));
if (uri.scheme === "codestream-diff") {
// FIXME -- this is where we should close the tab, but vscode
// doesn't provide the right API call yet to do that
// await e.show(e.viewColumn);
// await commands.executeCommand("workbench.action.closeActiveEditor");
}
}
return true;
}
@command("newComment", { showErrorMessage: "Unable to add comment" })
newComment(args?: NewCodemarkCommandArgs) {
return this.newCodemarkRequest(CodemarkType.Comment, args);
}
@command("newIssue", { showErrorMessage: "Unable to create issue" })
newIssue(args?: NewCodemarkCommandArgs) {
return this.newCodemarkRequest(CodemarkType.Issue, args);
}
@command("newReview", { showErrorMessage: "Unable to request a review" })
newReview(args?: NewReviewCommandArgs) {
return this.newReviewRequest(args);
}
@command("showNextChangedFile", { showErrorMessage: "Unable to show next changed file" })
showNextChangedFile() {
return this.showNextChangedFileRequest();
}
@command("showPreviousChangedFile", { showErrorMessage: "Unable to show previous changed file" })
showPreviousChangedFile() {
return this.showPreviousChangedFileRequest();
}
@command("newBookmark", { showErrorMessage: "Unable to add bookmark" })
newBookmark(args?: NewCodemarkCommandArgs) {
return this.newCodemarkRequest(CodemarkType.Bookmark, args);
}
@command("newPermalink", { showErrorMessage: "Unable to get permalink" })
newPermalink(args?: NewCodemarkCommandArgs) {
return this.newCodemarkRequest(CodemarkType.Link, args);
}
@command("copyPermalink", { showErrorMessage: "Unable to copy permalink" })
async copyPermalink(_args?: NewCodemarkCommandArgs) {
const editor = window.activeTextEditor;
if (editor === undefined) return;
const response = await Container.agent.documentMarkers.createPermalink(
editor.document.uri,
editor.selection,
"private"
);
if (response === undefined) return;
return env.clipboard.writeText(response.linkUrl);
}
@command("gotoCodemark0", {
args: ([args]) => [{ ...(args || {}), index: 0 }],
showErrorMessage: "Unable to jump to codemark #0"
})
@command("gotoCodemark1", {
args: ([args]) => [{ ...(args || {}), index: 1 }],
showErrorMessage: "Unable to jump to codemark #1"
})
@command("gotoCodemark2", {
args: ([args]) => [{ ...(args || {}), index: 2 }],
showErrorMessage: "Unable to jump to codemark #2"
})
@command("gotoCodemark3", {
args: ([args]) => [{ ...(args || {}), index: 3 }],
showErrorMessage: "Unable to jump to codemark #3"
})
@command("gotoCodemark4", {
args: ([args]) => [{ ...(args || {}), index: 4 }],
showErrorMessage: "Unable to jump to codemark #4"
})
@command("gotoCodemark5", {
args: ([args]) => [{ ...(args || {}), index: 5 }],
showErrorMessage: "Unable to jump to codemark #5"
})
@command("gotoCodemark6", {
args: ([args]) => [{ ...(args || {}), index: 6 }],
showErrorMessage: "Unable to jump to codemark #6"
})
@command("gotoCodemark7", {
args: ([args]) => [{ ...(args || {}), index: 7 }],
showErrorMessage: "Unable to jump to codemark #7"
})
@command("gotoCodemark8", {
args: ([args]) => [{ ...(args || {}), index: 8 }],
showErrorMessage: "Unable to jump to codemark #8"
})
@command("gotoCodemark9", {
args: ([args]) => [{ ...(args || {}), index: 9 }],
showErrorMessage: "Unable to jump to codemark #9"
})
async gotoCodemark(args?: GotoCodemarkCommandArgs) {
if (args === undefined) return;
Container.agent.telemetry.track("Codemark Clicked", { "Codemark Location": "Shortcut" });
const response = await Container.agent.documentMarkers.getDocumentFromKeyBinding(args.index);
if (response == null) return;
const uri = Uri.parse(response.textDocument.uri);
await Editor.selectRange(
uri,
new Range(
response.range.start.line,
response.range.start.character,
response.range.start.line,
response.range.start.character
),
undefined,
{
preserveFocus: false
}
);
await Container.webview.openCodemark(response.marker.codemarkId, {
onlyWhenVisible: true,
sourceUri: uri
});
}
@command("openCodemark", { showErrorMessage: "Unable to open comment" })
async openCodemark(args: OpenCodemarkCommandArgs): Promise<void> {
if (args === undefined) return;
Container.agent.telemetry.track("Codemark Clicked", { "Codemark Location": "Source File" });
const { codemarkId: _codemarkId, ...options } = args;
return Container.webview.openCodemark(args.codemarkId, options);
}
@command("openReview", { showErrorMessage: "Unable to open review" })
async openReview(args: OpenReviewCommandArgs): Promise<void> {
if (args === undefined) return;
Container.agent.telemetry.track("Review Clicked", { "Review Location": "Source File" });
const { reviewId: _reviewId, ...options } = args;
return Container.webview.openReview(args.reviewId, options);
}
@command("openStream", { showErrorMessage: "Unable to open stream" })
async openStream(args: OpenStreamCommandArgs): Promise<StreamThread | undefined> {
if (args == null || args.streamThread === undefined) return undefined;
return Container.webview.show(args.streamThread);
}
@command("signIn", { customErrorHandling: true })
async signIn() {
try {
const token = await TokenManager.get(Container.config.serverUrl, Container.config.email);
if (!token) {
await Container.context.workspaceState.update(WorkspaceState.TeamId, undefined);
await Container.webview.show();
} else {
await Container.session.login(Container.config.email, token);
}
} catch (ex) {
Logger.error(ex);
}
}
@command("signOut")
async signOut(reason = SessionSignedOutReason.UserSignedOutFromExtension) {
try {
if (reason === SessionSignedOutReason.UserSignedOutFromExtension) {
Container.webview.hide();
}
await Container.session.logout(reason);
} catch (ex) {
Logger.error(ex);
}
}
@command("toggle")
async toggle() {
try {
return await Container.webview.toggle();
} catch (ex) {
Logger.error(ex);
}
}
private async newCodemarkRequest(type: CodemarkType, args: NewCodemarkCommandArgs = {}) {
const editor = window.activeTextEditor;
// if (editor === undefined) return;
await Container.webview.newCodemarkRequest(type, editor, args.source || "Context Menu");
}
private async newReviewRequest(args: NewCodemarkCommandArgs = {}) {
const editor = window.activeTextEditor;
// if (editor === undefined) return;
await Container.webview.newReviewRequest(editor, args.source || "Context Menu");
}
private async showNextChangedFileRequest() {
await Container.webview.showNextChangedFile();
}
private async showPreviousChangedFileRequest() {
await Container.webview.showPreviousChangedFile();
}
private async openWorkingFileForMarkerCore(marker: CSMarkerIdentifier) {
const resp = await Container.agent.documentMarkers.getDocumentFromMarker(marker);
if (resp === undefined || resp === null) return undefined;
const uri = Uri.parse(resp.textDocument.uri);
const normalizedUri = uri.toString(false);
const editor = window.activeTextEditor;
if (editor !== undefined && editor.document.uri.toString(false) === normalizedUri) {
return editor;
}
for (const e of window.visibleTextEditors) {
if (e.document.uri.toString(false) === normalizedUri) {
return window.showTextDocument(e.document, e.viewColumn);
}
}
// FYI, this doesn't always work, see https://github.com/Microsoft/vscode/issues/56097
let column = Container.webview.viewColumn as number | undefined;
if (column !== undefined) {
column--;
if (column <= 0) {
column = undefined;
}
}
const document = await workspace.openTextDocument();
return window.showTextDocument(document, {
preserveFocus: false,
preview: false,
viewColumn: column || ViewColumn.Beside
});
}
}