volar-service-typescript 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/out/configs/getFormatCodeSettings.d.ts +5 -0
  2. package/out/configs/getFormatCodeSettings.js +34 -0
  3. package/out/configs/getUserPreferences.d.ts +4 -0
  4. package/out/configs/getUserPreferences.js +97 -0
  5. package/out/index.d.ts +3 -0
  6. package/out/index.js +421 -0
  7. package/out/protocol.const.d.ts +72 -0
  8. package/out/protocol.const.js +92 -0
  9. package/out/services/callHierarchy.d.ts +7 -0
  10. package/out/services/callHierarchy.js +126 -0
  11. package/out/services/codeAction.d.ts +26 -0
  12. package/out/services/codeAction.js +242 -0
  13. package/out/services/codeActionResolve.d.ts +9 -0
  14. package/out/services/codeActionResolve.js +51 -0
  15. package/out/services/completions/basic.d.ts +16 -0
  16. package/out/services/completions/basic.js +322 -0
  17. package/out/services/completions/directiveComment.d.ts +3 -0
  18. package/out/services/completions/directiveComment.js +69 -0
  19. package/out/services/completions/jsDoc.d.ts +3 -0
  20. package/out/services/completions/jsDoc.js +106 -0
  21. package/out/services/completions/resolve.d.ts +5 -0
  22. package/out/services/completions/resolve.js +170 -0
  23. package/out/services/definition.d.ts +3 -0
  24. package/out/services/definition.js +20 -0
  25. package/out/services/diagnostics.d.ts +10 -0
  26. package/out/services/diagnostics.js +144 -0
  27. package/out/services/documentHighlight.d.ts +3 -0
  28. package/out/services/documentHighlight.js +56 -0
  29. package/out/services/documentSymbol.d.ts +3 -0
  30. package/out/services/documentSymbol.js +106 -0
  31. package/out/services/fileReferences.d.ts +3 -0
  32. package/out/services/fileReferences.js +19 -0
  33. package/out/services/fileRename.d.ts +3 -0
  34. package/out/services/fileRename.js +25 -0
  35. package/out/services/foldingRanges.d.ts +3 -0
  36. package/out/services/foldingRanges.js +73 -0
  37. package/out/services/formatting.d.ts +6 -0
  38. package/out/services/formatting.js +58 -0
  39. package/out/services/hover.d.ts +3 -0
  40. package/out/services/hover.js +64 -0
  41. package/out/services/implementation.d.ts +3 -0
  42. package/out/services/implementation.js +20 -0
  43. package/out/services/inlayHints.d.ts +3 -0
  44. package/out/services/inlayHints.js +54 -0
  45. package/out/services/prepareRename.d.ts +6 -0
  46. package/out/services/prepareRename.js +51 -0
  47. package/out/services/references.d.ts +3 -0
  48. package/out/services/references.js +20 -0
  49. package/out/services/rename.d.ts +5 -0
  50. package/out/services/rename.js +135 -0
  51. package/out/services/selectionRanges.d.ts +3 -0
  52. package/out/services/selectionRanges.js +53 -0
  53. package/out/services/semanticTokens.d.ts +3 -0
  54. package/out/services/semanticTokens.js +128 -0
  55. package/out/services/signatureHelp.d.ts +3 -0
  56. package/out/services/signatureHelp.js +87 -0
  57. package/out/services/tsconfig.d.ts +6 -0
  58. package/out/services/tsconfig.js +199 -0
  59. package/out/services/typeDefinition.d.ts +3 -0
  60. package/out/services/typeDefinition.js +20 -0
  61. package/out/services/workspaceSymbol.d.ts +3 -0
  62. package/out/services/workspaceSymbol.js +80 -0
  63. package/out/shared.d.ts +5 -0
  64. package/out/shared.js +33 -0
  65. package/out/types.d.ts +5 -0
  66. package/out/types.js +3 -0
  67. package/out/utils/errorCodes.d.ts +11 -0
  68. package/out/utils/errorCodes.js +19 -0
  69. package/out/utils/fixNames.d.ts +13 -0
  70. package/out/utils/fixNames.js +21 -0
  71. package/out/utils/modifiers.d.ts +1 -0
  72. package/out/utils/modifiers.js +12 -0
  73. package/out/utils/previewer.d.ts +13 -0
  74. package/out/utils/previewer.js +182 -0
  75. package/out/utils/snippetForFunctionCall.d.ts +8 -0
  76. package/out/utils/snippetForFunctionCall.js +112 -0
  77. package/out/utils/transforms.d.ts +10 -0
  78. package/out/utils/transforms.js +76 -0
  79. package/out/utils/typeConverters.d.ts +4 -0
  80. package/out/utils/typeConverters.js +67 -0
  81. package/package.json +32 -0
  82. package/rules.d.ts +13 -0
