forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathissueFile.ts
More file actions
132 lines (119 loc) · 4.43 KB
/
Copy pathissueFile.ts
File metadata and controls
132 lines (119 loc) · 4.43 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { RepositoriesManager } from '../github/repositoriesManager';
export const NEW_ISSUE_SCHEME = 'newIssue';
export const NEW_ISSUE_FILE = 'NewIssue.md';
export const ASSIGNEES = vscode.l10n.t('Assignees:');
export const LABELS = vscode.l10n.t('Labels:');
const NEW_ISSUE_CACHE = 'newIssue.cache';
export function extractIssueOriginFromQuery(uri: vscode.Uri): vscode.Uri | undefined {
const query = JSON.parse(uri.query);
if (query.origin) {
return vscode.Uri.parse(query.origin);
}
}
export class IssueFileSystemProvider implements vscode.FileSystemProvider {
private content: Uint8Array | undefined;
private createTime: number = 0;
private modifiedTime: number = 0;
private _onDidChangeFile: vscode.EventEmitter<vscode.FileChangeEvent[]> = new vscode.EventEmitter<
vscode.FileChangeEvent[]
>();
constructor(private readonly cache: NewIssueCache) { };
onDidChangeFile: vscode.Event<vscode.FileChangeEvent[]> = this._onDidChangeFile.event;
watch(_uri: vscode.Uri, _options: { recursive: boolean; excludes: string[] }): vscode.Disposable {
const disposable = this.onDidChangeFile(e => {
if (e.length === 0 && e[0].type === vscode.FileChangeType.Deleted) {
disposable.dispose();
}
});
return disposable;
}
stat(_uri: vscode.Uri): vscode.FileStat {
return {
type: vscode.FileType.File,
ctime: this.createTime,
mtime: this.modifiedTime,
size: this.content?.length ?? 0,
};
}
readDirectory(_uri: vscode.Uri): [string, vscode.FileType][] | Thenable<[string, vscode.FileType][]> {
return [];
}
createDirectory(_uri: vscode.Uri): void { }
readFile(_uri: vscode.Uri): Uint8Array | Thenable<Uint8Array> {
return this.content ?? new Uint8Array(0);
}
writeFile(
uri: vscode.Uri,
content: Uint8Array,
_options: { create: boolean; overwrite: boolean } = { create: false, overwrite: false },
): void | Thenable<void> {
const oldContent = this.content;
this.content = content;
if (oldContent === undefined) {
this.createTime = new Date().getTime();
this._onDidChangeFile.fire([{ uri: uri, type: vscode.FileChangeType.Created }]);
} else {
this.modifiedTime = new Date().getTime();
this._onDidChangeFile.fire([{ uri: uri, type: vscode.FileChangeType.Changed }]);
}
this.cache.cache(content);
}
delete(uri: vscode.Uri, _options: { recursive: boolean }): void | Thenable<void> {
this.content = undefined;
this.createTime = 0;
this.modifiedTime = 0;
this._onDidChangeFile.fire([{ uri: uri, type: vscode.FileChangeType.Deleted }]);
}
rename(_oldUri: vscode.Uri, _newUri: vscode.Uri, _options: { overwrite: boolean }): void | Thenable<void> { }
}
export class LabelCompletionProvider implements vscode.CompletionItemProvider {
constructor(private manager: RepositoriesManager) { }
async provideCompletionItems(
document: vscode.TextDocument,
position: vscode.Position,
_token: vscode.CancellationToken,
_context: vscode.CompletionContext,
): Promise<vscode.CompletionItem[]> {
if (!document.lineAt(position.line).text.startsWith(LABELS)) {
return [];
}
const originFile = extractIssueOriginFromQuery(document.uri);
if (!originFile) {
return [];
}
const folderManager = this.manager.getManagerForFile(originFile);
if (!folderManager) {
return [];
}
const defaults = await folderManager.getPullRequestDefaults();
const labels = await folderManager.getLabels(undefined, defaults);
return labels.map(label => {
const item = new vscode.CompletionItem(label.name, vscode.CompletionItemKind.Color);
item.documentation = `#${label.color}`;
item.commitCharacters = [' ', ','];
return item;
});
}
}
export class NewIssueCache {
constructor(private readonly context: vscode.ExtensionContext) {
this.clear();
}
public cache(issueFileContent: Uint8Array) {
this.context.workspaceState.update(NEW_ISSUE_CACHE, issueFileContent);
}
public clear() {
this.context.workspaceState.update(NEW_ISSUE_CACHE, undefined);
}
public get(): string | undefined {
const content = this.context.workspaceState.get<Uint8Array | undefined>(NEW_ISSUE_CACHE);
if (content) {
return content.toString();
}
}
}