summaryrefslogtreecommitdiff
path: root/lsp.h
blob: 61c56107fba89263ff1e67cc65304de33f6db4af (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
// functions for dealing with LSP (Language Server Protocol) servers.
// don't assume any of the public functions defined here are thread-safe.

#ifndef LSP_H_
#define LSP_H_

#include "base.h"
#include "ds.h"
#include "os.h"

// an ID specific to a path. 0 is a valid document ID, currently.
typedef u32 LSPDocumentID;
// ID of an LSP server. a server's ID is never 0.
typedef u32 LSPID;
// a request ID. this is unique across all servers. a request's ID is never 0.
typedef u32 LSPRequestID;
typedef struct SDL_mutex *LSPMutex;
typedef struct SDL_semaphore *LSPSemaphore;
typedef struct SDL_Thread *LSPThread;

// a struct for keeping track of a LSP server ID and a request ID
typedef struct {
	LSPID lsp;
	LSPRequestID id;
} LSPServerRequestID;

// interface Position in the LSP spec
typedef struct {
	u32 line;
	// NOTE: this is the UTF-16 character index!
	u32 character;
} LSPPosition;

// interface TextDocumentPositionParams in the LSP spec
typedef struct {
	LSPDocumentID document;
	LSPPosition pos;
} LSPDocumentPosition;

typedef enum {
	LSP_REQUEST,
	LSP_RESPONSE
} LSPMessageType;

// A string in a LSPResponse
typedef struct {
	// offset into string_data
	u32 offset;
} LSPString;

// interface Range in the LSP spec
typedef struct {
	LSPPosition start;
	LSPPosition end;
} LSPRange;

// interface Location in the LSP spec
typedef struct {
	LSPDocumentID document;
	LSPRange range;
} LSPLocation;

typedef enum {
	LSP_REQUEST_NONE,
	
	// client-to-server
	LSP_REQUEST_INITIALIZE, // initialize
	LSP_REQUEST_INITIALIZED, // initialized
	LSP_REQUEST_CANCEL, // $/cancelRequest
	LSP_REQUEST_CONFIGURATION, // workspace/didChangeConfiguration
	LSP_REQUEST_SHUTDOWN, // shutdown
	LSP_REQUEST_EXIT, // exit
	LSP_REQUEST_DID_OPEN, // textDocument/didOpen
	LSP_REQUEST_DID_CLOSE, // textDocument/didClose
	LSP_REQUEST_DID_CHANGE, // textDocument/didChange
	LSP_REQUEST_COMPLETION, // textDocument/completion
	LSP_REQUEST_SIGNATURE_HELP, // textDocument/signatureHelp
	LSP_REQUEST_HOVER, // textDocument/hover
	LSP_REQUEST_DEFINITION, // textDocument/definition
	LSP_REQUEST_DECLARATION, // textDocument/declaration
	LSP_REQUEST_TYPE_DEFINITION, // textDocument/typeDefinition
	LSP_REQUEST_IMPLEMENTATION, // textDocument/implementation
	LSP_REQUEST_HIGHLIGHT, // textDocument/documentHighlight
	LSP_REQUEST_REFERENCES, // textDocument/references
	LSP_REQUEST_RENAME, // textDocument/rename
	LSP_REQUEST_WORKSPACE_SYMBOLS, // workspace/symbol
	LSP_REQUEST_DID_CHANGE_WORKSPACE_FOLDERS, // workspace/didChangeWorkspaceFolders
	
	// server-to-client
	LSP_REQUEST_SHOW_MESSAGE, // window/showMessage and window/showMessageRequest
	LSP_REQUEST_LOG_MESSAGE, // window/logMessage
	LSP_REQUEST_WORKSPACE_FOLDERS, // workspace/workspaceFolders - NOTE: this is handled directly in lsp-parse.c (because it only needs information from the LSP struct)
} LSPRequestType;

typedef enum {
	LSP_ERROR_PARSE = -32700,
	LSP_ERROR_INVALID_REQUEST = -32600,
	LSP_ERROR_METHOD_NOT_FOUND = -32601,
	LSP_ERROR_INVALID_PARAMS = -32602,
	LSP_ERROR_INTERNAL = -32603,
	LSP_ERROR_SERVER_NOT_INITIALIZED = -32002,
	LSP_ERROR_UNKNOWN_CODE = -32001,
	LSP_ERROR_REQUEST_FAILED = -32803,
	LSP_ERROR_SERVER_CANCELLED = -32802,
	LSP_ERROR_CONTENT_MODIFIED = -32801,
	LSP_ERROR_REQUEST_CANCELLED = -32800,
} LSPError;

typedef struct {
	LSPRequestID id;
} LSPRequestCancel;

typedef struct {
	Language language;
	LSPDocumentID document;
	// freed by lsp_request_free
	char *file_contents;
} LSPRequestDidOpen;

typedef struct {
	LSPDocumentID document;
} LSPRequestDidClose;

// see TextDocumentContentChangeEvent in the LSP spec
typedef struct {
	LSPRange range;
	// new text. will be freed. you can use NULL for the empty string.
	char *text;
} LSPDocumentChangeEvent;

typedef struct {
	LSPDocumentID document;
	LSPDocumentChangeEvent *changes; // dynamic array
} LSPRequestDidChange;

typedef enum {
	LSP_WINDOW_MESSAGE_ERROR = 1,
	LSP_WINDOW_MESSAGE_WARNING = 2,
	LSP_WINDOW_MESSAGE_INFO = 3,
	LSP_WINDOW_MESSAGE_LOG = 4
} LSPWindowMessageType;

typedef struct {
	LSPWindowMessageType type;
	// freed by lsp_request_free
	char *message;
} LSPRequestMessage;


// these triggers are used for completion context and signature help context.
#define LSP_TRIGGER_NONE 0 // not actually defined in LSP spec
#define LSP_TRIGGER_INVOKED 1
#define LSP_TRIGGER_CHARACTER 2
#define LSP_TRIGGER_INCOMPLETE 3
#define LSP_TRIGGER_CONTENT_CHANGE 3
typedef u8 LSPCompletionTriggerKind;
typedef u8 LSPSignatureHelpTriggerKind;

typedef struct {
	LSPCompletionTriggerKind trigger_kind;
	char trigger_character[5];
} LSPCompletionContext;

typedef struct {
	LSPDocumentPosition position;
	LSPCompletionContext context;
} LSPRequestCompletion;

typedef struct {
	LSPDocumentPosition position;
} LSPRequestSignatureHelp;

typedef struct {
	LSPDocumentPosition position;
} LSPRequestHover;

typedef struct {
	LSPDocumentPosition position;
} LSPRequestDefinition;

typedef struct {
	LSPDocumentPosition position;
} LSPRequestHighlight;

typedef struct {
	LSPDocumentPosition position;
	// include the declaration of the symbol as a reference
	bool include_declaration;
} LSPRequestReferences;

typedef struct {
	// string to filter the symbols by
	char *query;
} LSPRequestWorkspaceSymbols;

typedef struct {
	LSPDocumentPosition position;
	char *new_name;
} LSPRequestRename;

typedef struct {
	LSPDocumentID *removed; // dynamic array
	LSPDocumentID *added; // dynamic array
} LSPRequestDidChangeWorkspaceFolders;

typedef struct {
	// this string should be valid JSON. will be freed.
	char *settings;
} LSPRequestConfiguration;

typedef struct {
	LSPRequestID id;
	LSPRequestType type;
	char *id_string; // if not NULL, this is the ID (only for server-to-client messages; we always use integer IDs)
	// one member of this union is set depending on `type`.
	union {	
		LSPRequestCancel cancel;
		LSPRequestDidOpen open;
		LSPRequestDidClose close;
		LSPRequestDidChange change;
		LSPRequestConfiguration configuration;
		LSPRequestCompletion completion;
		LSPRequestSignatureHelp signature_help;
		LSPRequestHover hover;
		// LSP_REQUEST_DEFINITION, LSP_REQUEST_DECLARATION, LSP_REQUEST_TYPE_DEFINITION, or LSP_REQUEST_IMPLEMENTATION
		LSPRequestDefinition definition;
		LSPRequestHighlight highlight;
		LSPRequestReferences references;
		LSPRequestWorkspaceSymbols workspace_symbols;
		// LSP_REQUEST_SHOW_MESSAGE or LSP_REQUEST_LOG_MESSAGE
		LSPRequestMessage message;
		LSPRequestDidChangeWorkspaceFolders change_workspace_folders;
		LSPRequestRename rename;
	} data;
} LSPRequest;

typedef enum {
	// LSP doesn't actually define this but this will be used for unrecognized values
	//  (in case they add more symbol kinds in the future)
	LSP_SYMBOL_OTHER = 0,
	
	#define LSP_SYMBOL_KIND_MIN 1
	LSP_SYMBOL_FILE = 1,
	LSP_SYMBOL_MODULE = 2,
	LSB_SYMBOL_NAMESPACE = 3,
	LSP_SYMBOL_PACKAGE = 4,
	LSP_SYMBOL_CLASS = 5,
	LSP_SYMBOL_METHOD = 6,
	LSP_SYMBOL_PROPERTY = 7,
	LSP_SYMBOL_FIELD = 8,
	LSP_SYMBOL_CONSTRUCTOR = 9,
	LSP_SYMBOL_ENUM = 10,
	LSP_SYMBOL_INTERFACE = 11,
	LSP_SYMBOL_FUNCTION = 12,
	LSP_SYMBOL_VARIABLE = 13,
	LSP_SYMBOL_CONSTANT = 14,
	LSP_SYMBOL_STRING = 15,
	LSP_SYMBOL_NUMBER = 16,
	LSP_SYMBOL_BOOLEAN = 17,
	LSP_SYMBOL_ARRAY = 18,
	LSP_SYMBOL_OBJECT = 19,
	LSP_SYMBOL_KEY = 20,
	LSP_SYMBOL_NULL = 21,
	LSP_SYMBOL_ENUMMEMBER = 22,
	LSP_SYMBOL_STRUCT = 23,
	LSP_SYMBOL_EVENT = 24,
	LSP_SYMBOL_OPERATOR = 25,
	LSP_SYMBOL_TYPEPARAMETER = 26,
	#define LSP_SYMBOL_KIND_MAX 26
} LSPSymbolKind;

typedef enum {
	#define LSP_COMPLETION_KIND_MIN 1
	LSP_COMPLETION_TEXT = 1,
	LSP_COMPLETION_METHOD = 2,
	LSP_COMPLETION_FUNCTION = 3,
	LSP_COMPLETION_CONSTRUCTOR = 4,
	LSP_COMPLETION_FIELD = 5,
	LSP_COMPLETION_VARIABLE = 6,
	LSP_COMPLETION_CLASS = 7,
	LSP_COMPLETION_INTERFACE = 8,
	LSP_COMPLETION_MODULE = 9,
	LSP_COMPLETION_PROPERTY = 10,
	LSP_COMPLETION_UNIT = 11,
	LSP_COMPLETION_VALUE = 12,
	LSP_COMPLETION_ENUM = 13,
	LSP_COMPLETION_KEYWORD = 14,
	LSP_COMPLETION_SNIPPET = 15,
	LSP_COMPLETION_COLOR = 16,
	LSP_COMPLETION_FILE = 17,
	LSP_COMPLETION_REFERENCE = 18,
	LSP_COMPLETION_FOLDER = 19,
	LSP_COMPLETION_ENUMMEMBER = 20,
	LSP_COMPLETION_CONSTANT = 21,
	LSP_COMPLETION_STRUCT = 22,
	LSP_COMPLETION_EVENT = 23,
	LSP_COMPLETION_OPERATOR = 24,
	LSP_COMPLETION_TYPEPARAMETER = 25,
	#define LSP_COMPLETION_KIND_MAX 25
} LSPCompletionKind;

// interface TextEdit in the LSP spec
typedef struct {	
	LSPRange range;
	LSPString new_text;
} LSPTextEdit;

// see InsertTextFormat in the LSP spec.
typedef enum {
	// plain text
	LSP_COMPLETION_EDIT_PLAIN = 1,
	// snippet   e.g. "some_method($1, $2)$0"
	LSP_COMPLETION_EDIT_SNIPPET = 2
} LSPCompletionEditType;


// interface CompletionItem in the LSP spec
typedef struct {
	// display text for this completion
	LSPString label;
	// text used to filter completions
	LSPString filter_text;
	// more detail for this item, e.g. the signature of a function
	LSPString detail;
	// documentation for this item (typically from a doc comment)
	LSPString documentation;
	// the edit to be applied when this completion is selected.
	LSPTextEdit text_edit;
	// type for text_edit
	LSPCompletionEditType edit_type;
	// if set to true, `text_edit.range` should be ignored
	//  -- this is a completion which uses insertText.
	// how to handle this:
	// "VS Code when code complete is requested in this example
	// `con<cursor position>` and a completion item with an `insertText` of
	// `console` is provided it will only insert `sole`"
	bool at_cursor;
	// note: the items are sorted here in this file,
	// so you probably don't need to access this.
	LSPString sort_text;
	// is this function/type/whatever deprecated?
	bool deprecated;
	// type of completion
	LSPCompletionKind kind;
} LSPCompletionItem;

typedef struct {
	// should completions be re-requested when more characters are typed?
	bool is_complete;
	// dynamic array
	LSPCompletionItem *items;
} LSPResponseCompletion;

typedef struct {
	LSPString label;
	// NOTE: LSP gives us parameter information for *all*
	// parameters, but we only really need it for the active parameter.
	
	// (UTF-16) indices into `label` indicating which
	// part of it should be highlighted for the active parameter
	u16 active_start;
	u16 active_end;
} LSPSignatureInformation;

typedef struct {
	// NOTE: the "active" signature will be the first one
	// in this array.
	LSPSignatureInformation *signatures;
} LSPResponseSignatureHelp;

typedef struct {
	// the range of text to highlight
	LSPRange range;
	// little tool tip to show
	LSPString contents;
} LSPResponseHover;

typedef struct {
	// where the symbol is defined (dynamic array)
	LSPLocation *locations;
} LSPResponseDefinition;

typedef enum {
#define LSP_HIGHLIGHT_MIN 1
	LSP_HIGHLIGHT_TEXT = 1,
	LSP_HIGHLIGHT_READ = 2,
	LSP_HIGHLIGHT_WRITE = 3,
#define LSP_HIGHLIGHT_MAX 3
} LSPHighlightKind;

// interface DocumentHighlight in the LSP spec
typedef struct {
	LSPRange range;
	LSPHighlightKind kind;
} LSPHighlight;

typedef struct {
	LSPHighlight *highlights;
} LSPResponseHighlight;

typedef struct {
	// these will be sorted by path (alphabetically), then by line number
	LSPLocation *locations;
} LSPResponseReferences;

typedef enum {
	#define LSP_SYMBOL_TAG_MIN 1
	LSP_SYMBOL_TAG_DEPRECATED = 1
	#define LSP_SYMBOL_TAG_MAX 1
} LSPSymbolTag;

// SymbolInformation in the LSP spec
typedef struct {
	LSPString name;
	LSPSymbolKind kind;
	bool deprecated;
	LSPLocation location;
	// the "symbol containing this symbol"
	// e.g. multiple classes might have a "foo" method, so this can be used to distinguish them.
	LSPString container;
} LSPSymbolInformation;

typedef struct {
	LSPSymbolInformation *symbols;
} LSPResponseWorkspaceSymbols;

typedef enum {
	LSP_CHANGE_EDIT = 1,
	LSP_CHANGE_CREATE,
	LSP_CHANGE_RENAME,
	LSP_CHANGE_DELETE
} LSPWorkspaceChangeType;

typedef struct {
	LSPDocumentID document;
	LSPTextEdit edit;
} LSPWorkspaceChangeEdit;

typedef struct {
	LSPDocumentID document;
	bool overwrite;
	bool ignore_if_exists;
} LSPWorkspaceChangeCreate;

typedef struct {
	LSPDocumentID old;
	LSPDocumentID new;
	bool overwrite;
	bool ignore_if_exists;
} LSPWorkspaceChangeRename;

typedef struct {
	LSPDocumentID document;
	bool recursive;
	bool ignore_if_not_exists;
} LSPWorkspaceChangeDelete;

// this doesn't exist in the LSP spec. it represents
// a single change from a WorkspaceEdit.
typedef struct {
	LSPWorkspaceChangeType type;
	union {
		LSPWorkspaceChangeEdit edit;
		LSPWorkspaceChangeCreate create;
		LSPWorkspaceChangeRename rename;
		LSPWorkspaceChangeDelete delete;
	} data;
} LSPWorkspaceChange;

typedef struct {
	LSPWorkspaceChange *changes;
} LSPWorkspaceEdit;
typedef LSPWorkspaceEdit LSPResponseRename;

typedef struct {
	LSPRequest request; // the request which this is a response to
	char *error; // if not NULL, the data field will just be zeroed
	// LSP responses tend to have a lot of strings.
	// to avoid doing a ton of allocations+frees,
	// they're all stored here.
	char *string_data;
	// one of these is filled based on request.type
	union {
		LSPResponseCompletion completion;
		LSPResponseSignatureHelp signature_help;
		LSPResponseHover hover;
		// LSP_REQUEST_DEFINITION, LSP_REQUEST_DECLARATION, LSP_REQUEST_TYPE_DEFINITION, or LSP_REQUEST_IMPLEMENTATION
		LSPResponseDefinition definition;
		LSPResponseWorkspaceSymbols workspace_symbols;
		LSPResponseRename rename;
		LSPResponseHighlight highlight;
		LSPResponseReferences references;
	} data;
} LSPResponse;

typedef struct {
	LSPMessageType type;
	union {
		LSPRequest request;
		LSPResponse response;
	} u;
} LSPMessage;

typedef struct {
	char *path;
	u32 version_number; // for LSP
} LSPDocumentData;

typedef struct {
	bool signature_help_support;
	bool completion_support;
	bool hover_support;
	bool definition_support;
	bool declaration_support;
	bool implementation_support;
	bool type_definition_support;
	bool workspace_symbols_support;
	bool highlight_support;
	// support for multiple root folders
	// sadly, as of me writing this, clangd and rust-analyzer don't support this
	// (but jdtls and gopls do)
	bool workspace_folders_support;
	bool rename_support;
	bool references_support;
} LSPCapabilities;

typedef struct LSP {
	// thread safety is important here!
	// every member should either be indented to indicate which mutex controls it,
	// or have a comment explaining why it doesn't need one

	// A unique ID number for this LSP.
	// thread-safety: only set once in lsp_create.
	LSPID id;
	
	// thread-safety: set once in lsp_create, then only used by communication thread
	FILE *log;
	
	// The server process
	// thread-safety: created in lsp_create, then only accessed by the communication thread
	Process *process;
	
	LSPMutex document_mutex;
		// for our purposes, folders are "documents"
		// the spec kinda does this too: WorkspaceFolder has a `uri: DocumentUri` member.
		StrHashTable document_ids; // values are u32. they are indices into document_data.
		// this is a dynamic array which just keeps growing.
		// but the user isn't gonna open millions of files so it's fine.
		LSPDocumentData *document_data;
	LSPMutex messages_mutex;
		LSPMessage *messages_server2client;
		LSPMessage *messages_client2server;
		// we keep track of client-to-server requests
		// so that we can process responses.
		// this also lets us re-send requests if that's ever necessary.
		LSPRequest *requests_sent;
	// has the response to the initialize request been sent?
	// thread-safety: this starts out false, and only gets set to true once
	//                (when the initialize response is received)
	bool initialized;
	// thread-safety: only set once in lsp_create.
	char *command;
	// this is set in lsp_create, then later set to NULL when we send over the configuration (after the initialized notification).
	// thread-safety: set once in lsp_create, then only acessed once in communication thread.
	char *configuration_to_send;
	LSPThread communication_thread;
	LSPSemaphore quit_sem;
	// thread-safety: only accessed in communication thread
	char *received_data; // dynamic array
	// thread-safety: in the communication thread, we fill this in, then set `initialized = true`.
	//                after that, this never changes.
	//                never accessed in main thread before `initialized = true`.
	LSPCapabilities capabilities;
	// thread-safety: same as `capabilities`
	char32_t *completion_trigger_chars; // dynamic array
	// thread-safety: same as `capabilities`
	char32_t *signature_help_trigger_chars; // dynamic array
	// thread-safety: same as `capabilities`
	char32_t *signature_help_retrigger_chars; // dynamic array
	LSPMutex workspace_folders_mutex;
		// dynamic array of root directories of LSP workspace folders
		LSPDocumentID *workspace_folders;
	LSPMutex error_mutex;
		char error[256];
} LSP;

// returns true if there's an error.
// returns false and sets error to "" if there's no error.
// if clear = true, the error will be cleared.
// you can set error = NULL, error_size = 0, clear = true to just clear the error
bool lsp_get_error(LSP *lsp, char *error, size_t error_size, bool clear);
void lsp_message_free(LSPMessage *message);
u32 lsp_document_id(LSP *lsp, const char *path);
// returned pointer lives as long as lsp.
const char *lsp_document_path(LSP *lsp, LSPDocumentID id);
// returns the ID of the sent request, or (LSPServerRequestID){0} if the request is not supported by the LSP
// don't free the contents of this request (even on failure)! let me handle it!
LSPServerRequestID lsp_send_request(LSP *lsp, LSPRequest *request);
// send a $/cancelRequest notification
// if id = 0, nothing will happen.
void lsp_cancel_request(LSP *lsp, LSPRequestID id);
// don't free the contents of this response! let me handle it!
void lsp_send_response(LSP *lsp, LSPResponse *response);
const char *lsp_response_string(const LSPResponse *response, LSPString string);
// Start up an LSP server.
// configuration and log can be NULL.
LSP *lsp_create(const char *root_dir, const char *command, const char *configuration, FILE *log);
// try to add a new "workspace folder" to the lsp.
// IMPORTANT: only call this if lsp->initialized is true
//            (if not we don't yet know whether the server supports workspace folders)
// returns true on success or if new_root_dir is already contained in a workspace folder for this LSP.
// if this fails (i.e. if the LSP does not have workspace support), create a new LSP
// with root directory `new_root_dir`.
bool lsp_try_add_root_dir(LSP *lsp, const char *new_root_dir);
// report that this document has changed
void lsp_document_changed(LSP *lsp, const char *document, LSPDocumentChangeEvent change);
// is this path in the LSP's workspace folders?
bool lsp_covers_path(LSP *lsp, const char *path);
// get next message from server
bool lsp_next_message(LSP *lsp, LSPMessage *message);
bool lsp_position_eq(LSPPosition a, LSPPosition b);
bool lsp_document_position_eq(LSPDocumentPosition a, LSPDocumentPosition b);
// get the start of location's range as a LSPDocumentPosition
LSPDocumentPosition lsp_location_start_position(LSPLocation location);
// get the end of location's range as a LSPDocumentPosition
LSPDocumentPosition lsp_location_end_position(LSPLocation location);
void lsp_free(LSP *lsp);

#endif // LSP_H_

#if defined LSP_INTERNAL && !defined LSP_INTERNAL_H_
#define LSP_INTERNAL_H_

#include "sdl-inc.h"

#define lsp_set_error(lsp, ...) do {\
		SDL_LockMutex(lsp->error_mutex);\
		strbuf_printf(lsp->error, __VA_ARGS__);\
		SDL_UnlockMutex(lsp->error_mutex);\
	} while (0)

