soma-core 1.3.3 → 1.3.4

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.
Files changed (106) hide show
  1. package/README.md +7 -6
  2. package/app/SomaPageClient.tsx +2 -72
  3. package/bin/soma-lsp.cjs +1 -4
  4. package/core/build/compiler/full-manifest-builder.ts +17 -26
  5. package/core/build/compiler/html-include-expander.ts +56 -0
  6. package/core/build/compiler/page-assets.ts +26 -0
  7. package/core/build/compiler/page-declaration-compiler.ts +85 -0
  8. package/core/build/compiler/rollup-plugin-soma.ts +4 -19
  9. package/core/build/compiler/route-builder.ts +0 -2
  10. package/core/build/compiler/rspack-plugin-soma.js +1 -10
  11. package/core/build/compiler/soma-manifest-webpack-plugin.js +54 -13
  12. package/core/build/compiler/soma-precompile.js +74 -0
  13. package/core/build/compiler/soma-precompile.ts +13 -0
  14. package/core/build/compiler/soma-rspack-yaml-loader.js +5 -11
  15. package/core/build/compiler/soma-webpack-loader.js +6 -21
  16. package/core/build/compiler/vite-plugin-soma.ts +9 -31
  17. package/core/build/component/pack-from-dir.ts +58 -0
  18. package/core/build/component/package-loader.ts +158 -12
  19. package/core/build/eject/plan-eject.ts +15 -21
  20. package/core/build/lsp/cross-file-diagnostics.cjs +82 -113
  21. package/core/build/lsp/extra-completion.cjs +14 -8
  22. package/core/build/lsp/lsp-navigation.cjs +50 -43
  23. package/core/build/lsp/soma-declaration.cjs +85 -0
  24. package/core/build/lsp/ui-yaml-completion.cjs +98 -104
  25. package/core/build/v2/block-compile.ts +200 -0
  26. package/core/build/v2/block-loader.ts +33 -0
  27. package/core/build/v2/html-index.ts +120 -0
  28. package/core/build/v2/migrate-v1.ts +370 -0
  29. package/core/build/v2/soma-file-loader.ts +216 -0
  30. package/core/client/component/ai-catalog.ts +15 -0
  31. package/core/client/component/component-pack.ts +205 -31
  32. package/core/client/component/contract-compat.ts +132 -0
  33. package/core/client/component/example-v2.ts +132 -0
  34. package/core/client/engine/dom-renderer.ts +14 -8
  35. package/core/client/engine/reactive-proxy.ts +8 -7
  36. package/core/client/engine/soma-bootstrap.ts +23 -5
  37. package/core/client/parser/v2/namespace.ts +182 -0
  38. package/core/client/parser/v2/page-parser.ts +359 -0
  39. package/core/client/parser/v2/use-parser.ts +412 -0
  40. package/core/client/parser/value-lang.ts +340 -0
  41. package/core/client/pipes/pipe-registry.ts +1 -1
  42. package/core/client/registry/declaration-renderer.ts +17 -15
  43. package/core/client/theme/harmony.ts +307 -0
  44. package/core/client/theme/scene-tokens.ts +134 -0
  45. package/core/client/theme/theme-axes.ts +228 -0
  46. package/core/client/theme/theme-engine.ts +333 -73
  47. package/core/client/theme/tone-solver.ts +118 -0
  48. package/core/index.ts +74 -2
  49. package/core/server/host/server-page-assets.ts +33 -16
  50. package/core/server/host/soma-page-compile.ts +33 -0
  51. package/core/server/index.ts +15 -0
  52. package/core/server/registry/declaration-precompiler.ts +24 -54
  53. package/core/server/registry/page-registry-store.ts +38 -9
  54. package/core/server/registry/services/pages/Get.ts +19 -9
  55. package/core/server/registry/validation/compile-gate.ts +14 -31
  56. package/core/server/registry/validation/cross-check.ts +7 -17
  57. package/core/server/registry/validation/declaration-safety-scan.ts +2 -2
  58. package/core/shared/types/component-contract.types.ts +10 -0
  59. package/dist/core/index.cjs +2138 -154
  60. package/dist/types/build/compiler/full-manifest-builder.d.ts +4 -4
  61. package/dist/types/build/compiler/html-include-expander.d.ts +19 -0
  62. package/dist/types/build/compiler/page-assets.d.ts +3 -0
  63. package/dist/types/build/compiler/page-declaration-compiler.d.ts +33 -0
  64. package/dist/types/build/compiler/rollup-plugin-soma.d.ts +0 -3
  65. package/dist/types/build/compiler/route-builder.d.ts +0 -1
  66. package/dist/types/build/compiler/soma-precompile.d.ts +8 -0
  67. package/dist/types/build/compiler/vite-plugin-soma.d.ts +2 -10
  68. package/dist/types/build/component/pack-from-dir.d.ts +17 -0
  69. package/dist/types/build/component/package-loader.d.ts +33 -2
  70. package/dist/types/build/v2/block-compile.d.ts +28 -0
  71. package/dist/types/build/v2/block-loader.d.ts +9 -0
  72. package/dist/types/build/v2/html-index.d.ts +37 -0
  73. package/dist/types/build/v2/migrate-v1.d.ts +32 -0
  74. package/dist/types/build/v2/soma-file-loader.d.ts +45 -0
  75. package/dist/types/client/component/component-pack.d.ts +23 -3
  76. package/dist/types/client/component/contract-compat.d.ts +16 -0
  77. package/dist/types/client/component/example-v2.d.ts +9 -0
  78. package/dist/types/client/engine/dom-renderer.d.ts +2 -0
  79. package/dist/types/client/engine/soma-bootstrap.d.ts +3 -1
  80. package/dist/types/client/parser/v2/namespace.d.ts +52 -0
  81. package/dist/types/client/parser/v2/page-parser.d.ts +21 -0
  82. package/dist/types/client/parser/v2/use-parser.d.ts +72 -0
  83. package/dist/types/client/parser/value-lang.d.ts +63 -0
  84. package/dist/types/client/registry/declaration-renderer.d.ts +1 -2
  85. package/dist/types/client/theme/harmony.d.ts +77 -0
  86. package/dist/types/client/theme/scene-tokens.d.ts +25 -0
  87. package/dist/types/client/theme/theme-axes.d.ts +81 -0
  88. package/dist/types/client/theme/theme-engine.d.ts +29 -1
  89. package/dist/types/client/theme/tone-solver.d.ts +47 -0
  90. package/dist/types/index.d.ts +13 -2
  91. package/dist/types/server/host/server-page-assets.d.ts +1 -4
  92. package/dist/types/server/host/soma-page-compile.d.ts +7 -0
  93. package/dist/types/server/index.d.ts +3 -1
  94. package/dist/types/server/registry/declaration-precompiler.d.ts +3 -3
  95. package/dist/types/server/registry/page-registry-store.d.ts +4 -4
  96. package/dist/types/server/registry/validation/compile-gate.d.ts +2 -0
  97. package/dist/types/server/registry/validation/cross-check.d.ts +4 -4
  98. package/dist/types/shared/types/component-contract.types.d.ts +10 -0
  99. package/editor/vscode/package.json +1 -24
  100. package/global.d.ts +1 -11
  101. package/next.config.js +32 -50
  102. package/package.json +3 -2
  103. package/tailwind.config.ts +0 -1
  104. package/core/build/lsp/event-yaml-completion.cjs +0 -106
  105. package/core/client/host-bridge/empty-host/data/README.md +0 -17
  106. package/editor/vscode/syntaxes/soma-event-yaml.tmLanguage.json +0 -30
