This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathtree-sitter-helpers.js
More file actions
101 lines (86 loc) · 2.82 KB
/
tree-sitter-helpers.js
File metadata and controls
101 lines (86 loc) · 2.82 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const dedent = require("dedent");
module.exports = {
// https://github.com/atom/atom/blob/b3d3a52d9e4eb41f33df7b91ad1f8a2657a04487/spec/tree-sitter-language-mode-spec.js#L47-L55
expectTokensToEqual(editor, expectedTokenLines, startingRow = 1) {
const lastRow = editor.getLastScreenRow();
for (let row = startingRow; row <= lastRow - startingRow; row++) {
const tokenLine = editor
.tokensForScreenRow(row)
.map(({ text, scopes }) => ({
text,
scopes: scopes.map((scope) =>
scope
.split(" ")
.map((className) => className.replace("syntax--", ""))
.join(".")
),
}));
const expectedTokenLine = expectedTokenLines[row - startingRow];
expect(tokenLine.length).toEqual(expectedTokenLine.length);
for (let i = 0; i < tokenLine.length; i++) {
expect(tokenLine[i].text).toEqual(
expectedTokenLine[i].text,
`Token ${i}, row: ${row}`
);
expect(tokenLine[i].scopes).toEqual(
expectedTokenLine[i].scopes,
`Token ${i}, row: ${row}, token: '${tokenLine[i].text}'`
);
}
}
},
toHaveScopesAtPosition(posn, token, expected, includeEmbeddedScopes = false) {
if (expected === undefined) {
expected = token;
}
if (token === undefined) {
expected = [];
}
// token is not used at this time; it's just a way to keep note where we are
// in the line
let filterEmbeddedScopes = (scope) =>
includeEmbeddedScopes ||
(scope !== "text.html.php" &&
scope !== "meta.embedded.block.php" &&
scope !== "meta.embedded.line.php");
let actual = this.actual
.scopeDescriptorForBufferPosition(posn)
.scopes.filter(filterEmbeddedScopes);
let notExpected = actual.filter((scope) => !expected.includes(scope));
let notReceived = expected.filter((scope) => !actual.includes(scope));
let pass = notExpected.length === 0 && notReceived.length === 0;
if (pass) {
this.message = () => "Scopes matched";
} else {
let line = this.actual.getBuffer().lineForRow(posn[0]);
let caret = " ".repeat(posn[1]) + "^";
this.message = () =>
`Failure:
Scopes did not match at position [${posn.join(", ")}]:
${line}
${caret}
These scopes were expected but not received:
${notReceived.join(", ")}
These scopes were received but not expected:
${notExpected.join(", ")}
`;
}
return pass;
},
setPhpText(content) {
this.setText(`<?php
${dedent(content)}
`);
},
nextHighlightingUpdate(editor) {
return new Promise((resolve) => {
const subscription = editor
.getBuffer()
.getLanguageMode()
.onDidChangeHighlighting(() => {
subscription.dispose();
resolve();
});
});
},
};