@driftsys/markspec 0.11.0 → 0.12.1

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 (176) hide show
  1. package/README.md +9 -0
  2. package/esm/ast/normalize.d.ts +4 -5
  3. package/esm/ast/normalize.d.ts.map +1 -1
  4. package/esm/ast/normalize.js +6 -37
  5. package/esm/collect/mod.d.ts +29 -9
  6. package/esm/collect/mod.d.ts.map +1 -1
  7. package/esm/collect/mod.js +36 -10
  8. package/esm/compiler/mod.d.ts.map +1 -1
  9. package/esm/compiler/mod.js +2 -6
  10. package/esm/config/markspec.d.ts +5 -3
  11. package/esm/config/markspec.d.ts.map +1 -1
  12. package/esm/config/mod.d.ts.map +1 -1
  13. package/esm/config/mod.js +47 -5
  14. package/esm/discovery/mod.d.ts +24 -8
  15. package/esm/discovery/mod.d.ts.map +1 -1
  16. package/esm/discovery/mod.js +57 -21
  17. package/esm/discovery/projects.d.ts +111 -0
  18. package/esm/discovery/projects.d.ts.map +1 -0
  19. package/esm/discovery/projects.js +242 -0
  20. package/esm/formatter/entry_body.d.ts +84 -0
  21. package/esm/formatter/entry_body.d.ts.map +1 -0
  22. package/esm/formatter/entry_body.js +291 -0
  23. package/esm/formatter/md_equiv.d.ts +6 -5
  24. package/esm/formatter/md_equiv.d.ts.map +1 -1
  25. package/esm/formatter/md_equiv.js +6 -5
  26. package/esm/formatter/mod.d.ts +16 -9
  27. package/esm/formatter/mod.d.ts.map +1 -1
  28. package/esm/formatter/mod.js +327 -53
  29. package/esm/formatter/tab_markers.d.ts +15 -0
  30. package/esm/formatter/tab_markers.d.ts.map +1 -0
  31. package/esm/formatter/tab_markers.js +133 -0
  32. package/esm/gates/mod.d.ts.map +1 -1
  33. package/esm/gates/mod.js +6 -5
  34. package/esm/lint/runner.d.ts.map +1 -1
  35. package/esm/lint/runner.js +4 -4
  36. package/esm/lock/check.d.ts.map +1 -1
  37. package/esm/lock/check.js +2 -1
  38. package/esm/lock/edge_drift.d.ts +2 -0
  39. package/esm/lock/edge_drift.d.ts.map +1 -1
  40. package/esm/lock/edge_drift.js +4 -0
  41. package/esm/lock/mod.d.ts +1 -1
  42. package/esm/lock/mod.d.ts.map +1 -1
  43. package/esm/lock/mod.js +1 -1
  44. package/esm/lock/upstream_deps.js +1 -1
  45. package/esm/mod.d.ts +11 -6
  46. package/esm/mod.d.ts.map +1 -1
  47. package/esm/mod.js +9 -6
  48. package/esm/model/mod.d.ts +22 -1
  49. package/esm/model/mod.d.ts.map +1 -1
  50. package/esm/model/mod.js +10 -0
  51. package/esm/parser/code_regions.d.ts +29 -0
  52. package/esm/parser/code_regions.d.ts.map +1 -0
  53. package/esm/parser/code_regions.js +38 -0
  54. package/esm/parser/markdown.d.ts.map +1 -1
  55. package/esm/parser/markdown.js +103 -40
  56. package/esm/parser/mod.d.ts.map +1 -1
  57. package/esm/parser/mod.js +7 -1
  58. package/esm/parser/translate.d.ts.map +1 -1
  59. package/esm/parser/translate.js +5 -0
  60. package/esm/refs/mod.d.ts +6 -2
  61. package/esm/refs/mod.d.ts.map +1 -1
  62. package/esm/refs/mod.js +15 -21
  63. package/esm/reporter/mod.d.ts.map +1 -1
  64. package/esm/reporter/mod.js +2 -2
  65. package/esm/sync/mapping.d.ts +4 -3
  66. package/esm/sync/mapping.d.ts.map +1 -1
  67. package/esm/sync/mapping.js +52 -9
  68. package/esm/util/display_id.d.ts +20 -0
  69. package/esm/util/display_id.d.ts.map +1 -0
  70. package/esm/util/display_id.js +19 -0
  71. package/esm/util/fence.d.ts +20 -29
  72. package/esm/util/fence.d.ts.map +1 -1
  73. package/esm/util/fence.js +42 -50
  74. package/esm/util/modals.d.ts +6 -0
  75. package/esm/util/modals.d.ts.map +1 -1
  76. package/esm/util/modals.js +37 -0
  77. package/esm/validator/body_blocks.d.ts +2 -1
  78. package/esm/validator/body_blocks.d.ts.map +1 -1
  79. package/esm/validator/body_blocks.js +80 -2
  80. package/esm/validator/corpus.d.ts +6 -0
  81. package/esm/validator/corpus.d.ts.map +1 -1
  82. package/esm/validator/corpus.js +16 -4
  83. package/esm/validator/mod.d.ts +1 -1
  84. package/esm/validator/mod.d.ts.map +1 -1
  85. package/esm/validator/mod.js +1 -1
  86. package/esm/validator/pipeline.d.ts +6 -4
  87. package/esm/validator/pipeline.d.ts.map +1 -1
  88. package/esm/validator/pipeline.js +24 -19
  89. package/package.json +1 -1
  90. package/script/ast/normalize.d.ts +4 -5
  91. package/script/ast/normalize.d.ts.map +1 -1
  92. package/script/ast/normalize.js +5 -36
  93. package/script/collect/mod.d.ts +29 -9
  94. package/script/collect/mod.d.ts.map +1 -1
  95. package/script/collect/mod.js +38 -10
  96. package/script/compiler/mod.d.ts.map +1 -1
  97. package/script/compiler/mod.js +1 -5
  98. package/script/config/markspec.d.ts +5 -3
  99. package/script/config/markspec.d.ts.map +1 -1
  100. package/script/config/mod.d.ts.map +1 -1
  101. package/script/config/mod.js +47 -5
  102. package/script/discovery/mod.d.ts +24 -8
  103. package/script/discovery/mod.d.ts.map +1 -1
  104. package/script/discovery/mod.js +56 -20
  105. package/script/discovery/projects.d.ts +111 -0
  106. package/script/discovery/projects.d.ts.map +1 -0
  107. package/script/discovery/projects.js +250 -0
  108. package/script/formatter/entry_body.d.ts +84 -0
  109. package/script/formatter/entry_body.d.ts.map +1 -0
  110. package/script/formatter/entry_body.js +296 -0
  111. package/script/formatter/md_equiv.d.ts +6 -5
  112. package/script/formatter/md_equiv.d.ts.map +1 -1
  113. package/script/formatter/md_equiv.js +6 -5
  114. package/script/formatter/mod.d.ts +16 -9
  115. package/script/formatter/mod.d.ts.map +1 -1
  116. package/script/formatter/mod.js +324 -50
  117. package/script/formatter/tab_markers.d.ts +15 -0
  118. package/script/formatter/tab_markers.d.ts.map +1 -0
  119. package/script/formatter/tab_markers.js +141 -0
  120. package/script/gates/mod.d.ts.map +1 -1
  121. package/script/gates/mod.js +15 -14
  122. package/script/lint/runner.d.ts.map +1 -1
  123. package/script/lint/runner.js +3 -3
  124. package/script/lock/check.d.ts.map +1 -1
  125. package/script/lock/check.js +2 -1
  126. package/script/lock/edge_drift.d.ts +2 -0
  127. package/script/lock/edge_drift.d.ts.map +1 -1
  128. package/script/lock/edge_drift.js +5 -0
  129. package/script/lock/mod.d.ts +1 -1
  130. package/script/lock/mod.d.ts.map +1 -1
  131. package/script/lock/mod.js +2 -1
  132. package/script/lock/upstream_deps.js +1 -1
  133. package/script/mod.d.ts +11 -6
  134. package/script/mod.d.ts.map +1 -1
  135. package/script/mod.js +23 -5
  136. package/script/model/mod.d.ts +22 -1
  137. package/script/model/mod.d.ts.map +1 -1
  138. package/script/model/mod.js +11 -0
  139. package/script/parser/code_regions.d.ts +29 -0
  140. package/script/parser/code_regions.d.ts.map +1 -0
  141. package/script/parser/code_regions.js +41 -0
  142. package/script/parser/markdown.d.ts.map +1 -1
  143. package/script/parser/markdown.js +102 -39
  144. package/script/parser/mod.d.ts.map +1 -1
  145. package/script/parser/mod.js +7 -1
  146. package/script/parser/translate.d.ts.map +1 -1
  147. package/script/parser/translate.js +5 -0
  148. package/script/refs/mod.d.ts +6 -2
  149. package/script/refs/mod.d.ts.map +1 -1
  150. package/script/refs/mod.js +14 -20
  151. package/script/reporter/mod.d.ts.map +1 -1
  152. package/script/reporter/mod.js +1 -1
  153. package/script/sync/mapping.d.ts +4 -3
  154. package/script/sync/mapping.d.ts.map +1 -1
  155. package/script/sync/mapping.js +52 -9
  156. package/script/util/display_id.d.ts +20 -0
  157. package/script/util/display_id.d.ts.map +1 -0
  158. package/script/util/display_id.js +22 -0
  159. package/script/util/fence.d.ts +20 -29
  160. package/script/util/fence.d.ts.map +1 -1
  161. package/script/util/fence.js +47 -54
  162. package/script/util/modals.d.ts +6 -0
  163. package/script/util/modals.d.ts.map +1 -1
  164. package/script/util/modals.js +38 -0
  165. package/script/validator/body_blocks.d.ts +2 -1
  166. package/script/validator/body_blocks.d.ts.map +1 -1
  167. package/script/validator/body_blocks.js +80 -2
  168. package/script/validator/corpus.d.ts +6 -0
  169. package/script/validator/corpus.d.ts.map +1 -1
  170. package/script/validator/corpus.js +16 -3
  171. package/script/validator/mod.d.ts +1 -1
  172. package/script/validator/mod.d.ts.map +1 -1
  173. package/script/validator/mod.js +2 -1
  174. package/script/validator/pipeline.d.ts +6 -4
  175. package/script/validator/pipeline.d.ts.map +1 -1
  176. package/script/validator/pipeline.js +24 -19
