dsh-remote-vs 0.26.0 → 0.28.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 +112 -28
- package/README.md +84 -25
- package/THIRD-PARTY-NOTICES.md +7 -2
- package/lib/client.js +1237 -254
- package/lib/index.js +821 -17
- package/lib/types/changes/recorder.d.ts +119 -2
- package/lib/types/client/CodeEditor.d.ts +29 -0
- package/lib/types/client/OutlinePanel.d.ts +22 -0
- package/lib/types/client/ProcessesSection.d.ts +19 -0
- package/lib/types/client/api.d.ts +22 -2
- package/lib/types/client/blame-model.d.ts +77 -0
- package/lib/types/client/changes/apply-failure.d.ts +41 -0
- package/lib/types/client/changes/block-confirm.d.ts +97 -0
- package/lib/types/client/changes/diff-outcome.d.ts +56 -0
- package/lib/types/client/changes/freshness.d.ts +51 -0
- package/lib/types/client/changes/model.d.ts +19 -1
- package/lib/types/client/changes/session-diff.d.ts +100 -0
- package/lib/types/client/changes/staleness.d.ts +98 -0
- package/lib/types/client/changes/turn-reverted.d.ts +82 -0
- package/lib/types/client/changes/turn-view.d.ts +131 -0
- package/lib/types/client/changes/undo-affordance.d.ts +44 -10
- package/lib/types/client/changes/undo-target.d.ts +12 -0
- package/lib/types/client/i18n.d.ts +1 -1
- package/lib/types/client/outline-model.d.ts +59 -0
- package/lib/types/client/scm/actions.d.ts +12 -1
- package/lib/types/client/store.d.ts +109 -1
- package/lib/types/client/vsc/blame.d.ts +19 -0
- package/lib/types/client/vsc/change-marks.d.ts +21 -0
- package/lib/types/client/vsc/kit.d.ts +0 -2
- package/lib/types/client/vsc/preview-document.d.ts +46 -0
- package/lib/types/client/vsc/preview-model.d.ts +31 -0
- package/lib/types/client/vsc/preview-services.d.ts +24 -0
- package/lib/types/git-ops.d.ts +18 -0
- package/lib/types/processes.d.ts +43 -0
- package/lib/types/protocol.d.ts +133 -1
- package/package.json +2 -1
- package/src/changes/recorder.ts +851 -8
- package/src/client/CodeEditor.tsx +86 -4
- package/src/client/EditorPane.tsx +581 -41
- package/src/client/MonitorView.tsx +16 -5
- package/src/client/OutlinePanel.tsx +287 -0
- package/src/client/ProcessesSection.tsx +292 -0
- package/src/client/api.ts +38 -3
- package/src/client/blame-model.ts +242 -0
- package/src/client/changes/ChangesDock.tsx +539 -54
- package/src/client/changes/apply-failure.ts +73 -0
- package/src/client/changes/block-confirm.ts +180 -0
- package/src/client/changes/changes.css +45 -0
- package/src/client/changes/diff-outcome.ts +97 -0
- package/src/client/changes/freshness.ts +68 -0
- package/src/client/changes/model.ts +56 -1
- package/src/client/changes/session-diff.ts +187 -0
- package/src/client/changes/staleness.ts +148 -0
- package/src/client/changes/turn-reverted.ts +254 -0
- package/src/client/changes/turn-view.ts +195 -0
- package/src/client/changes/undo-affordance.ts +295 -63
- package/src/client/changes/undo-target.ts +12 -0
- package/src/client/i18n.ts +294 -2
- package/src/client/index.tsx +4 -0
- package/src/client/outline-model.ts +775 -0
- package/src/client/scm/DiffView.tsx +32 -13
- package/src/client/scm/HistoryPanel.tsx +38 -1
- package/src/client/scm/actions.ts +55 -2
- package/src/client/store.tsx +446 -21
- package/src/client/vsc/blame.ts +147 -0
- package/src/client/vsc/change-marks.ts +21 -0
- package/src/client/vsc/kit.tsx +3 -11
- package/src/client/vsc/preview-document.tsx +326 -0
- package/src/client/vsc/preview-model.ts +216 -0
- package/src/client/vsc/preview-services.ts +118 -0
- package/src/client/vsc/vsc.css +965 -39
- package/src/git-ops.ts +34 -0
- package/src/processes.ts +177 -0
- package/src/protocol.ts +138 -1
- package/src/routes.ts +138 -9
|
@@ -0,0 +1,775 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文件符号大纲的纯函数模型:文本 + 路径 → 有序符号列表。
|
|
3
|
+
*
|
|
4
|
+
* 为什么不用编辑器里现成的 lezer 语法树
|
|
5
|
+
* --------------------------------------
|
|
6
|
+
* 1. `ensureSyntaxTree` 有时间预算,长文件尾部会解析不出来;大纲沉默地只覆盖
|
|
7
|
+
* 前一半,比没有大纲更误导(审计对 blame 窗口提过同一类坑)。
|
|
8
|
+
* 2. 大纲必须在文件 dirty 时按**实时 buffer**重算,读 `EditorView` 会把本模块
|
|
9
|
+
* 绑死在编辑器身上,也让纯函数门禁没法跑。
|
|
10
|
+
* 3. 没有 lezer 语法的语言(shell、配置、C 头文件…)本来就要有降级路径;既然
|
|
11
|
+
* 降级要写,就让同一套行扫描服务全部语言 —— 排序/嵌套规则因此完全一致。
|
|
12
|
+
*
|
|
13
|
+
* 做法:先把注释和字符串「挖空」(长度不变、换行保留),再逐行匹配声明。挖空
|
|
14
|
+
* 之后 `// class Fake` 或 `"func Fake()"` 里的关键字不再可能是符号;嵌套用大括
|
|
15
|
+
* 号深度(C 家族)或缩进(Python/未知语言)表达。
|
|
16
|
+
*
|
|
17
|
+
* 这是**大纲**,不是解析器:多行签名、嵌套泛型、宏生成的名字会漏;宁可漏也不
|
|
18
|
+
* 误报成另一种符号 —— UI 会把「猜的」明确标出来。
|
|
19
|
+
*/
|
|
20
|
+
import { fuzzyScore } from './quickopen-model.ts'
|
|
21
|
+
|
|
22
|
+
export type OutlineLanguage =
|
|
23
|
+
| 'java' | 'c' | 'cpp' | 'go' | 'rust' | 'python' | 'javascript' | 'typescript' | 'unknown'
|
|
24
|
+
|
|
25
|
+
export type OutlineKind =
|
|
26
|
+
| 'class' | 'interface' | 'struct' | 'enum' | 'trait' | 'impl' | 'namespace'
|
|
27
|
+
| 'module' | 'function' | 'method' | 'constructor' | 'field' | 'variable' | 'type' | 'macro'
|
|
28
|
+
|
|
29
|
+
export interface OutlineEntry {
|
|
30
|
+
name: string
|
|
31
|
+
kind: OutlineKind
|
|
32
|
+
/** 1-based 行号(与 `revealRange` 同一套语义)。 */
|
|
33
|
+
line: number
|
|
34
|
+
/** 嵌套层数,0 = 顶层。用缩进表达嵌套,不暴露大括号深度。 */
|
|
35
|
+
depth: number
|
|
36
|
+
/** 直接外层类型/命名空间的名字(没有就是顶层)。 */
|
|
37
|
+
container?: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface OutlineResult {
|
|
41
|
+
language: OutlineLanguage
|
|
42
|
+
entries: OutlineEntry[]
|
|
43
|
+
/** 扫描到的符号总数;可能大于 `entries.length`(被上限截断)。 */
|
|
44
|
+
total: number
|
|
45
|
+
truncated: boolean
|
|
46
|
+
/** 语言未识别:条目是按缩进 + 关键字猜的,UI 必须标注。 */
|
|
47
|
+
guessed: boolean
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 单个文件最多列出的条目数。
|
|
52
|
+
*
|
|
53
|
+
* 20k 行的 Java 文件大约几百个符号,5000 已经远超真实代码;设上限是为了让病态
|
|
54
|
+
* 输入(生成代码、压缩成一行的 JSON 式脚本)不会把 DOM 拖垮,同时 `total` 仍然
|
|
55
|
+
* 报出真实数量,截断不撒谎。
|
|
56
|
+
*/
|
|
57
|
+
export const OUTLINE_MAX_ENTRIES = 5000
|
|
58
|
+
|
|
59
|
+
/** 面板一次渲染的候选上限(过滤后)。 */
|
|
60
|
+
export const OUTLINE_RESULT_LIMIT = 300
|
|
61
|
+
|
|
62
|
+
/** 扩展名 → 语言。C 头文件与 C++ 头文件都按 C/C++ 处理。 */
|
|
63
|
+
const LANGUAGE_BY_EXTENSION: Record<string, OutlineLanguage> = {
|
|
64
|
+
java: 'java',
|
|
65
|
+
c: 'c', h: 'c',
|
|
66
|
+
cc: 'cpp', cpp: 'cpp', cxx: 'cpp', hpp: 'cpp', hh: 'cpp', hxx: 'cpp', inl: 'cpp', ipp: 'cpp',
|
|
67
|
+
go: 'go',
|
|
68
|
+
rs: 'rust',
|
|
69
|
+
py: 'python', pyw: 'python', pyi: 'python',
|
|
70
|
+
js: 'javascript', jsx: 'javascript', mjs: 'javascript', cjs: 'javascript',
|
|
71
|
+
ts: 'typescript', tsx: 'typescript', mts: 'typescript', cts: 'typescript',
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** 由远端路径判断语言;不认识就返回 `unknown`(走降级大纲)。 */
|
|
75
|
+
export function outlineLanguageOf(path: string): OutlineLanguage {
|
|
76
|
+
const name = path.slice(path.lastIndexOf('/') + 1).replace(/[?#].*$/, '')
|
|
77
|
+
const dot = name.lastIndexOf('.')
|
|
78
|
+
if (dot <= 0) return 'unknown'
|
|
79
|
+
return LANGUAGE_BY_EXTENSION[name.slice(dot + 1).toLowerCase()] ?? 'unknown'
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// --------------------------------------------------------------- 注释/字符串挖空
|
|
83
|
+
|
|
84
|
+
interface MaskRules {
|
|
85
|
+
/** `//` 行注释 */
|
|
86
|
+
lineSlash: boolean
|
|
87
|
+
/** `/* ... *\/` 块注释 */
|
|
88
|
+
blockSlash: boolean
|
|
89
|
+
/** `#` 行注释(Python / 未知语言) */
|
|
90
|
+
hash: boolean
|
|
91
|
+
/** `'...'` 字符串(Rust 关掉:`'a` 是生命周期,不是字符字面量) */
|
|
92
|
+
single: boolean
|
|
93
|
+
double: boolean
|
|
94
|
+
backtick: boolean
|
|
95
|
+
/** `'''` / `"""` 多行字符串(Python 文档字符串) */
|
|
96
|
+
triple: boolean
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function maskRulesFor(language: OutlineLanguage): MaskRules {
|
|
100
|
+
if (language === 'python') {
|
|
101
|
+
return { lineSlash: false, blockSlash: false, hash: true, single: true, double: true, backtick: false, triple: true }
|
|
102
|
+
}
|
|
103
|
+
if (language === 'unknown') {
|
|
104
|
+
return { lineSlash: true, blockSlash: true, hash: true, single: true, double: true, backtick: true, triple: false }
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
lineSlash: true,
|
|
108
|
+
blockSlash: true,
|
|
109
|
+
hash: false,
|
|
110
|
+
single: language !== 'rust',
|
|
111
|
+
double: true,
|
|
112
|
+
backtick: true,
|
|
113
|
+
// Java text blocks are the one non-Python form whose body can contain
|
|
114
|
+
// braces/`class` words; masking them keeps a block from shifting depth.
|
|
115
|
+
triple: language === 'java',
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* 把注释与字符串逐字符替换成空格(换行保留),返回按行切好的副本。
|
|
121
|
+
*
|
|
122
|
+
* 「长度不变」很重要:任何后续的行内偏移都还对得上原文件,行号也直接来自下标。
|
|
123
|
+
* 状态机是近似的(C++ 原始字符串、Java 文本块、JS 正则字面量不特殊处理),目标
|
|
124
|
+
* 是让「注释/字符串里的伪符号」不出现,不是做词法分析。
|
|
125
|
+
*/
|
|
126
|
+
export function maskSource(text: string, language: OutlineLanguage): string[] {
|
|
127
|
+
const rules = maskRulesFor(language)
|
|
128
|
+
const masked: string[] = []
|
|
129
|
+
let mode: 'code' | 'line' | 'block' | 'single' | 'double' | 'backtick' | 'triple' = 'code'
|
|
130
|
+
let tripleQuote = ''
|
|
131
|
+
let escaped = false
|
|
132
|
+
for (let at = 0; at < text.length; at += 1) {
|
|
133
|
+
const character = text[at] as string
|
|
134
|
+
if (character === '\n') {
|
|
135
|
+
// 行注释和单行字符串到行尾结束;块注释/三引号跨行延续。
|
|
136
|
+
if (mode === 'line' || mode === 'single' || mode === 'double') mode = 'code'
|
|
137
|
+
escaped = false
|
|
138
|
+
masked.push('\n')
|
|
139
|
+
continue
|
|
140
|
+
}
|
|
141
|
+
if (mode === 'code') {
|
|
142
|
+
const next = text[at + 1]
|
|
143
|
+
if (rules.lineSlash && character === '/' && next === '/') { mode = 'line'; masked.push(' '); at += 1; continue }
|
|
144
|
+
if (rules.blockSlash && character === '/' && next === '*') { mode = 'block'; masked.push(' '); at += 1; continue }
|
|
145
|
+
if (rules.hash && character === '#') { mode = 'line'; masked.push(' '); continue }
|
|
146
|
+
if (rules.triple && (character === '"' || character === "'") && text.startsWith(character.repeat(3), at)) {
|
|
147
|
+
mode = 'triple'
|
|
148
|
+
tripleQuote = character
|
|
149
|
+
masked.push(' ')
|
|
150
|
+
at += 2
|
|
151
|
+
continue
|
|
152
|
+
}
|
|
153
|
+
// Rust char literals (`'{'`, `'\n'`, `'a'`) are masked here because the
|
|
154
|
+
// generic single-quote mode is disabled for lifetimes (`'a`). Without
|
|
155
|
+
// this a brace inside a char literal raises the depth for the rest of
|
|
156
|
+
// the file.
|
|
157
|
+
if (language === 'rust' && character === "'") {
|
|
158
|
+
const charLiteral = /^'(?:\\.|[^'\\])'/.exec(text.slice(at))
|
|
159
|
+
if (charLiteral !== null) {
|
|
160
|
+
const width = charLiteral[0].length
|
|
161
|
+
masked.push(' '.repeat(width))
|
|
162
|
+
at += width - 1
|
|
163
|
+
continue
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (rules.single && character === "'") { mode = 'single'; masked.push(' '); continue }
|
|
167
|
+
if (rules.double && character === '"') { mode = 'double'; masked.push(' '); continue }
|
|
168
|
+
if (rules.backtick && character === '`') { mode = 'backtick'; masked.push(' '); continue }
|
|
169
|
+
masked.push(character)
|
|
170
|
+
continue
|
|
171
|
+
}
|
|
172
|
+
// 注释/字符串内部:一律挖成空格,只有结束符能把状态切回去。
|
|
173
|
+
masked.push(' ')
|
|
174
|
+
if (mode === 'block') {
|
|
175
|
+
if (character === '*' && text[at + 1] === '/') { mode = 'code'; masked.push(' '); at += 1 }
|
|
176
|
+
continue
|
|
177
|
+
}
|
|
178
|
+
if (escaped) { escaped = false; continue }
|
|
179
|
+
if (character === '\\') { escaped = true; continue }
|
|
180
|
+
if (mode === 'triple') {
|
|
181
|
+
if (character === tripleQuote && text.startsWith(tripleQuote.repeat(3), at)) {
|
|
182
|
+
mode = 'code'
|
|
183
|
+
masked.push(' ')
|
|
184
|
+
at += 2
|
|
185
|
+
}
|
|
186
|
+
continue
|
|
187
|
+
}
|
|
188
|
+
if ((mode === 'single' && character === "'")
|
|
189
|
+
|| (mode === 'double' && character === '"')
|
|
190
|
+
|| (mode === 'backtick' && character === '`')) mode = 'code'
|
|
191
|
+
}
|
|
192
|
+
return masked.join('').split('\n')
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// ------------------------------------------------------------------ 行内工具
|
|
196
|
+
|
|
197
|
+
/** 一行里从 `startDepth` 开始、走到 `position` 之前的大括号深度。 */
|
|
198
|
+
function depthAtIn(line: string, startDepth: number, position: number): number {
|
|
199
|
+
let next = startDepth
|
|
200
|
+
const end = Math.min(position, line.length)
|
|
201
|
+
for (let at = 0; at < end; at += 1) {
|
|
202
|
+
const character = line[at]
|
|
203
|
+
if (character === '{') next += 1
|
|
204
|
+
else if (character === '}') next -= 1
|
|
205
|
+
}
|
|
206
|
+
return next
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** 处理完整行之后的大括号深度。 */
|
|
210
|
+
function depthAfter(line: string, startDepth: number): number {
|
|
211
|
+
return depthAtIn(line, startDepth, line.length)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Net `(` minus `)` on one masked line, for multi-line signature detection. */
|
|
215
|
+
function parenDelta(line: string): number {
|
|
216
|
+
let delta = 0
|
|
217
|
+
for (const character of line) {
|
|
218
|
+
if (character === '(') delta += 1
|
|
219
|
+
else if (character === ')') delta -= 1
|
|
220
|
+
}
|
|
221
|
+
return delta
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** 行的缩进列数(tab 按 4 列算,与常见编辑器默认一致)。 */
|
|
225
|
+
function indentOf(line: string): number {
|
|
226
|
+
let width = 0
|
|
227
|
+
for (let at = 0; at < line.length; at += 1) {
|
|
228
|
+
const character = line[at]
|
|
229
|
+
if (character === ' ') width += 1
|
|
230
|
+
else if (character === '\t') width += 4
|
|
231
|
+
else break
|
|
232
|
+
}
|
|
233
|
+
return width
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function escapeRegExp(value: string): string {
|
|
237
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** 出现在「名字 + (`」位置但绝不是函数声明的关键字。 */
|
|
241
|
+
const NOT_CALLABLE = new Set([
|
|
242
|
+
'if', 'for', 'while', 'switch', 'catch', 'return', 'new', 'else', 'do', 'try', 'case', 'throw',
|
|
243
|
+
'delete', 'sizeof', 'typeof', 'await', 'yield', 'goto', 'assert', 'using', 'import', 'package',
|
|
244
|
+
'typedef', 'namespace', 'define', 'include', 'ifdef', 'ifndef', 'endif', 'pragma', 'undef',
|
|
245
|
+
'class', 'struct', 'enum', 'interface', 'func', 'fn', 'def', 'type', 'let', 'const', 'var',
|
|
246
|
+
'public', 'private', 'protected', 'static', 'final', 'abstract', 'synchronized', 'native',
|
|
247
|
+
'transient', 'volatile', 'default', 'readonly', 'override', 'declare', 'export', 'async',
|
|
248
|
+
])
|
|
249
|
+
|
|
250
|
+
/** 这些容器直接成员算「方法」;其余(模块、命名空间…)里的可调用成员算「函数」。 */
|
|
251
|
+
const METHOD_OWNERS = new Set<OutlineKind>([
|
|
252
|
+
'class', 'interface', 'struct', 'trait', 'impl', 'enum',
|
|
253
|
+
])
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* 会打开一个「成员作用域」的条目。
|
|
257
|
+
*
|
|
258
|
+
* 只有类型/命名空间进栈:函数体不是文件结构,把方法也压栈会让 2k 行 Java 文件
|
|
259
|
+
* 里的每个局部变量都变成一条「字段」(审计样本里就是这个问题)。
|
|
260
|
+
*/
|
|
261
|
+
const BLOCK_KINDS = new Set<OutlineKind>([
|
|
262
|
+
'class', 'interface', 'struct', 'enum', 'trait', 'impl', 'namespace', 'module',
|
|
263
|
+
])
|
|
264
|
+
|
|
265
|
+
interface RawSymbol {
|
|
266
|
+
name: string
|
|
267
|
+
kind: OutlineKind
|
|
268
|
+
line: number
|
|
269
|
+
depth: number
|
|
270
|
+
container?: string
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
interface OpenContainer {
|
|
274
|
+
name: string
|
|
275
|
+
kind: OutlineKind
|
|
276
|
+
/** 这个容器**直接成员**所在的大括号深度。 */
|
|
277
|
+
bodyDepth: number
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
interface Match {
|
|
281
|
+
name: string
|
|
282
|
+
kind: OutlineKind
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// ------------------------------------------------------------- C 家族(大括号)
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Java/C/C++/Go/Rust/JS/TS 共用的扫描骨架。
|
|
289
|
+
*
|
|
290
|
+
* 只收「顶层」与「类型体的直接成员」:函数体里的局部声明(`int x = 1;`)全都
|
|
291
|
+
* 在更深的大括号层级,被 `direct` 判定挡掉 —— 否则 2k 行的 Java 文件会列出一万
|
|
292
|
+
* 条局部变量,大纲就没法用了。
|
|
293
|
+
*/
|
|
294
|
+
function scanBraceLanguages(
|
|
295
|
+
text: string,
|
|
296
|
+
language: Exclude<OutlineLanguage, 'python' | 'unknown'>,
|
|
297
|
+
): RawSymbol[] {
|
|
298
|
+
const lines = maskSource(text, language)
|
|
299
|
+
const found: RawSymbol[] = []
|
|
300
|
+
const containers: OpenContainer[] = []
|
|
301
|
+
let depth = 0
|
|
302
|
+
let continuationParens = 0
|
|
303
|
+
/** A type declaration whose `{` is on the next line (Allman style). */
|
|
304
|
+
let pendingContainer: (OpenContainer & { at: number }) | undefined
|
|
305
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
306
|
+
const line = lines[index] as string
|
|
307
|
+
const trimmed = line.trim()
|
|
308
|
+
if (trimmed === '') continue
|
|
309
|
+
const startDepth = depth
|
|
310
|
+
const owner = containers.length === 0 ? undefined : containers[containers.length - 1]
|
|
311
|
+
// 预处理行(`#include` / `#[derive]`)不是声明;同时避免 `#define F(x)` 被
|
|
312
|
+
// 当成函数。JS/TS 的 `#private` 字段不是指令,不能整行跳过。
|
|
313
|
+
const directiveLine = trimmed.startsWith('#') && language !== 'javascript' && language !== 'typescript'
|
|
314
|
+
// 预处理行也不参与大括号计数:`#define BEGIN {` 的 `{` 不是作用域,记进去会
|
|
315
|
+
// 让后面每一行都深一层(C 代码里很常见)。
|
|
316
|
+
if (directiveLine) continue
|
|
317
|
+
// Allman style: `class Foo` followed by `{` on its own line. Hold the
|
|
318
|
+
// declaration until that brace arrives instead of silently losing every
|
|
319
|
+
// member of the type.
|
|
320
|
+
if (pendingContainer !== undefined) {
|
|
321
|
+
if (trimmed.startsWith('{')) {
|
|
322
|
+
const bodyDepth = depthAfter(line, startDepth)
|
|
323
|
+
containers.push({
|
|
324
|
+
name: pendingContainer.name,
|
|
325
|
+
kind: pendingContainer.kind,
|
|
326
|
+
bodyDepth: Math.max(startDepth + 1, bodyDepth),
|
|
327
|
+
})
|
|
328
|
+
depth = Math.max(0, bodyDepth)
|
|
329
|
+
pendingContainer = undefined
|
|
330
|
+
continue
|
|
331
|
+
}
|
|
332
|
+
pendingContainer = undefined
|
|
333
|
+
}
|
|
334
|
+
// `extern "C" {` is a transparent wrapper: its braces must not shift every
|
|
335
|
+
// wrapped declaration one level down (the old behaviour made the whole
|
|
336
|
+
// file outline empty). The matching close brace clamps at 0.
|
|
337
|
+
if (/^extern\s*\{/.test(trimmed)) {
|
|
338
|
+
depth = startDepth
|
|
339
|
+
continue
|
|
340
|
+
}
|
|
341
|
+
// A multi-line parameter list: continuation lines such as `int a,` are
|
|
342
|
+
// parameters, not fields. Suppress declaration matching until the
|
|
343
|
+
// parenthesis balance returns to zero (braces still update normally).
|
|
344
|
+
const continuation = continuationParens > 0
|
|
345
|
+
if (!continuation) {
|
|
346
|
+
// Java text blocks (and a few other constructs) can leave a `;` before
|
|
347
|
+
// the next declaration on the closing line; it is punctuation, not part
|
|
348
|
+
// of the declaration name.
|
|
349
|
+
const candidate = trimmed.replace(/^[;,]+\s*/, '')
|
|
350
|
+
const match = matchDeclaration(language, candidate, owner)
|
|
351
|
+
if (match !== undefined) {
|
|
352
|
+
const lead = line.length - line.trimStart().length
|
|
353
|
+
const at = depthAtIn(line, startDepth, lead)
|
|
354
|
+
const direct = owner === undefined ? at === 0 : at === owner.bodyDepth
|
|
355
|
+
if (direct) {
|
|
356
|
+
found.push({
|
|
357
|
+
name: match.name,
|
|
358
|
+
kind: match.kind,
|
|
359
|
+
line: index + 1,
|
|
360
|
+
depth: containers.length,
|
|
361
|
+
...(owner === undefined ? {} : { container: owner.name }),
|
|
362
|
+
})
|
|
363
|
+
const bodyDepth = depthAfter(line, startDepth)
|
|
364
|
+
if (bodyDepth > at && BLOCK_KINDS.has(match.kind)) {
|
|
365
|
+
containers.push({ name: match.name, kind: match.kind, bodyDepth })
|
|
366
|
+
} else if (BLOCK_KINDS.has(match.kind)) {
|
|
367
|
+
pendingContainer = { name: match.name, kind: match.kind, bodyDepth: at, at }
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
continuationParens = Math.max(0, continuationParens + parenDelta(line))
|
|
373
|
+
// 夹到 0:不配平的文件(半截代码、被注释掉的括号)不能让深度变成负数。
|
|
374
|
+
depth = Math.max(0, depthAfter(line, startDepth))
|
|
375
|
+
while (containers.length > 0 && (containers[containers.length - 1] as OpenContainer).bodyDepth > depth) {
|
|
376
|
+
containers.pop()
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return found
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function matchDeclaration(
|
|
383
|
+
language: Exclude<OutlineLanguage, 'python' | 'unknown'>,
|
|
384
|
+
trimmed: string,
|
|
385
|
+
owner: OpenContainer | undefined,
|
|
386
|
+
): Match | undefined {
|
|
387
|
+
if (language === 'java') return matchJava(trimmed, owner)
|
|
388
|
+
if (language === 'c' || language === 'cpp') return matchCpp(trimmed, owner)
|
|
389
|
+
if (language === 'go') return matchGo(trimmed, owner)
|
|
390
|
+
if (language === 'rust') return matchRust(trimmed, owner)
|
|
391
|
+
return matchScript(trimmed, owner)
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const JAVA_MODIFIERS = /^(?:(?:@[A-Za-z_$][\w$.]*(?:\([^)]*\))?|public|private|protected|static|final|abstract|synchronized|native|transient|volatile|strictfp|sealed|non-sealed)\s+)+/
|
|
395
|
+
|
|
396
|
+
function matchJava(trimmed: string, owner: OpenContainer | undefined): Match | undefined {
|
|
397
|
+
const rest = trimmed.replace(JAVA_MODIFIERS, '')
|
|
398
|
+
const type = /^(class|interface|enum|record)\s+([A-Za-z_$][\w$]*)/.exec(rest)
|
|
399
|
+
if (type !== null) {
|
|
400
|
+
return { name: type[2] as string, kind: type[1] === 'record' ? 'class' : type[1] as OutlineKind }
|
|
401
|
+
}
|
|
402
|
+
// 构造器:名字就是外层类型名(`public Foo(` / `Foo<T>(`)。
|
|
403
|
+
if (owner !== undefined && new RegExp(`^${escapeRegExp(owner.name)}(?:\\s*<[^>]*>)?\\s*\\(`).test(rest)) {
|
|
404
|
+
return { name: owner.name, kind: 'constructor' }
|
|
405
|
+
}
|
|
406
|
+
// 方法:`返回类型 名字(`。`new Foo(` / `return Foo(` 由关键字表挡掉。
|
|
407
|
+
const method = /^(?:<[^>]+>\s*)?([A-Za-z_$][\w$.]*(?:\s*<[^>]*>)?(?:\s*\[\s*\])*)\s+([A-Za-z_$][\w$]*)\s*\(/.exec(rest)
|
|
408
|
+
if (method !== null && !NOT_CALLABLE.has(method[1] as string)) {
|
|
409
|
+
return { name: method[2] as string, kind: owner === undefined ? 'function' : 'method' }
|
|
410
|
+
}
|
|
411
|
+
// 字段:`类型 名字 = / ; / ,`。名字后面跟 `(` 的是方法,上面已经处理。
|
|
412
|
+
const field = /^([A-Za-z_$][\w$.]*(?:\s*<[^>]*>)?(?:\s*\[\s*\])*)\s+([A-Za-z_$][\w$]*)\s*(?:=|;|,)/.exec(rest)
|
|
413
|
+
if (field !== null && owner !== undefined && !NOT_CALLABLE.has(field[1] as string)) {
|
|
414
|
+
return { name: field[2] as string, kind: 'field' }
|
|
415
|
+
}
|
|
416
|
+
return undefined
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
const CPP_SPECIFIERS = /^(?:inline|static|extern|constexpr|consteval|constinit|virtual|explicit|friend|const|volatile|unsigned|signed|register|thread_local|struct|class|enum|typename)\s+/
|
|
420
|
+
|
|
421
|
+
function matchCpp(trimmed: string, owner: OpenContainer | undefined): Match | undefined {
|
|
422
|
+
// `template<...>` 前缀不是声明本身;`extern "C" {` 同理只是包装。
|
|
423
|
+
const head = trimmed.replace(/^template\s*<[^>]*>\s*/, '').replace(/^typedef\s+/, '')
|
|
424
|
+
// 类型声明必须先在「去掉说明符」之前匹配:`struct` 既是说明符也是声明关键字,
|
|
425
|
+
// 先剥说明符会把 `struct Point {` 剥成 `Point {`,整个结构体就没了。
|
|
426
|
+
const type = /^(struct|union|enum|class)\s+(?:class\s+|struct\s+)?([A-Za-z_]\w*)/.exec(head)
|
|
427
|
+
if (type !== null) {
|
|
428
|
+
const keyword = type[1] as string
|
|
429
|
+
const kind: OutlineKind = keyword === 'enum' ? 'enum' : keyword === 'class' ? 'class' : 'struct'
|
|
430
|
+
return { name: type[2] as string, kind }
|
|
431
|
+
}
|
|
432
|
+
const namespace = /^(?:inline\s+)?namespace\s+([A-Za-z_]\w*)/.exec(head)
|
|
433
|
+
if (namespace !== null) return { name: namespace[1] as string, kind: 'namespace' }
|
|
434
|
+
let rest = head
|
|
435
|
+
rest = rest.replace(/^extern\s*$/, '')
|
|
436
|
+
for (let guard = 0; guard < 8; guard += 1) {
|
|
437
|
+
const next = rest.replace(CPP_SPECIFIERS, '')
|
|
438
|
+
if (next === rest) break
|
|
439
|
+
rest = next
|
|
440
|
+
}
|
|
441
|
+
// 类体内的构造/析构:`Widget(` / `Widget() : x_(0)` / `~Widget()` 没有返回类型,
|
|
442
|
+
// 通用的「类型 名字(」模式匹配不到;只认名字等于类名或以 `~` 开头,避免把宏调用
|
|
443
|
+
// (`SOME_MACRO(x)`)当成构造器。
|
|
444
|
+
if (owner !== undefined && METHOD_OWNERS.has(owner.kind)) {
|
|
445
|
+
const bare = /^(~?[A-Za-z_]\w*)\s*\(/.exec(rest)
|
|
446
|
+
if (bare !== null) {
|
|
447
|
+
const name = bare[1] as string
|
|
448
|
+
if ((name === owner.name || name.startsWith('~')) && !NOT_CALLABLE.has(name)) {
|
|
449
|
+
return { name, kind: 'constructor' }
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
// `typedef struct {...} Name;` 的名字在收尾行上,这里只处理带名字的直接声明。
|
|
454
|
+
const callable = /^([A-Za-z_][\w:<>,\s*&]*?)\s+([A-Za-z_]\w*(?:::[A-Za-z_]\w*)*|~[A-Za-z_]\w*)\s*\(/.exec(rest)
|
|
455
|
+
if (callable !== null
|
|
456
|
+
&& !NOT_CALLABLE.has((callable[1] as string).trim())
|
|
457
|
+
&& !NOT_CALLABLE.has(callable[2] as string)) {
|
|
458
|
+
const name = callable[2] as string
|
|
459
|
+
return {
|
|
460
|
+
name,
|
|
461
|
+
// 类外定义的成员函数(`Widget::draw`)也是方法:限定名本身就是类名。
|
|
462
|
+
kind: (owner !== undefined && METHOD_OWNERS.has(owner.kind)) || name.includes('::')
|
|
463
|
+
? 'method'
|
|
464
|
+
: 'function',
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
// 成员/全局变量:`类型 [*&]名字 [数组] (;|=)`。
|
|
468
|
+
const variable = /^([A-Za-z_][\w:<>,\s*&]*?)\s+[*&]*([A-Za-z_]\w*)\s*(?:\[[^\]]*\]\s*)*(?:=|;)/.exec(rest)
|
|
469
|
+
if (variable !== null && !NOT_CALLABLE.has(variable[1] as string)) {
|
|
470
|
+
return { name: variable[2] as string, kind: owner === undefined ? 'variable' : 'field' }
|
|
471
|
+
}
|
|
472
|
+
return undefined
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
function matchGo(trimmed: string, owner: OpenContainer | undefined): Match | undefined {
|
|
476
|
+
const callable = /^func\s+(?:\(\s*[A-Za-z_]\w*\s+\*?\s*([A-Za-z_]\w*)\s*\)\s*)?([A-Za-z_]\w*)\s*(?:\[[^\]]*\])?\s*\(/.exec(trimmed)
|
|
477
|
+
if (callable !== null) {
|
|
478
|
+
return { name: callable[2] as string, kind: callable[1] === undefined ? 'function' : 'method' }
|
|
479
|
+
}
|
|
480
|
+
const type = /^type\s+([A-Za-z_]\w*)\s+(struct|interface|func|map|chan|\[|\*|[A-Za-z_])/.exec(trimmed)
|
|
481
|
+
if (type !== null) {
|
|
482
|
+
const keyword = type[2] as string
|
|
483
|
+
const kind: OutlineKind = keyword === 'struct' ? 'struct' : keyword === 'interface' ? 'interface' : 'type'
|
|
484
|
+
return { name: type[1] as string, kind }
|
|
485
|
+
}
|
|
486
|
+
if (owner !== undefined && (owner.kind === 'struct' || owner.kind === 'interface')) {
|
|
487
|
+
// 接口里的方法声明没有 `func` 关键字:`Read(p []byte) (int, error)`。方法体
|
|
488
|
+
// 没有 `{`(接口方法不带实现),所以行内出现 `(` 就是签名。
|
|
489
|
+
if (owner.kind === 'interface') {
|
|
490
|
+
const signature = /^([A-Za-z_]\w*)\s*\(/.exec(trimmed)
|
|
491
|
+
if (signature !== null && !NOT_CALLABLE.has(signature[1] as string)) {
|
|
492
|
+
return { name: signature[1] as string, kind: 'method' }
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
// `Name Type` / `A, B Type`。嵌入字段没有名字,不列。
|
|
496
|
+
const field = /^([A-Za-z_]\w*)(?:\s*,\s*[A-Za-z_]\w*)*\s+[*\[]?[\w.\[\]*]+$/.exec(trimmed)
|
|
497
|
+
if (field !== null && !NOT_CALLABLE.has(field[1] as string)) {
|
|
498
|
+
return { name: field[1] as string, kind: 'field' }
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
return undefined
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function matchRust(trimmed: string, owner: OpenContainer | undefined): Match | undefined {
|
|
505
|
+
const callable = /^(?:pub(?:\s*\([^)]*\))?\s+)?(?:(?:async|unsafe|const|extern|default)\s+)*fn\s+(?:<[^>]*>\s*)?([A-Za-z_]\w*)/.exec(trimmed)
|
|
506
|
+
if (callable !== null) {
|
|
507
|
+
return {
|
|
508
|
+
name: callable[1] as string,
|
|
509
|
+
kind: owner !== undefined && METHOD_OWNERS.has(owner.kind) ? 'method' : 'function',
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
const impl = /^(?:pub(?:\s*\([^)]*\))?\s+)?(?:unsafe\s+)?(?:default\s+)?impl(?:\s*<[^>]*>)?\s+(?:[\w:<>, ]+?\s+for\s+)?([A-Za-z_]\w*)/.exec(trimmed)
|
|
513
|
+
if (impl !== null) return { name: impl[1] as string, kind: 'impl' }
|
|
514
|
+
const type = /^(?:pub(?:\s*\([^)]*\))?\s+)?(struct|enum|trait|union)\s+([A-Za-z_]\w*)/.exec(trimmed)
|
|
515
|
+
if (type !== null) {
|
|
516
|
+
const keyword = type[1] as string
|
|
517
|
+
const kind: OutlineKind = keyword === 'trait' ? 'trait' : keyword === 'enum' ? 'enum' : 'struct'
|
|
518
|
+
return { name: type[2] as string, kind }
|
|
519
|
+
}
|
|
520
|
+
const module = /^(?:pub(?:\s*\([^)]*\))?\s+)?mod\s+([A-Za-z_]\w*)/.exec(trimmed)
|
|
521
|
+
if (module !== null) return { name: module[1] as string, kind: 'module' }
|
|
522
|
+
const alias = /^(?:pub(?:\s*\([^)]*\))?\s+)?type\s+([A-Za-z_]\w*)/.exec(trimmed)
|
|
523
|
+
if (alias !== null) return { name: alias[1] as string, kind: 'type' }
|
|
524
|
+
const constant = /^(?:pub(?:\s*\([^)]*\))?\s+)?(?:const|static)\s+(?:mut\s+)?([A-Za-z_]\w*)/.exec(trimmed)
|
|
525
|
+
if (constant !== null) {
|
|
526
|
+
return { name: constant[1] as string, kind: owner === undefined ? 'variable' : 'field' }
|
|
527
|
+
}
|
|
528
|
+
const macro = /^macro_rules!\s*([A-Za-z_]\w*)/.exec(trimmed)
|
|
529
|
+
if (macro !== null) return { name: macro[1] as string, kind: 'macro' }
|
|
530
|
+
if (owner !== undefined && (owner.kind === 'struct' || owner.kind === 'trait' || owner.kind === 'enum')) {
|
|
531
|
+
const field = /^(?:pub(?:\s*\([^)]*\))?\s+)?([A-Za-z_]\w*)\s*:\s*\S/.exec(trimmed)
|
|
532
|
+
if (field !== null && !NOT_CALLABLE.has(field[1] as string)) {
|
|
533
|
+
return { name: field[1] as string, kind: 'field' }
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
return undefined
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
const SCRIPT_KEYWORDS = /^(?:export\s+)?(?:default\s+)?(?:declare\s+)?/
|
|
540
|
+
const SCRIPT_METHOD_MODIFIERS = /^(?:(?:@[A-Za-z_$][\w$.]*(?:\([^)]*\))?|public|private|protected|static|readonly|async|get|set|override|abstract|declare|accessor)\s+)+/
|
|
541
|
+
|
|
542
|
+
function matchScript(trimmed: string, owner: OpenContainer | undefined): Match | undefined {
|
|
543
|
+
const rest = trimmed.replace(SCRIPT_KEYWORDS, '')
|
|
544
|
+
const type = /^(?:abstract\s+)?(class|interface|enum|namespace|module)\s+([A-Za-z_$][\w$]*)/.exec(rest)
|
|
545
|
+
if (type !== null) {
|
|
546
|
+
return { name: type[2] as string, kind: type[1] as OutlineKind }
|
|
547
|
+
}
|
|
548
|
+
const alias = /^type\s+([A-Za-z_$][\w$]*)\s*[=<]/.exec(rest)
|
|
549
|
+
if (alias !== null) return { name: alias[1] as string, kind: 'type' }
|
|
550
|
+
const named = /^(?:async\s+)?function\s*\*?\s*([A-Za-z_$][\w$]*)/.exec(rest)
|
|
551
|
+
if (named !== null) {
|
|
552
|
+
return {
|
|
553
|
+
name: named[1] as string,
|
|
554
|
+
kind: owner !== undefined && METHOD_OWNERS.has(owner.kind) ? 'method' : 'function',
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
const declaration = /^(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*(?::[^=]*)?=\s*([\s\S]*)$/.exec(rest)
|
|
558
|
+
if (declaration !== null) {
|
|
559
|
+
const value = declaration[2] as string
|
|
560
|
+
const callable = /^(?:async\s+)?(?:function\b|\([^()]*\)\s*(?::[^=]*)?=>|[A-Za-z_$][\w$]*\s*=>)/.test(value)
|
|
561
|
+
return { name: declaration[1] as string, kind: callable ? 'function' : 'variable' }
|
|
562
|
+
}
|
|
563
|
+
if (owner !== undefined && METHOD_OWNERS.has(owner.kind)) {
|
|
564
|
+
const member = rest.replace(SCRIPT_METHOD_MODIFIERS, '')
|
|
565
|
+
// 成员方法/接口签名的形状:`name(...)` 之后跟可选的 `: 返回类型`,再跟 `{`
|
|
566
|
+
// (类里的实现)或 `;`(interface/abstract 的签名)。类体里的调用或字段初始化
|
|
567
|
+
// 器(`x = foo(...)`)两者都不满足,所以不会被当成方法。
|
|
568
|
+
const method = /^#?([A-Za-z_$][\w$]*)\s*(?:<[^>]*>)?\s*\([^;{]*\)\s*(?::[^{;]*)?[;{]/.exec(member)
|
|
569
|
+
if (method !== null && !NOT_CALLABLE.has(method[1] as string)) {
|
|
570
|
+
const name = method[1] as string
|
|
571
|
+
if (name === 'constructor' || name === owner.name) return { name, kind: 'constructor' }
|
|
572
|
+
return { name, kind: 'method' }
|
|
573
|
+
}
|
|
574
|
+
const field = /^#?([A-Za-z_$][\w$]*)\s*(?:[?!])?\s*[:=]/.exec(member)
|
|
575
|
+
if (field !== null && !NOT_CALLABLE.has(field[1] as string)) {
|
|
576
|
+
return { name: field[1] as string, kind: 'field' }
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
return undefined
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
// ------------------------------------------------------- Python(缩进 + 关键字)
|
|
583
|
+
|
|
584
|
+
const PYTHON_KEYWORDS = new Set([
|
|
585
|
+
'if', 'elif', 'else', 'for', 'while', 'return', 'yield', 'with', 'try', 'except', 'finally',
|
|
586
|
+
'import', 'from', 'assert', 'del', 'raise', 'global', 'nonlocal', 'lambda', 'and', 'or', 'not',
|
|
587
|
+
'print', 'pass', 'break', 'continue', 'def', 'class', 'async', 'await', 'in', 'is', 'as', 'match',
|
|
588
|
+
])
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Python 大纲:`class` / `def` / 模块与类体里的赋值。
|
|
592
|
+
*
|
|
593
|
+
* 缩进就是结构:`def`/`class` 压栈,缩进回退就出栈。函数体里的局部变量**不列**
|
|
594
|
+
* (那是噪音,不是文件结构),所以只有「模块级」和「类体直接成员」进入结果。
|
|
595
|
+
*/
|
|
596
|
+
function scanPython(text: string): RawSymbol[] {
|
|
597
|
+
const lines = maskSource(text, 'python')
|
|
598
|
+
const found: RawSymbol[] = []
|
|
599
|
+
const containers: { indent: number; name: string; kind: OutlineKind }[] = []
|
|
600
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
601
|
+
const line = lines[index] as string
|
|
602
|
+
const trimmed = line.trim()
|
|
603
|
+
if (trimmed === '') continue
|
|
604
|
+
const indent = indentOf(line)
|
|
605
|
+
while (containers.length > 0 && (containers[containers.length - 1] as { indent: number }).indent >= indent) {
|
|
606
|
+
containers.pop()
|
|
607
|
+
}
|
|
608
|
+
const owner = containers.length > 0 ? containers[containers.length - 1] : undefined
|
|
609
|
+
const direct = owner === undefined || owner.kind === 'class'
|
|
610
|
+
if (direct) {
|
|
611
|
+
const classDecl = /^class\s+([A-Za-z_]\w*)/.exec(trimmed)
|
|
612
|
+
const def = /^(?:async\s+)?def\s+([A-Za-z_]\w*)/.exec(trimmed)
|
|
613
|
+
const assignment = /^([A-Za-z_]\w*)\s*(?::[^=]+)?=(?!=)/.exec(trimmed)
|
|
614
|
+
if (classDecl !== null) {
|
|
615
|
+
found.push({
|
|
616
|
+
name: classDecl[1] as string,
|
|
617
|
+
kind: 'class',
|
|
618
|
+
line: index + 1,
|
|
619
|
+
depth: containers.length,
|
|
620
|
+
...(owner === undefined ? {} : { container: owner.name }),
|
|
621
|
+
})
|
|
622
|
+
containers.push({ indent, name: classDecl[1] as string, kind: 'class' })
|
|
623
|
+
continue
|
|
624
|
+
}
|
|
625
|
+
if (def !== null) {
|
|
626
|
+
const kind: OutlineKind = owner !== undefined && owner.kind === 'class' ? 'method' : 'function'
|
|
627
|
+
found.push({
|
|
628
|
+
name: def[1] as string,
|
|
629
|
+
kind,
|
|
630
|
+
line: index + 1,
|
|
631
|
+
depth: containers.length,
|
|
632
|
+
...(owner === undefined ? {} : { container: owner.name }),
|
|
633
|
+
})
|
|
634
|
+
// 函数也压栈,这样函数体里的行缩进更深却被 `direct` 挡掉;同时保证
|
|
635
|
+
// 退出函数后类的成员还能正确挂回类上。
|
|
636
|
+
containers.push({ indent, name: def[1] as string, kind: 'function' })
|
|
637
|
+
continue
|
|
638
|
+
}
|
|
639
|
+
if (assignment !== null && !PYTHON_KEYWORDS.has(assignment[1] as string)) {
|
|
640
|
+
found.push({
|
|
641
|
+
name: assignment[1] as string,
|
|
642
|
+
kind: owner === undefined ? 'variable' : 'field',
|
|
643
|
+
line: index + 1,
|
|
644
|
+
depth: containers.length,
|
|
645
|
+
...(owner === undefined ? {} : { container: owner.name }),
|
|
646
|
+
})
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
return found
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
// -------------------------------------------------- 未知语言(缩进 + 关键字猜测)
|
|
654
|
+
|
|
655
|
+
const GUESS_TYPE_KEYWORDS = /^(?:export\s+|public\s+|private\s+|protected\s+|static\s+|final\s+|abstract\s+|pub\s+|async\s+|default\s+)*(class|interface|struct|enum|trait|impl|namespace|module)\s+([A-Za-z_$][\w$]*)/
|
|
656
|
+
|
|
657
|
+
/** 未知语言:关键字 + 缩进,结果一律标记为「猜的」。 */
|
|
658
|
+
function scanGuessed(text: string): RawSymbol[] {
|
|
659
|
+
const lines = maskSource(text, 'unknown')
|
|
660
|
+
const found: RawSymbol[] = []
|
|
661
|
+
const containers: { indent: number; name: string; kind: OutlineKind }[] = []
|
|
662
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
663
|
+
const line = lines[index] as string
|
|
664
|
+
const trimmed = line.trim()
|
|
665
|
+
if (trimmed === '' || trimmed.startsWith('#')) continue
|
|
666
|
+
const indent = indentOf(line)
|
|
667
|
+
while (containers.length > 0 && (containers[containers.length - 1] as { indent: number }).indent >= indent) {
|
|
668
|
+
containers.pop()
|
|
669
|
+
}
|
|
670
|
+
const owner = containers.length > 0 ? containers[containers.length - 1] : undefined
|
|
671
|
+
const push = (name: string, kind: OutlineKind): void => {
|
|
672
|
+
found.push({
|
|
673
|
+
name,
|
|
674
|
+
kind,
|
|
675
|
+
line: index + 1,
|
|
676
|
+
depth: containers.length,
|
|
677
|
+
...(owner === undefined ? {} : { container: owner.name }),
|
|
678
|
+
})
|
|
679
|
+
containers.push({ indent, name, kind })
|
|
680
|
+
}
|
|
681
|
+
const type = GUESS_TYPE_KEYWORDS.exec(trimmed)
|
|
682
|
+
if (type !== null) {
|
|
683
|
+
const keyword = type[1] as string
|
|
684
|
+
const kind: OutlineKind = keyword === 'class' ? 'class'
|
|
685
|
+
: keyword === 'interface' ? 'interface'
|
|
686
|
+
: keyword === 'struct' ? 'struct'
|
|
687
|
+
: keyword === 'enum' ? 'enum'
|
|
688
|
+
: keyword === 'trait' ? 'trait'
|
|
689
|
+
: keyword === 'impl' ? 'impl'
|
|
690
|
+
: keyword === 'namespace' ? 'namespace' : 'module'
|
|
691
|
+
push(type[2] as string, kind)
|
|
692
|
+
continue
|
|
693
|
+
}
|
|
694
|
+
const callable = /^(?:export\s+|pub\s+|async\s+|static\s+|public\s+|private\s+)*(?:def|func|function|fn|sub|proc|method)\s+([A-Za-z_$][\w$]*)/.exec(trimmed)
|
|
695
|
+
if (callable !== null) {
|
|
696
|
+
push(callable[1] as string, owner !== undefined && METHOD_OWNERS.has(owner.kind) ? 'method' : 'function')
|
|
697
|
+
continue
|
|
698
|
+
}
|
|
699
|
+
// shell 的 `name() {` 之类:关键字表抓不到,靠形状猜(结果本来就标着「猜的」)。
|
|
700
|
+
const shaped = /^([A-Za-z_$][\w$]*)\s*\([^()]*\)\s*(?::[^{]*)?\{/.exec(trimmed)
|
|
701
|
+
if (shaped !== null && !NOT_CALLABLE.has(shaped[1] as string)) {
|
|
702
|
+
push(shaped[1] as string, owner !== undefined && METHOD_OWNERS.has(owner.kind) ? 'method' : 'function')
|
|
703
|
+
continue
|
|
704
|
+
}
|
|
705
|
+
const assignment = /^([A-Za-z_][\w$]*)\s*[:=](?!=)/.exec(trimmed)
|
|
706
|
+
if (assignment !== null && !NOT_CALLABLE.has(assignment[1] as string)) {
|
|
707
|
+
found.push({
|
|
708
|
+
name: assignment[1] as string,
|
|
709
|
+
kind: owner === undefined ? 'variable' : 'field',
|
|
710
|
+
line: index + 1,
|
|
711
|
+
depth: containers.length,
|
|
712
|
+
...(owner === undefined ? {} : { container: owner.name }),
|
|
713
|
+
})
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
return found
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
// ---------------------------------------------------------------------- 入口
|
|
720
|
+
|
|
721
|
+
function finish(language: OutlineLanguage, raw: RawSymbol[], guessed: boolean): OutlineResult {
|
|
722
|
+
const total = raw.length
|
|
723
|
+
const entries: OutlineEntry[] = raw.slice(0, OUTLINE_MAX_ENTRIES)
|
|
724
|
+
return {
|
|
725
|
+
language,
|
|
726
|
+
entries,
|
|
727
|
+
total,
|
|
728
|
+
truncated: total > entries.length,
|
|
729
|
+
guessed,
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
/**
|
|
734
|
+
* 从源文本生成大纲。
|
|
735
|
+
*
|
|
736
|
+
* `text` 必须是**实时缓冲区**(dirty 时不是 `tab.text`);行号 1-based 直接对应
|
|
737
|
+
* `CodeEditorHandle.revealRange`。
|
|
738
|
+
*/
|
|
739
|
+
export function buildOutline(text: string, path: string): OutlineResult {
|
|
740
|
+
const language = outlineLanguageOf(path)
|
|
741
|
+
if (language === 'python') return finish(language, scanPython(text), false)
|
|
742
|
+
if (language === 'unknown') return finish(language, scanGuessed(text), true)
|
|
743
|
+
const raw = scanBraceLanguages(text, language)
|
|
744
|
+
// A known brace language that produced nothing while the file clearly has
|
|
745
|
+
// structure (an unbalanced/lone brace from `extern "C" {`, a disabled block,
|
|
746
|
+
// or a string form the masker does not understand) must not claim "no
|
|
747
|
+
// symbols". Fall back to the indentation heuristic and say it is guessed.
|
|
748
|
+
if (raw.length === 0 && /[{}]/.test(text)) return finish(language, scanGuessed(text), true)
|
|
749
|
+
return finish(language, raw, false)
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
/**
|
|
753
|
+
* 按查询过滤并排序符号。
|
|
754
|
+
*
|
|
755
|
+
* 空查询 = 源码顺序(大纲的默认视图);有查询时复用 quick open 的 `fuzzyScore`
|
|
756
|
+
* 打分,同分按行号升序 —— 排序必须确定,否则两次相同的输入会闪出两种顺序。
|
|
757
|
+
* 名字不匹配但 `容器.名字` 匹配(用户常敲 `Foo.bar`)时也算命中。
|
|
758
|
+
*/
|
|
759
|
+
export function rankOutline(
|
|
760
|
+
query: string,
|
|
761
|
+
entries: readonly OutlineEntry[],
|
|
762
|
+
limit: number,
|
|
763
|
+
): { hits: OutlineEntry[]; truncated: boolean } {
|
|
764
|
+
const cap = Math.max(0, Math.floor(limit))
|
|
765
|
+
const needle = query.trim()
|
|
766
|
+
if (needle === '') return { hits: entries.slice(0, cap), truncated: entries.length > cap }
|
|
767
|
+
const scored: { entry: OutlineEntry; score: number }[] = []
|
|
768
|
+
for (const entry of entries) {
|
|
769
|
+
const own = fuzzyScore(needle, entry.name)
|
|
770
|
+
const score = own ?? (entry.container === undefined ? undefined : fuzzyScore(needle, `${entry.container}.${entry.name}`))
|
|
771
|
+
if (score !== undefined) scored.push({ entry, score })
|
|
772
|
+
}
|
|
773
|
+
scored.sort((left, right) => (right.score - left.score) || (left.entry.line - right.entry.line))
|
|
774
|
+
return { hits: scored.slice(0, cap).map(item => item.entry), truncated: scored.length > cap }
|
|
775
|
+
}
|