@@ -1,160 +1,129 @@
1
- /**
2
- * LSP 跨文件锚点一致性诊断
3
- * html data-soma-id ↔ _ui.yaml show ↔ _event.yaml 事件锚点
4
- */
1
+ /** LSP 跨文件诊断:HTML ↔ 唯一 SOMA DSL 声明。 */
5
2
  const fs = require('node:fs');
6
3
  const path = require('node:path');
7
- const yaml = require('js-yaml');
4
+ const { declarationFacts } = require('./soma-declaration.cjs');
8
5
 
9
6
  function extractSomaIdsFromHtml(html) {
10
7
  const ids = new Set();
11
- for (const m of html.matchAll(/data-soma-id=["']([^"']+)["']/g)) {
12
- ids.add(m[1]);
13
- }
14
- return ids;
15
- }
16
-
17
- function extractShowKeys(uiYaml) {
18
- const keys = new Set();
19
- try {
20
- const doc = yaml.load(uiYaml);
21
- if (doc?.show && typeof doc.show === 'object') {
22
- for (const k of Object.keys(doc.show)) keys.add(k);
23
- }
24
- } catch {
25
- /* invalid yaml */
26
- }
27
- return keys;
28
- }
29
-
30
- function extractEventIds(eventYaml) {
31
- const ids = new Set();
32
- for (const line of eventYaml.split('\n')) {
33
- const trimmed = line.trim();
34
- if (!trimmed || trimmed.startsWith('#')) continue;
35
- const dot = trimmed.indexOf('.');
36
- if (dot > 0) ids.add(trimmed.slice(0, dot));
37
- }
8
+ for (const match of html.matchAll(/data-soma-id=["']([^"']+)["']/g)) ids.add(match[1]);
38
9
  return ids;
39
10
  }
