@sanity/groq-lsp 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-present Sanity.io
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,329 @@
1
+ # @sanity/groq-lsp
2
+
3
+ Language Server Protocol (LSP) implementation for GROQ - provides IDE features for GROQ queries in any editor.
4
+
5
+ ## Features
6
+
7
+ | Feature | Description |
8
+ | --------------- | ----------------------------------------- |
9
+ | **Diagnostics** | Real-time linting via `@sanity/groq-lint` |
10
+ | **Hover** | Type information and documentation |
11
+ | **Completion** | Field names, functions, document types |
12
+ | **Formatting** | Via `prettier-plugin-groq` |
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ npm install @sanity/groq-lsp
18
+ # or
19
+ pnpm add @sanity/groq-lsp
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ### As a Language Server (CLI)
25
+
26
+ Start the server using Node IPC:
27
+
28
+ ```bash
29
+ npx @sanity/groq-lsp
30
+ ```
31
+
32
+ The server communicates via stdio and can be connected to any LSP-compatible editor.
33
+
34
+ ### As a Library
35
+
36
+ ```typescript
37
+ import {
38
+ SchemaLoader,
39
+ extractQueries,
40
+ computeDocumentDiagnostics,
41
+ getCompletions,
42
+ getHoverInfo,
43
+ formatQuery,
44
+ } from '@sanity/groq-lsp'
45
+
46
+ // Load schema
47
+ const loader = new SchemaLoader()
48
+ loader.loadFromPath('./schema.json')
49
+ // Or auto-discover: loader.discoverSchema(workspaceRoot)
50
+
51
+ // Extract GROQ queries from source file
52
+ const { queries } = extractQueries(sourceCode, 'typescript')
53
+
54
+ // Get diagnostics
55
+ const diagnostics = computeDocumentDiagnostics(queries, {
56
+ schema: loader.getSchema(),
57
+ })
58
+
59
+ // Get completions at a position
60
+ const completions = getCompletions(query, cursorOffset, {
61
+ schema: loader.getSchema(),
62
+ })
63
+
64
+ // Get hover info at a position
65
+ const hover = getHoverInfo(query, cursorOffset, {
66
+ schema: loader.getSchema(),
67
+ })
68
+
69
+ // Format a query
70
+ const edits = await formatQuery(query, { tabSize: 2 })
71
+ ```
72
+
73
+ ## Editor Integration
74
+
75
+ ### VS Code
76
+
77
+ The server is designed to work with VS Code's LSP client. Configure your extension's `package.json`:
78
+
79
+ ```json
80
+ {
81
+ "contributes": {
82
+ "languages": [
83
+ {
84
+ "id": "groq",
85
+ "extensions": [".groq"],
86
+ "aliases": ["GROQ"]
87
+ }
88
+ ]
89
+ }
90
+ }
91
+ ```
92
+
93
+ And in your extension code:
94
+
95
+ ```typescript
96
+ import { LanguageClient, TransportKind } from 'vscode-languageclient/node'
97
+
98
+ const serverModule = require.resolve('@sanity/groq-lsp/dist/server.js')
99
+
100
+ const client = new LanguageClient(
101
+ 'groqLanguageServer',
102
+ 'GROQ Language Server',
103
+ {
104
+ run: { module: serverModule, transport: TransportKind.ipc },
105
+ debug: { module: serverModule, transport: TransportKind.ipc },
106
+ },
107
+ {
108
+ documentSelector: [
109
+ { scheme: 'file', language: 'groq' },
110
+ { scheme: 'file', language: 'typescript' },
111
+ { scheme: 'file', language: 'typescriptreact' },
112
+ ],
113
+ }
114
+ )
115
+
116
+ client.start()
117
+ ```
118
+
119
+ ### Neovim (nvim-lspconfig)
120
+
121
+ ```lua
122
+ local lspconfig = require('lspconfig')
123
+ local configs = require('lspconfig.configs')
124
+
125
+ if not configs.groq_lsp then
126
+ configs.groq_lsp = {
127
+ default_config = {
128
+ cmd = { 'npx', '@sanity/groq-lsp' },
129
+ filetypes = { 'groq', 'typescript', 'typescriptreact' },
130
+ root_dir = lspconfig.util.root_pattern('schema.json', 'sanity.config.ts'),
131
+ },
132
+ }
133
+ end
134
+
135
+ lspconfig.groq_lsp.setup{}
136
+ ```
137
+
138
+ ## Schema Discovery
139
+
140
+ The server automatically searches for schema files in these locations:
141
+
142
+ 1. `schema.json`
143
+ 2. `sanity.schema.json`
144
+ 3. `.sanity/schema.json`
145
+ 4. `studio/schema.json`
146
+
147
+ Generate a schema file from your Sanity project:
148
+
149
+ ```bash
150
+ npx sanity schema extract --path schema.json
151
+ ```
152
+
153
+ ### Configuration
154
+
155
+ The server accepts configuration via the LSP `workspace/configuration` request:
156
+
157
+ ```typescript
158
+ interface Settings {
159
+ // Path to schema.json file
160
+ schemaPath?: string
161
+ // Maximum number of diagnostics to report (default: 100)
162
+ maxDiagnostics?: number
163
+ // Enable formatting (default: true)
164
+ enableFormatting?: boolean
165
+ }
166
+ ```
167
+
168
+ In VS Code, configure via `settings.json`:
169
+
170
+ ```json
171
+ {
172
+ "groq.schemaPath": "./studio/schema.json",
173
+ "groq.maxDiagnostics": 50,
174
+ "groq.enableFormatting": true
175
+ }
176
+ ```
177
+
178
+ ## Supported Languages
179
+
180
+ The server provides features for:
181
+
182
+ | Language | File Types | Query Detection |
183
+ | ---------- | ------------- | ----------------------------- |
184
+ | GROQ | `.groq` | Entire file |
185
+ | TypeScript | `.ts`, `.tsx` | `groq`...`` template literals |
186
+ | JavaScript | `.js`, `.jsx` | `groq`...`` template literals |
187
+
188
+ ## LSP Capabilities
189
+
190
+ ### Diagnostics
191
+
192
+ Automatically validates GROQ queries on document change:
193
+
194
+ - **Performance rules** - Always enabled (join-in-filter, pagination, etc.)
195
+ - **Schema-aware rules** - When schema is available (invalid-type-filter, unknown-field)
196
+
197
+ ### Completion
198
+
199
+ Provides intelligent completions based on context:
200
+
201
+ ```groq
202
+ *[_type == "|"] // Suggests: "post", "author", etc.
203
+ *[_type == "post"]{ | } // Suggests: title, body, _id, etc.
204
+ cou| // Suggests: count()
205
+ ```
206
+
207
+ ### Hover
208
+
209
+ Shows type information and documentation:
210
+
211
+ ```
212
+ _type: string
213
+ Document type name
214
+ ```
215
+
216
+ ### Formatting
217
+
218
+ Formats GROQ queries using `prettier-plugin-groq`:
219
+
220
+ ```groq
221
+ // Before
222
+ *[_type=="post"&&published==true]{title,body,...}
223
+
224
+ // After
225
+ *[_type == "post" && published == true] {
226
+ title,
227
+ body,
228
+ ...
229
+ }
230
+ ```
231
+
232
+ ## API Reference
233
+
234
+ ### `SchemaLoader`
235
+
236
+ Manages schema loading and caching.
237
+
238
+ ```typescript
239
+ const loader = new SchemaLoader()
240
+
241
+ // Load from specific path
242
+ loader.loadFromPath('./schema.json')
243
+
244
+ // Auto-discover in workspace
245
+ loader.discoverSchema('/path/to/workspace')
246
+
247
+ // Get current schema
248
+ const schema = loader.getSchema()
249
+
250
+ // Watch for changes
251
+ loader.startWatching((newSchema) => {
252
+ console.log('Schema updated')
253
+ })
254
+
255
+ // Clean up
256
+ loader.stopWatching()
257
+ loader.clear()
258
+ ```
259
+
260
+ ### `extractQueries`
261
+
262
+ Extracts GROQ queries from source files.
263
+
264
+ ```typescript
265
+ const { queries, errors } = extractQueries(content, 'typescript')
266
+ // Returns: { queries: GroqQuery[], errors: string[] }
267
+ ```
268
+
269
+ ### `computeDocumentDiagnostics`
270
+
271
+ Computes LSP diagnostics for extracted queries.
272
+
273
+ ```typescript
274
+ const diagnostics = computeDocumentDiagnostics(queries, { schema })
275
+ // Returns: Diagnostic[]
276
+ ```
277
+
278
+ ### `getCompletions`
279
+
280
+ Gets completion items at a position.
281
+
282
+ ```typescript
283
+ const items = getCompletions(query, cursorOffset, { schema })
284
+ // Returns: CompletionItem[]
285
+ ```
286
+
287
+ ### `getHoverInfo`
288
+
289
+ Gets hover information at a position.
290
+
291
+ ```typescript
292
+ const hover = getHoverInfo(query, cursorOffset, { schema })
293
+ // Returns: Hover | null
294
+ ```
295
+
296
+ ### `formatQuery` / `formatDocument`
297
+
298
+ Formats GROQ queries using Prettier.
299
+
300
+ ```typescript
301
+ const edits = await formatQuery(query, { tabSize: 2 })
302
+ // Returns: TextEdit[]
303
+ ```
304
+
305
+ ## Development
306
+
307
+ ```bash
308
+ # Install dependencies
309
+ pnpm install
310
+
311
+ # Build
312
+ pnpm build
313
+
314
+ # Test
315
+ pnpm test
316
+
317
+ # Watch mode
318
+ pnpm dev
319
+ ```
320
+
321
+ ## Related Packages
322
+
323
+ - [`@sanity/groq-lint`](../groq-lint) - GROQ linting rules
324
+ - [`prettier-plugin-groq`](../prettier-plugin-groq) - Prettier plugin for GROQ
325
+ - [`eslint-plugin-sanity`](../eslint-plugin) - ESLint plugin for Sanity
326
+
327
+ ## License
328
+
329
+ MIT