tokless 0.5.1 → 0.5.2

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.
@@ -9,28 +9,32 @@ const {
9
9
  stripLeadingAttributesAndAnnotations,
10
10
  scanBraceLanguage,
11
11
  } = require('../../core/block-anchor');
12
+ const { parseCCpp } = require('./c-cpp-parser');
12
13
 
13
14
  const ID = 'c-family';
14
- const EXTENSIONS = ['.c', '.h', '.cpp', '.hpp', '.cc', '.cs'];
15
- const HEADER_EXTS = new Set(['.h', '.hpp']);
16
- const CPP_CONTAINERS = new Set(['class', 'struct', 'union', 'enum', 'namespace']);
15
+ const EXTENSIONS = [
16
+ '.c',
17
+ '.h',
18
+ '.cpp',
19
+ '.hpp',
20
+ '.cc',
21
+ '.cxx',
22
+ '.c++',
23
+ '.hh',
24
+ '.hxx',
25
+ '.h++',
26
+ '.inl',
27
+ '.inc',
28
+ '.cs',
29
+ ];
17
30
  const CS_CONTAINERS = new Set(['class', 'struct', 'interface', 'enum', 'record', 'namespace']);
18
31
 
19
- function parse(lines, ext = '.c', options = {}) {
20
- if (ext && typeof ext === 'object') {
21
- options = ext;
22
- ext = '.c';
23
- }
24
- const normalizedExt = String(ext || '.c').toLowerCase();
25
- const isCs = normalizedExt === '.cs';
26
- const isHeader = HEADER_EXTS.has(normalizedExt);
27
- const lineOffset = options.lineOffset || 0;
28
-
32
+ function parseCSharp(lines, lineOffset = 0) {
29
33
  const analyzed = preprocessBraceLines(lines, 'c');
30
34
 
31
35
  return scanBraceLanguage(analyzed, {
32
36
  lexer: 'c',
33
- containers: isCs ? CS_CONTAINERS : CPP_CONTAINERS,
37
+ containers: CS_CONTAINERS,
34
38
  lineOffset,
35
39
  onStatementLine({
36
40
  analyzed: rows,
@@ -51,7 +55,7 @@ function parse(lines, ext = '.c', options = {}) {
51
55
  const inTypeScope =
52
56
  activeScope !== null && activeScope.kind !== 'namespace' && activeScope.kind !== 'enum';
53
57
 
54
- if (isCs && inTypeScope) {
58
+ if (inTypeScope) {
55
59
  const propMatch = core.match(
56
60
  /^(?:(?:public|private|protected|internal|static|virtual|override|abstract|sealed|readonly|new|required|unsafe)\s+)+[\w.<>,?\[\]\s]+\s+([A-Za-z_]\w*)\s*(?:\{|=>)/
57
61
  );
@@ -78,31 +82,7 @@ function parse(lines, ext = '.c', options = {}) {
78
82
  }
79
83
  }
80
84
 
81
- if (!isCs && /\boperator\s*(?:<<|>>|\+|-|\*|\/|%|\^|&|\|)=/.test(core)) {
82
- const range = findDeclarationRange(rows, lineIdx, 'c');
83
- if (range.hasBlock || isHeader) {
84
- const opMatch = core.match(/\b(operator\s*(?:<<|>>|\+|-|\*|\/|%|\^|&|\|)=)/);
85
- if (opMatch) {
86
- const signature = buildCompactSignature(
87
- rows,
88
- lineIdx,
89
- range.headerEndIdx,
90
- range.hasBlock
91
- );
92
- items.push({
93
- kind: inTypeScope ? 'method' : 'function',
94
- name: opMatch[1].replace(/\s+/g, ''),
95
- signature,
96
- startLine: lineIdx + 1 + offset,
97
- endLine: range.endIdx + 1 + offset,
98
- depth: scopeStack.length,
99
- });
100
- return { jumpToLine: range.endIdx };
101
- }
102
- }
103
- }
104
-
105
- if (isHeader || (isCs && activeScope && activeScope.kind === 'interface')) {
85
+ if (activeScope && activeScope.kind === 'interface') {
106
86
  const range = findDeclarationRange(rows, lineIdx, 'c');
107
87
  if (range.hasBlock) return null;
108
88
 
@@ -113,50 +93,21 @@ function parse(lines, ext = '.c', options = {}) {
113
93
  .join(' ')
114
94
  ).trim();
115
95
  if (!joined.endsWith(';')) return null;
116
- if (/^(?:typedef|using|friend|extern\s+"C"|template\b[^;]*;$)/.test(joined)) return null;
117
96
 
118
- const cleanedProto = joined.replace(/^(?:public|private|protected)\s*:\s*/, '').trim();
119
- const parenIdx = cleanedProto.indexOf('(');
97
+ const parenIdx = joined.indexOf('(');
120
98
  if (parenIdx <= 0) return null;
121
- const beforeParen = cleanedProto.slice(0, parenIdx).trim();
99
+ const beforeParen = joined.slice(0, parenIdx).trim();
122
100
  if (hasTopLevelChar(beforeParen, '=')) return null;
123
101
 
124
- const firstWordMatch = beforeParen.match(/^([A-Za-z_]\w*)\b/);
125
- if (firstWordMatch && DEFAULT_CONTROL_KEYWORDS.has(firstWordMatch[1])) return null;
126
-
127
- let name = '';
128
- const opProto = beforeParen.match(/\b(operator\s*(?:\(\)|\[\]|[^\s(]+))\s*$/);
129
- if (opProto) {
130
- name = opProto[1].replace(/\s+/g, '');
131
- } else {
132
- const nm = beforeParen.match(/([~A-Za-z_]\w*)$/);
133
- if (!nm || DEFAULT_CONTROL_KEYWORDS.has(nm[1])) return null;
134
- name = nm[1];
135
- const prefix = beforeParen.slice(0, beforeParen.length - name.length).trim();
136
- const isCtorOrDtor =
137
- inTypeScope &&
138
- activeScope &&
139
- (name === activeScope.name || name === `~${activeScope.name}`);
140
- // 陷阱: 头文件宏调用(如 `DECLARE_API(foo);`)无返回类型前缀,必须要求显式返回类型或类构造/析构名才认作函数原型
141
- if (!prefix && !isCtorOrDtor) return null;
142
- }
143
-
144
- const afterCloseIdx = cleanedProto.lastIndexOf(')');
145
- if (afterCloseIdx === -1) return null;
146
- const tail = cleanedProto.slice(afterCloseIdx + 1, -1).trim();
147
- if (
148
- tail &&
149
- !/^(?:(?:const|volatile|noexcept(?:\([^)]*\))?|override|final)\s*)*(?:=\s*(?:0|default|delete))?$/.test(
150
- tail
151
- )
152
- ) {
153
- return null;
154
- }
102
+ const nm = beforeParen.match(/([A-Za-z_]\w*)$/);
103
+ if (!nm || DEFAULT_CONTROL_KEYWORDS.has(nm[1])) return null;
104
+ const prefix = beforeParen.slice(0, beforeParen.length - nm[1].length).trim();
105
+ if (!prefix) return null;
155
106
 
156
107
  const signature = buildCompactSignature(rows, lineIdx, range.headerEndIdx, false);
157
108
  items.push({
158
- kind: inTypeScope ? 'method' : 'function',
159
- name,
109
+ kind: 'method',
110
+ name: nm[1],
160
111
  signature,
161
112
  startLine: lineIdx + 1 + offset,
162
113
  endLine: range.endIdx + 1 + offset,
@@ -170,6 +121,20 @@ function parse(lines, ext = '.c', options = {}) {
170
121
  });
171
122
  }
172
123
 
124
+ function parse(lines, ext = '.c', options = {}) {
125
+ if (ext && typeof ext === 'object') {
126
+ options = ext;
127
+ ext = '.c';
128
+ }
129
+ const normalizedExt = String(ext || '.c').toLowerCase();
130
+ const lineOffset = options.lineOffset || 0;
131
+
132
+ if (normalizedExt === '.cs') {
133
+ return parseCSharp(lines, lineOffset);
134
+ }
135
+ return parseCCpp(lines, normalizedExt, options);
136
+ }
137
+
173
138
  module.exports = {
174
139
  id: ID,
175
140
  extensions: EXTENSIONS,
@@ -14,6 +14,12 @@ const IGNORED_DIRS = new Set([
14
14
  '.next',
15
15
  'target',
16
16
  '__pycache__',
17
+ 'Objects',
18
+ 'Listings',
19
+ '.cache',
20
+ '.clangd',
21
+ '.vs',
22
+ '.idea',
17
23
  ]);
18
24
 
19
25
  function formatSkeletonItems(result, options = {}) {
@@ -112,6 +118,16 @@ function scanDirectorySkeleton(dirPath, options = {}) {
112
118
  ? options.depth
113
119
  : null;
114
120
  const maxDepth = explicitMaxDepth !== null ? explicitMaxDepth : isMap ? 8 : 2;
121
+ const extraExclude = new Set();
122
+ if (Array.isArray(options.exclude) || options.exclude instanceof Set) {
123
+ for (const item of options.exclude) {
124
+ if (typeof item === 'string' && item.trim()) extraExclude.add(item.trim());
125
+ }
126
+ } else if (typeof options.exclude === 'string' && options.exclude.trim()) {
127
+ for (const part of options.exclude.split(',')) {
128
+ if (part.trim()) extraExclude.add(part.trim());
129
+ }
130
+ }
115
131
  const root = path.resolve(dirPath);
116
132
  const files = [];
117
133
 
@@ -127,7 +143,13 @@ function scanDirectorySkeleton(dirPath, options = {}) {
127
143
 
128
144
  for (const entry of entries) {
129
145
  if (entry.isDirectory()) {
130
- if (IGNORED_DIRS.has(entry.name)) continue;
146
+ if (
147
+ IGNORED_DIRS.has(entry.name) ||
148
+ entry.name.startsWith('cmake-build-') ||
149
+ extraExclude.has(entry.name)
150
+ ) {
151
+ continue;
152
+ }
131
153
  if (currentDepth < maxDepth) {
132
154
  walk(path.join(currentDir, entry.name), currentDepth + 1);
133
155
  }