tokless 0.4.0 → 0.5.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.
- package/README.en.md +20 -7
- package/README.md +23 -8
- package/bin/tokless.js +1 -1
- package/index.js +3 -0
- package/package.json +2 -2
- package/plugin.json +1 -1
- package/tools/repo-skeleton/cli.js +25 -7
- package/tools/repo-skeleton/lib/core/block-anchor.js +851 -0
- package/tools/repo-skeleton/lib/core/lexer.js +722 -0
- package/tools/repo-skeleton/lib/core/registry.js +64 -0
- package/tools/repo-skeleton/lib/langs/c-family/index.js +177 -0
- package/tools/repo-skeleton/lib/langs/go/index.js +108 -0
- package/tools/repo-skeleton/lib/langs/java/index.js +149 -0
- package/tools/repo-skeleton/lib/langs/javascript/index.js +288 -0
- package/tools/repo-skeleton/lib/langs/kotlin/index.js +167 -0
- package/tools/repo-skeleton/lib/langs/markdown/index.js +79 -0
- package/tools/repo-skeleton/lib/langs/python/index.js +208 -0
- package/tools/repo-skeleton/lib/langs/rust/index.js +115 -0
- package/tools/repo-skeleton/lib/langs/sfc/index.js +72 -0
- package/tools/repo-skeleton/lib/skeleton.js +144 -1309
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const ID = 'python';
|
|
4
|
+
const EXTENSIONS = ['.py'];
|
|
5
|
+
|
|
6
|
+
function preprocessPythonLines(lines) {
|
|
7
|
+
const result = new Array(lines.length);
|
|
8
|
+
let tripleQuote = null;
|
|
9
|
+
|
|
10
|
+
for (let i = 0; i < lines.length; i++) {
|
|
11
|
+
const raw = lines[i];
|
|
12
|
+
const startedInTriple = tripleQuote !== null;
|
|
13
|
+
let inSingle = false;
|
|
14
|
+
let inDouble = false;
|
|
15
|
+
const out = [];
|
|
16
|
+
|
|
17
|
+
for (let k = 0; k < raw.length; k++) {
|
|
18
|
+
const ch = raw[k];
|
|
19
|
+
if (tripleQuote) {
|
|
20
|
+
if (raw.slice(k, k + 3) === tripleQuote) {
|
|
21
|
+
tripleQuote = null;
|
|
22
|
+
out.push(' ');
|
|
23
|
+
k += 2;
|
|
24
|
+
} else {
|
|
25
|
+
out.push(' ');
|
|
26
|
+
}
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
if (inSingle) {
|
|
30
|
+
if (ch === '\\') {
|
|
31
|
+
out.push(' ');
|
|
32
|
+
k++;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (ch === "'") inSingle = false;
|
|
36
|
+
out.push(' ');
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (inDouble) {
|
|
40
|
+
if (ch === '\\') {
|
|
41
|
+
out.push(' ');
|
|
42
|
+
k++;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (ch === '"') inDouble = false;
|
|
46
|
+
out.push(' ');
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (ch === '#') {
|
|
51
|
+
while (k < raw.length) {
|
|
52
|
+
out.push(' ');
|
|
53
|
+
k++;
|
|
54
|
+
}
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
if (raw.slice(k, k + 3) === '"""' || raw.slice(k, k + 3) === "'''") {
|
|
58
|
+
tripleQuote = raw.slice(k, k + 3);
|
|
59
|
+
out.push(' ');
|
|
60
|
+
k += 2;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (ch === "'") {
|
|
64
|
+
inSingle = true;
|
|
65
|
+
out.push("'");
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (ch === '"') {
|
|
69
|
+
inDouble = true;
|
|
70
|
+
out.push('"');
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
out.push(ch);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const expanded = raw.replace(/^\t+/, (m) => ' '.repeat(m.length));
|
|
77
|
+
const indent = expanded.length - expanded.trimStart().length;
|
|
78
|
+
|
|
79
|
+
result[i] = {
|
|
80
|
+
raw,
|
|
81
|
+
clean: out.join(''),
|
|
82
|
+
indent,
|
|
83
|
+
startedInTriple,
|
|
84
|
+
endedInTriple: tripleQuote !== null,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function parse(lines, ext = '.py', options = {}) {
|
|
92
|
+
if (ext && typeof ext === 'object') {
|
|
93
|
+
options = ext;
|
|
94
|
+
ext = '.py';
|
|
95
|
+
}
|
|
96
|
+
const lineOffset = options.lineOffset || 0;
|
|
97
|
+
const analyzed = preprocessPythonLines(lines);
|
|
98
|
+
const items = [];
|
|
99
|
+
const classStack = [];
|
|
100
|
+
|
|
101
|
+
for (let i = 0; i < analyzed.length; i++) {
|
|
102
|
+
const info = analyzed[i];
|
|
103
|
+
if (info.startedInTriple) continue;
|
|
104
|
+
|
|
105
|
+
const trimmed = info.clean.trim();
|
|
106
|
+
if (!trimmed) continue;
|
|
107
|
+
|
|
108
|
+
while (classStack.length > 0 && info.indent <= classStack[classStack.length - 1].indent) {
|
|
109
|
+
classStack.pop();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const m = trimmed.match(/^(class|async\s+def|def)\s+([A-Za-z_]\w*)\b/);
|
|
113
|
+
if (!m) continue;
|
|
114
|
+
|
|
115
|
+
const rawKind = m[1];
|
|
116
|
+
const name = m[2];
|
|
117
|
+
const isClass = rawKind === 'class';
|
|
118
|
+
const headerIndent = info.indent;
|
|
119
|
+
|
|
120
|
+
if (headerIndent > 0 && classStack.length === 0) continue;
|
|
121
|
+
if (classStack.length > 1) continue;
|
|
122
|
+
|
|
123
|
+
let startIdx = i;
|
|
124
|
+
let probe = i - 1;
|
|
125
|
+
while (probe >= 0) {
|
|
126
|
+
const pInfo = analyzed[probe];
|
|
127
|
+
if (pInfo.startedInTriple) break;
|
|
128
|
+
const pTrim = pInfo.clean.trim();
|
|
129
|
+
if (pInfo.indent === headerIndent && pTrim.startsWith('@')) {
|
|
130
|
+
startIdx = probe;
|
|
131
|
+
probe--;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
let headerEndIdx = i;
|
|
138
|
+
let paren = 0;
|
|
139
|
+
let bracket = 0;
|
|
140
|
+
for (let j = i; j < analyzed.length; j++) {
|
|
141
|
+
const c = analyzed[j].clean;
|
|
142
|
+
for (let k = 0; k < c.length; k++) {
|
|
143
|
+
if (c[k] === '(') paren++;
|
|
144
|
+
else if (c[k] === ')') paren = Math.max(0, paren - 1);
|
|
145
|
+
else if (c[k] === '[') bracket++;
|
|
146
|
+
else if (c[k] === ']') bracket = Math.max(0, bracket - 1);
|
|
147
|
+
}
|
|
148
|
+
if (paren === 0 && bracket === 0 && c.trimEnd().endsWith(':')) {
|
|
149
|
+
headerEndIdx = j;
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
if (j > i && paren === 0 && bracket === 0 && analyzed[j].indent <= headerIndent) {
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
let lastContentIdx = headerEndIdx;
|
|
158
|
+
for (let j = headerEndIdx + 1; j < analyzed.length; j++) {
|
|
159
|
+
const nextInfo = analyzed[j];
|
|
160
|
+
// 陷阱: 多行三引号 docstring 内部行可能顶格书写(indent === 0),必须按三引号状态归入当前函数/类体防提前截断
|
|
161
|
+
if (nextInfo.startedInTriple || nextInfo.endedInTriple) {
|
|
162
|
+
lastContentIdx = j;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
const nextTrimmed = nextInfo.clean.trim();
|
|
166
|
+
if (!nextTrimmed) continue;
|
|
167
|
+
if (nextInfo.indent <= headerIndent) {
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
lastContentIdx = j;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const sigParts = [];
|
|
174
|
+
for (let j = i; j <= headerEndIdx; j++) {
|
|
175
|
+
const t = analyzed[j].raw.trim();
|
|
176
|
+
if (t) sigParts.push(t);
|
|
177
|
+
}
|
|
178
|
+
let signature = sigParts.join(' ').replace(/\s+/g, ' ').replace(/:\s*$/, '').trim();
|
|
179
|
+
if (signature.length > 140) {
|
|
180
|
+
signature = signature.slice(0, 137).trimEnd() + '...';
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const depth = classStack.length > 0 ? 1 : 0;
|
|
184
|
+
items.push({
|
|
185
|
+
kind: isClass ? 'class' : depth === 1 ? 'method' : 'def',
|
|
186
|
+
name,
|
|
187
|
+
signature,
|
|
188
|
+
startLine: startIdx + 1 + lineOffset,
|
|
189
|
+
endLine: lastContentIdx + 1 + lineOffset,
|
|
190
|
+
depth,
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
if (isClass) {
|
|
194
|
+
classStack.push({ indent: headerIndent, endIdx: lastContentIdx });
|
|
195
|
+
i = headerEndIdx;
|
|
196
|
+
} else {
|
|
197
|
+
i = lastContentIdx;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return items;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
module.exports = {
|
|
205
|
+
id: ID,
|
|
206
|
+
extensions: EXTENSIONS,
|
|
207
|
+
parse,
|
|
208
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
preprocessBraceLines,
|
|
5
|
+
findMatchingForward,
|
|
6
|
+
} = require('../../core/lexer');
|
|
7
|
+
const {
|
|
8
|
+
stripLeadingAttributesAndAnnotations,
|
|
9
|
+
scanBraceLanguage,
|
|
10
|
+
} = require('../../core/block-anchor');
|
|
11
|
+
|
|
12
|
+
const ID = 'rust';
|
|
13
|
+
const EXTENSIONS = ['.rs'];
|
|
14
|
+
const RUST_CONTAINERS = new Set(['struct', 'enum', 'trait', 'impl', 'mod']);
|
|
15
|
+
|
|
16
|
+
function parse(lines, ext = '.rs', options = {}) {
|
|
17
|
+
if (ext && typeof ext === 'object') {
|
|
18
|
+
options = ext;
|
|
19
|
+
ext = '.rs';
|
|
20
|
+
}
|
|
21
|
+
const lineOffset = options.lineOffset || 0;
|
|
22
|
+
const analyzed = preprocessBraceLines(lines, 'rust');
|
|
23
|
+
|
|
24
|
+
return scanBraceLanguage(analyzed, {
|
|
25
|
+
lexer: 'rust',
|
|
26
|
+
containers: RUST_CONTAINERS,
|
|
27
|
+
lineOffset,
|
|
28
|
+
mapCallableKind() {
|
|
29
|
+
return 'fn';
|
|
30
|
+
},
|
|
31
|
+
onStatementLine({
|
|
32
|
+
analyzed: rows,
|
|
33
|
+
lineIdx,
|
|
34
|
+
scopeStack,
|
|
35
|
+
items,
|
|
36
|
+
lineOffset: offset,
|
|
37
|
+
findDeclarationRange,
|
|
38
|
+
buildCompactSignature,
|
|
39
|
+
}) {
|
|
40
|
+
const trimmed = rows[lineIdx].clean.trim();
|
|
41
|
+
if (!trimmed) return null;
|
|
42
|
+
|
|
43
|
+
if (/^#!?\[/.test(trimmed) && !trimmed.includes('fn ') && !trimmed.includes('struct ') && !trimmed.includes('type ') && !trimmed.includes('macro_rules!')) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const core = stripLeadingAttributesAndAnnotations(trimmed);
|
|
48
|
+
if (!core) return null;
|
|
49
|
+
|
|
50
|
+
const macroMatch = core.match(/^(?:pub(?:\s*\([^)]*\))?\s+)?macro_rules!\s+([A-Za-z_]\w*)/);
|
|
51
|
+
if (macroMatch) {
|
|
52
|
+
let endIdx = lineIdx;
|
|
53
|
+
const cleanLine = rows[lineIdx].clean;
|
|
54
|
+
const openMatch = cleanLine.match(/[\{(\[]/);
|
|
55
|
+
if (openMatch) {
|
|
56
|
+
const openChar = openMatch[0];
|
|
57
|
+
const closePos = findMatchingForward(rows, lineIdx, openMatch.index, openChar);
|
|
58
|
+
if (closePos) endIdx = closePos.line;
|
|
59
|
+
}
|
|
60
|
+
const signature = buildCompactSignature(rows, lineIdx, lineIdx, true);
|
|
61
|
+
items.push({
|
|
62
|
+
kind: 'macro',
|
|
63
|
+
name: macroMatch[1],
|
|
64
|
+
signature,
|
|
65
|
+
startLine: lineIdx + 1 + offset,
|
|
66
|
+
endLine: endIdx + 1 + offset,
|
|
67
|
+
depth: scopeStack.length,
|
|
68
|
+
});
|
|
69
|
+
return { jumpToLine: endIdx };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const semiCandidate = core.match(
|
|
73
|
+
/^(?:pub(?:\s*\([^)]*\))?\s+)?(?:default\s+)?(?:const\s+)?(?:async\s+)?(?:unsafe\s+)?(?:extern(?:\s+"[^"]*")?\s+)?(fn|struct|type|mod)\s+([A-Za-z_]\w*)/
|
|
74
|
+
);
|
|
75
|
+
if (!semiCandidate) return null;
|
|
76
|
+
|
|
77
|
+
const range = findDeclarationRange(rows, lineIdx, 'rust');
|
|
78
|
+
if (range.hasBlock) return null;
|
|
79
|
+
|
|
80
|
+
const joinedClean = rows
|
|
81
|
+
.slice(lineIdx, range.endIdx + 1)
|
|
82
|
+
.map((r) => r.clean)
|
|
83
|
+
.join(' ')
|
|
84
|
+
.trim();
|
|
85
|
+
if (!joinedClean.endsWith(';')) return null;
|
|
86
|
+
|
|
87
|
+
let startIdx = lineIdx;
|
|
88
|
+
while (startIdx > 0) {
|
|
89
|
+
const prevTrim = rows[startIdx - 1].clean.trim();
|
|
90
|
+
if (/^#\[/.test(prevTrim) && rows[startIdx - 1].braceStart === rows[lineIdx].braceStart) {
|
|
91
|
+
startIdx--;
|
|
92
|
+
} else {
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const signature = buildCompactSignature(rows, startIdx, range.headerEndIdx, false);
|
|
98
|
+
items.push({
|
|
99
|
+
kind: semiCandidate[1],
|
|
100
|
+
name: semiCandidate[2],
|
|
101
|
+
signature,
|
|
102
|
+
startLine: startIdx + 1 + offset,
|
|
103
|
+
endLine: range.endIdx + 1 + offset,
|
|
104
|
+
depth: scopeStack.length,
|
|
105
|
+
});
|
|
106
|
+
return { jumpToLine: range.endIdx };
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
module.exports = {
|
|
112
|
+
id: ID,
|
|
113
|
+
extensions: EXTENSIONS,
|
|
114
|
+
parse,
|
|
115
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const jsLang = require('../javascript');
|
|
4
|
+
|
|
5
|
+
const ID = 'sfc';
|
|
6
|
+
const EXTENSIONS = ['.vue', '.svelte'];
|
|
7
|
+
|
|
8
|
+
function resolveScriptExt(openTagLine) {
|
|
9
|
+
const langMatch = openTagLine.match(/\blang\s*=\s*["']?([A-Za-z0-9_-]+)["']?/i);
|
|
10
|
+
if (!langMatch) return '.js';
|
|
11
|
+
const lang = langMatch[1].toLowerCase();
|
|
12
|
+
if (lang === 'ts' || lang === 'typescript') return '.ts';
|
|
13
|
+
if (lang === 'tsx') return '.tsx';
|
|
14
|
+
if (lang === 'jsx') return '.jsx';
|
|
15
|
+
return '.js';
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function parse(lines, ext = '.vue', options = {}) {
|
|
19
|
+
if (ext && typeof ext === 'object') {
|
|
20
|
+
options = ext;
|
|
21
|
+
ext = '.vue';
|
|
22
|
+
}
|
|
23
|
+
const baseOffset = options.lineOffset || 0;
|
|
24
|
+
const items = [];
|
|
25
|
+
const blockRegex = /^<(template|script|style)(\b[^>]*)?>/i;
|
|
26
|
+
|
|
27
|
+
for (let i = 0; i < lines.length; i++) {
|
|
28
|
+
const trimmed = lines[i].trim();
|
|
29
|
+
const m = trimmed.match(blockRegex);
|
|
30
|
+
if (!m) continue;
|
|
31
|
+
|
|
32
|
+
const tag = m[1].toLowerCase();
|
|
33
|
+
const closeTag = `</${tag}>`;
|
|
34
|
+
let endIdx = i;
|
|
35
|
+
for (let j = i; j < lines.length; j++) {
|
|
36
|
+
if (lines[j].toLowerCase().includes(closeTag)) {
|
|
37
|
+
endIdx = j;
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
items.push({
|
|
43
|
+
kind: 'section',
|
|
44
|
+
name: tag,
|
|
45
|
+
signature: trimmed.replace(/>.*$/, '>'),
|
|
46
|
+
startLine: i + 1 + baseOffset,
|
|
47
|
+
endLine: endIdx + 1 + baseOffset,
|
|
48
|
+
depth: 0,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
if (tag === 'script' && endIdx > i + 1) {
|
|
52
|
+
const innerLines = lines.slice(i + 1, endIdx);
|
|
53
|
+
const scriptExt = resolveScriptExt(trimmed);
|
|
54
|
+
const scriptItems = jsLang.parse(innerLines, scriptExt, {
|
|
55
|
+
lineOffset: baseOffset + i + 1,
|
|
56
|
+
});
|
|
57
|
+
for (const si of scriptItems) {
|
|
58
|
+
items.push({ ...si, depth: si.depth + 1 });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
i = endIdx;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return items;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
module.exports = {
|
|
69
|
+
id: ID,
|
|
70
|
+
extensions: EXTENSIONS,
|
|
71
|
+
parse,
|
|
72
|
+
};
|