-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
117 lines (117 loc) · 4.34 KB
/
Copy pathparser.js
File metadata and controls
117 lines (117 loc) · 4.34 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
"use strict";
// ## Parser Module
// Strips language comment tokens and classifies comment content.
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractCommentContent = extractCommentContent;
exports.parseLine = parseLine;
exports.parseDocument = parseDocument;
// @param langId string - VS Code languageId
// @returns string[] - comment prefixes for that language
const LANG_PREFIXES = {
python: ['#'],
javascript: ['//'],
typescript: ['//'],
javascriptreact: ['//'],
typescriptreact: ['//'],
c: ['//'],
cpp: ['//'],
csharp: ['//'],
java: ['//'],
go: ['//'],
rust: ['//'],
swift: ['//'],
kotlin: ['//'],
php: ['//'],
ruby: ['#'],
shellscript: ['#'],
yaml: ['#'],
r: ['#'],
lua: ['--'],
haskell: ['--'],
sql: ['--'],
html: ['<!--', '-->'],
css: ['/*', '*/'],
scss: ['//'],
less: ['//'],
};
// @param line string - raw source line
// @param langId string - VS Code languageId
// @returns string|null - comment content or null if not a comment line
function extractCommentContent(line, langId) {
const prefixes = LANG_PREFIXES[langId] ?? ['//'];
const trimmed = line.trim();
for (const prefix of prefixes) {
if (trimmed.startsWith(prefix)) {
return trimmed.slice(prefix.length).trim();
}
}
return null;
}
// @param content string - inner comment text (after stripping prefix)
// @param lineNum number - 0-based line index
// @param raw string - original full line
// @returns ParsedLine
function parseLine(content, lineNum, raw) {
const c = content.trim();
const base = { raw, line: lineNum };
if (/^#\s+.+/.test(c))
return { ...base, type: 'h1', content: c.slice(1).trim() };
if (/^##\s+.+/.test(c))
return { ...base, type: 'h2', content: c.slice(2).trim() };
if (/^###\s+.+/.test(c))
return { ...base, type: 'h3', content: c.slice(3).trim() };
if (c.startsWith('!! DANGER:'))
return { ...base, type: 'danger', content: c.slice(10).trim() };
if (c.startsWith('! WARNING:'))
return { ...base, type: 'warning', content: c.slice(10).trim() };
if (c.startsWith('> Note:'))
return { ...base, type: 'note', content: c.slice(7).trim() };
if (c.startsWith('[x]') || c.toLowerCase().startsWith('[x] done:'))
return { ...base, type: 'done', content: c.replace(/^\[x\]\s*(done:)?\s*/i, '') };
if (c.startsWith('[ ]') || c.toLowerCase().startsWith('[ ] todo:'))
return { ...base, type: 'todo', content: c.replace(/^\[ \]\s*(todo:)?\s*/i, '') };
if (/^~~.+~~$/.test(c))
return { ...base, type: 'deprecated', content: c.slice(2, -2).trim() };
if (/^---\s*.+\s*---$/.test(c))
return { ...base, type: 'divider', content: c.replace(/---/g, '').trim() };
if (c.startsWith('@param'))
return { ...base, type: 'param', content: c.slice(6).trim() };
if (c.startsWith('@returns'))
return { ...base, type: 'returns', content: c.slice(8).trim() };
if (c.startsWith('"""') && c.endsWith('"""') && c.length > 6)
return { ...base, type: 'paragraph_start', content: c.slice(3, -3).trim() };
if (c.startsWith('"""'))
return { ...base, type: 'paragraph_start', content: c.slice(3).trim() };
if (c.endsWith('"""'))
return { ...base, type: 'paragraph_end', content: c.slice(0, -3).trim() };
if (/\*.+\*/.test(c))
return { ...base, type: 'bold', content: c };
return { ...base, type: 'normal', content: c };
}
// @param document vscode.TextDocument - active document
// @param langId string
// @returns ParsedLine[] - all comment lines parsed
function parseDocument(lines, langId) {
const results = [];
let inParagraph = false;
for (let i = 0; i < lines.length; i++) {
const raw = lines[i];
const content = extractCommentContent(raw, langId);
if (content === null) {
inParagraph = false;
continue;
}
const parsed = parseLine(content, i, raw);
if (parsed.type === 'paragraph_start')
inParagraph = true;
else if (parsed.type === 'paragraph_end')
inParagraph = false;
else if (inParagraph) {
results.push({ ...parsed, type: 'paragraph_mid' });
continue;
}
results.push(parsed);
}
return results;
}
//# sourceMappingURL=parser.js.map