@cosense-toolbox/lsp 0.1.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +57 -0
- package/dist/check.d.mts +57 -0
- package/dist/check.d.mts.map +1 -0
- package/dist/check.mjs +89 -0
- package/dist/check.mjs.map +1 -0
- package/dist/completion.d.mts +52 -0
- package/dist/completion.d.mts.map +1 -0
- package/dist/completion.mjs +139 -0
- package/dist/completion.mjs.map +1 -0
- package/dist/main.d.mts +1 -0
- package/dist/main.mjs +21 -0
- package/dist/main.mjs.map +1 -0
- package/dist/server-DPPxtJ6Y.mjs +100 -0
- package/dist/server-DPPxtJ6Y.mjs.map +1 -0
- package/dist/tokens-D0NP_uLo.mjs +312 -0
- package/dist/tokens-D0NP_uLo.mjs.map +1 -0
- package/dist/tokens.d.mts +80 -0
- package/dist/tokens.d.mts.map +1 -0
- package/dist/tokens.mjs +2 -0
- package/dist/workspace-DouAOZnK.mjs +171 -0
- package/dist/workspace-DouAOZnK.mjs.map +1 -0
- package/dist/workspace-pdw-fg9B.d.mts +29 -0
- package/dist/workspace-pdw-fg9B.d.mts.map +1 -0
- package/package.json +63 -0
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import { Array, Match, Option, Order, pipe } from "effect";
|
|
2
|
+
import { normalizeLineEndings, parse } from "@cosense-toolbox/parser";
|
|
3
|
+
import { customDecorations } from "@cosense-toolbox/parser/extensions";
|
|
4
|
+
import { visit } from "@cosense-toolbox/parser/utils";
|
|
5
|
+
//#region src/tree.ts
|
|
6
|
+
const leaf = (items) => ({
|
|
7
|
+
items,
|
|
8
|
+
descend: false
|
|
9
|
+
});
|
|
10
|
+
const branch = (items) => ({
|
|
11
|
+
items,
|
|
12
|
+
descend: true
|
|
13
|
+
});
|
|
14
|
+
/**
|
|
15
|
+
* Everything `pick` takes from `tree`, in document order.
|
|
16
|
+
*
|
|
17
|
+
* The parser's `visit` hands nodes to a callback, so something has to hold what the callback
|
|
18
|
+
* finds. That is kept to here: `pick` stays a pure function of one node.
|
|
19
|
+
*/
|
|
20
|
+
const gather = (tree, pick) => {
|
|
21
|
+
const found = [];
|
|
22
|
+
visit(tree, (node) => {
|
|
23
|
+
const { items, descend } = pick(node);
|
|
24
|
+
found.push(...items);
|
|
25
|
+
return descend ? void 0 : "skip";
|
|
26
|
+
});
|
|
27
|
+
return found;
|
|
28
|
+
};
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/tokens.ts
|
|
31
|
+
/**
|
|
32
|
+
* Cosense notation as LSP semantic tokens.
|
|
33
|
+
*
|
|
34
|
+
* These names are the ones this package reasons in, and they are not what goes on the wire.
|
|
35
|
+
* A client announces the token types it knows at initialize and drops everything else —
|
|
36
|
+
* some send the LSP's own list and nothing more — so `quote` would simply never be drawn.
|
|
37
|
+
* LEGEND below is what is sent, and LSP_TYPE is the translation.
|
|
38
|
+
*/
|
|
39
|
+
const TOKEN_TYPES = [
|
|
40
|
+
"title",
|
|
41
|
+
"link",
|
|
42
|
+
"projectLink",
|
|
43
|
+
"externalLink",
|
|
44
|
+
"hashtag",
|
|
45
|
+
"code",
|
|
46
|
+
"codeBlock",
|
|
47
|
+
"formula",
|
|
48
|
+
"icon",
|
|
49
|
+
"quote",
|
|
50
|
+
"bold",
|
|
51
|
+
"italic",
|
|
52
|
+
"strike",
|
|
53
|
+
"underline",
|
|
54
|
+
"image",
|
|
55
|
+
"table",
|
|
56
|
+
"bold2",
|
|
57
|
+
"bold3",
|
|
58
|
+
"component",
|
|
59
|
+
"attribute",
|
|
60
|
+
"attributeValue",
|
|
61
|
+
"expression",
|
|
62
|
+
"frontmatter",
|
|
63
|
+
"notation"
|
|
64
|
+
];
|
|
65
|
+
/**
|
|
66
|
+
* The legend sent to the client: the LSP's own token types, in its own order.
|
|
67
|
+
*
|
|
68
|
+
* Every entry is in the set a client is expected to understand, which is what makes the
|
|
69
|
+
* colours appear without the reader configuring anything.
|
|
70
|
+
*/
|
|
71
|
+
const LEGEND = [
|
|
72
|
+
"namespace",
|
|
73
|
+
"type",
|
|
74
|
+
"function",
|
|
75
|
+
"variable",
|
|
76
|
+
"property",
|
|
77
|
+
"parameter",
|
|
78
|
+
"decorator",
|
|
79
|
+
"macro",
|
|
80
|
+
"keyword",
|
|
81
|
+
"string",
|
|
82
|
+
"number",
|
|
83
|
+
"comment"
|
|
84
|
+
];
|
|
85
|
+
/**
|
|
86
|
+
* What each notation is drawn as. Chosen so that things a reader separates by eye land on
|
|
87
|
+
* different entries: a link is not a tag, and a quote is not a heading. `bold` and its
|
|
88
|
+
* louder siblings share one entry, because the LSP has no notion of weight and a theme
|
|
89
|
+
* gives each type a single colour.
|
|
90
|
+
*/
|
|
91
|
+
const LSP_TYPE = {
|
|
92
|
+
title: "namespace",
|
|
93
|
+
link: "function",
|
|
94
|
+
projectLink: "function",
|
|
95
|
+
externalLink: "string",
|
|
96
|
+
icon: "function",
|
|
97
|
+
image: "string",
|
|
98
|
+
hashtag: "decorator",
|
|
99
|
+
code: "string",
|
|
100
|
+
codeBlock: "string",
|
|
101
|
+
formula: "number",
|
|
102
|
+
table: "property",
|
|
103
|
+
quote: "comment",
|
|
104
|
+
frontmatter: "comment",
|
|
105
|
+
component: "type",
|
|
106
|
+
attribute: "parameter",
|
|
107
|
+
attributeValue: "string",
|
|
108
|
+
expression: "variable",
|
|
109
|
+
bold: "keyword",
|
|
110
|
+
bold2: "keyword",
|
|
111
|
+
bold3: "keyword",
|
|
112
|
+
italic: "macro",
|
|
113
|
+
strike: "comment",
|
|
114
|
+
underline: "variable",
|
|
115
|
+
notation: "decorator"
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* LEGEND with the names of `notations` after it, each once: the legend for a client that
|
|
119
|
+
* accepts token types of the caller's own naming. The LSP's types keep their places.
|
|
120
|
+
*/
|
|
121
|
+
const legendOf = (notations) => Array.dedupe([...LEGEND, ...Array.map(notations, (notation) => notation.name)]);
|
|
122
|
+
/** `parseOptions` that also read the markers of `notations` as decorations. */
|
|
123
|
+
const withNotations = (parseOptions, notations) => Array.match(notations, {
|
|
124
|
+
onEmpty: () => parseOptions,
|
|
125
|
+
onNonEmpty: (added) => ({
|
|
126
|
+
...parseOptions,
|
|
127
|
+
extensions: [...parseOptions.extensions ?? [], customDecorations(Array.map(added, (notation) => notation.marker))]
|
|
128
|
+
})
|
|
129
|
+
});
|
|
130
|
+
/** Leaf inline nodes that map 1:1. Each sits on one line, so no splitting is needed. */
|
|
131
|
+
const INLINE_TOKEN_TYPE = {
|
|
132
|
+
internalLink: "link",
|
|
133
|
+
externalLink: "externalLink",
|
|
134
|
+
projectLink: "projectLink",
|
|
135
|
+
hashtag: "hashtag",
|
|
136
|
+
inlineCode: "code",
|
|
137
|
+
image: "image",
|
|
138
|
+
icon: "icon",
|
|
139
|
+
formula: "formula"
|
|
140
|
+
};
|
|
141
|
+
/** Top to bottom, then left to right: the order the LSP encoding needs. */
|
|
142
|
+
const BY_POSITION = Order.combine(Order.mapInput(Order.number, (token) => token.line), Order.mapInput(Order.number, (token) => token.char));
|
|
143
|
+
const onlyIf = (condition, value) => condition ? Option.some(value) : Option.none();
|
|
144
|
+
/** A token over `[from, to)` of one line. */
|
|
145
|
+
const token = (type, line, from, to) => ({
|
|
146
|
+
line,
|
|
147
|
+
char: from,
|
|
148
|
+
length: to - from,
|
|
149
|
+
type
|
|
150
|
+
});
|
|
151
|
+
const spanToken = (type, position) => token(type, position.start.line, position.start.column, position.end.column);
|
|
152
|
+
/** How many characters from `from` the `^`-anchored `pattern` takes; 0 when it takes none. */
|
|
153
|
+
const matchLength = (pattern, text, from) => pipe(Option.fromNullable(pattern.exec(text.slice(from))), Option.match({
|
|
154
|
+
onNone: () => 0,
|
|
155
|
+
onSome: ([matched]) => matched.length
|
|
156
|
+
}));
|
|
157
|
+
/** Cosense sizes emphasis by asterisk count (`[*]`..`[*****]`); sizeLevel is 0-indexed. */
|
|
158
|
+
const boldTokenType = (sizeLevel) => Match.value(sizeLevel).pipe(Match.when((level) => level >= 2, () => "bold3"), Match.when(1, () => "bold2"), Match.orElse(() => "bold"));
|
|
159
|
+
/** Every decoration a run carries, since Cosense applies all of them: `[-* x]` is both. */
|
|
160
|
+
const decorationTokenTypes = (node) => Array.getSomes([
|
|
161
|
+
onlyIf(node.underline, "underline"),
|
|
162
|
+
onlyIf(node.strike, "strike"),
|
|
163
|
+
onlyIf(node.italic, "italic"),
|
|
164
|
+
onlyIf(node.bold, boldTokenType(node.sizeLevel))
|
|
165
|
+
]);
|
|
166
|
+
const COMPONENT_LINE = /^\s*<\/?[A-Z][A-Za-z0-9_.]*(\s|\/?>)/;
|
|
167
|
+
const TAG_OPEN = /^(\s*<\/?)([A-Z][A-Za-z0-9_.]*)/;
|
|
168
|
+
const ATTRIBUTE_NAME = /^[A-Za-z_:][A-Za-z0-9_:.-]*/;
|
|
169
|
+
const SPACE = /^\s*/;
|
|
170
|
+
/** Where a quoted value opened at `from` ends: after its closing quote, or at the line's end. */
|
|
171
|
+
const quotedEnd = (text, from) => pipe(text.indexOf(text.charAt(from), from + 1), Option.liftPredicate((close) => close >= 0), Option.match({
|
|
172
|
+
onNone: () => text.length,
|
|
173
|
+
onSome: (close) => close + 1
|
|
174
|
+
}));
|
|
175
|
+
/** Where `{ ... }` opened at `from` ends, counting nested braces: after its `}`, or the line's end. */
|
|
176
|
+
const bracedEnd = (text, from) => pipe(text.slice(from).split(""), Array.scan(0, (depth, char) => char === "{" ? depth + 1 : char === "}" ? depth - 1 : depth), Array.drop(1), Array.findFirstIndex((depth) => depth === 0), Option.match({
|
|
177
|
+
onNone: () => text.length,
|
|
178
|
+
onSome: (index) => from + index + 1
|
|
179
|
+
}));
|
|
180
|
+
/** The attribute value that starts at `from`: its type and where it ends. */
|
|
181
|
+
const valueAt = (text, from) => Match.value(text.charAt(from)).pipe(Match.when((char) => char === "\"" || char === "'", () => Option.some(["attributeValue", quotedEnd(text, from)])), Match.when("{", () => Option.some(["expression", bracedEnd(text, from)])), Match.orElse(() => Option.none()));
|
|
182
|
+
/** `at` past any whitespace. */
|
|
183
|
+
const skipSpace = (text, at) => at + matchLength(SPACE, text, at);
|
|
184
|
+
/** Whether the tag closes at `at`, with `>` or `/>`, or the line ends there. */
|
|
185
|
+
const closesAt = (text, at) => at >= text.length || text.startsWith(">", at) || text.startsWith("/>", at);
|
|
186
|
+
/**
|
|
187
|
+
* An attribute named over `[at, nameEnd)`: the name, then `=` and a value if one follows.
|
|
188
|
+
* A name with no `=` after it stands alone, as JSX reads `<Dialog open>`.
|
|
189
|
+
*/
|
|
190
|
+
const attribute = (line, text, at, nameEnd) => {
|
|
191
|
+
const name = token("attribute", line, at, nameEnd);
|
|
192
|
+
const afterName = skipSpace(text, nameEnd);
|
|
193
|
+
return pipe(Option.liftPredicate(afterName, (equals) => text.charAt(equals) === "="), Option.map((equals) => skipSpace(text, equals + 1)), Option.match({
|
|
194
|
+
onNone: () => [[name], afterName],
|
|
195
|
+
onSome: (valueStart) => pipe(valueAt(text, valueStart), Option.match({
|
|
196
|
+
onNone: () => [[name], valueStart],
|
|
197
|
+
onSome: ([type, end]) => [[name, token(type, line, valueStart, end)], end]
|
|
198
|
+
}))
|
|
199
|
+
}));
|
|
200
|
+
};
|
|
201
|
+
/** One attribute from `from`, or None once the tag has closed. */
|
|
202
|
+
const attributeStep = (line, text) => (from) => pipe(Option.liftPredicate(skipSpace(text, from), (at) => !closesAt(text, at)), Option.map((at) => pipe(Option.liftPredicate(at + matchLength(ATTRIBUTE_NAME, text, at), (end) => end > at), Option.match({
|
|
203
|
+
onNone: () => [[], at + 1],
|
|
204
|
+
onSome: (nameEnd) => attribute(line, text, at, nameEnd)
|
|
205
|
+
}))));
|
|
206
|
+
/**
|
|
207
|
+
* The tag that opens a component line, read as JSX: its name, then each attribute and its
|
|
208
|
+
* value, up to the `>` that closes it. Text after the tag is left alone; it is neither
|
|
209
|
+
* Cosense notation nor part of the tag.
|
|
210
|
+
*/
|
|
211
|
+
const componentTokens = (line, text) => pipe(Option.fromNullable(TAG_OPEN.exec(text)), Option.map(([head, lead = "", name = ""]) => [token("component", line, lead.length, lead.length + name.length), ...Array.flatten(Array.unfold(head.length, attributeStep(line, text)))]), Option.getOrElse(() => []));
|
|
212
|
+
/** The last line of the `---` fence around YAML at the very top, if the file opens with one. */
|
|
213
|
+
const frontmatterEnd = (lines) => pipe(Option.liftPredicate(lines, (all) => all[0]?.trim() === "---"), Option.flatMap(Array.findFirstIndex((line, index) => index > 0 && line.trim() === "---")));
|
|
214
|
+
/**
|
|
215
|
+
* `frontmatterEnd`, or None when the caller reads no frontmatter: a Cosense page has none,
|
|
216
|
+
* and a page whose title is `---` would otherwise lose its first lines to it.
|
|
217
|
+
*/
|
|
218
|
+
const fenceOf = (lines, frontmatter) => Option.flatMap(onlyIf(frontmatter, lines), frontmatterEnd);
|
|
219
|
+
/** Each non-empty line from `first` to `last`, whole. */
|
|
220
|
+
const lineSpans = (type, lines, first, last) => Array.filterMap(Array.range(first, last), (line) => pipe(Option.fromNullable(lines[line]), Option.filter((text) => text.length > 0), Option.map((text) => token(type, line, 0, text.length))));
|
|
221
|
+
/** The line's text, when the file reads components and the line opens with a tag. */
|
|
222
|
+
const componentLine = (page, line) => pipe(Option.fromNullable(page.lines[line]), Option.filter((text) => page.components && COMPONENT_LINE.test(text)));
|
|
223
|
+
/** The `>` and spaces that open a quote line, from its indentation onwards. */
|
|
224
|
+
const quoteToken = (page, node, line) => pipe(onlyIf(node.quote, page.lines[line] ?? ""), Option.map((text) => matchLength(/^[> ]*/, text, node.indent)), Option.filter((length) => length > 0), Option.map((length) => token("quote", line, node.indent, node.indent + length)));
|
|
225
|
+
/** A token for each of `notations` whose marker opens the decoration. */
|
|
226
|
+
const notationTokens = (notations, node, position) => Array.filterMap(notations, ({ marker, name }) => onlyIf(node.markers.includes(marker), {
|
|
227
|
+
line: position.start.line,
|
|
228
|
+
char: position.start.column,
|
|
229
|
+
length: position.end.column - position.start.column,
|
|
230
|
+
type: "notation",
|
|
231
|
+
name
|
|
232
|
+
}));
|
|
233
|
+
const nodeTokens = (page) => (node) => {
|
|
234
|
+
const lineOf = (position) => position.start.line + page.offset;
|
|
235
|
+
const shift = (position) => ({
|
|
236
|
+
start: {
|
|
237
|
+
...position.start,
|
|
238
|
+
line: position.start.line + page.offset
|
|
239
|
+
},
|
|
240
|
+
end: {
|
|
241
|
+
...position.end,
|
|
242
|
+
line: position.end.line + page.offset
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
return Match.value(node).pipe(Match.discriminators("type")({
|
|
246
|
+
title: (title) => leaf(pipe(componentLine(page, lineOf(title.position)), Option.match({
|
|
247
|
+
onNone: () => [spanToken("title", shift(title.position))],
|
|
248
|
+
onSome: (text) => componentTokens(lineOf(title.position), text)
|
|
249
|
+
}))),
|
|
250
|
+
codeBlock: (block) => leaf(lineSpans("codeBlock", page.lines, lineOf(block.position), block.position.end.line + page.offset)),
|
|
251
|
+
table: (table) => leaf(lineSpans("table", page.lines, lineOf(table.position), table.position.end.line + page.offset)),
|
|
252
|
+
line: (line) => pipe(componentLine(page, lineOf(line.position)), Option.match({
|
|
253
|
+
onSome: (text) => leaf(componentTokens(lineOf(line.position), text)),
|
|
254
|
+
onNone: () => branch(Option.toArray(quoteToken(page, line, lineOf(line.position))))
|
|
255
|
+
})),
|
|
256
|
+
decoration: (decoration) => branch([...decorationTokenTypes(decoration).map((type) => spanToken(type, shift(decoration.position))), ...notationTokens(page.notations, decoration, shift(decoration.position))])
|
|
257
|
+
}), Match.orElse((other) => branch(pipe(Option.fromNullable(INLINE_TOKEN_TYPE[other.type]), Option.map((type) => spanToken(type, shift(other.position))), Option.toArray))));
|
|
258
|
+
};
|
|
259
|
+
/** Pure text -> tokens. The LSP delta encoding lives in encodeTokens, so this stays testable. */
|
|
260
|
+
const computeTokens = (text, options = {}) => {
|
|
261
|
+
const lines = normalizeLineEndings(text).split("\n");
|
|
262
|
+
const notations = options.notations ?? [];
|
|
263
|
+
const fence = fenceOf(lines, options.frontmatter ?? true);
|
|
264
|
+
const page = {
|
|
265
|
+
lines,
|
|
266
|
+
offset: Option.match(fence, {
|
|
267
|
+
onNone: () => 0,
|
|
268
|
+
onSome: (end) => end + 1
|
|
269
|
+
}),
|
|
270
|
+
components: options.components ?? false,
|
|
271
|
+
notations
|
|
272
|
+
};
|
|
273
|
+
const frontmatterTokens = Option.match(fence, {
|
|
274
|
+
onNone: () => [],
|
|
275
|
+
onSome: (end) => lineSpans("frontmatter", lines, 0, end)
|
|
276
|
+
});
|
|
277
|
+
const body = parse(lines.slice(page.offset).join("\n"), withNotations(options.parseOptions ?? {}, notations));
|
|
278
|
+
return pipe([...frontmatterTokens, ...gather(body, nodeTokens(page))], Array.filter((t) => t.length > 0), Array.sort(BY_POSITION));
|
|
279
|
+
};
|
|
280
|
+
/**
|
|
281
|
+
* Each token's place in `legend`: a notation's own name when the legend has it, else the
|
|
282
|
+
* LSP type its kind is drawn as.
|
|
283
|
+
*/
|
|
284
|
+
const legendIndex = (legend) => {
|
|
285
|
+
const placeOf = (type) => Option.liftPredicate(legend.indexOf(type), (index) => index >= 0);
|
|
286
|
+
const drawnAs = (token) => Option.getOrElse(placeOf(LSP_TYPE[token.type]), () => 0);
|
|
287
|
+
return (token) => Match.value(token).pipe(Match.when({ type: "notation" }, (notation) => Option.getOrElse(placeOf(notation.name), () => drawnAs(notation))), Match.orElse(drawnAs));
|
|
288
|
+
};
|
|
289
|
+
/**
|
|
290
|
+
* LSP relative encoding: [deltaLine, deltaStartChar, length, tokenType, tokenModifiers]*.
|
|
291
|
+
* `legend` is the one sent at initialize: LEGEND, or `legendOf(notations)`.
|
|
292
|
+
*/
|
|
293
|
+
const encodeTokens = (tokens, legend = LEGEND) => {
|
|
294
|
+
const indexOf = legendIndex(legend);
|
|
295
|
+
return pipe(Array.sort(tokens, BY_POSITION), Array.mapAccum({
|
|
296
|
+
line: 0,
|
|
297
|
+
char: 0
|
|
298
|
+
}, (previous, t) => [{
|
|
299
|
+
line: t.line,
|
|
300
|
+
char: t.char
|
|
301
|
+
}, [
|
|
302
|
+
t.line - previous.line,
|
|
303
|
+
t.line === previous.line ? t.char - previous.char : t.char,
|
|
304
|
+
t.length,
|
|
305
|
+
indexOf(t),
|
|
306
|
+
0
|
|
307
|
+
]]), ([, rows]) => Array.flatten(rows));
|
|
308
|
+
};
|
|
309
|
+
//#endregion
|
|
310
|
+
export { encodeTokens as a, legendOf as c, gather as d, leaf as f, computeTokens as i, withNotations as l, LEGEND as n, fenceOf as o, TOKEN_TYPES as r, frontmatterEnd as s, COMPONENT_LINE as t, branch as u };
|
|
311
|
+
|
|
312
|
+
//# sourceMappingURL=tokens-D0NP_uLo.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokens-D0NP_uLo.mjs","names":["Arr"],"sources":["../src/tree.ts","../src/tokens.ts"],"sourcesContent":["import type { AnyNode } from \"@cosense-toolbox/parser\"\nimport { visit } from \"@cosense-toolbox/parser/utils\"\n\n/** What one node gives, and whether its children are read too. */\nexport interface Picked<A> {\n readonly items: ReadonlyArray<A>\n readonly descend: boolean\n}\n\nexport const leaf = <A>(items: ReadonlyArray<A>): Picked<A> => ({ items, descend: false })\n\nexport const branch = <A>(items: ReadonlyArray<A>): Picked<A> => ({ items, descend: true })\n\n/**\n * Everything `pick` takes from `tree`, in document order.\n *\n * The parser's `visit` hands nodes to a callback, so something has to hold what the callback\n * finds. That is kept to here: `pick` stays a pure function of one node.\n */\nexport const gather = <A>(tree: AnyNode, pick: (node: AnyNode) => Picked<A>): ReadonlyArray<A> => {\n const found: A[] = []\n visit(tree, (node) => {\n const { items, descend } = pick(node)\n found.push(...items)\n return descend ? undefined : \"skip\"\n })\n return found\n}\n","import type {\n AnyNode,\n AnyNodeType,\n Decoration,\n NodeOfType,\n ParseOptions,\n Position,\n} from \"@cosense-toolbox/parser\"\nimport { normalizeLineEndings, parse } from \"@cosense-toolbox/parser\"\nimport { customDecorations } from \"@cosense-toolbox/parser/extensions\"\nimport { Array as Arr, Match, Option, Order, pipe } from \"effect\"\n\nimport { branch, gather, leaf, type Picked } from \"./tree\"\n\n/**\n * Cosense notation as LSP semantic tokens.\n *\n * These names are the ones this package reasons in, and they are not what goes on the wire.\n * A client announces the token types it knows at initialize and drops everything else —\n * some send the LSP's own list and nothing more — so `quote` would simply never be drawn.\n * LEGEND below is what is sent, and LSP_TYPE is the translation.\n */\nexport const TOKEN_TYPES = [\n \"title\",\n \"link\",\n \"projectLink\",\n \"externalLink\",\n \"hashtag\",\n \"code\",\n \"codeBlock\",\n \"formula\",\n \"icon\",\n \"quote\",\n \"bold\",\n \"italic\",\n \"strike\",\n \"underline\",\n \"image\",\n \"table\",\n \"bold2\",\n \"bold3\",\n // `.csnx` only: a line that opens with a component tag. The tag's name, its attribute\n // names, and the two kinds of value, as JSX reads them.\n \"component\",\n \"attribute\",\n \"attributeValue\",\n \"expression\",\n // The YAML at the top of the file, which is not Cosense notation at all.\n \"frontmatter\",\n // A notation the caller defines (see `Notation`), such as `[! 注意]`. The token carries\n // the name the caller gave it.\n \"notation\",\n] as const\n\nexport type TokenType = (typeof TOKEN_TYPES)[number]\n\n/** Every token type but `notation`, whose tokens also carry a name. */\ntype BuiltinTokenType = Exclude<TokenType, \"notation\">\n\ninterface Span {\n readonly line: number\n readonly char: number\n readonly length: number\n}\n\nexport type RawToken =\n | (Span & { readonly type: BuiltinTokenType })\n | (Span & { readonly type: \"notation\"; readonly name: string })\n\n/**\n * A notation the caller adds to Cosense's: a marker that opens a decoration, as `!` opens\n * `[! 注意]`, and the token type it is sent as. The marker is also taught to the parser, so\n * the bracket is not read as a link.\n */\nexport interface Notation {\n readonly marker: string\n readonly name: string\n}\n\n/**\n * The legend sent to the client: the LSP's own token types, in its own order.\n *\n * Every entry is in the set a client is expected to understand, which is what makes the\n * colours appear without the reader configuring anything.\n */\nexport const LEGEND = [\n \"namespace\",\n \"type\",\n \"function\",\n \"variable\",\n \"property\",\n \"parameter\",\n \"decorator\",\n \"macro\",\n \"keyword\",\n \"string\",\n \"number\",\n \"comment\",\n] as const\n\n/**\n * What each notation is drawn as. Chosen so that things a reader separates by eye land on\n * different entries: a link is not a tag, and a quote is not a heading. `bold` and its\n * louder siblings share one entry, because the LSP has no notion of weight and a theme\n * gives each type a single colour.\n */\nconst LSP_TYPE: Record<TokenType, (typeof LEGEND)[number]> = {\n title: \"namespace\",\n link: \"function\",\n projectLink: \"function\",\n externalLink: \"string\",\n icon: \"function\",\n image: \"string\",\n hashtag: \"decorator\",\n code: \"string\",\n codeBlock: \"string\",\n formula: \"number\",\n table: \"property\",\n quote: \"comment\",\n frontmatter: \"comment\",\n component: \"type\",\n attribute: \"parameter\",\n attributeValue: \"string\",\n expression: \"variable\",\n bold: \"keyword\",\n bold2: \"keyword\",\n bold3: \"keyword\",\n italic: \"macro\",\n strike: \"comment\",\n underline: \"variable\",\n // What a caller's notation is sent as when the legend has no type of its name.\n notation: \"decorator\",\n}\n\n/**\n * LEGEND with the names of `notations` after it, each once: the legend for a client that\n * accepts token types of the caller's own naming. The LSP's types keep their places.\n */\nexport const legendOf = (notations: ReadonlyArray<Notation>): ReadonlyArray<string> =>\n Arr.dedupe([...LEGEND, ...Arr.map(notations, (notation) => notation.name)])\n\n/** `parseOptions` that also read the markers of `notations` as decorations. */\nexport const withNotations = (\n parseOptions: ParseOptions,\n notations: ReadonlyArray<Notation>,\n): ParseOptions =>\n Arr.match(notations, {\n onEmpty: () => parseOptions,\n onNonEmpty: (added): ParseOptions => ({\n ...parseOptions,\n extensions: [\n ...(parseOptions.extensions ?? []),\n customDecorations(Arr.map(added, (notation) => notation.marker)),\n ],\n }),\n })\n\n/** Leaf inline nodes that map 1:1. Each sits on one line, so no splitting is needed. */\nconst INLINE_TOKEN_TYPE: Partial<Record<AnyNodeType, BuiltinTokenType>> = {\n internalLink: \"link\",\n externalLink: \"externalLink\",\n projectLink: \"projectLink\",\n hashtag: \"hashtag\",\n inlineCode: \"code\",\n image: \"image\",\n icon: \"icon\",\n formula: \"formula\",\n}\n\n/** Top to bottom, then left to right: the order the LSP encoding needs. */\nconst BY_POSITION: Order.Order<RawToken> = Order.combine(\n Order.mapInput(Order.number, (token: RawToken) => token.line),\n Order.mapInput(Order.number, (token: RawToken) => token.char),\n)\n\nconst onlyIf = <A>(condition: boolean, value: A): Option.Option<A> =>\n condition ? Option.some(value) : Option.none()\n\n/** A token over `[from, to)` of one line. */\nconst token = (type: BuiltinTokenType, line: number, from: number, to: number): RawToken => ({\n line,\n char: from,\n length: to - from,\n type,\n})\n\nconst spanToken = (type: BuiltinTokenType, position: Position): RawToken =>\n token(type, position.start.line, position.start.column, position.end.column)\n\n/** How many characters from `from` the `^`-anchored `pattern` takes; 0 when it takes none. */\nconst matchLength = (pattern: RegExp, text: string, from: number): number =>\n pipe(\n Option.fromNullable(pattern.exec(text.slice(from))),\n Option.match({ onNone: () => 0, onSome: ([matched]) => matched.length }),\n )\n\n// --- Emphasis ---------------------------------------------------------------------------\n\n/** Cosense sizes emphasis by asterisk count (`[*]`..`[*****]`); sizeLevel is 0-indexed. */\nconst boldTokenType = (sizeLevel: number): BuiltinTokenType =>\n Match.value(sizeLevel).pipe(\n Match.when(\n (level) => level >= 2,\n (): BuiltinTokenType => \"bold3\",\n ),\n Match.when(1, (): BuiltinTokenType => \"bold2\"),\n Match.orElse((): BuiltinTokenType => \"bold\"),\n )\n\n/** Every decoration a run carries, since Cosense applies all of them: `[-* x]` is both. */\nconst decorationTokenTypes = (node: Decoration): ReadonlyArray<BuiltinTokenType> =>\n Arr.getSomes([\n onlyIf(node.underline, \"underline\" as const),\n onlyIf(node.strike, \"strike\" as const),\n onlyIf(node.italic, \"italic\" as const),\n onlyIf(node.bold, boldTokenType(node.sizeLevel)),\n ])\n\n// --- Component lines (`.csnx`) ----------------------------------------------------------\n\nexport const COMPONENT_LINE = /^\\s*<\\/?[A-Z][A-Za-z0-9_.]*(\\s|\\/?>)/\nconst TAG_OPEN = /^(\\s*<\\/?)([A-Z][A-Za-z0-9_.]*)/\nconst ATTRIBUTE_NAME = /^[A-Za-z_:][A-Za-z0-9_:.-]*/\nconst SPACE = /^\\s*/\n\n/** Where a quoted value opened at `from` ends: after its closing quote, or at the line's end. */\nconst quotedEnd = (text: string, from: number): number =>\n pipe(\n text.indexOf(text.charAt(from), from + 1),\n Option.liftPredicate((close) => close >= 0),\n Option.match({ onNone: () => text.length, onSome: (close) => close + 1 }),\n )\n\n/** Where `{ ... }` opened at `from` ends, counting nested braces: after its `}`, or the line's end. */\nconst bracedEnd = (text: string, from: number): number =>\n pipe(\n text.slice(from).split(\"\"),\n Arr.scan(0, (depth, char) => (char === \"{\" ? depth + 1 : char === \"}\" ? depth - 1 : depth)),\n // scan starts with the depth before any character; the rest line up with the characters.\n Arr.drop(1),\n Arr.findFirstIndex((depth) => depth === 0),\n Option.match({ onNone: () => text.length, onSome: (index) => from + index + 1 }),\n )\n\ntype ValueType = \"attributeValue\" | \"expression\"\n\n/** The attribute value that starts at `from`: its type and where it ends. */\nconst valueAt = (text: string, from: number): Option.Option<readonly [ValueType, number]> =>\n Match.value(text.charAt(from)).pipe(\n Match.when(\n (char) => char === '\"' || char === \"'\",\n () => Option.some([\"attributeValue\", quotedEnd(text, from)] as const),\n ),\n Match.when(\"{\", () => Option.some([\"expression\", bracedEnd(text, from)] as const)),\n Match.orElse(() => Option.none()),\n )\n\n/** The tokens of one attribute, and where the next may start. */\ntype Step = readonly [ReadonlyArray<RawToken>, number]\n\n/** `at` past any whitespace. */\nconst skipSpace = (text: string, at: number): number => at + matchLength(SPACE, text, at)\n\n/** Whether the tag closes at `at`, with `>` or `/>`, or the line ends there. */\nconst closesAt = (text: string, at: number): boolean =>\n at >= text.length || text.startsWith(\">\", at) || text.startsWith(\"/>\", at)\n\n/**\n * An attribute named over `[at, nameEnd)`: the name, then `=` and a value if one follows.\n * A name with no `=` after it stands alone, as JSX reads `<Dialog open>`.\n */\nconst attribute = (line: number, text: string, at: number, nameEnd: number): Step => {\n const name = token(\"attribute\", line, at, nameEnd)\n const afterName = skipSpace(text, nameEnd)\n return pipe(\n Option.liftPredicate(afterName, (equals) => text.charAt(equals) === \"=\"),\n Option.map((equals) => skipSpace(text, equals + 1)),\n Option.match({\n onNone: (): Step => [[name], afterName],\n onSome: (valueStart): Step =>\n pipe(\n valueAt(text, valueStart),\n Option.match({\n onNone: (): Step => [[name], valueStart],\n onSome: ([type, end]): Step => [[name, token(type, line, valueStart, end)], end],\n }),\n ),\n }),\n )\n}\n\n/** One attribute from `from`, or None once the tag has closed. */\nconst attributeStep =\n (line: number, text: string) =>\n (from: number): Option.Option<Step> =>\n pipe(\n Option.liftPredicate(skipSpace(text, from), (at) => !closesAt(text, at)),\n Option.map((at) =>\n pipe(\n Option.liftPredicate(at + matchLength(ATTRIBUTE_NAME, text, at), (end) => end > at),\n Option.match({\n // Not a name: step over the character, since the tag may still go on after it.\n onNone: (): Step => [[], at + 1],\n onSome: (nameEnd) => attribute(line, text, at, nameEnd),\n }),\n ),\n ),\n )\n\n/**\n * The tag that opens a component line, read as JSX: its name, then each attribute and its\n * value, up to the `>` that closes it. Text after the tag is left alone; it is neither\n * Cosense notation nor part of the tag.\n */\nconst componentTokens = (line: number, text: string): ReadonlyArray<RawToken> =>\n pipe(\n Option.fromNullable(TAG_OPEN.exec(text)),\n Option.map(([head, lead = \"\", name = \"\"]) => [\n token(\"component\", line, lead.length, lead.length + name.length),\n ...Arr.flatten(Arr.unfold(head.length, attributeStep(line, text))),\n ]),\n Option.getOrElse(() => []),\n )\n\n// --- The page ---------------------------------------------------------------------------\n\n/** The last line of the `---` fence around YAML at the very top, if the file opens with one. */\nexport const frontmatterEnd = (lines: ReadonlyArray<string>): Option.Option<number> =>\n pipe(\n Option.liftPredicate(lines, (all) => all[0]?.trim() === \"---\"),\n Option.flatMap(Arr.findFirstIndex((line, index) => index > 0 && line.trim() === \"---\")),\n )\n\n/**\n * `frontmatterEnd`, or None when the caller reads no frontmatter: a Cosense page has none,\n * and a page whose title is `---` would otherwise lose its first lines to it.\n */\nexport const fenceOf = (\n lines: ReadonlyArray<string>,\n frontmatter: boolean,\n): Option.Option<number> => Option.flatMap(onlyIf(frontmatter, lines), frontmatterEnd)\n\n/** What the node visitor needs to know about the file around the parsed body. */\ninterface Page {\n readonly lines: ReadonlyArray<string>\n /** Lines before the body: the frontmatter, which the parser never sees. */\n readonly offset: number\n readonly components: boolean\n readonly notations: ReadonlyArray<Notation>\n}\n\n/** Each non-empty line from `first` to `last`, whole. */\nconst lineSpans = (\n type: BuiltinTokenType,\n lines: ReadonlyArray<string>,\n first: number,\n last: number,\n): ReadonlyArray<RawToken> =>\n Arr.filterMap(Arr.range(first, last), (line) =>\n pipe(\n Option.fromNullable(lines[line]),\n Option.filter((text) => text.length > 0),\n Option.map((text) => token(type, line, 0, text.length)),\n ),\n )\n\n/** The line's text, when the file reads components and the line opens with a tag. */\nconst componentLine = (page: Page, line: number): Option.Option<string> =>\n pipe(\n Option.fromNullable(page.lines[line]),\n Option.filter((text) => page.components && COMPONENT_LINE.test(text)),\n )\n\n/** The `>` and spaces that open a quote line, from its indentation onwards. */\nconst quoteToken = (page: Page, node: NodeOfType<\"line\">, line: number): Option.Option<RawToken> =>\n pipe(\n onlyIf(node.quote, page.lines[line] ?? \"\"),\n Option.map((text) => matchLength(/^[> ]*/, text, node.indent)),\n Option.filter((length) => length > 0),\n Option.map((length) => token(\"quote\", line, node.indent, node.indent + length)),\n )\n\n/** A token for each of `notations` whose marker opens the decoration. */\nconst notationTokens = (\n notations: ReadonlyArray<Notation>,\n node: Decoration,\n position: Position,\n): ReadonlyArray<RawToken> =>\n Arr.filterMap(notations, ({ marker, name }) =>\n onlyIf(node.markers.includes(marker), {\n line: position.start.line,\n char: position.start.column,\n length: position.end.column - position.start.column,\n type: \"notation\" as const,\n name,\n }),\n )\n\nconst nodeTokens =\n (page: Page) =>\n (node: AnyNode): Picked<RawToken> => {\n const lineOf = (position: Position) => position.start.line + page.offset\n const shift = (position: Position): Position => ({\n start: { ...position.start, line: position.start.line + page.offset },\n end: { ...position.end, line: position.end.line + page.offset },\n })\n return Match.value(node).pipe(\n Match.discriminators(\"type\")({\n // A page that opens with a component has no title line: the tag is what it is.\n title: (title) =>\n leaf(\n pipe(\n componentLine(page, lineOf(title.position)),\n Option.match({\n onNone: () => [spanToken(\"title\", shift(title.position))],\n onSome: (text) => componentTokens(lineOf(title.position), text),\n }),\n ),\n ),\n codeBlock: (block) =>\n leaf(\n lineSpans(\n \"codeBlock\",\n page.lines,\n lineOf(block.position),\n block.position.end.line + page.offset,\n ),\n ),\n table: (table) =>\n leaf(\n lineSpans(\n \"table\",\n page.lines,\n lineOf(table.position),\n table.position.end.line + page.offset,\n ),\n ),\n // A component tag owns its line, so nothing on it is Cosense notation.\n line: (line) =>\n pipe(\n componentLine(page, lineOf(line.position)),\n Option.match({\n onSome: (text) => leaf(componentTokens(lineOf(line.position), text)),\n onNone: () => branch(Option.toArray(quoteToken(page, line, lineOf(line.position)))),\n }),\n ),\n // Descend: a decoration can wrap a link or an image (`[* [page]]`), and that child\n // needs its own token over the same span.\n decoration: (decoration) =>\n branch([\n ...decorationTokenTypes(decoration).map((type) =>\n spanToken(type, shift(decoration.position)),\n ),\n ...notationTokens(page.notations, decoration, shift(decoration.position)),\n ]),\n }),\n Match.orElse((other) =>\n branch(\n pipe(\n Option.fromNullable(INLINE_TOKEN_TYPE[other.type]),\n Option.map((type) => spanToken(type, shift(other.position))),\n Option.toArray,\n ),\n ),\n ),\n )\n }\n\nexport interface ComputeTokensOptions {\n /** `.csnx` also reads a line that is one component tag. `.csn` never does. */\n readonly components?: boolean\n /** How to parse: the site's notation extensions, so `[! 注意]` is not read as a link. */\n readonly parseOptions?: ParseOptions\n /** Notations the caller adds, each sent as a token of its own name (see `Notation`). */\n readonly notations?: ReadonlyArray<Notation>\n /**\n * Whether a `---` fence on the first line opens YAML to skip (default: true). Off for\n * Cosense pages, which have no frontmatter.\n */\n readonly frontmatter?: boolean\n}\n\n/** Pure text -> tokens. The LSP delta encoding lives in encodeTokens, so this stays testable. */\nexport const computeTokens = (text: string, options: ComputeTokensOptions = {}): RawToken[] => {\n const lines = normalizeLineEndings(text).split(\"\\n\")\n const notations = options.notations ?? []\n // Frontmatter is YAML, so the Cosense parser must not see it: `---` would otherwise read\n // as a strikethrough marker and the keys as plain text on the title line.\n const fence = fenceOf(lines, options.frontmatter ?? true)\n const page: Page = {\n lines,\n offset: Option.match(fence, { onNone: () => 0, onSome: (end) => end + 1 }),\n components: options.components ?? false,\n notations,\n }\n\n const frontmatterTokens = Option.match(fence, {\n onNone: () => [],\n onSome: (end) => lineSpans(\"frontmatter\", lines, 0, end),\n })\n const body = parse(\n lines.slice(page.offset).join(\"\\n\"),\n withNotations(options.parseOptions ?? {}, notations),\n )\n\n return pipe(\n [...frontmatterTokens, ...gather(body, nodeTokens(page))],\n Arr.filter((t) => t.length > 0),\n Arr.sort(BY_POSITION),\n )\n}\n\n/**\n * Each token's place in `legend`: a notation's own name when the legend has it, else the\n * LSP type its kind is drawn as.\n */\nconst legendIndex = (legend: ReadonlyArray<string>) => {\n const placeOf = (type: string): Option.Option<number> =>\n Option.liftPredicate(legend.indexOf(type), (index) => index >= 0)\n const drawnAs = (token: RawToken) => Option.getOrElse(placeOf(LSP_TYPE[token.type]), () => 0)\n return (token: RawToken): number =>\n Match.value(token).pipe(\n Match.when({ type: \"notation\" }, (notation) =>\n Option.getOrElse(placeOf(notation.name), () => drawnAs(notation)),\n ),\n Match.orElse(drawnAs),\n )\n}\n\n/**\n * LSP relative encoding: [deltaLine, deltaStartChar, length, tokenType, tokenModifiers]*.\n * `legend` is the one sent at initialize: LEGEND, or `legendOf(notations)`.\n */\nexport const encodeTokens = (\n tokens: ReadonlyArray<RawToken>,\n legend: ReadonlyArray<string> = LEGEND,\n): number[] => {\n const indexOf = legendIndex(legend)\n return pipe(\n Arr.sort(tokens, BY_POSITION),\n Arr.mapAccum({ line: 0, char: 0 }, (previous, t) => [\n { line: t.line, char: t.char },\n [\n t.line - previous.line,\n t.line === previous.line ? t.char - previous.char : t.char,\n t.length,\n indexOf(t),\n 0,\n ],\n ]),\n ([, rows]) => Arr.flatten(rows),\n )\n}\n"],"mappings":";;;;;AASA,MAAa,QAAW,WAAwC;CAAE;CAAO,SAAS;AAAM;AAExF,MAAa,UAAa,WAAwC;CAAE;CAAO,SAAS;AAAK;;;;;;;AAQzF,MAAa,UAAa,MAAe,SAAyD;CAChG,MAAM,QAAa,CAAC;CACpB,MAAM,OAAO,SAAS;EACpB,MAAM,EAAE,OAAO,YAAY,KAAK,IAAI;EACpC,MAAM,KAAK,GAAG,KAAK;EACnB,OAAO,UAAU,KAAA,IAAY;CAC/B,CAAC;CACD,OAAO;AACT;;;;;;;;;;;ACLA,MAAa,cAAc;CACzB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CAGA;CACA;CACA;CACA;CAEA;CAGA;AACF;;;;;;;AAiCA,MAAa,SAAS;CACpB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;;;;;;;AAQA,MAAM,WAAuD;CAC3D,OAAO;CACP,MAAM;CACN,aAAa;CACb,cAAc;CACd,MAAM;CACN,OAAO;CACP,SAAS;CACT,MAAM;CACN,WAAW;CACX,SAAS;CACT,OAAO;CACP,OAAO;CACP,aAAa;CACb,WAAW;CACX,WAAW;CACX,gBAAgB;CAChB,YAAY;CACZ,MAAM;CACN,OAAO;CACP,OAAO;CACP,QAAQ;CACR,QAAQ;CACR,WAAW;CAEX,UAAU;AACZ;;;;;AAMA,MAAa,YAAY,cACvBA,MAAI,OAAO,CAAC,GAAG,QAAQ,GAAGA,MAAI,IAAI,YAAY,aAAa,SAAS,IAAI,CAAC,CAAC;;AAG5E,MAAa,iBACX,cACA,cAEAA,MAAI,MAAM,WAAW;CACnB,eAAe;CACf,aAAa,WAAyB;EACpC,GAAG;EACH,YAAY,CACV,GAAI,aAAa,cAAc,CA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S;EAClB,EAAE,SAAS,SAAS,OAAO,EAAE,OAAO,SAAS,OAAO,EAAE;EACtD,EAAE;EACF,QAAQ,CAAC;EACT;CACF,CACF,CAAC,IACA,GAAG,UAAUA,MAAI,QAAQ,IAAI,CAChC;AACF"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Option } from "effect";
|
|
2
|
+
import { ParseOptions } from "@cosense-toolbox/parser";
|
|
3
|
+
//#region src/tokens.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Cosense notation as LSP semantic tokens.
|
|
6
|
+
*
|
|
7
|
+
* These names are the ones this package reasons in, and they are not what goes on the wire.
|
|
8
|
+
* A client announces the token types it knows at initialize and drops everything else —
|
|
9
|
+
* some send the LSP's own list and nothing more — so `quote` would simply never be drawn.
|
|
10
|
+
* LEGEND below is what is sent, and LSP_TYPE is the translation.
|
|
11
|
+
*/
|
|
12
|
+
declare const TOKEN_TYPES: readonly ["title", "link", "projectLink", "externalLink", "hashtag", "code", "codeBlock", "formula", "icon", "quote", "bold", "italic", "strike", "underline", "image", "table", "bold2", "bold3", "component", "attribute", "attributeValue", "expression", "frontmatter", "notation"];
|
|
13
|
+
type TokenType = (typeof TOKEN_TYPES)[number];
|
|
14
|
+
/** Every token type but `notation`, whose tokens also carry a name. */
|
|
15
|
+
type BuiltinTokenType = Exclude<TokenType, "notation">;
|
|
16
|
+
interface Span {
|
|
17
|
+
readonly line: number;
|
|
18
|
+
readonly char: number;
|
|
19
|
+
readonly length: number;
|
|
20
|
+
}
|
|
21
|
+
type RawToken = (Span & {
|
|
22
|
+
readonly type: BuiltinTokenType;
|
|
23
|
+
}) | (Span & {
|
|
24
|
+
readonly type: "notation";
|
|
25
|
+
readonly name: string;
|
|
26
|
+
});
|
|
27
|
+
/**
|
|
28
|
+
* A notation the caller adds to Cosense's: a marker that opens a decoration, as `!` opens
|
|
29
|
+
* `[! 注意]`, and the token type it is sent as. The marker is also taught to the parser, so
|
|
30
|
+
* the bracket is not read as a link.
|
|
31
|
+
*/
|
|
32
|
+
interface Notation {
|
|
33
|
+
readonly marker: string;
|
|
34
|
+
readonly name: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The legend sent to the client: the LSP's own token types, in its own order.
|
|
38
|
+
*
|
|
39
|
+
* Every entry is in the set a client is expected to understand, which is what makes the
|
|
40
|
+
* colours appear without the reader configuring anything.
|
|
41
|
+
*/
|
|
42
|
+
declare const LEGEND: readonly ["namespace", "type", "function", "variable", "property", "parameter", "decorator", "macro", "keyword", "string", "number", "comment"];
|
|
43
|
+
/**
|
|
44
|
+
* LEGEND with the names of `notations` after it, each once: the legend for a client that
|
|
45
|
+
* accepts token types of the caller's own naming. The LSP's types keep their places.
|
|
46
|
+
*/
|
|
47
|
+
declare const legendOf: (notations: ReadonlyArray<Notation>) => ReadonlyArray<string>;
|
|
48
|
+
/** `parseOptions` that also read the markers of `notations` as decorations. */
|
|
49
|
+
declare const withNotations: (parseOptions: ParseOptions, notations: ReadonlyArray<Notation>) => ParseOptions;
|
|
50
|
+
declare const COMPONENT_LINE: RegExp;
|
|
51
|
+
/** The last line of the `---` fence around YAML at the very top, if the file opens with one. */
|
|
52
|
+
declare const frontmatterEnd: (lines: ReadonlyArray<string>) => Option.Option<number>;
|
|
53
|
+
/**
|
|
54
|
+
* `frontmatterEnd`, or None when the caller reads no frontmatter: a Cosense page has none,
|
|
55
|
+
* and a page whose title is `---` would otherwise lose its first lines to it.
|
|
56
|
+
*/
|
|
57
|
+
declare const fenceOf: (lines: ReadonlyArray<string>, frontmatter: boolean) => Option.Option<number>;
|
|
58
|
+
interface ComputeTokensOptions {
|
|
59
|
+
/** `.csnx` also reads a line that is one component tag. `.csn` never does. */
|
|
60
|
+
readonly components?: boolean;
|
|
61
|
+
/** How to parse: the site's notation extensions, so `[! 注意]` is not read as a link. */
|
|
62
|
+
readonly parseOptions?: ParseOptions;
|
|
63
|
+
/** Notations the caller adds, each sent as a token of its own name (see `Notation`). */
|
|
64
|
+
readonly notations?: ReadonlyArray<Notation>;
|
|
65
|
+
/**
|
|
66
|
+
* Whether a `---` fence on the first line opens YAML to skip (default: true). Off for
|
|
67
|
+
* Cosense pages, which have no frontmatter.
|
|
68
|
+
*/
|
|
69
|
+
readonly frontmatter?: boolean;
|
|
70
|
+
}
|
|
71
|
+
/** Pure text -> tokens. The LSP delta encoding lives in encodeTokens, so this stays testable. */
|
|
72
|
+
declare const computeTokens: (text: string, options?: ComputeTokensOptions) => RawToken[];
|
|
73
|
+
/**
|
|
74
|
+
* LSP relative encoding: [deltaLine, deltaStartChar, length, tokenType, tokenModifiers]*.
|
|
75
|
+
* `legend` is the one sent at initialize: LEGEND, or `legendOf(notations)`.
|
|
76
|
+
*/
|
|
77
|
+
declare const encodeTokens: (tokens: ReadonlyArray<RawToken>, legend?: ReadonlyArray<string>) => number[];
|
|
78
|
+
//#endregion
|
|
79
|
+
export { COMPONENT_LINE, ComputeTokensOptions, LEGEND, Notation, RawToken, TOKEN_TYPES, TokenType, computeTokens, encodeTokens, fenceOf, frontmatterEnd, legendOf, withNotations };
|
|
80
|
+
//# sourceMappingURL=tokens.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokens.d.mts","names":[],"sources":["../src/tokens.ts"],"mappings":";;;;;;;;;;;cAsBa;KAgCD,oBAAoB;;KAG3B,mBAAmB,QAAQ;UAEtB;WACC;WACA;WACA;;KAGC,YACP;WAAkB,MAAM;MACxB;WAAkB;WAA2B;;;;;;;UAOjC;WACN;WACA;;;;;;;;cASE;;;;;cAqDA,WAAY,WAAW,cAAc,cAAY;;cAIjD,gBACX,cAAc,cACd,WAAW,cAAc,cACxB;cA2EU,gBAAc;;cA2Gd,iBAAkB,OAAO,0BAAwB,OAAO;;;;;cAUxD,UACX,OAAO,uBACP,yBACC,OAAO;UAgIO;;WAEN;;WAEA,eAAe;;WAEf,YAAY,cAAc;;;;;WAK1B;;;cAIE,gBAAiB,cAAc,UAAS,yBAA4B;;;;;cAkDpE,eACX,QAAQ,cAAc,WACtB,SAAQ"}
|
package/dist/tokens.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as encodeTokens, c as legendOf, i as computeTokens, l as withNotations, n as LEGEND, o as fenceOf, r as TOKEN_TYPES, s as frontmatterEnd, t as COMPONENT_LINE } from "./tokens-D0NP_uLo.mjs";
|
|
2
|
+
export { COMPONENT_LINE, LEGEND, TOKEN_TYPES, computeTokens, encodeTokens, fenceOf, frontmatterEnd, legendOf, withNotations };
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { d as gather, f as leaf, o as fenceOf, t as COMPONENT_LINE, u as branch } from "./tokens-D0NP_uLo.mjs";
|
|
2
|
+
import { normalizeForMatch } from "./completion.mjs";
|
|
3
|
+
import { Array, Effect, Match, Option, Record, Schema, pipe } from "effect";
|
|
4
|
+
import { normalizeLineEndings, parse } from "@cosense-toolbox/parser";
|
|
5
|
+
import { customDecorations } from "@cosense-toolbox/parser/extensions";
|
|
6
|
+
import { DiagnosticSeverity } from "vscode-languageserver/node";
|
|
7
|
+
import { basename, extname, join, relative, resolve, sep } from "node:path";
|
|
8
|
+
import { readFile, readdir } from "node:fs/promises";
|
|
9
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
10
|
+
//#region src/diagnostics.ts
|
|
11
|
+
/**
|
|
12
|
+
* Links to pages no file holds.
|
|
13
|
+
*
|
|
14
|
+
* Only `[title]` links are checked. A tag names a tag page more often than a page, and a
|
|
15
|
+
* `[/project/page]` link points outside the workspace; neither is expected to have a file.
|
|
16
|
+
*/
|
|
17
|
+
/** How loudly an unresolved link is reported, as the reader sets it. */
|
|
18
|
+
const UnresolvedSeverity = Schema.Literal("off", "hint", "information", "warning", "error");
|
|
19
|
+
/**
|
|
20
|
+
* The severity a setting names, or `warning` for one that is missing or unknown: the build
|
|
21
|
+
* warns about the same links by default (`unresolved: "warn"`).
|
|
22
|
+
*/
|
|
23
|
+
const severityOf = (setting) => Option.getOrElse(Schema.decodeUnknownOption(UnresolvedSeverity)(setting), () => "warning");
|
|
24
|
+
/** The LSP's severity for a setting, or None for `off`. */
|
|
25
|
+
const lspSeverity = (severity) => Match.value(severity).pipe(Match.when("off", () => Option.none()), Match.when("hint", () => Option.some(DiagnosticSeverity.Hint)), Match.when("information", () => Option.some(DiagnosticSeverity.Information)), Match.when("warning", () => Option.some(DiagnosticSeverity.Warning)), Match.when("error", () => Option.some(DiagnosticSeverity.Error)), Match.exhaustive);
|
|
26
|
+
/**
|
|
27
|
+
* Every `[title]` link in the body, with the line it is on in the file. The title line holds
|
|
28
|
+
* no links, and neither does a `.csnx` component line.
|
|
29
|
+
*/
|
|
30
|
+
const linksIn = (text, { components = false, parseOptions = {}, frontmatter = true }) => {
|
|
31
|
+
const lines = normalizeLineEndings(text).split("\n");
|
|
32
|
+
const offset = Option.match(fenceOf(lines, frontmatter), {
|
|
33
|
+
onNone: () => 0,
|
|
34
|
+
onSome: (end) => end + 1
|
|
35
|
+
});
|
|
36
|
+
const isComponentLine = (line) => components && COMPONENT_LINE.test(lines[line + offset] ?? "");
|
|
37
|
+
return gather(parse(lines.slice(offset).join("\n"), parseOptions), (node) => Match.value(node).pipe(Match.when({ type: "title" }, () => leaf([])), Match.when({ type: "line" }, (line) => isComponentLine(line.position.start.line) ? leaf([]) : branch([])), Match.when({ type: "internalLink" }, (link) => branch([{
|
|
38
|
+
link,
|
|
39
|
+
line: link.position.start.line + offset
|
|
40
|
+
}])), Match.orElse(() => branch([]))));
|
|
41
|
+
};
|
|
42
|
+
/** A diagnostic for each `[title]` link in `text` whose page is not in `index`. */
|
|
43
|
+
const unresolvedLinkDiagnostics = (index, text, options) => {
|
|
44
|
+
const withFile = new Set(Array.map(index.pages, (page) => normalizeForMatch(page.title)));
|
|
45
|
+
return pipe(lspSeverity(options.severity), Option.match({
|
|
46
|
+
onNone: () => [],
|
|
47
|
+
onSome: (severity) => pipe(linksIn(text, options), Array.filter(({ link }) => !withFile.has(normalizeForMatch(link.target))), Array.map(({ link, line }) => ({
|
|
48
|
+
range: {
|
|
49
|
+
start: {
|
|
50
|
+
line,
|
|
51
|
+
character: link.position.start.column
|
|
52
|
+
},
|
|
53
|
+
end: {
|
|
54
|
+
line,
|
|
55
|
+
character: link.position.end.column
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
severity,
|
|
59
|
+
source: "cosense",
|
|
60
|
+
code: "unresolved-link",
|
|
61
|
+
message: `リンク先のページが見つからない: [${link.target}]`
|
|
62
|
+
})))
|
|
63
|
+
}));
|
|
64
|
+
};
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region src/settings.ts
|
|
67
|
+
const defaultSettings = {
|
|
68
|
+
sources: [],
|
|
69
|
+
decorations: [],
|
|
70
|
+
unresolvedLinks: "warning",
|
|
71
|
+
frontmatter: true
|
|
72
|
+
};
|
|
73
|
+
/** `schema`'s reading of `value`, or `fallback` when `value` is not of that shape. */
|
|
74
|
+
const decodeOr = (schema, fallback) => (value) => Option.getOrElse(Schema.decodeUnknownOption(schema)(value), () => fallback);
|
|
75
|
+
/** The options as an object, which the editor may send in any shape or not at all. */
|
|
76
|
+
const OptionsObject = Schema.Record({
|
|
77
|
+
key: Schema.String,
|
|
78
|
+
value: Schema.Unknown
|
|
79
|
+
});
|
|
80
|
+
/**
|
|
81
|
+
* The non-empty strings of a list. Anything else in it is dropped rather than failing the
|
|
82
|
+
* whole list, and anything but a list counts as none.
|
|
83
|
+
*/
|
|
84
|
+
const stringsOf = (value) => Array.filter(decodeOr(Schema.Array(Schema.Unknown), [])(value), Schema.is(Schema.NonEmptyString));
|
|
85
|
+
/**
|
|
86
|
+
* Each setting read on its own, so one of the wrong shape falls back to its default
|
|
87
|
+
* without taking the others with it.
|
|
88
|
+
*/
|
|
89
|
+
const settingsOf = (options) => {
|
|
90
|
+
const field = (key) => pipe(Schema.decodeUnknownOption(OptionsObject)(options), Option.flatMap(Record.get(key)), Option.getOrUndefined);
|
|
91
|
+
return {
|
|
92
|
+
sources: stringsOf(field("sources")),
|
|
93
|
+
decorations: stringsOf(field("decorations")),
|
|
94
|
+
unresolvedLinks: severityOf(field("unresolvedLinks")),
|
|
95
|
+
frontmatter: decodeOr(Schema.Boolean, defaultSettings.frontmatter)(field("frontmatter"))
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
/** How the pages are parsed, so the server reads notation as the site's build does. */
|
|
99
|
+
const parseOptionsOf = ({ decorations }) => Array.match(decorations, {
|
|
100
|
+
onEmpty: () => ({}),
|
|
101
|
+
onNonEmpty: (markers) => ({ extensions: [customDecorations(markers)] })
|
|
102
|
+
});
|
|
103
|
+
//#endregion
|
|
104
|
+
//#region src/workspace.ts
|
|
105
|
+
const emptyIndex = { pages: [] };
|
|
106
|
+
const PAGE_EXTENSIONS = [".csn", ".csnx"];
|
|
107
|
+
const SKIP_DIRECTORIES = [
|
|
108
|
+
"node_modules",
|
|
109
|
+
".git",
|
|
110
|
+
"dist",
|
|
111
|
+
"build",
|
|
112
|
+
".next",
|
|
113
|
+
".astro",
|
|
114
|
+
".turbo",
|
|
115
|
+
".cache"
|
|
116
|
+
];
|
|
117
|
+
/** A directory's entries. One that cannot be read holds nothing, rather than failing the walk. */
|
|
118
|
+
const entriesOf = (directory) => pipe(Effect.tryPromise(() => readdir(directory, { withFileTypes: true })), Effect.orElseSucceed(() => []));
|
|
119
|
+
/** The page files one entry of `directory` holds: itself, what is under it, or none. */
|
|
120
|
+
const filesAt = (directory, entry) => Match.value(entry).pipe(Match.when((dir) => dir.isDirectory() && Array.contains(SKIP_DIRECTORIES, dir.name), () => Effect.succeed([])), Match.when((dir) => dir.isDirectory(), () => pageFiles(join(directory, entry.name))), Match.when((file) => Array.contains(PAGE_EXTENSIONS, extname(file.name)), () => Effect.succeed([join(directory, entry.name)])), Match.orElse(() => Effect.succeed([])));
|
|
121
|
+
/** Every page file under `directory`, depth-first, in the order the directory lists them. */
|
|
122
|
+
const pageFiles = (directory) => pipe(entriesOf(directory), Effect.map(Array.filter((entry) => !entry.name.startsWith("."))), Effect.flatMap(Effect.forEach((entry) => filesAt(directory, entry))), Effect.map(Array.flatten));
|
|
123
|
+
/** `text` past the leading `---` fence, which is YAML rather than a line of the page. */
|
|
124
|
+
const withoutFrontmatter = (text) => pipe(Option.liftPredicate(text, (all) => all.startsWith("---\n")), Option.flatMap((all) => pipe(all.indexOf("\n---", 3), Option.liftPredicate((end) => end !== -1), Option.map((end) => all.slice(end + 4)))), Option.getOrElse(() => text));
|
|
125
|
+
const firstLine = (text) => pipe(text.split("\n"), Array.map((line) => line.trim()), Array.findFirst((line) => line !== ""));
|
|
126
|
+
/**
|
|
127
|
+
* What the file calls itself: its first line.
|
|
128
|
+
*
|
|
129
|
+
* The frontmatter is skipped rather than read. Cosense has no notion of a title held apart
|
|
130
|
+
* from the text — the first line *is* the title, and a link finds a page by it. A `title:`
|
|
131
|
+
* that disagreed with the first line would name a page nobody can see, so it is ignored
|
|
132
|
+
* here even though `metadata.ts` still prefers it.
|
|
133
|
+
*
|
|
134
|
+
* An empty file falls back to its name, so it stays linkable.
|
|
135
|
+
*/
|
|
136
|
+
const titleOf = (path, text, frontmatter) => pipe(text.replace(/\r\n?/g, "\n"), (normalized) => Option.getOrElse(Option.map(Option.liftPredicate(normalized, () => frontmatter), withoutFrontmatter), () => normalized), firstLine, Option.getOrElse(() => basename(path, extname(path))));
|
|
137
|
+
/** `path` from `root`, with `/` between directories whatever the platform. */
|
|
138
|
+
const locationOf = (root, path) => relative(root, path).split(sep).join("/");
|
|
139
|
+
/** A file that cannot be read is left out rather than failing the rest. */
|
|
140
|
+
const readPageFile = (root, path, { frontmatter = true }) => pipe(Effect.tryPromise(() => readFile(path, "utf8")), Effect.map((text) => Option.some({
|
|
141
|
+
page: {
|
|
142
|
+
title: titleOf(path, text, frontmatter),
|
|
143
|
+
uri: pathToFileURL(path).href,
|
|
144
|
+
location: locationOf(root, path)
|
|
145
|
+
},
|
|
146
|
+
path,
|
|
147
|
+
text
|
|
148
|
+
})), Effect.orElseSucceed(() => Option.none()));
|
|
149
|
+
/** The page files under one root. */
|
|
150
|
+
const filesUnder = (options) => (root) => pipe(pageFiles(root), Effect.flatMap(Effect.forEach((path) => readPageFile(root, path, options))), Effect.map(Array.getSomes));
|
|
151
|
+
/**
|
|
152
|
+
* Every page file under `roots`. Roots may overlap (`src` and `src/content`), so a file
|
|
153
|
+
* found twice is kept once, where it was first found.
|
|
154
|
+
*/
|
|
155
|
+
const readPageFiles = (roots, options = {}) => pipe(Effect.forEach(roots, filesUnder(options)), Effect.map((found) => Array.dedupeWith(Array.flatten(found), (a, b) => a.path === b.path)));
|
|
156
|
+
/** The pages of `files`, as an index. */
|
|
157
|
+
const indexOf = (files) => ({ pages: Array.map(files, (file) => file.page) });
|
|
158
|
+
/** Read every page under `roots` and index it. */
|
|
159
|
+
const readIndex = (roots, options = {}) => Effect.map(readPageFiles(roots, options), indexOf);
|
|
160
|
+
/**
|
|
161
|
+
* Where to read pages: each workspace folder, or each of `sources` under it when the reader
|
|
162
|
+
* has set them. A folder that is not a `file://` URI has no files to read.
|
|
163
|
+
*/
|
|
164
|
+
const rootsOf = (folders, sources) => pipe(Array.filterMap(folders ?? [], ({ uri }) => Option.liftThrowable(fileURLToPath)(uri)), Array.flatMap((folder) => Array.match(sources, {
|
|
165
|
+
onEmpty: () => [folder],
|
|
166
|
+
onNonEmpty: (under) => Array.map(under, (source) => resolve(folder, source))
|
|
167
|
+
})));
|
|
168
|
+
//#endregion
|
|
169
|
+
export { rootsOf as a, settingsOf as c, readPageFiles as i, severityOf as l, indexOf as n, defaultSettings as o, readIndex as r, parseOptionsOf as s, emptyIndex as t, unresolvedLinkDiagnostics as u };
|
|
170
|
+
|
|
171
|
+
//# sourceMappingURL=workspace-DouAOZnK.mjs.map
|