// a string
typedef struct {
	u32 pos;
	u32 len;
} JSONString;

typedef struct JSONValue JSONValue;

typedef struct {
	u32 len;
	// this is an index into the values array
	//   values[items..items+len] store the names
	//   values[items+len..items+2*len] store the values
	u32 items;
} JSONObject;

typedef struct {
	u32 len;
	// this is an index into the values array
	//    values[elements..elements+len] are the elements
	u32 elements;
} JSONArray;

typedef enum {
	// note: json doesn't actually include undefined.
	// this is only for returning things from json_get etc.
	JSON_UNDEFINED,
	JSON_NULL,
	JSON_FALSE,
	JSON_TRUE,
	JSON_NUMBER,
	JSON_STRING,
	JSON_OBJECT,
	JSON_ARRAY
} JSONValueType;

struct JSONValue {
	JSONValueType type;
	union {
		double number;
		JSONString string;
		JSONArray array;
		JSONObject object;
	} val;
};


typedef struct {
	char error[64];
	bool is_text_copied; // if this is true, then json_free will call free on text
	const char *text;
	// root = values[0]
	JSONValue *values;
} JSON;


void process_message(LSP *lsp, JSON *json);
void write_request(LSP *lsp, LSPRequest *request);
void write_message(LSP *lsp, LSPMessage *message);
void lsp_request_free(LSPRequest *r);
void lsp_response_free(LSPResponse *r);

