From 4232df89b50754779814a97f374451fce2e36447 Mon Sep 17 00:00:00 2001 From: pommicket Date: Fri, 30 Dec 2022 11:05:19 -0500 Subject: global request IDs --- lsp.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'lsp.c') diff --git a/lsp.c b/lsp.c index f45603e..3efa830 100644 --- a/lsp.c +++ b/lsp.c @@ -15,6 +15,13 @@ static void lsp_response_free(LSPResponse *r); #include "lsp-write.c" #include "lsp-parse.c" +// it's nice to have request IDs be totally unique, including across LSP servers. +static LSPRequestID get_request_id(void) { + static _Atomic LSPRequestID last_request_id; + // it's important that this never returns 0, since that's reserved for "no ID" + return ++last_request_id; +} + bool lsp_get_error(LSP *lsp, char *error, size_t error_size, bool clear) { bool has_err = false; SDL_LockMutex(lsp->error_mutex); @@ -201,15 +208,12 @@ LSPRequestID lsp_send_request(LSP *lsp, LSPRequest *request) { } bool is_notification = request_type_is_notification(request->type); - LSPRequestID id = 0; - if (!is_notification) { - id = ++lsp->request_id; - request->id = id; - } + if (!is_notification) + request->id = get_request_id(); LSPMessage message = {.type = LSP_REQUEST}; message.u.request = *request; lsp_send_message(lsp, &message); - return id; + return request->id; } void lsp_send_response(LSP *lsp, LSPResponse *response) { @@ -434,7 +438,7 @@ LSP *lsp_create(const char *root_dir, Language language, const char *analyzer_co LSPRequest initialize = { .type = LSP_REQUEST_INITIALIZE }; - initialize.id = ++lsp->request_id; + initialize.id = get_request_id(); // immediately send the request rather than queueing it. // this is a small request, so it shouldn't be a problem. write_request(lsp, &initialize); -- cgit v1.2.3