@@ -0,0 +1,322 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.handleKindModifiers = exports.register = void 0;
27
+ const semver = __importStar(require("semver"));
28
+ const vscode = __importStar(require("vscode-languageserver-protocol"));
29
+ const getUserPreferences_1 = require("../../configs/getUserPreferences");
30
+ const PConst = __importStar(require("../../protocol.const"));
31
+ const modifiers_1 = require("../../utils/modifiers");
32
+ const shared_1 = require("../../shared");
33
+ function register(ctx) {
34
+ const { ts } = ctx;
35
+ const lt_320 = semver.lt(ts.version, '3.2.0');
36
+ const gte_300 = semver.gte(ts.version, '3.0.0');
37
+ return async (uri, position, options) => {
38
+ const document = ctx.getTextDocument(uri);
39
+ if (!document)
40
+ return;
41
+ const preferences = await (0, getUserPreferences_1.getUserPreferences)(ctx, document);
42
+ const fileName = ctx.env.uriToFileName(document.uri);
43
+ const offset = document.offsetAt(position);
44
+ const completionContext = (0, shared_1.safeCall)(() => ctx.typescript.languageService.getCompletionsAtPosition(fileName, offset, {
45
+ ...preferences,
46
+ ...options,
47
+ }));
48
+ if (completionContext === undefined)
49
+ return;
50
+ const wordRange = completionContext.optionalReplacementSpan ? vscode.Range.create(document.positionAt(completionContext.optionalReplacementSpan.start), document.positionAt(completionContext.optionalReplacementSpan.start + completionContext.optionalReplacementSpan.length)) : undefined;
51
+ let line = document.getText({
52
+ start: { line: position.line, character: 0 },
53
+ end: { line: position.line + 1, character: 0 },
54
+ });
55
+ if (line.endsWith('\n')) {
56
+ line = line.substring(0, line.length - 1);
57
+ }
58
+ const dotAccessorContext = getDotAccessorContext(document);
59
+ const entries = completionContext.entries
60
+ .map(tsEntry => toVScodeItem(tsEntry, document));
61
+ return {
62
+ isIncomplete: !!completionContext.isIncomplete,
63
+ items: entries,
64
+ };
65
+ function toVScodeItem(tsEntry, document) {
66
+ const item = vscode.CompletionItem.create(tsEntry.name);
67
+ item.kind = convertKind(tsEntry.kind);
68
+ if (tsEntry.source && tsEntry.hasAction) {
69
+ // De-prioritize auto-imports
70
+ // https://github.com/microsoft/vscode/issues/40311
71
+ item.sortText = '\uffff' + tsEntry.sortText;
72
+ }
73
+ else {
74
+ item.sortText = tsEntry.sortText;
75
+ }
76
+ const { sourceDisplay, isSnippet, labelDetails } = tsEntry;
77
+ if (sourceDisplay) {
78
+ item.labelDetails ??= {};
79
+ item.labelDetails.description = ts.displayPartsToString(sourceDisplay);
80
+ }
81
+ if (labelDetails) {
82
+ item.labelDetails ??= {};
83
+ Object.assign(item.labelDetails, labelDetails);
84
+ }
85
+ item.preselect = tsEntry.isRecommended;
86
+ let range = getRangeFromReplacementSpan(tsEntry, document);
87
+ item.commitCharacters = getCommitCharacters(tsEntry, {
88
+ isNewIdentifierLocation: completionContext.isNewIdentifierLocation,
89
+ isInValidCommitCharacterContext: isInValidCommitCharacterContext(document, position),
90
+ enableCallCompletions: true, // TODO: suggest.completeFunctionCalls
91
+ });
92
+ item.insertText = tsEntry.insertText;
93
+ item.insertTextFormat = isSnippet ? vscode.InsertTextFormat.Snippet : vscode.InsertTextFormat.PlainText;
94
+ item.filterText = getFilterText(tsEntry, wordRange, line, tsEntry.insertText);
95
+ if (completionContext?.isMemberCompletion && dotAccessorContext && !isSnippet) {
96
+ item.filterText = dotAccessorContext.text + (item.insertText || item.label);
97
+ if (!range) {
98
+ const replacementRange = wordRange;
99
+ if (replacementRange) {
100
+ range = {
101
+ inserting: dotAccessorContext.range,
102
+ replacing: rangeUnion(dotAccessorContext.range, replacementRange),
103
+ };
104
+ }
105
+ else {
106
+ range = dotAccessorContext.range;
107
+ }
108
+ item.insertText = item.filterText;
109
+ }
110
+ }
111
+ handleKindModifiers(item, tsEntry);
112
+ if (!range && wordRange) {
113
+ range = {
114
+ inserting: vscode.Range.create(wordRange.start, position),
115
+ replacing: wordRange,
116
+ };
117
+ }
118
+ if (range) {
119
+ if (vscode.Range.is(range)) {
120
+ item.textEdit = vscode.TextEdit.replace(range, item.insertText || item.label);
121
+ }
122
+ else {
123
+ item.textEdit = vscode.InsertReplaceEdit.create(item.insertText || item.label, range.inserting, range.replacing);
124
+ }
125
+ }
126
+ return {
127
+ ...item,
128
+ data: {
129
+ uri,
130
+ fileName,
131
+ offset,
132
+ originalItem: {
133
+ name: tsEntry.name,
134
+ source: tsEntry.source,
135
+ data: tsEntry.data,
136
+ labelDetails: tsEntry.labelDetails,
137
+ },
138
+ },
139
+ };
140
+ }
141
+ function getDotAccessorContext(document) {
142
+ let dotAccessorContext;
143
+ if (gte_300) {
144
+ if (!completionContext)
145
+ return;
146
+ const isMemberCompletion = completionContext.isMemberCompletion;
147
+ if (isMemberCompletion) {
148
+ const dotMatch = line.slice(0, position.character).match(/\??\.\s*$/) || undefined;
149
+ if (dotMatch) {
150
+ const range = vscode.Range.create({ line: position.line, character: position.character - dotMatch[0].length }, position);
151
+ const text = document.getText(range);
152
+ dotAccessorContext = { range, text };
153
+ }
154
+ }
155
+ }
156
+ return dotAccessorContext;
157
+ }
158
+ // from vscode typescript
159
+ function getRangeFromReplacementSpan(tsEntry, document) {
160
+ if (!tsEntry.replacementSpan) {
161
+ return;
162
+ }
163
+ let replaceRange = vscode.Range.create(document.positionAt(tsEntry.replacementSpan.start), document.positionAt(tsEntry.replacementSpan.start + tsEntry.replacementSpan.length));
164
+ // Make sure we only replace a single line at most
165
+ if (replaceRange.start.line !== replaceRange.end.line) {
166
+ replaceRange = vscode.Range.create(replaceRange.start.line, replaceRange.start.character, replaceRange.start.line, document.positionAt(document.offsetAt({ line: replaceRange.start.line + 1, character: 0 }) - 1).character);
167
+ }
168
+ // If TS returns an explicit replacement range, we should use it for both types of completion
169
+ return {
170
+ inserting: replaceRange,
171
+ replacing: replaceRange,
172
+ };
173
+ }
174
+ function getFilterText(tsEntry, wordRange, line, insertText) {
175
+ // Handle private field completions
176
+ if (tsEntry.name.startsWith('#')) {
177
+ const wordStart = wordRange ? line.charAt(wordRange.start.character) : undefined;
178
+ if (insertText) {
179
+ if (insertText.startsWith('this.#')) {
180
+ return wordStart === '#' ? insertText : insertText.replace(/^this\.#/, '');
181
+ }
182
+ else {
183
+ return insertText;
184
+ }
185
+ }
186
+ else {
187
+ return wordStart === '#' ? undefined : tsEntry.name.replace(/^#/, '');
188
+ }
189
+ }
190
+ // For `this.` completions, generally don't set the filter text since we don't want them to be overly prioritized. #74164
191
+ if (insertText?.startsWith('this.')) {
192
+ return undefined;
193
+ }
194
+ // Handle the case:
195
+ // ```
196
+ // const xyz = { 'ab c': 1 };
197
+ // xyz.ab|
198
+ // ```
199
+ // In which case we want to insert a bracket accessor but should use `.abc` as the filter text instead of
200
+ // the bracketed insert text.
201
+ else if (insertText?.startsWith('[')) {
202
+ return insertText.replace(/^\[['"](.+)[['"]\]$/, '.$1');
203
+ }
204
+ // In all other cases, fallback to using the insertText
205
+ return insertText;
206
+ }
207
+ function convertKind(kind) {
208
+ switch (kind) {
209
+ case PConst.Kind.primitiveType:
210
+ case PConst.Kind.keyword:
211
+ return vscode.CompletionItemKind.Keyword;
212
+ case PConst.Kind.const:
213
+ case PConst.Kind.let:
214
+ case PConst.Kind.variable:
215
+ case PConst.Kind.localVariable:
216
+ case PConst.Kind.alias:
217
+ case PConst.Kind.parameter:
218
+ return vscode.CompletionItemKind.Variable;
219
+ case PConst.Kind.memberVariable:
220
+ case PConst.Kind.memberGetAccessor:
221
+ case PConst.Kind.memberSetAccessor:
222
+ return vscode.CompletionItemKind.Field;
223
+ case PConst.Kind.function:
224
+ case PConst.Kind.localFunction:
225
+ return vscode.CompletionItemKind.Function;
226
+ case PConst.Kind.method:
227
+ case PConst.Kind.constructSignature:
228
+ case PConst.Kind.callSignature:
229
+ case PConst.Kind.indexSignature:
230
+ return vscode.CompletionItemKind.Method;
231
+ case PConst.Kind.enum:
232
+ return vscode.CompletionItemKind.Enum;
233
+ case PConst.Kind.enumMember:
234
+ return vscode.CompletionItemKind.EnumMember;
235
+ case PConst.Kind.module:
236
+ case PConst.Kind.externalModuleName:
237
+ return vscode.CompletionItemKind.Module;
238
+ case PConst.Kind.class:
239
+ case PConst.Kind.type:
240
+ return vscode.CompletionItemKind.Class;
241
+ case PConst.Kind.interface:
242
+ return vscode.CompletionItemKind.Interface;
243
+ case PConst.Kind.warning:
244
+ return vscode.CompletionItemKind.Text;
245
+ case PConst.Kind.script:
246
+ return vscode.CompletionItemKind.File;
247
+ case PConst.Kind.directory:
248
+ return vscode.CompletionItemKind.Folder;
249
+ case PConst.Kind.string:
250
+ return vscode.CompletionItemKind.Constant;
251
+ default:
252
+ return vscode.CompletionItemKind.Property;
253
+ }
254
+ }
255
+ function getCommitCharacters(entry, context) {
256
+ if (entry.kind === PConst.Kind.warning) { // Ambient JS word based suggestion
257
+ return undefined;
258
+ }
259
+ if (context.isNewIdentifierLocation || !context.isInValidCommitCharacterContext) {
260
+ return undefined;
261
+ }
262
+ const commitCharacters = ['.', ',', ';'];
263
+ if (context.enableCallCompletions) {
264
+ commitCharacters.push('(');
265
+ }
266
+ return commitCharacters;
267
+ }
268
+ function isInValidCommitCharacterContext(document, position) {
269
+ if (lt_320) {
270
+ // Workaround for https://github.com/microsoft/TypeScript/issues/27742
271
+ // Only enable dot completions when the previous character is not a dot preceded by whitespace.
272
+ // Prevents incorrectly completing while typing spread operators.
273
+ if (position.character > 1) {
274
+ const preText = document.getText(vscode.Range.create(position.line, 0, position.line, position.character));
275
+ return preText.match(/(\s|^)\.$/ig) === null;
276
+ }
277
+ }
278
+ return true;
279
+ }
280
+ };
281
+ }
282
+ exports.register = register;
283
+ function handleKindModifiers(item, tsEntry) {
284
+ if (tsEntry.kindModifiers) {
285
+ const kindModifiers = (0, modifiers_1.parseKindModifier)(tsEntry.kindModifiers);
286
+ if (kindModifiers.has(PConst.KindModifiers.optional)) {
287
+ if (!item.insertText) {
288
+ item.insertText = item.label;
289
+ }
290
+ if (!item.filterText) {
291
+ item.filterText = item.label;
292
+ }
293
+ item.label += '?';
294
+ }
295
+ if (kindModifiers.has(PConst.KindModifiers.deprecated)) {
296
+ item.tags = [vscode.CompletionItemTag.Deprecated];
297
+ }
298
+ if (kindModifiers.has(PConst.KindModifiers.color)) {
299
+ item.kind = vscode.CompletionItemKind.Color;
300
+ }
301
+ if (tsEntry.kind === PConst.Kind.script) {
302
+ for (const extModifier of PConst.KindModifiers.fileExtensionKindModifiers) {
303
+ if (kindModifiers.has(extModifier)) {
304
+ if (tsEntry.name.toLowerCase().endsWith(extModifier)) {
305
+ item.detail = tsEntry.name;
306
+ }
307
+ else {
308
+ item.detail = tsEntry.name + extModifier;
309
+ }
310
+ break;
311
+ }
312
+ }
313
+ }
314
+ }
315
+ }
316
+ exports.handleKindModifiers = handleKindModifiers;
317
+ function rangeUnion(a, b) {
318
+ const start = (a.start.line < b.start.line || (a.start.line === b.start.line && a.start.character < b.start.character)) ? a.start : b.start;
319
+ const end = (a.end.line > b.end.line || (a.end.line === b.end.line && a.end.character > b.end.character)) ? a.end : b.end;
320
+ return { start, end };
321
+ }
322
+ //# sourceMappingURL=basic.js.map
@@ -0,0 +1,3 @@
1
+ import * as vscode from 'vscode-languageserver-protocol';
2
+ import { SharedContext } from '../../types';
3
+ export declare function register(ctx: SharedContext): (uri: string, position: vscode.Position) => vscode.CompletionItem[] | undefined;
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.register = void 0;
27
+ const vscode = __importStar(require("vscode-languageserver-protocol"));
28
+ const nls = __importStar(require("vscode-nls"));
29
+ const localize = nls.loadMessageBundle(); // TODO: not working
30
+ const directives = [
31
+ {
32
+ value: '@ts-check',
33
+ description: localize('ts-check', "Enables semantic checking in a JavaScript file. Must be at the top of a file.")
34
+ }, {
35
+ value: '@ts-nocheck',
36
+ description: localize('ts-nocheck', "Disables semantic checking in a JavaScript file. Must be at the top of a file.")
37
+ }, {
38
+ value: '@ts-ignore',
39
+ description: localize('ts-ignore', "Suppresses @ts-check errors on the next line of a file.")
40
+ }, {
41
+ value: '@ts-expect-error',
42
+ description: localize('ts-expect-error', "Suppresses @ts-check errors on the next line of a file, expecting at least one to exist.")
43
+ }
44
+ ];
45
+ function register(ctx) {
46
+ return (uri, position) => {
47
+ const document = ctx.getTextDocument(uri);
48
+ if (!document)
49
+ return;
50
+ const prefix = document.getText({
51
+ start: { line: position.line, character: 0 },
52
+ end: position,
53
+ });
54
+ const match = prefix.match(/^\s*\/\/+\s?(@[a-zA-Z\-]*)?$/);
55
+ if (match) {
56
+ return directives.map(directive => {
57
+ const item = vscode.CompletionItem.create(directive.value);
58
+ item.insertTextFormat = vscode.InsertTextFormat.Snippet;
59
+ item.detail = directive.description;
60
+ const range = vscode.Range.create(position.line, Math.max(0, position.character - (match[1] ? match[1].length : 0)), position.line, position.character);
61
+ item.textEdit = vscode.TextEdit.replace(range, directive.value);
62
+ return item;
63
+ });
64
+ }
65
+ return [];
66
+ };
67
+ }
68
+ exports.register = register;
69
+ //# sourceMappingURL=directiveComment.js.map
@@ -0,0 +1,3 @@
1
+ import { SharedContext } from '../../types';
2
+ import * as vscode from 'vscode-languageserver-protocol';
3
+ export declare function register(ctx: SharedContext): (uri: string, position: vscode.Position) => vscode.CompletionItem | undefined;
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.register = void 0;
27
+ const vscode = __importStar(require("vscode-languageserver-protocol"));
28
+ const nls = __importStar(require("vscode-nls"));
29
+ const resolve_1 = require("./resolve");
30
+ const localize = nls.loadMessageBundle(); // TODO: not working
31
+ const defaultJsDoc = `/**\n * $0\n */`;
32
+ function register(ctx) {
33
+ return (uri, position) => {
34
+ const document = ctx.getTextDocument(uri);
35
+ if (!document)
36
+ return;
37
+ if (!isPotentiallyValidDocCompletionPosition(document, position))
38
+ return;
39
+ const fileName = ctx.env.uriToFileName(document.uri);
40
+ const offset = document.offsetAt(position);
41
+ const docCommentTemplate = ctx.typescript.languageService.getDocCommentTemplateAtPosition(fileName, offset);
42
+ if (!docCommentTemplate)
43
+ return;
44
+ let insertText;
45
+ // Workaround for #43619
46
+ // docCommentTemplate previously returned undefined for empty jsdoc templates.
47
+ // TS 2.7 now returns a single line doc comment, which breaks indentation.
48
+ if (docCommentTemplate.newText === '/** */') {
49
+ insertText = defaultJsDoc;
50
+ }
51
+ else {
52
+ insertText = templateToSnippet(docCommentTemplate.newText);
53
+ }
54
+ const item = createCompletionItem(document, position, insertText);
55
+ return item;
56
+ };
57
+ }
58
+ exports.register = register;
59
+ function createCompletionItem(document, position, insertText) {
60
+ const item = vscode.CompletionItem.create('/** */');
61
+ item.kind = vscode.CompletionItemKind.Text;
62
+ item.detail = localize('typescript.jsDocCompletionItem.documentation', 'JSDoc comment');
63
+ item.sortText = '\0';
64
+ item.insertTextFormat = vscode.InsertTextFormat.Snippet;
65
+ const line = (0, resolve_1.getLineText)(document, position.line);
66
+ const prefix = line.slice(0, position.character).match(/\/\**\s*$/);
67
+ const suffix = line.slice(position.character).match(/^\s*\**\//);
68
+ const start = vscode.Position.create(position.line, position.character + (prefix ? -prefix[0].length : 0));
69
+ const end = vscode.Position.create(position.line, position.character + (suffix ? suffix[0].length : 0));
70
+ const range = vscode.Range.create(start, end);
71
+ item.textEdit = vscode.TextEdit.replace(range, insertText);
72
+ return item;
73
+ }
74
+ function isPotentiallyValidDocCompletionPosition(document, position) {
75
+ // Only show the JSdoc completion when the everything before the cursor is whitespace
76
+ // or could be the opening of a comment
77
+ const line = (0, resolve_1.getLineText)(document, position.line);
78
+ const prefix = line.slice(0, position.character);
79
+ if (!/^\s*$|\/\*\*\s*$|^\s*\/\*\*+\s*$/.test(prefix)) {
80
+ return false;
81
+ }
82
+ // And everything after is possibly a closing comment or more whitespace
83
+ const suffix = line.slice(position.character);
84
+ return /^\s*(\*+\/)?\s*$/.test(suffix);
85
+ }
86
+ function templateToSnippet(template) {
87
+ // TODO: use append placeholder
88
+ let snippetIndex = 1;
89
+ template = template.replace(/\$/g, '\\$');
90
+ template = template.replace(/^[ \t]*(?=(\/|[ ]\*))/gm, '');
91
+ template = template.replace(/^(\/\*\*\s*\*[ ]*)$/m, (x) => x + `\$0`);
92
+ template = template.replace(/\* @param([ ]\{\S+\})?\s+(\S+)[ \t]*$/gm, (_param, type, post) => {
93
+ let out = '* @param ';
94
+ if (type === ' {any}' || type === ' {*}') {
95
+ out += `{\$\{${snippetIndex++}:*\}} `;
96
+ }
97
+ else if (type) {
98
+ out += type + ' ';
99
+ }
100
+ out += post + ` \${${snippetIndex++}}`;
101
+ return out;
102
+ });
103
+ template = template.replace(/\* @returns[ \t]*$/gm, `* @returns \${${snippetIndex++}}`);
104
+ return template;
105
+ }
106
+ //# sourceMappingURL=jsDoc.js.map
@@ -0,0 +1,5 @@
1
+ import { SharedContext } from '../../types';
2
+ import * as vscode from 'vscode-languageserver-protocol';
3
+ import { TextDocument } from 'vscode-languageserver-textdocument';
4
+ export declare function register(ctx: SharedContext): (item: vscode.CompletionItem, newPosition?: vscode.Position) => Promise<vscode.CompletionItem>;
5
+ export declare function getLineText(document: TextDocument, line: number): string;