40
11
 
41
12
  function collectHtmlPages(htmlDir) {
42
13
  const pages = [];
43
14
  if (!fs.existsSync(htmlDir)) return pages;
44
- function walk(dir, prefix = '') {
15
+ const walk = (dir, prefix = '') => {
45
16
  for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
46
17
  const rel = prefix ? `${prefix}/${entry.name}` : entry.name;
47
18
  const full = path.join(dir, entry.name);
48
19
  if (entry.isDirectory()) walk(full, rel);
49
20
  else if (entry.name.endsWith('.html')) pages.push(rel.replace(/\.html$/, ''));
50
21
  }
51
- }
22
+ };
52
23
  walk(htmlDir);
53
24
  return pages;
54
25
  }
55
26
 
56
- function findAnchorInHtml(html, somaId) {
57
- const re = new RegExp(`data-soma-id=["']${somaId.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&')}["']`, 'm');
58
- const m = re.exec(html);
59
- if (!m) return null;
60
- const before = html.slice(0, m.index);
61
- const line = before.split('\n').length - 1;
62
- const character = m.index - before.lastIndexOf('\n') - 1;
63
- return { line, character, length: m[0].length };
27
+ function escapeRegex(value) {
28
+ return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
64
29
  }
65
30
 
66
- function findKeyInYaml(yamlText, key) {
67
- const re = new RegExp(`^\\s{0,4}${key.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&')}\\s*:`, 'm');
68
- const m = re.exec(yamlText);
69
- if (!m) return null;
70
- const before = yamlText.slice(0, m.index);
71
- const line = before.split('\n').length - 1;
72
- const character = m.index - before.lastIndexOf('\n') - 1;
73
- return { line, character, length: key.length };
31
+ function locate(text, expression, length = 1) {
32
+ const match = expression.exec(text);
33
+ if (!match) return null;
34
+ const before = text.slice(0, match.index);
35
+ return {
36
+ line: before.split('\n').length - 1,
37
+ character: match.index - before.lastIndexOf('\n') - 1,
38
+ length,
39
+ };
74
40
  }
75
41
 
76
- function rangeFromLoc(loc) {
77
- if (!loc) return { start: { line: 0, character: 0 }, end: { line: 0, character: 1 } };
42
+ function range(loc) {
43
+ const item = loc ?? { line: 0, character: 0, length: 1 };
78
44
  return {
79
- start: { line: loc.line, character: loc.character },
80
- end: { line: loc.line, character: loc.character + loc.length },
45
+ start: { line: item.line, character: item.character },
46
+ end: { line: item.line, character: item.character + item.length },
81
47
  };
82
48
  }
83
49
 
50
+ function diagnostic(uri, code, message, loc, severity = 1) {
51
+ return { uri, range: range(loc), severity, code, source: 'soma-cross-file', message };
52
+ }
53
+
54
+ function retiredSidecarPaths(hostDir, base) {
55
+ return [
56
+ path.join(hostDir, 'event', `${base}_event.yaml`),
57
+ path.join(hostDir, 'ui', `${base}_style.yaml`),
58
+ path.join(hostDir, 'data', `${base}_data.yaml`),
59
+ path.join(hostDir, 'ui', `${base}_data.yaml`),
60
+ ];
61
+ }
62
+
84
63
  function lintAnchorConsistency(hostDir) {
85
64
  const htmlDir = path.join(hostDir, 'html');
86
65
  const diagnostics = [];
87
-
88
66
  for (const base of collectHtmlPages(htmlDir)) {
89
67
  const htmlPath = path.join(htmlDir, `${base}.html`);
90
- const uiPath = path.join(hostDir, 'ui', `${base}_ui.yaml`);
91
- const eventPath = path.join(hostDir, 'event', `${base}_event.yaml`);
92
- const uri = `file://${htmlPath}`;
93
-
94
- if (!fs.existsSync(htmlPath)) continue;
68
+ const declarationPath = path.join(hostDir, 'ui', `${base}_ui.yaml`);
95
69
  const html = fs.readFileSync(htmlPath, 'utf8');
96
70
  const htmlIds = extractSomaIdsFromHtml(html);
97
71
 
98
- const uiYaml = fs.existsSync(uiPath) ? fs.readFileSync(uiPath, 'utf8') : '';
99
- const eventYaml = fs.existsSync(eventPath) ? fs.readFileSync(eventPath, 'utf8') : '';
100
- const showKeys = extractShowKeys(uiYaml);
101
- const eventIds = extractEventIds(eventYaml);
72
+ for (const sidecar of retiredSidecarPaths(hostDir, base).filter(fs.existsSync)) {
73
+ diagnostics.push(diagnostic(
74
+ `file://${sidecar}`,
75
+ 'retired-page-sidecar',
76
+ `[${base}] 页面侧车已退役;事件、样式状态与数据对象必须写进 ${base}_ui.yaml`,
77
+ null,
78
+ ));
79
+ }
102
80
 
103
- for (const id of htmlIds) {
104
- if (uiYaml && showKeys.size > 0 && !showKeys.has(id)) {
105
- diagnostics.push({
106
- uri,
107
- range: rangeFromLoc(findAnchorInHtml(html, id)),
108
- severity: 2,
109
- code: 'orphan-html-anchor',
110
- source: 'soma-cross-file',
111
- message: `[${base}] HTML 锚点 "${id}" 未在 ${base}_ui.yaml show 中声明`,
112
- });
113
- }
81
+ if (!fs.existsSync(declarationPath)) {
82
+ diagnostics.push(diagnostic(
83
+ `file://${htmlPath}`,
84
+ 'missing-soma-declaration',
85
+ `[${base}] 缺少唯一 SOMA DSL 声明 ${base}_ui.yaml`,
86
+ null,
87
+ ));
88
+ continue;
114
89
  }
115
90
 
116
- for (const id of showKeys) {
117
- if (!htmlIds.has(id)) {
118
- diagnostics.push({
119
- uri: `file://${uiPath}`,
120
- range: rangeFromLoc(findKeyInYaml(uiYaml, id)),
121
- severity: 2,
122
- code: 'missing-html-anchor',
123
- source: 'soma-cross-file',
124
- message: `[${base}] ui show "${id}" 在 HTML 中无 data-soma-id`,
125
- });
126
- }
91
+ const source = fs.readFileSync(declarationPath, 'utf8');
92
+ let facts;
93
+ try {
94
+ facts = declarationFacts(source);
95
+ } catch (error) {
96
+ diagnostics.push(diagnostic(
97
+ `file://${declarationPath}`,
98
+ 'invalid-soma-declaration',
99
+ `[${base}] ${(error instanceof Error ? error.message : String(error))}`,
100
+ null,
101
+ ));
102
+ continue;
127
103
  }
128
104
 
129
- for (const id of eventIds) {
130
- if (!htmlIds.has(id)) {
131
- const eventYamlText = fs.existsSync(eventPath) ? fs.readFileSync(eventPath, 'utf8') : eventYaml;
132
- const loc = eventYamlText
133
- ? (() => {
134
- const re = new RegExp(`^\\s*${id.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&')}\\.`, 'm');
135
- const m = re.exec(eventYamlText);
136
- if (!m) return null;
137
- const before = eventYamlText.slice(0, m.index);
138
- return {
139
- line: before.split('\n').length - 1,
140
- character: 0,
141
- length: id.length,
142
- };
143
- })()
144
- : null;
145
- diagnostics.push({
146
- uri: `file://${eventPath}`,
147
- range: rangeFromLoc(loc),
148
- severity: 1,
149
- code: 'orphan-event-anchor',
150
- source: 'soma-cross-file',
151
- message: `[${base}] event "${id}" 在 HTML 中无对应 data-soma-id`,
152
- });
153
- }
105
+ const declared = new Set([...facts.uiAnchors, ...facts.componentIds]);
106
+ for (const id of declared) {
107
+ if (htmlIds.has(id)) continue;
108
+ diagnostics.push(diagnostic(
109
+ `file://${declarationPath}`,
110
+ 'missing-html-anchor',
111
+ `[${base}] 声明锚点 "${id}" 在 HTML 中无 data-soma-id`,
112
+ locate(source, new RegExp(`^\\s*${escapeRegex(id)}(?:[.@]|\\s*:)`, 'm'), id.length),
113
+ 2,
114
+ ));
115
+ }
116
+ for (const id of facts.eventAnchors) {
117
+ if (htmlIds.has(id)) continue;
118
+ diagnostics.push(diagnostic(
119
+ `file://${declarationPath}`,
120
+ 'orphan-event-anchor',
121
+ `[${base}] 事件锚点 "${id}" 在 HTML 中无对应 data-soma-id`,
122
+ locate(source, new RegExp(`^\\s*${escapeRegex(id)}\\.`, 'm'), id.length),
123
+ ));
154
124
  }
155
125
  }
156
-
157
126
  return diagnostics;
158
127
  }
159
128
 
160
- module.exports = { lintAnchorConsistency, extractSomaIdsFromHtml, extractShowKeys, extractEventIds };
129
+ module.exports = { lintAnchorConsistency, extractSomaIdsFromHtml, declarationFacts };
@@ -3,7 +3,7 @@
3
3
  */
4
4
  const fs = require('node:fs');
5
5
  const path = require('node:path');
6
- const yaml = require('js-yaml');
6
+ const { declarationFacts } = require('./soma-declaration.cjs');
7
7
 
8
8
  const ANCHOR_PREFIXES = ['btn_', 'input_', 'txt_', 'list_', 'section_', 'nav_', 'link_', 'hero_'];
9
9
 
@@ -29,7 +29,10 @@ function getLocaleYamlCompletions(hostDir, _uri, text, position) {
29
29
 
30
30
  function getHtmlAnchorCompletions(hostDir, uri, text, position) {
31
31
  const filePath = decodeURIComponent(uri.replace(/^file:\/\//, ''));
32
- const pageBase = path.basename(filePath, '.html');
32
+ const rel = path.relative(path.join(hostDir, 'html'), filePath);
33
+ const pageBase = !rel.startsWith('..') && rel.endsWith('.html')
34
+ ? rel.replace(/\\/g, '/').replace(/\.html$/, '')
35
+ : path.basename(filePath, '.html');
33
36
  const uiPath = path.join(hostDir, 'ui', `${pageBase}_ui.yaml`);
34
37
  const existing = new Set();
35
38
  for (const m of text.matchAll(/data-soma-id=["']([^"']+)["']/g)) existing.add(m[1]);
@@ -37,12 +40,15 @@ function getHtmlAnchorCompletions(hostDir, uri, text, position) {
37
40
  const suggestions = [];
38
41
  if (fs.existsSync(uiPath)) {
39
42
  try {
40
- const doc = yaml.load(fs.readFileSync(uiPath, 'utf8'));
41
- if (doc?.show) {
42
- for (const k of Object.keys(doc.show)) {
43
- if (!existing.has(k)) {
44
- suggestions.push({ label: `data-soma-id="${k}"`, kind: 5, detail: 'ui.yaml show 键' });
45
- }
43
+ const facts = declarationFacts(fs.readFileSync(uiPath, 'utf8'));
44
+ const declared = new Set([...facts.uiAnchors, ...facts.componentIds, ...facts.eventAnchors]);
45
+ for (const key of [...declared].sort()) {
46
+ if (!existing.has(key)) {
47
+ suggestions.push({
48
+ label: `data-soma-id="${key}"`,
49
+ kind: 5,
50
+ detail: 'SOMA DSL 声明锚点',
51
+ });
46
52
  }
47
53
  }
48
54
  } catch {
@@ -1,9 +1,10 @@
1
1
  /**
2
- * LSP Hover / Go-to-Definition — html ↔ ui ↔ event ↔ domain
2
+ * LSP Hover / Go-to-Definition — HTML ↔ 唯一 SOMA DSL 声明 ↔ Domain
3
3
  */
4
4
  const fs = require('node:fs');
5
5
  const path = require('node:path');
6
6
  const { scanDomainProperties } = require('./ui-yaml-completion.cjs');
7
+ const { anchorOf, declarationFacts } = require('./soma-declaration.cjs');
7
8
 
8
9
  function filePathFromUri(uri) {
9
10
  return decodeURIComponent(uri.replace(/^file:\/\//, ''));
@@ -17,8 +18,6 @@ function pageBaseFromPath(filePath, hostDir) {
17
18
  const rel = path.relative(hostRoot, resolved);
18
19
  let mm = rel.match(/^ui[/\\](.+)_ui\.yaml$/);
19
20
  if (mm) return mm[1].replace(/\\/g, '/');
20
- mm = rel.match(/^event[/\\](.+)_event\.yaml$/);
21
- if (mm) return mm[1].replace(/\\/g, '/');
22
21
  mm = rel.match(/^html[/\\](.+)\.html$/);
23
22
  if (mm) return mm[1].replace(/\\/g, '/');
24
23
  }
@@ -26,8 +25,6 @@ function pageBaseFromPath(filePath, hostDir) {
26
25
  const name = path.basename(filePath);
27
26
  let m = name.match(/^(.+)_ui\.yaml$/);
28
27
  if (m) return m[1];
29
- m = name.match(/^(.+)_event\.yaml$/);
30
- if (m) return m[1];
31
28
  m = name.match(/^(.+)\.html$/);
32
29
  if (m) return m[1].replace(/\\/g, '/');
33
30
  return null;
@@ -52,13 +49,31 @@ function findAnchorInHtml(html, somaId) {
52
49
  }
53
50
 
54
51
  function findKeyInYaml(yaml, key) {
55
- const re = new RegExp(`^\\s{0,4}${key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\s*:`, 'm');
56
- const m = re.exec(yaml);
52
+ const escaped = key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
53
+ const candidates = [
54
+ new RegExp(`^${escaped}\\s*:`, 'm'),
55
+ new RegExp(`^[ \\t]+["']?${escaped}(?:[.@][^"':\\s]+)?["']?[ \\t]*:`, 'm'),
56
+ ];
57
+ const matches = candidates.map((re) => re.exec(yaml)).filter(Boolean).sort((a, b) => a.index - b.index);
58
+ const m = matches[0];
57
59
  if (!m) return null;
58
60
  const line = lineAtIndex(yaml, m.index);
59
61
  return { line, character: m.index - yaml.lastIndexOf('\n', m.index - 1) - 1, length: key.length };
60
62
  }
61
63
 
64
+ function leadingYamlKey(line) {
65
+ const match = /^\s*["']?([\w@.-]+)["']?\s*:/.exec(line);
66
+ return match?.[1] ?? null;
67
+ }
68
+
69
+ function factsOrNull(source) {
70
+ try {
71
+ return declarationFacts(source);
72
+ } catch {
73
+ return null;
74
+ }
75
+ }
76
+
62
77
  function findEventAnchorInYaml(yaml, anchorId) {
63
78
  const re = new RegExp(`^\\s*${anchorId.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\.`, 'm');
64
79
  const m = re.exec(yaml);
@@ -69,10 +84,15 @@ function findEventAnchorInYaml(yaml, anchorId) {
69
84
  function getHover(hostDir, uri, text, position) {
70
85
  const filePath = filePathFromUri(uri);
71
86
  const fileName = path.basename(filePath);
72
- const line = (text.split('\n')[position.line] ?? '').trim();
87
+ const rawLine = text.split('\n')[position.line] ?? '';
88
+ const line = rawLine.trim();
89
+ const colon = rawLine.indexOf(':');
90
+ const keyHoverRequested = fileName.endsWith('_ui.yaml')
91
+ && colon >= 0
92
+ && position.character <= colon;
73
93
 
74
94
  const pathMatch = line.match(/page\.([\w.]+)/);
75
- if (pathMatch && fileName.endsWith('_ui.yaml')) {
95
+ if (pathMatch && fileName.endsWith('_ui.yaml') && !keyHoverRequested) {
76
96
  return {
77
97
  contents: {
78
98
  kind: 'markdown',
@@ -81,26 +101,24 @@ function getHover(hostDir, uri, text, position) {
81
101
  };
82
102
  }
83
103
 
84
- const showKey = line.match(/^([\w@.-]+)\s*:/);
85
- if (showKey && fileName.endsWith('_ui.yaml')) {
104
+ const yamlKey = leadingYamlKey(line);
105
+ if (yamlKey && fileName.endsWith('_ui.yaml')) {
86
106
  const pageBase = pageBaseFromPath(filePath, hostDir);
87
107
  const htmlPath = path.join(hostDir, 'html', `${pageBase}.html`);
88
108
  const html = fs.existsSync(htmlPath) ? fs.readFileSync(htmlPath, 'utf8') : '';
89
- const inHtml = html.includes(`data-soma-id="${showKey[1]}"`);
109
+ const facts = factsOrNull(text);
110
+ const anchor = anchorOf(yamlKey.split('.')[0]);
111
+ if (!facts || (!facts.uiAnchors.has(anchor) && !facts.componentIds.has(anchor)
112
+ && !facts.eventAnchors.has(anchor))) return null;
113
+ const inHtml = html.includes(`data-soma-id="${anchor}"`)
114
+ || html.includes(`data-soma-id='${anchor}'`);
115
+ const isEvent = facts.eventAnchors.has(anchor) && yamlKey.includes('.');
90
116
  return {
91
117
  contents: {
92
118
  kind: 'markdown',
93
- value: `**锚点** \`${showKey[1]}\`\n\nHTML: ${inHtml ? '✓ 已声明' : '✗ 未找到'}`,
94
- },
95
- };
96
- }
97
-
98
- const eventAnchor = line.match(/^([\w@.-]+)\.([\w.]+)\s*:/);
99
- if (eventAnchor && fileName.endsWith('_event.yaml')) {
100
- return {
101
- contents: {
102
- kind: 'markdown',
103
- value: `**事件** \`${eventAnchor[1]}.${eventAnchor[2]}\`\n\n类型:Pipeline 事件锚点`,
119
+ value: isEvent
120
+ ? `**事件** \`${yamlKey}\`\n\nHTML 锚点: ${inHtml ? '✓ 已声明' : '✗ 未找到'}`
121
+ : `**锚点** \`${anchor}\`\n\nHTML: ${inHtml ? '✓ 已声明' : '✗ 未找到'}`,
104
122
  },
105
123
  };
106
124
  }
@@ -129,29 +147,15 @@ function getDefinition(hostDir, uri, text, position) {
129
147
 
130
148
  const htmlPath = path.join(hostDir, 'html', `${pageBase}.html`);
131
149
  const uiPath = path.join(hostDir, 'ui', `${pageBase}_ui.yaml`);
132
- const eventPath = path.join(hostDir, 'event', `${pageBase}_event.yaml`);
133
150
 
134
151
  if (fileName.endsWith('_ui.yaml')) {
135
- const keyMatch = currentLine.match(/^\s*([\w@.-]+)\s*:/);
136
- if (keyMatch && fs.existsSync(htmlPath)) {
137
- const html = fs.readFileSync(htmlPath, 'utf8');
138
- const loc = findAnchorInHtml(html, keyMatch[1]);
139
- if (!loc) return null;
140
- return {
141
- uri: `file://${htmlPath}`,
142
- range: {
143
- start: { line: loc.line, character: loc.character },
144
- end: { line: loc.line, character: loc.character + loc.length },
145
- },
146
- };
147
- }
148
- }
149
-
150
- if (fileName.endsWith('_event.yaml')) {
151
- const anchor = currentLine.match(/^\s*([\w@.-]+)\./);
152
- if (anchor && fs.existsSync(htmlPath)) {
152
+ const key = leadingYamlKey(currentLine);
153
+ const facts = factsOrNull(text);
154
+ const anchor = key ? anchorOf(key.split('.')[0]) : null;
155
+ if (anchor && facts && (facts.uiAnchors.has(anchor) || facts.componentIds.has(anchor)
156
+ || facts.eventAnchors.has(anchor)) && fs.existsSync(htmlPath)) {
153
157
  const html = fs.readFileSync(htmlPath, 'utf8');
154
- const loc = findAnchorInHtml(html, anchor[1]);
158
+ const loc = findAnchorInHtml(html, anchor);
155
159
  if (!loc) return null;
156
160
  return {
157
161
  uri: `file://${htmlPath}`,
@@ -167,6 +171,9 @@ function getDefinition(hostDir, uri, text, position) {
167
171
  const m = currentLine.match(/data-soma-id=["']([^"']+)["']/);
168
172
  if (m && fs.existsSync(uiPath)) {
169
173
  const uiYaml = fs.readFileSync(uiPath, 'utf8');
174
+ const facts = factsOrNull(uiYaml);
175
+ if (!facts || (!facts.uiAnchors.has(m[1]) && !facts.componentIds.has(m[1])
176
+ && !facts.eventAnchors.has(m[1]))) return null;
170
177
  const loc = findKeyInYaml(uiYaml, m[1]);
171
178
  if (!loc) return null;
172
179
  return {
@@ -0,0 +1,85 @@
1
+ const yaml = require('js-yaml');
2
+
3
+ function stripDeclarationSwitches(source) {
4
+ const lines = String(source ?? '').split('\n');
5
+ let open = -1;
6
+ let close = -1;
7
+ for (let index = 0; index < lines.length; index += 1) {
8
+ const text = lines[index].trim();
9
+ if (!text || text.startsWith('#')) continue;
10
+ if (open < 0) {
11
+ if (text !== '{{') throw new Error('SOMA DSL 声明必须以 `{{` 开关行开头');
12
+ open = index;
13
+ continue;
14
+ }
15
+ if (text === '}}') close = index;
16
+ }
17
+ if (open < 0 || close < 0 || close <= open) {
18
+ throw new Error('SOMA DSL 声明缺少成对的 `{{` / `}}` 开关行');
19
+ }
20
+ return lines.slice(open + 1, close).join('\n');
21
+ }
22
+
23
+ function parseSomaDeclaration(source) {
24
+ const doc = yaml.load(stripDeclarationSwitches(source));
25
+ if (!doc || typeof doc !== 'object' || Array.isArray(doc)) {
26
+ throw new Error('SOMA DSL 开关内必须是块映射');
27
+ }
28
+ if (!doc.page || typeof doc.page !== 'object' || Array.isArray(doc.page)) {
29
+ throw new Error('SOMA DSL 缺少 page 块');
30
+ }
31
+ return doc;
32
+ }
33
+
34
+ function anchorOf(key) {
35
+ const at = key.indexOf('@');
36
+ const dot = key.lastIndexOf('.');
37
+ const cut = at > 0 ? at : dot > 0 && ['when', 'model', 'class'].includes(key.slice(dot + 1)) ? dot : -1;
38
+ return cut > 0 ? key.slice(0, cut) : key;
39
+ }
40
+
41
+ function collectUiAnchors(ui, result = new Set()) {
42
+ if (!ui || typeof ui !== 'object' || Array.isArray(ui)) return result;
43
+ for (const [key, value] of Object.entries(ui)) {
44
+ if (key === 'each') continue;
45
+ result.add(anchorOf(key));
46
+ if (value && typeof value === 'object' && !Array.isArray(value) && typeof value.each === 'string') {
47
+ collectUiAnchors(value, result);
48
+ }
49
+ }
50
+ return result;
51
+ }
52
+
53
+ function collectEventAnchors(node, result = new Set()) {
54
+ if (!node || typeof node !== 'object' || Array.isArray(node)) return result;
55
+ for (const [key, value] of Object.entries(node)) {
56
+ if (typeof value === 'string') {
57
+ const anchor = key.split('.')[0];
58
+ if (anchor) result.add(anchor);
59
+ } else if (value && typeof value === 'object' && !Array.isArray(value)) {
60
+ collectEventAnchors(value, result);
61
+ }
62
+ }
63
+ return result;
64
+ }
65
+
66
+ function declarationFacts(source) {
67
+ const doc = parseSomaDeclaration(source);
68
+ const page = doc.page;
69
+ const componentIds = new Set(Object.keys(doc).filter((key) => key !== 'page'));
70
+ const uiAnchors = collectUiAnchors(page.ui);
71
+ const eventAnchors = collectEventAnchors(page.event?.on);
72
+ for (const id of componentIds) {
73
+ if (doc[id]?.event?.on && Object.keys(doc[id].event.on).length > 0) eventAnchors.add(id);
74
+ }
75
+ return { doc, page, componentIds, uiAnchors, eventAnchors };
76
+ }
77
+
78
+ module.exports = {
79
+ stripDeclarationSwitches,
80
+ parseSomaDeclaration,
81
+ anchorOf,
82
+ collectUiAnchors,
83
+ collectEventAnchors,
84
+ declarationFacts,
85
+ };