-
Notifications
You must be signed in to change notification settings - Fork 543
Expand file tree
/
Copy pathHelpCompletion.ts
More file actions
234 lines (197 loc) · 7.05 KB
/
HelpCompletion.ts
File metadata and controls
234 lines (197 loc) · 7.05 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
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import {
Disposable,
EndOfLine,
Range,
SnippetString,
type TextDocument,
type TextDocumentChangeEvent,
window,
workspace,
} from "vscode";
import { RequestType } from "vscode-languageclient";
import { LanguageClient } from "vscode-languageclient/node";
import { LanguageClientConsumer } from "../languageClientConsumer";
enum CommentType {
Disabled = "Disabled",
BlockComment = "BlockComment",
LineComment = "LineComment",
}
interface ICommentHelpRequestArguments {}
interface ICommentHelpRequestResponse {
content: string[];
}
export const CommentHelpRequestType = new RequestType<
ICommentHelpRequestArguments,
ICommentHelpRequestResponse,
void
>("powerShell/getCommentHelp");
enum SearchState {
Searching,
Locked,
Found,
}
export class HelpCompletionFeature extends LanguageClientConsumer {
private helpCompletionProvider: HelpCompletionProvider | undefined;
private disposable: Disposable | undefined;
constructor() {
super();
const helpCompletion = workspace
.getConfiguration("powershell")
.get<CommentType>("helpCompletion", CommentType.BlockComment);
if (helpCompletion !== CommentType.Disabled) {
this.helpCompletionProvider = new HelpCompletionProvider();
this.disposable = workspace.onDidChangeTextDocument(async (e) => {
await this.onEvent(e);
});
}
}
public dispose(): void {
this.disposable?.dispose();
}
public override onLanguageClientSet(languageClient: LanguageClient): void {
// Our helper class isn't in the session's list of language client
// consumers since we optionally create it, so we have to set it
// manually.
this.helpCompletionProvider?.onLanguageClientSet(languageClient);
}
public async onEvent(changeEvent: TextDocumentChangeEvent): Promise<void> {
// If it's not a PowerShell script, we don't care about it.
if (changeEvent.document.languageId !== "powershell") {
return;
}
if (changeEvent.contentChanges.length > 0) {
this.helpCompletionProvider?.updateState(
changeEvent.document,
changeEvent.contentChanges[0].text,
changeEvent.contentChanges[0].range,
);
// TODO: Raise an event when trigger is found, and attach complete() to the event.
if (this.helpCompletionProvider?.triggerFound) {
await this.helpCompletionProvider.complete();
this.helpCompletionProvider.reset();
}
}
}
}
class TriggerFinder {
private state: SearchState;
private document: TextDocument | undefined;
private count: number;
constructor(private triggerCharacters: string) {
this.state = SearchState.Searching;
this.count = 0;
}
public get found(): boolean {
return this.state === SearchState.Found;
}
public updateState(document: TextDocument, changeText: string): void {
switch (this.state) {
case SearchState.Searching:
if (
changeText.length === 1 &&
// eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
changeText[0] === this.triggerCharacters[this.count]
) {
this.state = SearchState.Locked;
this.document = document;
this.count++;
}
break;
case SearchState.Locked:
if (
document === this.document &&
changeText.length === 1 &&
// eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
changeText[0] === this.triggerCharacters[this.count]
) {
this.count++;
if (this.count === this.triggerCharacters.length) {
this.state = SearchState.Found;
}
} else {
this.reset();
}
break;
default:
this.reset();
break;
}
}
public reset(): void {
this.state = SearchState.Searching;
this.count = 0;
}
}
class HelpCompletionProvider extends LanguageClientConsumer {
private triggerFinderHelpComment: TriggerFinder;
private lastChangeRange: Range | undefined;
private lastDocument: TextDocument | undefined;
constructor() {
super();
this.triggerFinderHelpComment = new TriggerFinder("##");
}
public get triggerFound(): boolean {
return this.triggerFinderHelpComment.found;
}
public override onLanguageClientSet(
_languageClient: LanguageClient,
// eslint-disable-next-line @typescript-eslint/no-empty-function
): void {}
public updateState(
document: TextDocument,
changeText: string,
changeRange: Range,
): void {
this.lastDocument = document;
this.lastChangeRange = changeRange;
this.triggerFinderHelpComment.updateState(document, changeText);
}
public reset(): void {
this.triggerFinderHelpComment.reset();
}
public async complete(): Promise<void> {
if (
this.lastChangeRange === undefined ||
this.lastDocument === undefined
) {
return;
}
const triggerStartPos = this.lastChangeRange.start;
const doc = this.lastDocument;
const client = await LanguageClientConsumer.getLanguageClient();
const helpCompletion = workspace
.getConfiguration("powershell")
.get<CommentType>("helpCompletion", CommentType.BlockComment);
const result = await client.sendRequest(CommentHelpRequestType, {
documentUri: doc.uri.toString(),
triggerPosition: triggerStartPos,
blockComment: helpCompletion === CommentType.BlockComment,
});
if (result.content.length === 0) {
return;
}
const replaceRange = new Range(
triggerStartPos.translate(0, -1),
triggerStartPos.translate(0, 1),
);
// TODO: add indentation level to the help content
// Trim leading whitespace (used by the rule for indentation) as VSCode takes care of the indentation.
// Trim the last empty line and join the strings.
const lines: string[] = result.content;
const text = lines.map((x) => x.trimStart()).join(this.getEOL(doc.eol));
const snippetString = new SnippetString(text);
await window.activeTextEditor?.insertSnippet(
snippetString,
replaceRange,
);
}
private getEOL(eol: EndOfLine): string {
// there are only two type of EndOfLine types.
if (eol === EndOfLine.CRLF) {
return "\r\n";
}
return "\n";
}
}