-
Notifications
You must be signed in to change notification settings - Fork 60
/
jsdoc.ts
691 lines (619 loc) · 21.7 KB
/
jsdoc.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
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/******************************************************************************
* Copyright 2023 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/
import { Position, Range } from 'vscode-languageserver-types';
import type { CstNode } from '../syntax-tree.js';
import { NEWLINE_REGEXP, escapeRegExp } from '../utils/regexp-utils.js';
import { URI } from '../utils/uri-utils.js';
export interface JSDocComment extends JSDocValue {
readonly elements: JSDocElement[]
getTag(name: string): JSDocTag | undefined
getTags(name: string): JSDocTag[]
}
export type JSDocElement = JSDocParagraph | JSDocTag;
export type JSDocInline = JSDocTag | JSDocLine;
export interface JSDocValue {
/**
* Represents the range that this JSDoc element occupies.
* If the JSDoc was parsed from a `CstNode`, the range will represent the location in the source document.
*/
readonly range: Range
/**
* Renders this JSDoc element to a plain text representation.
*/
toString(): string
/**
* Renders this JSDoc element to a markdown representation.
*
* @param options Rendering options to customize the markdown result.
*/
toMarkdown(options?: JSDocRenderOptions): string
}
export interface JSDocParagraph extends JSDocValue {
readonly inlines: JSDocInline[]
}
export interface JSDocLine extends JSDocValue {
readonly text: string
}
export interface JSDocTag extends JSDocValue {
readonly name: string
readonly content: JSDocParagraph
readonly inline: boolean
}
export interface JSDocParseOptions {
/**
* The start symbol of your comment format. Defaults to `/**`.
*/
readonly start?: RegExp | string
/**
* The symbol that start a line of your comment format. Defaults to `*`.
*/
readonly line?: RegExp | string
/**
* The end symbol of your comment format. Defaults to `*\/`.
*/
readonly end?: RegExp | string
}
export interface JSDocRenderOptions {
/**
* Determines the style for rendering tags. Defaults to `italic`.
*/
tag?: 'plain' | 'italic' | 'bold' | 'bold-italic'
/**
* Determines the default for rendering `@link` tags. Defaults to `plain`.
*/
link?: 'code' | 'plain'
/**
* Custom tag rendering function.
* Return a markdown formatted tag or `undefined` to fall back to the default rendering.
*/
renderTag?(tag: JSDocTag): string | undefined
/**
* Custom link rendering function. Accepts a link target and a display value for the link.
* Return a markdown formatted link with the format `[$display]($link)` or `undefined` if the link is not a valid target.
*/
renderLink?(link: string, display: string): string | undefined
}
/**
* Parses a JSDoc from a `CstNode` containing a comment.
*
* @param node A `CstNode` from a parsed Langium document.
* @param options Parsing options specialized to your language. See {@link JSDocParseOptions}.
*/
export function parseJSDoc(node: CstNode, options?: JSDocParseOptions): JSDocComment;
/**
* Parses a JSDoc from a string comment.
*
* @param content A string containing the source of the JSDoc comment.
* @param start The start position the comment occupies in the source document.
* @param options Parsing options specialized to your language. See {@link JSDocParseOptions}.
*/
export function parseJSDoc(content: string, start?: Position, options?: JSDocParseOptions): JSDocComment;
export function parseJSDoc(node: CstNode | string, start?: Position | JSDocParseOptions, options?: JSDocParseOptions): JSDocComment {
let opts: JSDocParseOptions | undefined;
let position: Position | undefined;
if (typeof node === 'string') {
position = start as Position | undefined;
opts = options as JSDocParseOptions | undefined;
} else {
position = node.range.start;
opts = start as JSDocParseOptions | undefined;
}
if (!position) {
position = Position.create(0, 0);
}
const lines = getLines(node);
const normalizedOptions = normalizeOptions(opts);
const tokens = tokenize({
lines,
position,
options: normalizedOptions
});
return parseJSDocComment({
index: 0,
tokens,
position
});
}
export function isJSDoc(node: CstNode | string, options?: JSDocParseOptions): boolean {
const normalizedOptions = normalizeOptions(options);
const lines = getLines(node);
if (lines.length === 0) {
return false;
}
const first = lines[0];
const last = lines[lines.length - 1];
const firstRegex = normalizedOptions.start;
const lastRegex = normalizedOptions.end;
return Boolean(firstRegex?.exec(first)) && Boolean(lastRegex?.exec(last));
}
function getLines(node: CstNode | string): string[] {
let content = '';
if (typeof node === 'string') {
content = node;
} else {
content = node.text;
}
const lines = content.split(NEWLINE_REGEXP);
return lines;
}
// Tokenization
interface JSDocToken {
type: 'text' | 'tag' | 'inline-tag' | 'break'
content: string
range: Range
}
const tagRegex = /\s*(@([\p{L}][\p{L}\p{N}]*)?)/uy;
const inlineTagRegex = /\{(@[\p{L}][\p{L}\p{N}]*)(\s*)([^\r\n}]+)?\}/gu;
function tokenize(context: TokenizationContext): JSDocToken[] {
const tokens: JSDocToken[] = [];
let currentLine = context.position.line;
let currentCharacter = context.position.character;
for (let i = 0; i < context.lines.length; i++) {
const first = i === 0;
const last = i === context.lines.length - 1;
let line = context.lines[i];
let index = 0;
if (first && context.options.start) {
const match = context.options.start?.exec(line);
if (match) {
index = match.index + match[0].length;
}
} else {
const match = context.options.line?.exec(line);
if (match) {
index = match.index + match[0].length;
}
}
if (last) {
const match = context.options.end?.exec(line);
if (match) {
line = line.substring(0, match.index);
}
}
line = line.substring(0, lastCharacter(line));
const whitespaceEnd = skipWhitespace(line, index);
if (whitespaceEnd >= line.length) {
// Only create a break token when we already have previous tokens
if (tokens.length > 0) {
const position = Position.create(currentLine, currentCharacter);
tokens.push({
type: 'break',
content: '',
range: Range.create(position, position)
});
}
} else {
tagRegex.lastIndex = index;
const tagMatch = tagRegex.exec(line);
if (tagMatch) {
const fullMatch = tagMatch[0];
const value = tagMatch[1];
const start = Position.create(currentLine, currentCharacter + index);
const end = Position.create(currentLine, currentCharacter + index + fullMatch.length);
tokens.push({
type: 'tag',
content: value,
range: Range.create(start, end)
});
index += fullMatch.length;
index = skipWhitespace(line, index);
}
if (index < line.length) {
const rest = line.substring(index);
const inlineTagMatches = Array.from(rest.matchAll(inlineTagRegex));
tokens.push(...buildInlineTokens(inlineTagMatches, rest, currentLine, currentCharacter + index));
}
}
currentLine++;
currentCharacter = 0;
}
// Remove last break token if there is one
if (tokens.length > 0 && tokens[tokens.length - 1].type === 'break') {
return tokens.slice(0, -1);
}
return tokens;
}
function buildInlineTokens(tags: RegExpMatchArray[], line: string, lineIndex: number, characterIndex: number): JSDocToken[] {
const tokens: JSDocToken[] = [];
if (tags.length === 0) {
const start = Position.create(lineIndex, characterIndex);
const end = Position.create(lineIndex, characterIndex + line.length);
tokens.push({
type: 'text',
content: line,
range: Range.create(start, end)
});
} else {
let lastIndex = 0;
for (const match of tags) {
const matchIndex = match.index!;
const startContent = line.substring(lastIndex, matchIndex);
if (startContent.length > 0) {
tokens.push({
type: 'text',
content: line.substring(lastIndex, matchIndex),
range: Range.create(
Position.create(lineIndex, lastIndex + characterIndex),
Position.create(lineIndex, matchIndex + characterIndex)
)
});
}
let offset = startContent.length + 1;
const tagName = match[1];
tokens.push({
type: 'inline-tag',
content: tagName,
range: Range.create(
Position.create(lineIndex, lastIndex + offset + characterIndex),
Position.create(lineIndex, lastIndex + offset + tagName.length + characterIndex)
)
});
offset += tagName.length;
if (match.length === 4) {
offset += match[2].length;
const value = match[3];
tokens.push({
type: 'text',
content: value,
range: Range.create(
Position.create(lineIndex, lastIndex + offset + characterIndex),
Position.create(lineIndex, lastIndex + offset + value.length + characterIndex)
)
});
} else {
tokens.push({
type: 'text',
content: '',
range: Range.create(
Position.create(lineIndex, lastIndex + offset + characterIndex),
Position.create(lineIndex, lastIndex + offset + characterIndex)
)
});
}
lastIndex = matchIndex + match[0].length;
}
const endContent = line.substring(lastIndex);
if (endContent.length > 0) {
tokens.push({
type: 'text',
content: endContent,
range: Range.create(
Position.create(lineIndex, lastIndex + characterIndex),
Position.create(lineIndex, lastIndex + characterIndex + endContent.length)
)
});
}
}
return tokens;
}
const nonWhitespaceRegex = /\S/;
const whitespaceEndRegex = /\s*$/;
function skipWhitespace(line: string, index: number): number {
const match = line.substring(index).match(nonWhitespaceRegex);
if (match) {
return index + match.index!;
} else {
return line.length;
}
}
function lastCharacter(line: string): number | undefined {
const match = line.match(whitespaceEndRegex);
if (match && typeof match.index === 'number') {
return match.index;
}
return undefined;
}
// Parsing
function parseJSDocComment(context: ParseContext): JSDocComment {
const startPosition: Position = Position.create(context.position.line, context.position.character);
if (context.tokens.length === 0) {
return new JSDocCommentImpl([], Range.create(startPosition, startPosition));
}
const elements: JSDocElement[] = [];
while (context.index < context.tokens.length) {
const element = parseJSDocElement(context, elements[elements.length - 1]);
if (element) {
elements.push(element);
}
}
const start = elements[0]?.range.start ?? startPosition;
const end = elements[elements.length - 1]?.range.end ?? startPosition;
return new JSDocCommentImpl(elements, Range.create(start, end));
}
function parseJSDocElement(context: ParseContext, last?: JSDocElement): JSDocElement | undefined {
const next = context.tokens[context.index];
if (next.type === 'tag') {
return parseJSDocTag(context, false);
} else if (next.type === 'text' || next.type === 'inline-tag') {
return parseJSDocText(context);
} else {
appendEmptyLine(next, last);
context.index++;
return undefined;
}
}
function appendEmptyLine(token: JSDocToken, element?: JSDocElement): void {
if (element) {
const line = new JSDocLineImpl('', token.range);
if ('inlines' in element) {
element.inlines.push(line);
} else {
element.content.inlines.push(line);
}
}
}
function parseJSDocText(context: ParseContext): JSDocParagraph {
let token = context.tokens[context.index];
const firstToken = token;
let lastToken = token;
const lines: JSDocInline[] = [];
while (token && token.type !== 'break' && token.type !== 'tag') {
lines.push(parseJSDocInline(context));
lastToken = token;
token = context.tokens[context.index];
}
return new JSDocTextImpl(lines, Range.create(firstToken.range.start, lastToken.range.end));
}
function parseJSDocInline(context: ParseContext): JSDocInline {
const token = context.tokens[context.index];
if (token.type === 'inline-tag') {
return parseJSDocTag(context, true);
} else {
return parseJSDocLine(context);
}
}
function parseJSDocTag(context: ParseContext, inline: boolean): JSDocTag {
const tagToken = context.tokens[context.index++];
const name = tagToken.content.substring(1);
const nextToken = context.tokens[context.index];
if (nextToken?.type === 'text') {
if (inline) {
const docLine = parseJSDocLine(context);
return new JSDocTagImpl(
name,
new JSDocTextImpl([docLine], docLine.range),
inline,
Range.create(tagToken.range.start, docLine.range.end)
);
} else {
const textDoc = parseJSDocText(context);
return new JSDocTagImpl(
name,
textDoc,
inline,
Range.create(tagToken.range.start, textDoc.range.end)
);
}
} else {
const range = tagToken.range;
return new JSDocTagImpl(name, new JSDocTextImpl([], range), inline, range);
}
}
function parseJSDocLine(context: ParseContext): JSDocLine {
const token = context.tokens[context.index++];
return new JSDocLineImpl(token.content, token.range);
}
interface NormalizedOptions {
start?: RegExp
end?: RegExp
line?: RegExp
}
interface TokenizationContext {
position: Position
lines: string[]
options: NormalizedOptions
}
interface ParseContext {
position: Position
tokens: JSDocToken[]
index: number
}
function normalizeOptions(options?: JSDocParseOptions): NormalizedOptions {
if (!options) {
return normalizeOptions({
start: '/**',
end: '*/',
line: '*'
});
}
const { start, end, line } = options;
return {
start: normalizeOption(start, true),
end: normalizeOption(end, false),
line: normalizeOption(line, true)
};
}
function normalizeOption(option: RegExp | string | undefined, start: boolean): RegExp | undefined {
if (typeof option === 'string' || typeof option === 'object') {
const escaped = typeof option === 'string' ? escapeRegExp(option) : option.source;
if (start) {
return new RegExp(`^\\s*${escaped}`);
} else {
return new RegExp(`\\s*${escaped}\\s*$`);
}
} else {
return option;
}
}
class JSDocCommentImpl implements JSDocComment {
readonly elements: JSDocElement[];
readonly range: Range;
constructor(elements: JSDocElement[], range: Range) {
this.elements = elements;
this.range = range;
}
getTag(name: string): JSDocTag | undefined {
return this.getAllTags().find(e => e.name === name);
}
getTags(name: string): JSDocTag[] {
return this.getAllTags().filter(e => e.name === name);
}
private getAllTags(): JSDocTag[] {
return this.elements.filter((e): e is JSDocTag => 'name' in e);
}
toString(): string {
let value = '';
for (const element of this.elements) {
if (value.length === 0) {
value = element.toString();
} else {
const text = element.toString();
value += fillNewlines(value) + text;
}
}
return value.trim();
}
toMarkdown(options?: JSDocRenderOptions): string {
let value = '';
for (const element of this.elements) {
if (value.length === 0) {
value = element.toMarkdown(options);
} else {
const text = element.toMarkdown(options);
value += fillNewlines(value) + text;
}
}
return value.trim();
}
}
class JSDocTagImpl implements JSDocTag {
name: string;
content: JSDocParagraph;
range: Range;
inline: boolean;
constructor(name: string, content: JSDocParagraph, inline: boolean, range: Range) {
this.name = name;
this.content = content;
this.inline = inline;
this.range = range;
}
toString(): string {
let text = `@${this.name}`;
const content = this.content.toString();
if (this.content.inlines.length === 1) {
text = `${text} ${content}`;
} else if (this.content.inlines.length > 1) {
text = `${text}\n${content}`;
}
if (this.inline) {
// Inline tags are surrounded by curly braces
return `{${text}}`;
} else {
return text;
}
}
toMarkdown(options?: JSDocRenderOptions): string {
return options?.renderTag?.(this) ?? this.toMarkdownDefault(options);
}
private toMarkdownDefault(options?: JSDocRenderOptions): string {
const content = this.content.toMarkdown(options);
if (this.inline) {
const rendered = renderInlineTag(this.name, content, options ?? {});
if (typeof rendered === 'string') {
return rendered;
}
}
let marker = '';
if (options?.tag === 'italic' || options?.tag === undefined) {
marker = '*';
} else if (options?.tag === 'bold') {
marker = '**';
} else if (options?.tag === 'bold-italic') {
marker = '***';
}
let text = `${marker}@${this.name}${marker}`;
if (this.content.inlines.length === 1) {
text = `${text} — ${content}`;
} else if (this.content.inlines.length > 1) {
text = `${text}\n${content}`;
}
if (this.inline) {
// Inline tags are surrounded by curly braces
return `{${text}}`;
} else {
return text;
}
}
}
function renderInlineTag(tag: string, content: string, options: JSDocRenderOptions): string | undefined {
if (tag === 'linkplain' || tag === 'linkcode' || tag === 'link') {
const index = content.indexOf(' ');
let display = content;
if (index > 0) {
const displayStart = skipWhitespace(content, index);
display = content.substring(displayStart);
content = content.substring(0, index);
}
if (tag === 'linkcode' || (tag === 'link' && options.link === 'code')) {
// Surround the display value in a markdown inline code block
display = `\`${display}\``;
}
const renderedLink = options.renderLink?.(content, display) ?? renderLinkDefault(content, display);
return renderedLink;
}
return undefined;
}
function renderLinkDefault(content: string, display: string): string {
try {
URI.parse(content, true);
return `[${display}](${content})`;
} catch {
return content;
}
}
class JSDocTextImpl implements JSDocParagraph {
inlines: JSDocInline[];
range: Range;
constructor(lines: JSDocInline[], range: Range) {
this.inlines = lines;
this.range = range;
}
toString(): string {
let text = '';
for (let i = 0; i < this.inlines.length; i++) {
const inline = this.inlines[i];
const next = this.inlines[i + 1];
text += inline.toString();
if (next && next.range.start.line > inline.range.start.line) {
text += '\n';
}
}
return text;
}
toMarkdown(options?: JSDocRenderOptions): string {
let text = '';
for (let i = 0; i < this.inlines.length; i++) {
const inline = this.inlines[i];
const next = this.inlines[i + 1];
text += inline.toMarkdown(options);
if (next && next.range.start.line > inline.range.start.line) {
text += '\n';
}
}
return text;
}
}
class JSDocLineImpl implements JSDocLine {
text: string;
range: Range;
constructor(text: string, range: Range) {
this.text = text;
this.range = range;
}
toString(): string {
return this.text;
}
toMarkdown(): string {
return this.text;
}
}
function fillNewlines(text: string): string {
if (text.endsWith('\n')) {
return '\n';
} else {
return '\n\n';
}
}