@@ -0,0 +1,111 @@
1
+ /**
2
+ * @module core/discovery/projects
3
+ *
4
+ * Project-root discovery shared by the CLI, the LSP and the MCP server.
5
+ *
6
+ * A "project root" is a directory that holds a `project.yaml` or a
7
+ * `.markspec.yaml`. {@linkcode discoverProjectRoots} finds the primary
8
+ * root by walking up from a start folder, then every nested root below
9
+ * it with the gitignore-aware file walk.
10
+ */
11
+ import { type DiscoveryIO } from "./mod.js";
12
+ /** `true` when `path` is `root` itself or lies below it. */
13
+ export declare function isWithin(root: string, path: string): boolean;
14
+ /**
15
+ * Return the deepest root in `roots` that is `dir` itself or an ancestor
16
+ * of `dir`, or `undefined` when none is.
17
+ */
18
+ export declare function nearestRoot(dir: string, roots: Iterable<string>): string | undefined;
19
+ /**
20
+ * Walk up from `startDir` to the nearest `project.yaml`, else the nearest
21
+ * `.markspec.yaml`. Returns `undefined` when neither exists.
22
+ *
23
+ * The LSP server and the MCP server use this order to find the root
24
+ * above their workspace. The CLI (`check`, `fmt`, `lint`) uses
25
+ * {@linkcode createProjectRootResolver}, which starts from this nearest
26
+ * config root but skips a config that an enclosing project hides (#845).
27
+ */
28
+ export declare function resolveConfigRoot(startDir: string, readFile: (path: string) => Promise<string | undefined>): Promise<string | undefined>;
29
+ /**
30
+ * Directories strictly below `root` that a document walk-up would
31
+ * resolve to, found with the gitignore-aware project walk (honouring
32
+ * `exclude`). Each discovered `project.yaml` or `.markspec.yaml` is
33
+ * resolved with {@linkcode resolveConfigRoot} from its own directory, so a
34
+ * `.markspec.yaml` below a `project.yaml` is not a root of its own — the
35
+ * same answer the CLI gives for a file there. Each found root is scanned
36
+ * again with its own exclude list, so its negations can re-include non-hidden
37
+ * folders that the enclosing project's walk skipped. A found root is dropped
38
+ * when the start root's exclude list or the `.markspec.yaml` `exclude:` list
39
+ * of another found root above it covers it: each project's exclude list applies
40
+ * to its own subtree, with patterns relative to that project. The result is sorted.
41
+ */
42
+ export declare function findNestedProjectRoots(root: string, io: DiscoveryIO, exclude?: readonly string[]): Promise<string[]>;
43
+ /** Options for {@linkcode discoverProjectRoots}. */
44
+ export interface ProjectDiscoveryOptions {
45
+ /** The user's home folder. When the primary root equals this folder, no
46
+ * downward scan runs from it. */
47
+ readonly homeDir?: string;
48
+ }
49
+ /**
50
+ * Find every MarkSpec project reachable from `startDir`.
51
+ *
52
+ * `primaryRoot` is the nearest config root found by walking up from
53
+ * `startDir` (see {@linkcode resolveConfigRoot}), or `startDir` itself
54
+ * when there is none. `roots` holds the primary root (only when it has a
55
+ * config) plus every nested root below it, sorted. The nested scan honours
56
+ * the primary project's `.markspec.yaml` `exclude:` list, and each nested
57
+ * project's own list for the folders below it.
58
+ *
59
+ * When the primary root is a filesystem root or `options.homeDir`, no
60
+ * downward scan runs, even when that folder holds a config: `roots` then
61
+ * holds only the primary root when it has a config, and is empty
62
+ * otherwise.
63
+ */
64
+ export declare function discoverProjectRoots(startDir: string, io: DiscoveryIO, options?: ProjectDiscoveryOptions): Promise<{
65
+ primaryRoot: string;
66
+ roots: string[];
67
+ }>;
68
+ /** Answers which project root owns a folder; see
69
+ * {@linkcode createProjectRootResolver}. */
70
+ export interface ProjectRootResolver {
71
+ /**
72
+ * The project root that owns files in `dir`, or `undefined` when no
73
+ * `project.yaml` or `.markspec.yaml` is at or above `dir`.
74
+ */
75
+ ownerOf(dir: string): Promise<string | undefined>;
76
+ /**
77
+ * The nested project roots below `root`
78
+ * ({@linkcode findNestedProjectRoots} with the `.markspec.yaml`
79
+ * `exclude:` list of `root`). Cached per root.
80
+ */
81
+ nestedRoots(root: string): Promise<string[]>;
82
+ /**
83
+ * The outermost project root at or above `dir`: the root from which the
84
+ * LSP would index `dir` when its workspace is the outermost project. A
85
+ * nested project's walk inherits the `.gitignore` files from this root
86
+ * down (`discoverFiles` option `inheritGitignoreFrom`). `undefined` when
87
+ * no `project.yaml` or `.markspec.yaml` is at or above `dir`.
88
+ */
89
+ outermostOf(dir: string): Promise<string | undefined>;
90
+ }
91
+ /**
92
+ * Create a resolver that gives, for any folder, the project root the LSP
93
+ * would give a document in that folder when the editor workspace is the
94
+ * outermost project root above it (#845).
95
+ *
96
+ * The nearest config root {@linkcode resolveConfigRoot} finds is not
97
+ * always the owner: when an enclosing project hides that config (an
98
+ * `exclude:` entry, a `.gitignore` entry, a hidden folder), the LSP and
99
+ * the MCP server never treat it as a root, and its files belong to the
100
+ * enclosing project. So the resolver walks up from the nearest config
101
+ * root to the outermost enclosing one, finds the nested roots below that
102
+ * outermost root the way the LSP does, and returns the deepest of them
103
+ * that contains `dir`.
104
+ *
105
+ * The walk up stops before a filesystem root and before
106
+ * `options.homeDir`: {@linkcode discoverProjectRoots} never scans down
107
+ * from those folders, so a stray config there must not claim every
108
+ * project below it. Results are cached per folder.
109
+ */
110
+ export declare function createProjectRootResolver(io: DiscoveryIO, options?: ProjectDiscoveryOptions): ProjectRootResolver;
111
+ //# sourceMappingURL=projects.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"projects.d.ts","sourceRoot":"","sources":["../../src/discovery/projects.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAKH,OAAO,EAAiB,KAAK,WAAW,EAAE,MAAM,UAAU,CAAC;AAS3D,4DAA4D;AAC5D,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAM5D;AAED;;;GAGG;AACH,wBAAgB,WAAW,CACzB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,GACtB,MAAM,GAAG,SAAS,CAOpB;AAED;;;;;;;;GAQG;AACH,wBAAsB,iBAAiB,CACrC,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,GACtD,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAG7B;AAED;;;;;;;;;;;;GAYG;AACH,wBAAsB,sBAAsB,CAC1C,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,WAAW,EACf,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,GAC1B,OAAO,CAAC,MAAM,EAAE,CAAC,CA+CnB;AAgBD,oDAAoD;AACpD,MAAM,WAAW,uBAAuB;IACtC;qCACiC;IACjC,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,oBAAoB,CACxC,QAAQ,EAAE,MAAM,EAChB,EAAE,EAAE,WAAW,EACf,OAAO,GAAE,uBAA4B,GACpC,OAAO,CAAC;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC,CAoBnD;AAED;4CAC4C;AAC5C,MAAM,WAAW,mBAAmB;IAClC;;;OAGG;IACH,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IAClD;;;;OAIG;IACH,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAC7C;;;;;;OAMG;IACH,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;CACvD;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,yBAAyB,CACvC,EAAE,EAAE,WAAW,EACf,OAAO,GAAE,uBAA4B,GACpC,mBAAmB,CAqErB"}
@@ -0,0 +1,242 @@
1
+ /**
2
+ * @module core/discovery/projects
3
+ *
4
+ * Project-root discovery shared by the CLI, the LSP and the MCP server.
5
+ *
6
+ * A "project root" is a directory that holds a `project.yaml` or a
7
+ * `.markspec.yaml`. {@linkcode discoverProjectRoots} finds the primary
8
+ * root by walking up from a start folder, then every nested root below
9
+ * it with the gitignore-aware file walk.
10
+ */
11
+ import { basename, dirname, isAbsolute, relative, SEPARATOR } from "@std/path";
12
+ import { discoverProjectRoot } from "../config/mod.js";
13
+ import { discoverMarkspecRoot, loadToolConfig } from "../config/markspec.js";
14
+ import { discoverFiles } from "./mod.js";
15
+ import { isIgnored, parseGitignore } from "./gitignore.js";
16
+ /** The file names whose directory can be a project root. */
17
+ const CONFIG_BASENAMES = new Set([
18
+ "project.yaml",
19
+ ".markspec.yaml",
20
+ ]);
21
+ /** `true` when `path` is `root` itself or lies below it. */
22
+ export function isWithin(root, path) {
23
+ const rel = relative(root, path);
24
+ // A child directory may itself start with `..` (e.g. `..cache`), so
25
+ // compare whole path segments, not a string prefix.
26
+ return rel === "" ||
27
+ (rel !== ".." && !rel.startsWith(`..${SEPARATOR}`) && !isAbsolute(rel));
28
+ }
29
+ /**
30
+ * Return the deepest root in `roots` that is `dir` itself or an ancestor
31
+ * of `dir`, or `undefined` when none is.
32
+ */
33
+ export function nearestRoot(dir, roots) {
34
+ let best;
35
+ for (const root of roots) {
36
+ if (!isWithin(root, dir))
37
+ continue;
38
+ if (best === undefined || root.length > best.length)
39
+ best = root;
40
+ }
41
+ return best;
42
+ }
43
+ /**
44
+ * Walk up from `startDir` to the nearest `project.yaml`, else the nearest
45
+ * `.markspec.yaml`. Returns `undefined` when neither exists.
46
+ *
47
+ * The LSP server and the MCP server use this order to find the root
48
+ * above their workspace. The CLI (`check`, `fmt`, `lint`) uses
49
+ * {@linkcode createProjectRootResolver}, which starts from this nearest
50
+ * config root but skips a config that an enclosing project hides (#845).
51
+ */
52
+ export async function resolveConfigRoot(startDir, readFile) {
53
+ return await discoverProjectRoot(startDir, readFile) ??
54
+ await discoverMarkspecRoot(startDir, readFile);
55
+ }
56
+ /**
57
+ * Directories strictly below `root` that a document walk-up would
58
+ * resolve to, found with the gitignore-aware project walk (honouring
59
+ * `exclude`). Each discovered `project.yaml` or `.markspec.yaml` is
60
+ * resolved with {@linkcode resolveConfigRoot} from its own directory, so a
61
+ * `.markspec.yaml` below a `project.yaml` is not a root of its own — the
62
+ * same answer the CLI gives for a file there. Each found root is scanned
63
+ * again with its own exclude list, so its negations can re-include non-hidden
64
+ * folders that the enclosing project's walk skipped. A found root is dropped
65
+ * when the start root's exclude list or the `.markspec.yaml` `exclude:` list
66
+ * of another found root above it covers it: each project's exclude list applies
67
+ * to its own subtree, with patterns relative to that project. The result is sorted.
68
+ */
69
+ export async function findNestedProjectRoots(root, io, exclude) {
70
+ const out = new Set();
71
+ const scans = [{ root, exclude }];
72
+ for (const scan of scans) {
73
+ for await (const path of discoverFiles(scan.root, io, {
74
+ extensions: new Set([".yaml"]),
75
+ exclude: scan.exclude,
76
+ inheritGitignoreFrom: root,
77
+ })) {
78
+ if (!CONFIG_BASENAMES.has(basename(path)))
79
+ continue;
80
+ const dir = dirname(path);
81
+ if (dir === scan.root)
82
+ continue;
83
+ const found = await resolveConfigRoot(dir, io.readFile);
84
+ if (found !== undefined && found !== root && isWithin(root, found) &&
85
+ !out.has(found)) {
86
+ out.add(found);
87
+ const { config } = await loadToolConfig(found, io.readFile);
88
+ scans.push({ root: found, exclude: config.exclude });
89
+ }
90
+ }
91
+ }
92
+ const nested = [...out];
93
+ const excludeRules = new Map();
94
+ excludeRules.set(root, parseGitignore((exclude ?? []).join("\n"), ""));
95
+ const kept = [];
96
+ for (const candidate of nested) {
97
+ let covered = false;
98
+ for (const ancestor of [root, ...nested]) {
99
+ if (ancestor === candidate || !isWithin(ancestor, candidate))
100
+ continue;
101
+ let rules = excludeRules.get(ancestor);
102
+ if (rules === undefined) {
103
+ const { config } = await loadToolConfig(ancestor, io.readFile);
104
+ rules = parseGitignore(config.exclude.join("\n"), "");
105
+ excludeRules.set(ancestor, rules);
106
+ }
107
+ if (isExcludedDir(relative(ancestor, candidate), rules)) {
108
+ covered = true;
109
+ break;
110
+ }
111
+ }
112
+ if (!covered)
113
+ kept.push(candidate);
114
+ }
115
+ return kept.sort();
116
+ }
117
+ /** `true` when the directory at `rel` (relative, host separators), or any
118
+ * directory above it, matches `rules` — the same pruning the walk does. */
119
+ function isExcludedDir(rel, rules) {
120
+ if (rules.length === 0)
121
+ return false;
122
+ const segments = rel.split(SEPARATOR);
123
+ for (let i = 1; i <= segments.length; i++) {
124
+ if (isIgnored(segments.slice(0, i).join("/"), true, rules))
125
+ return true;
126
+ }
127
+ return false;
128
+ }
129
+ /**
130
+ * Find every MarkSpec project reachable from `startDir`.
131
+ *
132
+ * `primaryRoot` is the nearest config root found by walking up from
133
+ * `startDir` (see {@linkcode resolveConfigRoot}), or `startDir` itself
134
+ * when there is none. `roots` holds the primary root (only when it has a
135
+ * config) plus every nested root below it, sorted. The nested scan honours
136
+ * the primary project's `.markspec.yaml` `exclude:` list, and each nested
137
+ * project's own list for the folders below it.
138
+ *
139
+ * When the primary root is a filesystem root or `options.homeDir`, no
140
+ * downward scan runs, even when that folder holds a config: `roots` then
141
+ * holds only the primary root when it has a config, and is empty
142
+ * otherwise.
143
+ */
144
+ export async function discoverProjectRoots(startDir, io, options = {}) {
145
+ const configRoot = await resolveConfigRoot(startDir, io.readFile);
146
+ const primaryRoot = configRoot ?? startDir;
147
+ const isFilesystemRoot = dirname(primaryRoot) === primaryRoot;
148
+ if (isFilesystemRoot || primaryRoot === options.homeDir) {
149
+ return {
150
+ primaryRoot,
151
+ roots: configRoot !== undefined ? [primaryRoot] : [],
152
+ };
153
+ }
154
+ const { config } = configRoot !== undefined
155
+ ? await loadToolConfig(primaryRoot, io.readFile)
156
+ : { config: undefined };
157
+ const nested = await findNestedProjectRoots(primaryRoot, io, config?.exclude);
158
+ const roots = configRoot !== undefined ? [primaryRoot, ...nested] : nested;
159
+ return { primaryRoot, roots: roots.sort() };
160
+ }
161
+ /**
162
+ * Create a resolver that gives, for any folder, the project root the LSP
163
+ * would give a document in that folder when the editor workspace is the
164
+ * outermost project root above it (#845).
165
+ *
166
+ * The nearest config root {@linkcode resolveConfigRoot} finds is not
167
+ * always the owner: when an enclosing project hides that config (an
168
+ * `exclude:` entry, a `.gitignore` entry, a hidden folder), the LSP and
169
+ * the MCP server never treat it as a root, and its files belong to the
170
+ * enclosing project. So the resolver walks up from the nearest config
171
+ * root to the outermost enclosing one, finds the nested roots below that
172
+ * outermost root the way the LSP does, and returns the deepest of them
173
+ * that contains `dir`.
174
+ *
175
+ * The walk up stops before a filesystem root and before
176
+ * `options.homeDir`: {@linkcode discoverProjectRoots} never scans down
177
+ * from those folders, so a stray config there must not claim every
178
+ * project below it. Results are cached per folder.
179
+ */
180
+ export function createProjectRootResolver(io, options = {}) {
181
+ const owners = new Map();
182
+ const outermosts = new Map();
183
+ const nested = new Map();
184
+ function nestedRoots(root) {
185
+ let found = nested.get(root);
186
+ if (found === undefined) {
187
+ found = loadToolConfig(root, io.readFile).then(({ config }) => findNestedProjectRoots(root, io, config.exclude));
188
+ nested.set(root, found);
189
+ }
190
+ return found;
191
+ }
192
+ function outermostOf(dir) {
193
+ let found = outermosts.get(dir);
194
+ if (found === undefined) {
195
+ found = resolveOutermost(dir);
196
+ outermosts.set(dir, found);
197
+ }
198
+ return found;
199
+ }
200
+ async function resolveOutermost(dir) {
201
+ const nearest = await resolveConfigRoot(dir, io.readFile);
202
+ return nearest === undefined ? undefined : await outermostFrom(nearest);
203
+ }
204
+ /** Walk up from the config root `nearest` to the outermost one. */
205
+ async function outermostFrom(nearest) {
206
+ let outermost = nearest;
207
+ while (true) {
208
+ const parent = dirname(outermost);
209
+ if (parent === outermost)
210
+ break;
211
+ const above = await resolveConfigRoot(parent, io.readFile);
212
+ if (above === undefined || dirname(above) === above ||
213
+ above === options.homeDir) {
214
+ break;
215
+ }
216
+ outermost = above;
217
+ }
218
+ return outermost;
219
+ }
220
+ async function resolveOwner(dir) {
221
+ const nearest = await resolveConfigRoot(dir, io.readFile);
222
+ if (nearest === undefined)
223
+ return undefined;
224
+ const outermost = await outermostFrom(nearest);
225
+ if (outermost === nearest)
226
+ return nearest;
227
+ const roots = [outermost, ...await nestedRoots(outermost)];
228
+ return nearestRoot(dir, roots) ?? outermost;
229
+ }
230
+ return {
231
+ ownerOf(dir) {
232
+ let owner = owners.get(dir);
233
+ if (owner === undefined) {
234
+ owner = resolveOwner(dir);
235
+ owners.set(dir, owner);
236
+ }
237
+ return owner;
238
+ },
239
+ nestedRoots,
240
+ outermostOf,
241
+ };
242
+ }
@@ -0,0 +1,84 @@
1
+ /**
2
+ * @module formatter/entry_body
3
+ *
4
+ * Read and replace the body of one entry in a Markdown file text.
5
+ *
6
+ * Splice-only contract: {@linkcode replaceEntryBody} changes only the
7
+ * lines of the target entry's body. Every other line of the file (other
8
+ * entries, prose, front matter, the title, the trailer block, and each
9
+ * line's own line ending) stays byte-for-byte unchanged. After the splice
10
+ * the text is parsed again; when the set of entries differs (a new entry,
11
+ * a lost entry, a changed title or trailer) or the target's body is not
12
+ * the proposal, the edit is refused.
13
+ *
14
+ * The body range starts after the title paragraph (up to the first blank
15
+ * line after the title line, so a soft-wrapped title is skipped) and the
16
+ * blank lines after it. It has as many lines as the parser's
17
+ * `entry.body`, so lazy continuation lines are included, and it must
18
+ * match that body line for line. The body indent is the content column of
19
+ * the list item: the marker column, plus the marker width, plus the spaces
20
+ * after the marker. Lines are split on `\r\n`, a
21
+ * lone `\r`, or `\n`, as in the parser.
22
+ *
23
+ * Pure: no I/O, Node-compatible.
24
+ */
25
+ /** Why {@linkcode replaceEntryBody} refused an edit. */
26
+ export type EntryBodyRefusal = "entry-not-found" | "body-empty" | "proposal-empty" | "trailer-invalid" | "entries-changed";
27
+ /** Result of {@linkcode replaceEntryBody}. */
28
+ export type EntryBodyEdit = {
29
+ readonly ok: true;
30
+ readonly text: string;
31
+ } | {
32
+ readonly ok: false;
33
+ readonly refusal: EntryBodyRefusal;
34
+ };
35
+ /**
36
+ * 0-based content column of the list item whose marker starts at `marker`
37
+ * on `line`: the marker column, plus 1 for the bullet, plus the whitespace
38
+ * after it. Tabs advance to the next four-column CommonMark tab stop.
39
+ * So `- [ID]` gives 2, `- [ID]` gives 4, and `-\t[ID]` gives 4 at
40
+ * column zero. Ordered lists never hold entries. When there is no bullet
41
+ * followed by whitespace at `marker`, use the marker column plus 2.
42
+ */
43
+ export declare function contentColumn(line: string, marker: number): number;
44
+ /**
45
+ * Body of the entry `displayId` as written in `text`: dedented, LF line
46
+ * endings, no surrounding blank lines. Returns `""` when the entry has no
47
+ * body and `undefined` when no entry has `displayId`. `file` names a
48
+ * `.md` file. When the body lines cannot be located exactly in `text`,
49
+ * the parser's body is returned (and {@linkcode replaceEntryBody}
50
+ * refuses to edit that entry).
51
+ */
52
+ export declare function readEntryBody(text: string, file: string, displayId: string): Promise<string | undefined>;
53
+ /**
54
+ * Return `text` with the body of entry `displayId` replaced by `body`,
55
+ * re-indented to the body indent. Every other line stays byte-for-byte
56
+ * unchanged. Inserted lines take the line ending of the title line. In
57
+ * `body`, `\r\n` and a lone `\r` count as line breaks.
58
+ *
59
+ * Refusals: `entry-not-found` (no such entry), `trailer-invalid` (the
60
+ * entry's trailer block did not parse, see below), `body-empty` (the entry
61
+ * has no body), `proposal-empty` (`body` is blank), `entries-changed`
62
+ * (two entries in `text` have `displayId`; or the body lines cannot be
63
+ * located exactly in `text`; or the edited
64
+ * text no longer parses to the same entries, with the same title and
65
+ * trailer for the target, and with the proposal as the target's body;
66
+ * or the parse raises more structural diagnostics of some code, such as
67
+ * MSL-P020, than the original text).
68
+ *
69
+ * `trailer-invalid`: when a trailer line is malformed (for example an
70
+ * empty `Labels:`, a key without a colon, or a lowercase key), the parser
71
+ * reads the whole trailer block, the `Id:` line included, as body. A
72
+ * splice would then delete the `Id:` and trailer lines, and the identity
73
+ * check cannot see it, because the entry has no `id` and no attributes
74
+ * before or after the edit. So the edit is refused when an `MSL-P020`,
75
+ * `MSL-P021` or `MSL-P022` names the entry (the message starts with
76
+ * `<displayId>:`). This is broader than that case: `MSL-P020` also fires
77
+ * when a body paragraph starts with a recognized trailer key, so such an
78
+ * entry is refused too. The file already fails `markspec check` there.
79
+ * An entry whose trailer-looking lines use only unrecognized keys, and
80
+ * that the parser reads as body without a diagnostic, is edited as the
81
+ * parser sees it: those lines are body.
82
+ */
83
+ export declare function replaceEntryBody(text: string, file: string, displayId: string, body: string): Promise<EntryBodyEdit>;
84
+ //# sourceMappingURL=entry_body.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entry_body.d.ts","sourceRoot":"","sources":["../../src/formatter/entry_body.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAKH,wDAAwD;AACxD,MAAM,MAAM,gBAAgB,GACxB,iBAAiB,GACjB,YAAY,GACZ,gBAAgB,GAChB,iBAAiB,GACjB,iBAAiB,CAAC;AAEtB,8CAA8C;AAC9C,MAAM,MAAM,aAAa,GACrB;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC5C;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,gBAAgB,CAAA;CAAE,CAAC;AA8E/D;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAQlE;AAyCD;;;;;;;GAOG;AACH,wBAAsB,aAAa,CACjC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAS7B;AASD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAsB,gBAAgB,CACpC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,MAAM,GACX,OAAO,CAAC,aAAa,CAAC,CAoDxB"}