@rayfold/lsp 0.1.0
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 +202 -0
- package/NOTICE +10 -0
- package/README.md +40 -0
- package/index.d.ts +41 -0
- package/index.js +307 -0
- package/index.js.map +1 -0
- package/package.json +42 -0
- package/positions.d.ts +52 -0
- package/positions.js +174 -0
- package/positions.js.map +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Rayfold
|
|
2
|
+
Copyright 2026 The Rayfold Authors
|
|
3
|
+
|
|
4
|
+
This product is licensed under the Apache License, Version 2.0 (see LICENSE).
|
|
5
|
+
|
|
6
|
+
The data in data/gutenberg.json.gz is derived from the Project Gutenberg catalogue
|
|
7
|
+
(https://www.gutenberg.org/ebooks/offline_catalogs.html) and is not covered by this
|
|
8
|
+
project's copyright. See data/README.md for its source. Project Gutenberg is a
|
|
9
|
+
registered trademark of the Project Gutenberg Literary Archive Foundation; Rayfold is
|
|
10
|
+
not affiliated with or endorsed by Project Gutenberg.
|
package/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# @rayfold/lsp
|
|
2
|
+
|
|
3
|
+
A language server for [Rayfold](https://github.com/eddyboutros/rayfold) schemas. It reads `.rayfold` documents with the
|
|
4
|
+
same parser and validator the runtime uses, so what an editor underlines is what `rayfold check` would report, in the
|
|
5
|
+
same words.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install --save-dev @rayfold/lsp @rayfold/cli
|
|
9
|
+
rayfold lsp # speaks LSP over stdin and stdout
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
What it answers:
|
|
13
|
+
|
|
14
|
+
| Request | What you get |
|
|
15
|
+
| --- | --- |
|
|
16
|
+
| diagnostics | every syntax error and every validation finding, placed on the declaration it is about |
|
|
17
|
+
| completion | declaration keywords, annotations (with where each one is allowed), and type names in a type position |
|
|
18
|
+
| hover | the declaration, its description and its annotations; an operation shows its signature, what it throws and what it emits |
|
|
19
|
+
| definition | jump from a type reference to its declaration |
|
|
20
|
+
| document symbols | an outline of types with their fields, and the operations |
|
|
21
|
+
|
|
22
|
+
The server holds no dependencies of its own beyond `@rayfold/schema`, and speaks the protocol's `Content-Length`
|
|
23
|
+
framing directly, so it runs anywhere Node does.
|
|
24
|
+
|
|
25
|
+
## Without the CLI
|
|
26
|
+
|
|
27
|
+
`serveStdio` puts it on any stream pair, and `RayfoldLanguageServer` is transport-free if you have a channel of your
|
|
28
|
+
own:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { serveStdio } from "@rayfold/lsp";
|
|
32
|
+
|
|
33
|
+
serveStdio(process.stdin, process.stdout);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`diagnosticsFor(text)` returns the same findings as a plain function, for a check that is not an editor.
|
|
37
|
+
|
|
38
|
+
Editor setup, including a TextMate grammar for highlighting, is in the [editor guide](../../docs/guide/editors.md).
|
|
39
|
+
|
|
40
|
+
Apache-2.0.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type Range } from "./positions.js";
|
|
2
|
+
export { declarationAt, indexDocument, rangeForPath, tokenAt, type Declaration, type DocumentIndex, type Position, type Range } from "./positions.js";
|
|
3
|
+
export interface IncomingMessage {
|
|
4
|
+
jsonrpc?: string;
|
|
5
|
+
id?: number | string | null;
|
|
6
|
+
method?: string;
|
|
7
|
+
params?: unknown;
|
|
8
|
+
}
|
|
9
|
+
export type Send = (message: Record<string, unknown>) => void;
|
|
10
|
+
export declare class RayfoldLanguageServer {
|
|
11
|
+
private readonly send;
|
|
12
|
+
private readonly onExit?;
|
|
13
|
+
private readonly documents;
|
|
14
|
+
/** [onExit] runs when the editor sends `exit`, so whatever hosts the server can stop with it. */
|
|
15
|
+
constructor(send: Send, onExit?: (() => void) | undefined);
|
|
16
|
+
/** One message from the editor. Responses and notifications go back through the send given to the constructor. */
|
|
17
|
+
receive(message: IncomingMessage): void;
|
|
18
|
+
/** The text the editor last sent for [uri], for a caller that keeps its own view of the workspace. */
|
|
19
|
+
document(uri: string): string | undefined;
|
|
20
|
+
private reply;
|
|
21
|
+
private publish;
|
|
22
|
+
private completion;
|
|
23
|
+
private hover;
|
|
24
|
+
private definition;
|
|
25
|
+
private symbols;
|
|
26
|
+
}
|
|
27
|
+
/** What the editor underlines: the syntax error, or every finding validation reports, placed in the text. */
|
|
28
|
+
export declare function diagnosticsFor(text: string): Array<{
|
|
29
|
+
range: Range;
|
|
30
|
+
severity: number;
|
|
31
|
+
code?: string;
|
|
32
|
+
source: string;
|
|
33
|
+
message: string;
|
|
34
|
+
}>;
|
|
35
|
+
/**
|
|
36
|
+
* The server over a stream pair, framed as the protocol asks (`Content-Length`, then the JSON). `rayfold lsp` runs
|
|
37
|
+
* this on the process's own stdin and stdout.
|
|
38
|
+
*/
|
|
39
|
+
export declare function serveStdio(input: NodeJS.ReadableStream, output: NodeJS.WritableStream, options?: {
|
|
40
|
+
onExit?: () => void;
|
|
41
|
+
}): RayfoldLanguageServer;
|
package/index.js
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A language server for `.rayfold` documents: diagnostics as you type, completion for keywords, annotations and type
|
|
3
|
+
* names, hover, go to definition, and an outline.
|
|
4
|
+
*
|
|
5
|
+
* The protocol handling is transport-free. [RayfoldLanguageServer] takes the messages an editor sends and calls back
|
|
6
|
+
* with the messages to send, so the same object serves over stdio (`rayfold lsp`) or any other channel, and a test
|
|
7
|
+
* can drive it without a process.
|
|
8
|
+
*/
|
|
9
|
+
import { BUILTIN_SCALARS, KNOWN_ANNOTATIONS, RayfoldSyntaxError, parseSchemaText, typeRefToString, validateIR, } from "@rayfold/schema";
|
|
10
|
+
import { DEF_KEYWORDS, declarationAt, indexDocument, rangeForPath, tokenAt } from "./positions.js";
|
|
11
|
+
export { declarationAt, indexDocument, rangeForPath, tokenAt } from "./positions.js";
|
|
12
|
+
/** Severity as the protocol numbers it. */
|
|
13
|
+
const ERROR = 1;
|
|
14
|
+
const WARNING = 2;
|
|
15
|
+
/** SymbolKind as the protocol numbers it. */
|
|
16
|
+
const SYMBOL = { field: 8, function: 12, property: 7, enumMember: 22, struct: 23 };
|
|
17
|
+
/** CompletionItemKind as the protocol numbers it. */
|
|
18
|
+
const COMPLETION = { keyword: 14, class: 7, property: 10, value: 12 };
|
|
19
|
+
const CAPABILITIES = {
|
|
20
|
+
textDocumentSync: 1, // full text on every change: a schema is small, and a full parse is what validation needs
|
|
21
|
+
completionProvider: { triggerCharacters: ["@", ":", "<", "|"] },
|
|
22
|
+
hoverProvider: true,
|
|
23
|
+
definitionProvider: true,
|
|
24
|
+
documentSymbolProvider: true,
|
|
25
|
+
};
|
|
26
|
+
const isIdentifier = (c) => (c >= "a" && c <= "z") || (c >= "A" && c <= "Z") || (c >= "0" && c <= "9") || c === "_";
|
|
27
|
+
/** The word being typed, and the punctuation that introduced it. */
|
|
28
|
+
function wordBefore(line, character) {
|
|
29
|
+
const end = Math.min(character, line.length);
|
|
30
|
+
let start = end;
|
|
31
|
+
while (start > 0 && isIdentifier(line[start - 1]))
|
|
32
|
+
start--;
|
|
33
|
+
let before = start;
|
|
34
|
+
while (before > 0 && (line[before - 1] === " " || line[before - 1] === "\t"))
|
|
35
|
+
before--;
|
|
36
|
+
return { word: line.slice(start, end), trigger: before > 0 ? line[before - 1] : "" };
|
|
37
|
+
}
|
|
38
|
+
const lines = (text) => text.split("\n").map((l) => (l.endsWith("\r") ? l.slice(0, -1) : l));
|
|
39
|
+
export class RayfoldLanguageServer {
|
|
40
|
+
send;
|
|
41
|
+
onExit;
|
|
42
|
+
documents = new Map();
|
|
43
|
+
/** [onExit] runs when the editor sends `exit`, so whatever hosts the server can stop with it. */
|
|
44
|
+
constructor(send, onExit) {
|
|
45
|
+
this.send = send;
|
|
46
|
+
this.onExit = onExit;
|
|
47
|
+
}
|
|
48
|
+
/** One message from the editor. Responses and notifications go back through the send given to the constructor. */
|
|
49
|
+
receive(message) {
|
|
50
|
+
const id = message.id;
|
|
51
|
+
switch (message.method) {
|
|
52
|
+
case "initialize":
|
|
53
|
+
return this.reply(id, { capabilities: CAPABILITIES, serverInfo: { name: "rayfold", version: "0.1" } });
|
|
54
|
+
case "initialized":
|
|
55
|
+
case "$/cancelRequest":
|
|
56
|
+
return;
|
|
57
|
+
case "exit":
|
|
58
|
+
this.onExit?.();
|
|
59
|
+
return;
|
|
60
|
+
case "shutdown":
|
|
61
|
+
return this.reply(id, null);
|
|
62
|
+
case "textDocument/didOpen": {
|
|
63
|
+
const p = params(message);
|
|
64
|
+
this.documents.set(p.textDocument.uri, p.textDocument.text);
|
|
65
|
+
return this.publish(p.textDocument.uri);
|
|
66
|
+
}
|
|
67
|
+
case "textDocument/didChange": {
|
|
68
|
+
const p = params(message);
|
|
69
|
+
const last = p.contentChanges[p.contentChanges.length - 1];
|
|
70
|
+
if (!last)
|
|
71
|
+
return;
|
|
72
|
+
this.documents.set(p.textDocument.uri, last.text);
|
|
73
|
+
return this.publish(p.textDocument.uri);
|
|
74
|
+
}
|
|
75
|
+
case "textDocument/didClose": {
|
|
76
|
+
const p = params(message);
|
|
77
|
+
this.documents.delete(p.textDocument.uri);
|
|
78
|
+
this.send({ jsonrpc: "2.0", method: "textDocument/publishDiagnostics", params: { uri: p.textDocument.uri, diagnostics: [] } });
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
case "textDocument/completion": {
|
|
82
|
+
const p = params(message);
|
|
83
|
+
return this.reply(id, this.completion(p.textDocument.uri, p.position));
|
|
84
|
+
}
|
|
85
|
+
case "textDocument/hover": {
|
|
86
|
+
const p = params(message);
|
|
87
|
+
return this.reply(id, this.hover(p.textDocument.uri, p.position));
|
|
88
|
+
}
|
|
89
|
+
case "textDocument/definition": {
|
|
90
|
+
const p = params(message);
|
|
91
|
+
return this.reply(id, this.definition(p.textDocument.uri, p.position));
|
|
92
|
+
}
|
|
93
|
+
case "textDocument/documentSymbol": {
|
|
94
|
+
const p = params(message);
|
|
95
|
+
return this.reply(id, this.symbols(p.textDocument.uri));
|
|
96
|
+
}
|
|
97
|
+
default:
|
|
98
|
+
// a notification we do not know is ignored; a request always gets an answer
|
|
99
|
+
if (id !== undefined && id !== null) {
|
|
100
|
+
this.send({ jsonrpc: "2.0", id, error: { code: -32601, message: `Method not found: ${String(message.method)}` } });
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/** The text the editor last sent for [uri], for a caller that keeps its own view of the workspace. */
|
|
105
|
+
document(uri) {
|
|
106
|
+
return this.documents.get(uri);
|
|
107
|
+
}
|
|
108
|
+
reply(id, result) {
|
|
109
|
+
if (id === undefined || id === null)
|
|
110
|
+
return;
|
|
111
|
+
this.send({ jsonrpc: "2.0", id, result });
|
|
112
|
+
}
|
|
113
|
+
publish(uri) {
|
|
114
|
+
const text = this.documents.get(uri) ?? "";
|
|
115
|
+
this.send({ jsonrpc: "2.0", method: "textDocument/publishDiagnostics", params: { uri, diagnostics: diagnosticsFor(text) } });
|
|
116
|
+
}
|
|
117
|
+
completion(uri, position) {
|
|
118
|
+
const text = this.documents.get(uri);
|
|
119
|
+
if (text === undefined)
|
|
120
|
+
return [];
|
|
121
|
+
const line = lines(text)[position.line] ?? "";
|
|
122
|
+
const { trigger } = wordBefore(line, position.character);
|
|
123
|
+
const index = indexDocument(text);
|
|
124
|
+
if (trigger === "@") {
|
|
125
|
+
return Object.entries(KNOWN_ANNOTATIONS).map(([name, on]) => ({
|
|
126
|
+
label: name,
|
|
127
|
+
kind: COMPLETION.property,
|
|
128
|
+
detail: `@${name}`,
|
|
129
|
+
documentation: `Allowed on: ${[...on].join(", ")}.`,
|
|
130
|
+
}));
|
|
131
|
+
}
|
|
132
|
+
const types = index.declarations
|
|
133
|
+
.filter((d) => d.kind === "type")
|
|
134
|
+
.map((d) => ({ label: d.name, kind: COMPLETION.class, detail: d.keyword }));
|
|
135
|
+
if (trigger === ":" || trigger === "<" || trigger === "|" || trigger === ",") {
|
|
136
|
+
const builtins = BUILTIN_SCALARS.map((name) => ({ label: name, kind: COMPLETION.value, detail: "scalar" }));
|
|
137
|
+
return [...types, ...builtins];
|
|
138
|
+
}
|
|
139
|
+
const keywords = DEF_KEYWORDS.map((word) => ({ label: word, kind: COMPLETION.keyword, detail: "declaration" }));
|
|
140
|
+
return [...keywords, ...types];
|
|
141
|
+
}
|
|
142
|
+
hover(uri, position) {
|
|
143
|
+
const text = this.documents.get(uri);
|
|
144
|
+
if (text === undefined)
|
|
145
|
+
return null;
|
|
146
|
+
const index = indexDocument(text);
|
|
147
|
+
const token = tokenAt(index, position);
|
|
148
|
+
const declaration = declarationAt(index, position) ?? (token?.kind === "name" ? index.byPath.get(token.value) : undefined);
|
|
149
|
+
if (!declaration)
|
|
150
|
+
return null;
|
|
151
|
+
const ir = irOf(text);
|
|
152
|
+
const body = [];
|
|
153
|
+
if (declaration.kind === "op") {
|
|
154
|
+
const op = ir?.ops[declaration.name];
|
|
155
|
+
const signature = op
|
|
156
|
+
? `${op.kind} ${op.name}(${op.args.map((a) => `${a.name}: ${typeRefToString(a.type)}`).join(", ")}): ${typeRefToString(op.returns)}`
|
|
157
|
+
: `${declaration.keyword} ${declaration.name}`;
|
|
158
|
+
body.push(code(signature));
|
|
159
|
+
if (op?.description)
|
|
160
|
+
body.push(op.description);
|
|
161
|
+
if (op?.throws.length)
|
|
162
|
+
body.push(`Fails with: ${op.throws.join(", ")}.`);
|
|
163
|
+
if (op?.emits.length)
|
|
164
|
+
body.push(`Emits: ${op.emits.join(", ")}.`);
|
|
165
|
+
}
|
|
166
|
+
else if (declaration.kind === "field" || declaration.kind === "enumValue") {
|
|
167
|
+
const owner = declaration.container ? ir?.types[declaration.container] : undefined;
|
|
168
|
+
const field = owner && "fields" in owner ? owner.fields.find((f) => f.name === declaration.name) : undefined;
|
|
169
|
+
body.push(code(field ? `${declaration.path}: ${typeRefToString(field.type)}` : declaration.path));
|
|
170
|
+
if (field?.description)
|
|
171
|
+
body.push(field.description);
|
|
172
|
+
const annotations = field?.annotations.map((a) => `@${a.name}`) ?? [];
|
|
173
|
+
if (annotations.length)
|
|
174
|
+
body.push(annotations.join(" "));
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
const type = ir?.types[declaration.name];
|
|
178
|
+
body.push(code(`${declaration.keyword} ${declaration.name}`));
|
|
179
|
+
if (type?.description)
|
|
180
|
+
body.push(type.description);
|
|
181
|
+
const annotations = type?.annotations.map((a) => `@${a.name}`) ?? [];
|
|
182
|
+
if (annotations.length)
|
|
183
|
+
body.push(annotations.join(" "));
|
|
184
|
+
}
|
|
185
|
+
return { contents: { kind: "markdown", value: body.join("\n\n") }, range: declaration.range };
|
|
186
|
+
}
|
|
187
|
+
definition(uri, position) {
|
|
188
|
+
const text = this.documents.get(uri);
|
|
189
|
+
if (text === undefined)
|
|
190
|
+
return null;
|
|
191
|
+
const index = indexDocument(text);
|
|
192
|
+
const token = tokenAt(index, position);
|
|
193
|
+
if (!token || token.kind !== "name")
|
|
194
|
+
return null;
|
|
195
|
+
const declaration = index.byPath.get(token.value) ?? declarationAt(index, position);
|
|
196
|
+
return declaration ? { uri, range: declaration.range } : null;
|
|
197
|
+
}
|
|
198
|
+
symbols(uri) {
|
|
199
|
+
const text = this.documents.get(uri);
|
|
200
|
+
if (text === undefined)
|
|
201
|
+
return [];
|
|
202
|
+
const index = indexDocument(text);
|
|
203
|
+
const children = new Map();
|
|
204
|
+
for (const d of index.declarations) {
|
|
205
|
+
if (!d.container)
|
|
206
|
+
continue;
|
|
207
|
+
const kind = d.kind === "enumValue" ? SYMBOL.enumMember : d.kind === "view" ? SYMBOL.property : SYMBOL.field;
|
|
208
|
+
const list = children.get(d.container) ?? [];
|
|
209
|
+
list.push({ name: d.name, detail: d.keyword, kind, range: d.full, selectionRange: d.range });
|
|
210
|
+
children.set(d.container, list);
|
|
211
|
+
}
|
|
212
|
+
return index.declarations
|
|
213
|
+
.filter((d) => !d.container)
|
|
214
|
+
.map((d) => ({
|
|
215
|
+
name: d.name,
|
|
216
|
+
detail: d.keyword,
|
|
217
|
+
kind: d.kind === "op" ? SYMBOL.function : SYMBOL.struct,
|
|
218
|
+
range: d.full,
|
|
219
|
+
selectionRange: d.range,
|
|
220
|
+
children: children.get(d.name) ?? [],
|
|
221
|
+
}));
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
const params = (message) => (message.params ?? {});
|
|
225
|
+
const code = (text) => ["```rayfold", text, "```"].join("\n");
|
|
226
|
+
function irOf(text) {
|
|
227
|
+
try {
|
|
228
|
+
return parseSchemaText(text);
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
return undefined;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
/** What the editor underlines: the syntax error, or every finding validation reports, placed in the text. */
|
|
235
|
+
export function diagnosticsFor(text) {
|
|
236
|
+
let ir;
|
|
237
|
+
try {
|
|
238
|
+
ir = parseSchemaText(text);
|
|
239
|
+
}
|
|
240
|
+
catch (e) {
|
|
241
|
+
const start = e instanceof RayfoldSyntaxError ? { line: Math.max(0, e.line - 1), character: Math.max(0, e.col - 1) } : { line: 0, character: 0 };
|
|
242
|
+
return [{ range: { start, end: start }, severity: ERROR, source: "rayfold", message: e.message }];
|
|
243
|
+
}
|
|
244
|
+
const index = indexDocument(text);
|
|
245
|
+
return validateIR(ir).map((d) => ({
|
|
246
|
+
range: rangeForPath(index, d.at),
|
|
247
|
+
severity: d.severity === "error" ? ERROR : WARNING,
|
|
248
|
+
code: d.code,
|
|
249
|
+
source: "rayfold",
|
|
250
|
+
message: d.message,
|
|
251
|
+
}));
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* The server over a stream pair, framed as the protocol asks (`Content-Length`, then the JSON). `rayfold lsp` runs
|
|
255
|
+
* this on the process's own stdin and stdout.
|
|
256
|
+
*/
|
|
257
|
+
export function serveStdio(input, output, options = {}) {
|
|
258
|
+
const server = new RayfoldLanguageServer((message) => {
|
|
259
|
+
const body = Buffer.from(JSON.stringify(message), "utf8");
|
|
260
|
+
output.write(`Content-Length: ${body.length}\r\n\r\n`);
|
|
261
|
+
output.write(body);
|
|
262
|
+
}, options.onExit);
|
|
263
|
+
let buffer = Buffer.alloc(0);
|
|
264
|
+
input.on("data", (chunk) => {
|
|
265
|
+
buffer = Buffer.concat([buffer, typeof chunk === "string" ? Buffer.from(chunk, "utf8") : chunk]);
|
|
266
|
+
for (;;) {
|
|
267
|
+
const headerEnd = buffer.indexOf("\r\n\r\n");
|
|
268
|
+
if (headerEnd < 0)
|
|
269
|
+
return;
|
|
270
|
+
const header = buffer.subarray(0, headerEnd).toString("utf8");
|
|
271
|
+
const length = contentLength(header);
|
|
272
|
+
if (length === undefined) {
|
|
273
|
+
buffer = buffer.subarray(headerEnd + 4); // a header we cannot read: drop it rather than stall the stream
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
const start = headerEnd + 4;
|
|
277
|
+
if (buffer.length < start + length)
|
|
278
|
+
return;
|
|
279
|
+
const body = buffer.subarray(start, start + length).toString("utf8");
|
|
280
|
+
buffer = buffer.subarray(start + length);
|
|
281
|
+
let message;
|
|
282
|
+
try {
|
|
283
|
+
message = JSON.parse(body);
|
|
284
|
+
}
|
|
285
|
+
catch {
|
|
286
|
+
continue; // not our message to answer: there is no id to answer it with
|
|
287
|
+
}
|
|
288
|
+
server.receive(message);
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
input.on("end", () => options.onExit?.()); // the editor closed the pipe: there is nothing left to serve
|
|
292
|
+
return server;
|
|
293
|
+
}
|
|
294
|
+
function contentLength(header) {
|
|
295
|
+
for (const line of header.split("\r\n")) {
|
|
296
|
+
const colon = line.indexOf(":");
|
|
297
|
+
if (colon < 0)
|
|
298
|
+
continue;
|
|
299
|
+
if (line.slice(0, colon).trim().toLowerCase() !== "content-length")
|
|
300
|
+
continue;
|
|
301
|
+
const value = Number(line.slice(colon + 1).trim());
|
|
302
|
+
if (Number.isInteger(value) && value >= 0)
|
|
303
|
+
return value;
|
|
304
|
+
}
|
|
305
|
+
return undefined;
|
|
306
|
+
}
|
|
307
|
+
//# sourceMappingURL=index.js.map
|
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EACL,eAAe,EACf,iBAAiB,EACjB,kBAAkB,EAClB,eAAe,EACf,eAAe,EACf,UAAU,GAEX,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,YAAY,EAAE,OAAO,EAA6B,MAAM,gBAAgB,CAAC;AAE9H,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,YAAY,EAAE,OAAO,EAAmE,MAAM,gBAAgB,CAAC;AAWtJ,2CAA2C;AAC3C,MAAM,KAAK,GAAG,CAAC,CAAC;AAChB,MAAM,OAAO,GAAG,CAAC,CAAC;AAElB,6CAA6C;AAC7C,MAAM,MAAM,GAAG,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE,QAAQ,EAAE,CAAC,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAW,CAAC;AAE5F,qDAAqD;AACrD,MAAM,UAAU,GAAG,EAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE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AC;IACH,CAAC,CAAC,CAAC;IACH,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,CAAC,6DAA6D;IACxG,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAS,aAAa,CAAC,MAAc;IACnC,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;QACxC,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAChC,IAAI,KAAK,GAAG,CAAC;YAAE,SAAS;QACxB,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,KAAK,gBAAgB;YAAE,SAAS;QAC7E,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;QACnD,IAAI,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC;YAAE,OAAO,KAAK,CAAC;IAC1D,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["/**\n * A language server for `.rayfold` documents: diagnostics as you type, completion for keywords, annotations and type\n * names, hover, go to definition, and an outline.\n *\n * The protocol handling is transport-free. [RayfoldLanguageServer] takes the messages an editor sends and calls back\n * with the messages to send, so the same object serves over stdio (`rayfold lsp`) or any other channel, and a test\n * can drive it without a process.\n */\nimport {\n BUILTIN_SCALARS,\n KNOWN_ANNOTATIONS,\n RayfoldSyntaxError,\n parseSchemaText,\n typeRefToString,\n validateIR,\n type RayfoldSchemaIR,\n} from \"@rayfold/schema\";\nimport { DEF_KEYWORDS, declarationAt, indexDocument, rangeForPath, tokenAt, type Position, type Range } from \"./positions.ts\";\n\nexport { declarationAt, indexDocument, rangeForPath, tokenAt, type Declaration, type DocumentIndex, type Position, type Range } from \"./positions.ts\";\n\nexport interface IncomingMessage {\n jsonrpc?: string;\n id?: number | string | null;\n method?: string;\n params?: unknown;\n}\n\nexport type Send = (message: Record<string, unknown>) => void;\n\n/** Severity as the protocol numbers it. */\nconst ERROR = 1;\nconst WARNING = 2;\n\n/** SymbolKind as the protocol numbers it. */\nconst SYMBOL = { field: 8, function: 12, property: 7, enumMember: 22, struct: 23 } as const;\n\n/** CompletionItemKind as the protocol numbers it. */\nconst COMPLETION = { keyword: 14, class: 7, property: 10, value: 12 } as const;\n\nconst CAPABILITIES = {\n textDocumentSync: 1, // full text on every change: a schema is small, and a full parse is what validation needs\n completionProvider: { triggerCharacters: [\"@\", \":\", \"<\", \"|\"] },\n hoverProvider: true,\n definitionProvider: true,\n documentSymbolProvider: true,\n};\n\nconst isIdentifier = (c: string): boolean => (c >= \"a\" && c <= \"z\") || (c >= \"A\" && c <= \"Z\") || (c >= \"0\" && c <= \"9\") || c === \"_\";\n\n/** The word being typed, and the punctuation that introduced it. */\nfunction wordBefore(line: string, character: number): { word: string; trigger: string } {\n const end = Math.min(character, line.length);\n let start = end;\n while (start > 0 && isIdentifier(line[start - 1]!)) start--;\n let before = start;\n while (before > 0 && (line[before - 1] === \" \" || line[before - 1] === \"\\t\")) before--;\n return { word: line.slice(start, end), trigger: before > 0 ? line[before - 1]! : \"\" };\n}\n\nconst lines = (text: string): string[] => text.split(\"\\n\").map((l) => (l.endsWith(\"\\r\") ? l.slice(0, -1) : l));\n\nexport class RayfoldLanguageServer {\n private readonly documents = new Map<string, string>();\n\n /** [onExit] runs when the editor sends `exit`, so whatever hosts the server can stop with it. */\n constructor(\n private readonly send: Send,\n private readonly onExit?: () => void,\n ) {}\n\n /** One message from the editor. Responses and notifications go back through the send given to the constructor. */\n receive(message: IncomingMessage): void {\n const id = message.id;\n switch (message.method) {\n case \"initialize\":\n return this.reply(id, { capabilities: CAPABILITIES, serverInfo: { name: \"rayfold\", version: \"0.1\" } });\n case \"initialized\":\n case \"$/cancelRequest\":\n return;\n case \"exit\":\n this.onExit?.();\n return;\n case \"shutdown\":\n return this.reply(id, null);\n case \"textDocument/didOpen\": {\n const p = params<{ textDocument: { uri: string; text: string } }>(message);\n this.documents.set(p.textDocument.uri, p.textDocument.text);\n return this.publish(p.textDocument.uri);\n }\n case \"textDocument/didChange\": {\n const p = params<{ textDocument: { uri: string }; contentChanges: Array<{ text: string }> }>(message);\n const last = p.contentChanges[p.contentChanges.length - 1];\n if (!last) return;\n this.documents.set(p.textDocument.uri, last.text);\n return this.publish(p.textDocument.uri);\n }\n case \"textDocument/didClose\": {\n const p = params<{ textDocument: { uri: string } }>(message);\n this.documents.delete(p.textDocument.uri);\n this.send({ jsonrpc: \"2.0\", method: \"textDocument/publishDiagnostics\", params: { uri: p.textDocument.uri, diagnostics: [] } });\n return;\n }\n case \"textDocument/completion\": {\n const p = params<TextDocumentPosition>(message);\n return this.reply(id, this.completion(p.textDocument.uri, p.position));\n }\n case \"textDocument/hover\": {\n const p = params<TextDocumentPosition>(message);\n return this.reply(id, this.hover(p.textDocument.uri, p.position));\n }\n case \"textDocument/definition\": {\n const p = params<TextDocumentPosition>(message);\n return this.reply(id, this.definition(p.textDocument.uri, p.position));\n }\n case \"textDocument/documentSymbol\": {\n const p = params<{ textDocument: { uri: string } }>(message);\n return this.reply(id, this.symbols(p.textDocument.uri));\n }\n default:\n // a notification we do not know is ignored; a request always gets an answer\n if (id !== undefined && id !== null) {\n this.send({ jsonrpc: \"2.0\", id, error: { code: -32601, message: `Method not found: ${String(message.method)}` } });\n }\n }\n }\n\n /** The text the editor last sent for [uri], for a caller that keeps its own view of the workspace. */\n document(uri: string): string | undefined {\n return this.documents.get(uri);\n }\n\n private reply(id: number | string | null | undefined, result: unknown): void {\n if (id === undefined || id === null) return;\n this.send({ jsonrpc: \"2.0\", id, result });\n }\n\n private publish(uri: string): void {\n const text = this.documents.get(uri) ?? \"\";\n this.send({ jsonrpc: \"2.0\", method: \"textDocument/publishDiagnostics\", params: { uri, diagnostics: diagnosticsFor(text) } });\n }\n\n private completion(uri: string, position: Position): unknown[] {\n const text = this.documents.get(uri);\n if (text === undefined) return [];\n const line = lines(text)[position.line] ?? \"\";\n const { trigger } = wordBefore(line, position.character);\n const index = indexDocument(text);\n\n if (trigger === \"@\") {\n return Object.entries(KNOWN_ANNOTATIONS).map(([name, on]) => ({\n label: name,\n kind: COMPLETION.property,\n detail: `@${name}`,\n documentation: `Allowed on: ${[...on].join(\", \")}.`,\n }));\n }\n\n const types = index.declarations\n .filter((d) => d.kind === \"type\")\n .map((d) => ({ label: d.name, kind: COMPLETION.class, detail: d.keyword }));\n\n if (trigger === \":\" || trigger === \"<\" || trigger === \"|\" || trigger === \",\") {\n const builtins = BUILTIN_SCALARS.map((name) => ({ label: name, kind: COMPLETION.value, detail: \"scalar\" }));\n return [...types, ...builtins];\n }\n\n const keywords = DEF_KEYWORDS.map((word) => ({ label: word, kind: COMPLETION.keyword, detail: \"declaration\" }));\n return [...keywords, ...types];\n }\n\n private hover(uri: string, position: Position): unknown {\n const text = this.documents.get(uri);\n if (text === undefined) return null;\n const index = indexDocument(text);\n const token = tokenAt(index, position);\n const declaration = declarationAt(index, position) ?? (token?.kind === \"name\" ? index.byPath.get(token.value) : undefined);\n if (!declaration) return null;\n\n const ir = irOf(text);\n const body: string[] = [];\n if (declaration.kind === \"op\") {\n const op = ir?.ops[declaration.name];\n const signature = op\n ? `${op.kind} ${op.name}(${op.args.map((a) => `${a.name}: ${typeRefToString(a.type)}`).join(\", \")}): ${typeRefToString(op.returns)}`\n : `${declaration.keyword} ${declaration.name}`;\n body.push(code(signature));\n if (op?.description) body.push(op.description);\n if (op?.throws.length) body.push(`Fails with: ${op.throws.join(\", \")}.`);\n if (op?.emits.length) body.push(`Emits: ${op.emits.join(\", \")}.`);\n } else if (declaration.kind === \"field\" || declaration.kind === \"enumValue\") {\n const owner = declaration.container ? ir?.types[declaration.container] : undefined;\n const field = owner && \"fields\" in owner ? owner.fields.find((f) => f.name === declaration.name) : undefined;\n body.push(code(field ? `${declaration.path}: ${typeRefToString(field.type)}` : declaration.path));\n if (field?.description) body.push(field.description);\n const annotations = field?.annotations.map((a) => `@${a.name}`) ?? [];\n if (annotations.length) body.push(annotations.join(\" \"));\n } else {\n const type = ir?.types[declaration.name];\n body.push(code(`${declaration.keyword} ${declaration.name}`));\n if (type?.description) body.push(type.description);\n const annotations = type?.annotations.map((a) => `@${a.name}`) ?? [];\n if (annotations.length) body.push(annotations.join(\" \"));\n }\n return { contents: { kind: \"markdown\", value: body.join(\"\\n\\n\") }, range: declaration.range };\n }\n\n private definition(uri: string, position: Position): unknown {\n const text = this.documents.get(uri);\n if (text === undefined) return null;\n const index = indexDocument(text);\n const token = tokenAt(index, position);\n if (!token || token.kind !== \"name\") return null;\n const declaration = index.byPath.get(token.value) ?? declarationAt(index, position);\n return declaration ? { uri, range: declaration.range } : null;\n }\n\n private symbols(uri: string): unknown[] {\n const text = this.documents.get(uri);\n if (text === undefined) return [];\n const index = indexDocument(text);\n const children = new Map<string, unknown[]>();\n for (const d of index.declarations) {\n if (!d.container) continue;\n const kind = d.kind === \"enumValue\" ? SYMBOL.enumMember : d.kind === \"view\" ? SYMBOL.property : SYMBOL.field;\n const list = children.get(d.container) ?? [];\n list.push({ name: d.name, detail: d.keyword, kind, range: d.full, selectionRange: d.range });\n children.set(d.container, list);\n }\n return index.declarations\n .filter((d) => !d.container)\n .map((d) => ({\n name: d.name,\n detail: d.keyword,\n kind: d.kind === \"op\" ? SYMBOL.function : SYMBOL.struct,\n range: d.full,\n selectionRange: d.range,\n children: children.get(d.name) ?? [],\n }));\n }\n}\n\ninterface TextDocumentPosition {\n textDocument: { uri: string };\n position: Position;\n}\n\nconst params = <T>(message: IncomingMessage): T => (message.params ?? {}) as T;\n\nconst code = (text: string): string => [\"```rayfold\", text, \"```\"].join(\"\\n\");\n\nfunction irOf(text: string): RayfoldSchemaIR | undefined {\n try {\n return parseSchemaText(text);\n } catch {\n return undefined;\n }\n}\n\n/** What the editor underlines: the syntax error, or every finding validation reports, placed in the text. */\nexport function diagnosticsFor(text: string): Array<{ range: Range; severity: number; code?: string; source: string; message: string }> {\n let ir: RayfoldSchemaIR;\n try {\n ir = parseSchemaText(text);\n } catch (e) {\n const start: Position =\n e instanceof RayfoldSyntaxError ? { line: Math.max(0, e.line - 1), character: Math.max(0, e.col - 1) } : { line: 0, character: 0 };\n return [{ range: { start, end: start }, severity: ERROR, source: \"rayfold\", message: (e as Error).message }];\n }\n const index = indexDocument(text);\n return validateIR(ir).map((d) => ({\n range: rangeForPath(index, d.at),\n severity: d.severity === \"error\" ? ERROR : WARNING,\n code: d.code,\n source: \"rayfold\",\n message: d.message,\n }));\n}\n\n/**\n * The server over a stream pair, framed as the protocol asks (`Content-Length`, then the JSON). `rayfold lsp` runs\n * this on the process's own stdin and stdout.\n */\nexport function serveStdio(\n input: NodeJS.ReadableStream,\n output: NodeJS.WritableStream,\n options: { onExit?: () => void } = {},\n): RayfoldLanguageServer {\n const server = new RayfoldLanguageServer((message) => {\n const body = Buffer.from(JSON.stringify(message), \"utf8\");\n output.write(`Content-Length: ${body.length}\\r\\n\\r\\n`);\n output.write(body);\n }, options.onExit);\n\n let buffer = Buffer.alloc(0);\n input.on(\"data\", (chunk: Buffer | string) => {\n buffer = Buffer.concat([buffer, typeof chunk === \"string\" ? Buffer.from(chunk, \"utf8\") : chunk]);\n for (;;) {\n const headerEnd = buffer.indexOf(\"\\r\\n\\r\\n\");\n if (headerEnd < 0) return;\n const header = buffer.subarray(0, headerEnd).toString(\"utf8\");\n const length = contentLength(header);\n if (length === undefined) {\n buffer = buffer.subarray(headerEnd + 4); // a header we cannot read: drop it rather than stall the stream\n continue;\n }\n const start = headerEnd + 4;\n if (buffer.length < start + length) return;\n const body = buffer.subarray(start, start + length).toString(\"utf8\");\n buffer = buffer.subarray(start + length);\n let message: IncomingMessage;\n try {\n message = JSON.parse(body) as IncomingMessage;\n } catch {\n continue; // not our message to answer: there is no id to answer it with\n }\n server.receive(message);\n }\n });\n input.on(\"end\", () => options.onExit?.()); // the editor closed the pipe: there is nothing left to serve\n return server;\n}\n\nfunction contentLength(header: string): number | undefined {\n for (const line of header.split(\"\\r\\n\")) {\n const colon = line.indexOf(\":\");\n if (colon < 0) continue;\n if (line.slice(0, colon).trim().toLowerCase() !== \"content-length\") continue;\n const value = Number(line.slice(colon + 1).trim());\n if (Number.isInteger(value) && value >= 0) return value;\n }\n return undefined;\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rayfold/lsp",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A language server for .rayfold schemas: diagnostics, completion, hover, go to definition and an outline, over stdio.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"rayfold",
|
|
7
|
+
"api",
|
|
8
|
+
"protocol",
|
|
9
|
+
"lsp",
|
|
10
|
+
"language-server",
|
|
11
|
+
"editor"
|
|
12
|
+
],
|
|
13
|
+
"license": "Apache-2.0",
|
|
14
|
+
"homepage": "https://eddyboutros.github.io/rayfold/",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/eddyboutros/rayfold/issues"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/eddyboutros/rayfold.git",
|
|
21
|
+
"directory": "packages/lsp"
|
|
22
|
+
},
|
|
23
|
+
"type": "module",
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=22"
|
|
27
|
+
},
|
|
28
|
+
"main": "./index.js",
|
|
29
|
+
"types": "./index.d.ts",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"types": "./index.d.ts",
|
|
33
|
+
"default": "./index.js"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@rayfold/schema": "^0.1.0"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/positions.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where each declaration sits in a `.rayfold` document.
|
|
3
|
+
*
|
|
4
|
+
* The IR carries no source positions, and `validateIR` reports its findings against paths ("Book.price", "books()").
|
|
5
|
+
* This walks the same token stream the parser reads and records a range for every declaration, so a diagnostic, a
|
|
6
|
+
* hover or a go-to-definition can be answered in the text the author is looking at.
|
|
7
|
+
*/
|
|
8
|
+
import { type Token } from "@rayfold/schema";
|
|
9
|
+
/** LSP positions are zero-based; the lexer's are one-based. */
|
|
10
|
+
export interface Position {
|
|
11
|
+
line: number;
|
|
12
|
+
character: number;
|
|
13
|
+
}
|
|
14
|
+
export interface Range {
|
|
15
|
+
start: Position;
|
|
16
|
+
end: Position;
|
|
17
|
+
}
|
|
18
|
+
export type DeclarationKind = "type" | "field" | "op" | "enumValue" | "view";
|
|
19
|
+
export interface Declaration {
|
|
20
|
+
/** "Book", "Book.title", "books" - the path `validateIR` reports against, without the trailing (). */
|
|
21
|
+
path: string;
|
|
22
|
+
name: string;
|
|
23
|
+
kind: DeclarationKind;
|
|
24
|
+
/** what the source calls it: entity, object, query, command, ... */
|
|
25
|
+
keyword: string;
|
|
26
|
+
container?: string;
|
|
27
|
+
/** the identifier itself, for a selection or a jump */
|
|
28
|
+
range: Range;
|
|
29
|
+
/** the whole declaration, for an outline */
|
|
30
|
+
full: Range;
|
|
31
|
+
}
|
|
32
|
+
export interface DocumentIndex {
|
|
33
|
+
tokens: Token[];
|
|
34
|
+
declarations: Declaration[];
|
|
35
|
+
byPath: Map<string, Declaration>;
|
|
36
|
+
}
|
|
37
|
+
export declare const TYPE_KEYWORDS: readonly ["entity", "object", "input", "enum", "union", "scalar", "error", "event"];
|
|
38
|
+
export declare const OP_KEYWORDS: readonly ["query", "command", "stream"];
|
|
39
|
+
export declare const DEF_KEYWORDS: readonly ["entity", "object", "input", "enum", "union", "scalar", "error", "event", "query", "command", "stream", "view"];
|
|
40
|
+
/** True when [position] falls inside [range], counting both ends so a cursor at either edge of a word still hits it. */
|
|
41
|
+
export declare function contains(range: Range, position: Position): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Every declaration in [text]. A document that cannot be tokenized at all (an unterminated string, say) indexes as
|
|
44
|
+
* empty: the syntax error is reported on its own, and half a token stream would only misplace things.
|
|
45
|
+
*/
|
|
46
|
+
export declare function indexDocument(text: string): DocumentIndex;
|
|
47
|
+
/** The declaration whose identifier is under [position]. */
|
|
48
|
+
export declare function declarationAt(index: DocumentIndex, position: Position): Declaration | undefined;
|
|
49
|
+
/** The token under [position], which is what a hover or a completion is about. */
|
|
50
|
+
export declare function tokenAt(index: DocumentIndex, position: Position): Token | undefined;
|
|
51
|
+
/** Where a `validateIR` diagnostic belongs in the text; the first line when the path names nothing we found. */
|
|
52
|
+
export declare function rangeForPath(index: DocumentIndex, path: string): Range;
|
package/positions.js
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where each declaration sits in a `.rayfold` document.
|
|
3
|
+
*
|
|
4
|
+
* The IR carries no source positions, and `validateIR` reports its findings against paths ("Book.price", "books()").
|
|
5
|
+
* This walks the same token stream the parser reads and records a range for every declaration, so a diagnostic, a
|
|
6
|
+
* hover or a go-to-definition can be answered in the text the author is looking at.
|
|
7
|
+
*/
|
|
8
|
+
import { tokenize } from "@rayfold/schema";
|
|
9
|
+
export const TYPE_KEYWORDS = ["entity", "object", "input", "enum", "union", "scalar", "error", "event"];
|
|
10
|
+
export const OP_KEYWORDS = ["query", "command", "stream"];
|
|
11
|
+
export const DEF_KEYWORDS = [...TYPE_KEYWORDS, ...OP_KEYWORDS, "view"];
|
|
12
|
+
const TYPES = new Set(TYPE_KEYWORDS);
|
|
13
|
+
const OPS = new Set(OP_KEYWORDS);
|
|
14
|
+
const DEFS = new Set(DEF_KEYWORDS);
|
|
15
|
+
const startOf = (t) => ({ line: t.line - 1, character: t.col - 1 });
|
|
16
|
+
const rangeOf = (t) => ({ start: startOf(t), end: { line: t.line - 1, character: t.col - 1 + t.value.length } });
|
|
17
|
+
/** True when [position] falls inside [range], counting both ends so a cursor at either edge of a word still hits it. */
|
|
18
|
+
export function contains(range, position) {
|
|
19
|
+
if (position.line < range.start.line || position.line > range.end.line)
|
|
20
|
+
return false;
|
|
21
|
+
if (position.line === range.start.line && position.character < range.start.character)
|
|
22
|
+
return false;
|
|
23
|
+
if (position.line === range.end.line && position.character > range.end.character)
|
|
24
|
+
return false;
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Every declaration in [text]. A document that cannot be tokenized at all (an unterminated string, say) indexes as
|
|
29
|
+
* empty: the syntax error is reported on its own, and half a token stream would only misplace things.
|
|
30
|
+
*/
|
|
31
|
+
export function indexDocument(text) {
|
|
32
|
+
let tokens;
|
|
33
|
+
try {
|
|
34
|
+
tokens = tokenize(text);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
tokens = [];
|
|
38
|
+
}
|
|
39
|
+
const declarations = [];
|
|
40
|
+
let depth = 0; // brace depth, counted only outside argument lists
|
|
41
|
+
let paren = 0;
|
|
42
|
+
let container; // the type whose body we are in
|
|
43
|
+
let pending; // a type declaration waiting for its body
|
|
44
|
+
let openOp; // an operation, which has no body to close it
|
|
45
|
+
let afterAt = false;
|
|
46
|
+
let previous;
|
|
47
|
+
const closeOp = (end) => {
|
|
48
|
+
if (openOp && end)
|
|
49
|
+
openOp.full = { start: openOp.full.start, end: rangeOf(end).end };
|
|
50
|
+
openOp = undefined;
|
|
51
|
+
};
|
|
52
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
53
|
+
const t = tokens[i];
|
|
54
|
+
if (t.kind === "eof")
|
|
55
|
+
break;
|
|
56
|
+
const next = tokens[i + 1];
|
|
57
|
+
if (t.kind === "punct") {
|
|
58
|
+
if (t.value === "@") {
|
|
59
|
+
afterAt = true;
|
|
60
|
+
previous = t;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (paren === 0 && t.value === "{") {
|
|
64
|
+
depth++;
|
|
65
|
+
if (depth === 1 && pending) {
|
|
66
|
+
container = pending;
|
|
67
|
+
pending = undefined;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
else if (paren === 0 && t.value === "}") {
|
|
71
|
+
depth--;
|
|
72
|
+
if (depth <= 0) {
|
|
73
|
+
depth = 0;
|
|
74
|
+
if (container)
|
|
75
|
+
container.full = { start: container.full.start, end: rangeOf(t).end };
|
|
76
|
+
container = undefined;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
else if (t.value === "(") {
|
|
80
|
+
paren++;
|
|
81
|
+
}
|
|
82
|
+
else if (t.value === ")") {
|
|
83
|
+
paren = Math.max(0, paren - 1);
|
|
84
|
+
}
|
|
85
|
+
afterAt = false;
|
|
86
|
+
previous = t;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (afterAt) {
|
|
90
|
+
afterAt = false; // the annotation's own name, not a member
|
|
91
|
+
previous = t;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (t.kind === "name" && depth === 0 && paren === 0 && DEFS.has(t.value)) {
|
|
95
|
+
closeOp(previous);
|
|
96
|
+
if (t.value === "view") {
|
|
97
|
+
const owner = next;
|
|
98
|
+
const dot = tokens[i + 2];
|
|
99
|
+
const member = tokens[i + 3];
|
|
100
|
+
if (owner?.kind === "name" && dot?.kind === "punct" && dot.value === "." && member?.kind === "name") {
|
|
101
|
+
declarations.push({
|
|
102
|
+
path: `${owner.value}.${member.value}`,
|
|
103
|
+
name: member.value,
|
|
104
|
+
kind: "view",
|
|
105
|
+
keyword: "view",
|
|
106
|
+
container: owner.value,
|
|
107
|
+
range: rangeOf(member),
|
|
108
|
+
full: { start: startOf(t), end: rangeOf(member).end },
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
previous = t;
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (next?.kind === "name") {
|
|
115
|
+
const declaration = {
|
|
116
|
+
path: next.value,
|
|
117
|
+
name: next.value,
|
|
118
|
+
kind: OPS.has(t.value) ? "op" : "type",
|
|
119
|
+
keyword: t.value,
|
|
120
|
+
range: rangeOf(next),
|
|
121
|
+
full: { start: startOf(t), end: rangeOf(next).end },
|
|
122
|
+
};
|
|
123
|
+
declarations.push(declaration);
|
|
124
|
+
if (TYPES.has(t.value))
|
|
125
|
+
pending = declaration;
|
|
126
|
+
else
|
|
127
|
+
openOp = declaration;
|
|
128
|
+
}
|
|
129
|
+
previous = t;
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
if (t.kind === "name" && depth === 1 && paren === 0 && container) {
|
|
133
|
+
const member = next?.kind === "punct" && (next.value === ":" || next.value === "(");
|
|
134
|
+
if (member || container.keyword === "enum") {
|
|
135
|
+
declarations.push({
|
|
136
|
+
path: `${container.name}.${t.value}`,
|
|
137
|
+
name: t.value,
|
|
138
|
+
kind: member ? "field" : "enumValue",
|
|
139
|
+
keyword: member ? "field" : "value",
|
|
140
|
+
container: container.name,
|
|
141
|
+
range: rangeOf(t),
|
|
142
|
+
full: rangeOf(t),
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
previous = t;
|
|
147
|
+
}
|
|
148
|
+
closeOp(previous);
|
|
149
|
+
const byPath = new Map();
|
|
150
|
+
for (const d of declarations)
|
|
151
|
+
if (!byPath.has(d.path))
|
|
152
|
+
byPath.set(d.path, d);
|
|
153
|
+
return { tokens, declarations, byPath };
|
|
154
|
+
}
|
|
155
|
+
/** The declaration whose identifier is under [position]. */
|
|
156
|
+
export function declarationAt(index, position) {
|
|
157
|
+
return index.declarations.find((d) => contains(d.range, position));
|
|
158
|
+
}
|
|
159
|
+
/** The token under [position], which is what a hover or a completion is about. */
|
|
160
|
+
export function tokenAt(index, position) {
|
|
161
|
+
return index.tokens.find((t) => t.kind !== "eof" && contains(rangeOf(t), position));
|
|
162
|
+
}
|
|
163
|
+
/** Where a `validateIR` diagnostic belongs in the text; the first line when the path names nothing we found. */
|
|
164
|
+
export function rangeForPath(index, path) {
|
|
165
|
+
const declaration = index.byPath.get(path.endsWith("()") ? path.slice(0, -2) : path);
|
|
166
|
+
if (declaration)
|
|
167
|
+
return declaration.range;
|
|
168
|
+
const owner = path.split(".")[0];
|
|
169
|
+
const type = owner ? index.byPath.get(owner) : undefined;
|
|
170
|
+
if (type)
|
|
171
|
+
return type.range;
|
|
172
|
+
return { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } };
|
|
173
|
+
}
|
|
174
|
+
//# sourceMappingURL=positions.js.map
|
package/positions.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"positions.js","sourceRoot":"","sources":["../src/positions.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,QAAQ,EAAc,MAAM,iBAAiB,CAAC;AAmCvD,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,QAAQ,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,CAAU,CAAC;AACjH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,OAAO,EAAE,SAAS,EAAE,QAAQ,CAAU,CAAC;AACnE,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,GAAG,aAAa,EAAE,GAAG,WAAW,EAAE,MAAM,CAAU,CAAC;AAEhF,MAAM,KAAK,GAAG,IAAI,GAAG,CAAS,aAAa,CAAC,CAAC;AAC7C,MAAM,GAAG,GAAG,IAAI,GAAG,CAAS,WAAW,CAAC,CAAC;AACzC,MAAM,IAAI,GAAG,IAAI,GAAG,CAAS,YAAY,CAAC,CAAC;AAE3C,MAAM,OAAO,GAAG,CAAC,CAAQ,EAAY,EAAE,CAAC,CAAC,EAAE,IA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EAAE,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,EAAE,CAAC;AAC9E,CAAC","sourcesContent":["/**\n * Where each declaration sits in a `.rayfold` document.\n *\n * The IR carries no source positions, and `validateIR` reports its findings against paths (\"Book.price\", \"books()\").\n * This walks the same token stream the parser reads and records a range for every declaration, so a diagnostic, a\n * hover or a go-to-definition can be answered in the text the author is looking at.\n */\nimport { tokenize, type Token } from \"@rayfold/schema\";\n\n/** LSP positions are zero-based; the lexer's are one-based. */\nexport interface Position {\n line: number;\n character: number;\n}\n\nexport interface Range {\n start: Position;\n end: Position;\n}\n\nexport type DeclarationKind = \"type\" | \"field\" | \"op\" | \"enumValue\" | \"view\";\n\nexport interface Declaration {\n /** \"Book\", \"Book.title\", \"books\" - the path `validateIR` reports against, without the trailing (). */\n path: string;\n name: string;\n kind: DeclarationKind;\n /** what the source calls it: entity, object, query, command, ... */\n keyword: string;\n container?: string;\n /** the identifier itself, for a selection or a jump */\n range: Range;\n /** the whole declaration, for an outline */\n full: Range;\n}\n\nexport interface DocumentIndex {\n tokens: Token[];\n declarations: Declaration[];\n byPath: Map<string, Declaration>;\n}\n\nexport const TYPE_KEYWORDS = [\"entity\", \"object\", \"input\", \"enum\", \"union\", \"scalar\", \"error\", \"event\"] as const;\nexport const OP_KEYWORDS = [\"query\", \"command\", \"stream\"] as const;\nexport const DEF_KEYWORDS = [...TYPE_KEYWORDS, ...OP_KEYWORDS, \"view\"] as const;\n\nconst TYPES = new Set<string>(TYPE_KEYWORDS);\nconst OPS = new Set<string>(OP_KEYWORDS);\nconst DEFS = new Set<string>(DEF_KEYWORDS);\n\nconst startOf = (t: Token): Position => ({ line: t.line - 1, character: t.col - 1 });\nconst rangeOf = (t: Token): Range => ({ start: startOf(t), end: { line: t.line - 1, character: t.col - 1 + t.value.length } });\n\n/** True when [position] falls inside [range], counting both ends so a cursor at either edge of a word still hits it. */\nexport function contains(range: Range, position: Position): boolean {\n if (position.line < range.start.line || position.line > range.end.line) return false;\n if (position.line === range.start.line && position.character < range.start.character) return false;\n if (position.line === range.end.line && position.character > range.end.character) return false;\n return true;\n}\n\n/**\n * Every declaration in [text]. A document that cannot be tokenized at all (an unterminated string, say) indexes as\n * empty: the syntax error is reported on its own, and half a token stream would only misplace things.\n */\nexport function indexDocument(text: string): DocumentIndex {\n let tokens: Token[];\n try {\n tokens = tokenize(text);\n } catch {\n tokens = [];\n }\n\n const declarations: Declaration[] = [];\n let depth = 0; // brace depth, counted only outside argument lists\n let paren = 0;\n let container: Declaration | undefined; // the type whose body we are in\n let pending: Declaration | undefined; // a type declaration waiting for its body\n let openOp: Declaration | undefined; // an operation, which has no body to close it\n let afterAt = false;\n let previous: Token | undefined;\n\n const closeOp = (end: Token | undefined): void => {\n if (openOp && end) openOp.full = { start: openOp.full.start, end: rangeOf(end).end };\n openOp = undefined;\n };\n\n for (let i = 0; i < tokens.length; i++) {\n const t = tokens[i]!;\n if (t.kind === \"eof\") break;\n const next = tokens[i + 1];\n\n if (t.kind === \"punct\") {\n if (t.value === \"@\") {\n afterAt = true;\n previous = t;\n continue;\n }\n if (paren === 0 && t.value === \"{\") {\n depth++;\n if (depth === 1 && pending) {\n container = pending;\n pending = undefined;\n }\n } else if (paren === 0 && t.value === \"}\") {\n depth--;\n if (depth <= 0) {\n depth = 0;\n if (container) container.full = { start: container.full.start, end: rangeOf(t).end };\n container = undefined;\n }\n } else if (t.value === \"(\") {\n paren++;\n } else if (t.value === \")\") {\n paren = Math.max(0, paren - 1);\n }\n afterAt = false;\n previous = t;\n continue;\n }\n\n if (afterAt) {\n afterAt = false; // the annotation's own name, not a member\n previous = t;\n continue;\n }\n\n if (t.kind === \"name\" && depth === 0 && paren === 0 && DEFS.has(t.value)) {\n closeOp(previous);\n if (t.value === \"view\") {\n const owner = next;\n const dot = tokens[i + 2];\n const member = tokens[i + 3];\n if (owner?.kind === \"name\" && dot?.kind === \"punct\" && dot.value === \".\" && member?.kind === \"name\") {\n declarations.push({\n path: `${owner.value}.${member.value}`,\n name: member.value,\n kind: \"view\",\n keyword: \"view\",\n container: owner.value,\n range: rangeOf(member),\n full: { start: startOf(t), end: rangeOf(member).end },\n });\n }\n previous = t;\n continue;\n }\n if (next?.kind === \"name\") {\n const declaration: Declaration = {\n path: next.value,\n name: next.value,\n kind: OPS.has(t.value) ? \"op\" : \"type\",\n keyword: t.value,\n range: rangeOf(next),\n full: { start: startOf(t), end: rangeOf(next).end },\n };\n declarations.push(declaration);\n if (TYPES.has(t.value)) pending = declaration;\n else openOp = declaration;\n }\n previous = t;\n continue;\n }\n\n if (t.kind === \"name\" && depth === 1 && paren === 0 && container) {\n const member = next?.kind === \"punct\" && (next.value === \":\" || next.value === \"(\");\n if (member || container.keyword === \"enum\") {\n declarations.push({\n path: `${container.name}.${t.value}`,\n name: t.value,\n kind: member ? \"field\" : \"enumValue\",\n keyword: member ? \"field\" : \"value\",\n container: container.name,\n range: rangeOf(t),\n full: rangeOf(t),\n });\n }\n }\n previous = t;\n }\n closeOp(previous);\n\n const byPath = new Map<string, Declaration>();\n for (const d of declarations) if (!byPath.has(d.path)) byPath.set(d.path, d);\n return { tokens, declarations, byPath };\n}\n\n/** The declaration whose identifier is under [position]. */\nexport function declarationAt(index: DocumentIndex, position: Position): Declaration | undefined {\n return index.declarations.find((d) => contains(d.range, position));\n}\n\n/** The token under [position], which is what a hover or a completion is about. */\nexport function tokenAt(index: DocumentIndex, position: Position): Token | undefined {\n return index.tokens.find((t) => t.kind !== \"eof\" && contains(rangeOf(t), position));\n}\n\n/** Where a `validateIR` diagnostic belongs in the text; the first line when the path names nothing we found. */\nexport function rangeForPath(index: DocumentIndex, path: string): Range {\n const declaration = index.byPath.get(path.endsWith(\"()\") ? path.slice(0, -2) : path);\n if (declaration) return declaration.range;\n const owner = path.split(\".\")[0];\n const type = owner ? index.byPath.get(owner) : undefined;\n if (type) return type.range;\n return { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } };\n}\n"]}
|