forked from PowerShell/vscode-powershell
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHelpCompletion.ts
201 lines (165 loc) · 6.49 KB
/
HelpCompletion.ts
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
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import { Disposable, EndOfLine, Position, Range, SnippetString,
TextDocument, TextDocumentChangeEvent, window, workspace } from "vscode";
import { LanguageClient, RequestType } from "vscode-languageclient";
import { IFeature } from "../feature";
import { Logger } from "../logging";
import Settings = require("../settings");
export const CommentHelpRequestType =
new RequestType<any, any, void, void>("powerShell/getCommentHelp");
interface ICommentHelpRequestParams {
documentUri: string;
triggerPosition: Position;
blockComment: boolean;
}
interface ICommentHelpRequestResult {
content: string[];
}
enum SearchState { Searching, Locked, Found }
export class HelpCompletionFeature implements IFeature {
private helpCompletionProvider: HelpCompletionProvider;
private languageClient: LanguageClient;
private disposable: Disposable;
private settings: Settings.ISettings;
constructor(private log: Logger) {
this.settings = Settings.load();
if (this.settings.helpCompletion !== Settings.HelpCompletion.Disabled) {
this.helpCompletionProvider = new HelpCompletionProvider();
const subscriptions = [];
workspace.onDidChangeTextDocument(this.onEvent, this, subscriptions);
this.disposable = Disposable.from(...subscriptions);
}
}
public dispose() {
if (this.disposable) {
this.disposable.dispose();
}
}
public setLanguageClient(languageClient: LanguageClient) {
this.languageClient = languageClient;
if (this.helpCompletionProvider) {
this.helpCompletionProvider.languageClient = languageClient;
}
}
public async onEvent(changeEvent: TextDocumentChangeEvent): Promise<void> {
if (!(changeEvent && changeEvent.contentChanges)) {
this.log.writeWarning(`<${HelpCompletionFeature.name}>: ` +
`Bad TextDocumentChangeEvent message: ${JSON.stringify(changeEvent)}`);
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();
await this.helpCompletionProvider.reset();
}
}
}
}
class TriggerFinder {
private state: SearchState;
private document: TextDocument;
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 && 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 &&
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 {
private triggerFinderHelpComment: TriggerFinder;
private lastChangeText: string;
private lastChangeRange: Range;
private lastDocument: TextDocument;
private langClient: LanguageClient;
private settings: Settings.ISettings;
constructor() {
this.triggerFinderHelpComment = new TriggerFinder("##");
this.settings = Settings.load();
}
public get triggerFound(): boolean {
return this.triggerFinderHelpComment.found;
}
public set languageClient(value: LanguageClient) {
this.langClient = value;
}
public updateState(document: TextDocument, changeText: string, changeRange: Range): void {
this.lastDocument = document;
this.lastChangeText = changeText;
this.lastChangeRange = changeRange;
this.triggerFinderHelpComment.updateState(document, changeText);
}
public reset(): void {
this.triggerFinderHelpComment.reset();
}
public async complete(): Promise<void> {
if (this.langClient === undefined) {
return;
}
const triggerStartPos = this.lastChangeRange.start;
const doc = this.lastDocument;
const result = await this.langClient.sendRequest(CommentHelpRequestType, {
documentUri: doc.uri.toString(),
triggerPosition: triggerStartPos,
blockComment: this.settings.helpCompletion === Settings.HelpCompletion.BlockComment,
});
if (!(result && result.content)) {
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 as any).trimLeft())
.join(this.getEOL(doc.eol));
const snippetString = new SnippetString(text);
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";
}
}