-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcontext.ts
More file actions
74 lines (65 loc) · 1.85 KB
/
Copy pathcontext.ts
File metadata and controls
74 lines (65 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import type * as YAML from "yaml";
import type * as YAML_CST from "../cst.js";
import { createPosition } from "../factories/position.js";
import type {
Comment,
Content,
Document,
Point,
Position,
Range,
} from "../types.js";
import { transformComment } from "./comment.js";
import { transformContentProperties } from "./content.js";
import { transformDocuments } from "./document.js";
import {
transformNode,
type TransformNodeProperties,
type YamlNode,
type YamlToUnist,
} from "./transform.js";
class Context {
text;
comments: Comment[] = [];
#lineCounter: YAML.LineCounter;
constructor(text: string, lineCounter: YAML.LineCounter) {
this.text = text;
this.#lineCounter = lineCounter;
}
transformOffset(offset: number): Point {
const { line, col } = this.#lineCounter.linePos(offset);
return { line, column: col, offset };
}
transformRange(range: Range): Position {
const [start, end] = range.map(position => this.transformOffset(position));
return createPosition(start, end);
}
transformDocuments(
documentNodes: YAML.Document.Parsed[],
cstTokens: YAML.CST.Token[],
): Document[] {
return transformDocuments(documentNodes, cstTokens, this);
}
transformNode<T extends YamlNode>(
node: T,
props: TransformNodeProperties,
): YamlToUnist<T> {
return transformNode(node, this, props);
}
transformComment(node: YAML_CST.CommentSourceToken): Comment {
const comment = transformComment(node, this);
this.comments.push(comment);
return comment;
}
transformContentProperties(
node:
| YAML.ParsedNode
| YAML.YAMLSeq.Parsed<
YAML.ParsedNode | YAML.Pair<YAML.ParsedNode, YAML.ParsedNode | null>
>,
tokens: YAML_CST.ContentPropertyToken[],
): Content {
return transformContentProperties(node, tokens, this);
}
}
export default Context;