const char *json_type_to_str(JSONValueType type);
void json_debug_print_array(const JSON *json, JSONArray array);
void json_debug_print_object(const JSON *json, JSONObject obj);
void json_debug_print_string(const JSON *json, JSONString string);
void json_debug_print_value(const JSON *json, JSONValue value);
void json_free(JSON *json);
// NOTE: text must live as long as json!!!
bool json_parse(JSON *json, const char *text);
// like json_parse, but a copy of text is made, so you can free/overwrite it immediately.
bool json_parse_copy(JSON *json, const char *text);
JSONValue json_object_get(const JSON *json, JSONObject object, const char *name);
JSONValue json_array_get(const JSON *json, JSONArray array, u64 i);
JSONValue json_object_key(const JSON *json, JSONObject object, u64 i);
JSONValue json_object_value(const JSON *json, JSONObject object, u64 i);
double json_force_number(JSONValue x);
double json_object_get_number(const JSON *json, JSONObject object, const char *name);
double json_array_get_number(const JSON *json, JSONArray array, size_t i);
bool json_force_bool(JSONValue x, bool default_value);
bool json_object_get_bool(const JSON *json, JSONObject object, const char *name, bool default_value);
bool json_array_get_bool(const JSON *json, JSONArray array, size_t i, bool default_value);
JSONString json_force_string(JSONValue x);
JSONString json_object_get_string(const JSON *json, JSONObject object, const char *name);
JSONString json_array_get_string(const JSON *json, JSONArray array, size_t i);
JSONObject json_force_object(JSONValue x);
JSONObject json_object_get_object(const JSON *json, JSONObject object, const char *name);
JSONObject json_array_get_object(const JSON *json, JSONArray array, size_t i);
JSONArray json_force_array(JSONValue x);
JSONArray json_object_get_array(const JSON *json, JSONObject object, const char *name);
JSONArray json_array_get_array(const JSON *json, JSONArray array, size_t i);
JSONValue json_root(const JSON *json);
JSONValue json_get(const JSON *json, const char *path);
bool json_has(const JSON *json, const char *path);
void json_string_get(const JSON *json, JSONString string, char *buf, size_t buf_sz);
// returns a malloc'd null-terminated string.
char *json_string_get_alloc(const JSON *json, JSONString string);
void json_debug_print(const JSON *json);
size_t json_escape_to(char *out, size_t out_sz, const char *in);
char *json_escape(const char *str);

// print server-to-client communication
#define LSP_SHOW_S2C 0
// print client-to-server communication
#define LSP_SHOW_C2S 0

#endif // LSP_INTERNAL