From 874d7d0c6555642b06c6d2c3e75c6eddf1c94bd0 Mon Sep 17 00:00:00 2001
From: pommicket <pommicket@gmail.com>
Date: Tue, 3 Jan 2023 18:36:05 -0500
Subject: don't start up multiple LSP servers with the same command

---
 lsp.c  | 14 ++++++++------
 lsp.h  |  4 ++--
 main.c |  1 -
 ted.c  |  4 ++--
 4 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/lsp.c b/lsp.c
index 3dc0fee..583c822 100644
--- a/lsp.c
+++ b/lsp.c
@@ -167,8 +167,8 @@ static bool lsp_supports_request(LSP *lsp, const LSPRequest *request) {
 	case LSP_REQUEST_EXIT:
 		return true;
 	case LSP_REQUEST_JDTLS_CONFIGURATION:
-		// @TODO: check if this is actually jdtls
-		return lsp->language == LANG_JAVA;
+		// not perfect but whatever
+		return strstr(lsp->command, "jdtls") != 0;
 	case LSP_REQUEST_COMPLETION:
 		return cap->completion_support;
 	case LSP_REQUEST_SIGNATURE_HELP:
@@ -444,7 +444,7 @@ const char *lsp_document_path(LSP *lsp, LSPDocumentID document) {
 	return path;
 }
 
-LSP *lsp_create(const char *root_dir, Language language, const char *analyzer_command) {
+LSP *lsp_create(const char *root_dir, const char *analyzer_command) {
 	LSP *lsp = calloc(1, sizeof *lsp);
 	if (!lsp) return NULL;
 	
@@ -454,13 +454,14 @@ LSP *lsp_create(const char *root_dir, Language language, const char *analyzer_co
 	static LSPID curr_id = 1;
 	lsp->id = curr_id++;
 	
+	
 	#if DEBUG
-		printf("Starting up LSP %p `%s` for language %s in %s\n",
-			(void *)lsp, analyzer_command, language_to_str(language), root_dir);
+		printf("Starting up LSP %p `%s` in %s\n",
+			(void *)lsp, analyzer_command, root_dir);
 	#endif
 	
 	str_hash_table_create(&lsp->document_ids, sizeof(u32));
-	lsp->language = language;
+	lsp->command = str_dup(analyzer_command);
 	lsp->quit_sem = SDL_CreateSemaphore(0);	
 	lsp->error_mutex = SDL_CreateMutex();
 	lsp->messages_mutex = SDL_CreateMutex();
@@ -564,6 +565,7 @@ void lsp_free(LSP *lsp) {
 	arr_free(lsp->completion_trigger_chars);
 	arr_free(lsp->signature_help_trigger_chars);
 	arr_free(lsp->signature_help_retrigger_chars);
+	free(lsp->command);
 	memset(lsp, 0, sizeof *lsp);
 	free(lsp);
 }
diff --git a/lsp.h b/lsp.h
index c146a9b..f65004f 100644
--- a/lsp.h
+++ b/lsp.h
@@ -530,7 +530,7 @@ typedef struct LSP {
 	//                (when the initialize response is received)
 	bool initialized;
 	// thread-safety: only set once in lsp_create.
-	Language language;
+	char *command;
 	LSPThread communication_thread;
 	LSPSemaphore quit_sem;
 	// thread-safety: only accessed in communication thread
@@ -569,7 +569,7 @@ 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);
-LSP *lsp_create(const char *root_dir, Language language, const char *analyzer_command);
+LSP *lsp_create(const char *root_dir, const char *analyzer_command);
 // 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)
diff --git a/main.c b/main.c
index 31866f2..a49e6dd 100644
--- a/main.c
+++ b/main.c
@@ -2,7 +2,6 @@
 @TODO:
 - fix problem where LSP_REQUEST_DEFINITION tag isn't being found
 - ted.h documentation
-- don't start up separate clangd servers for C and C++
 - handle multiple symbols with same name in go-to-definition menu
 - better non-error window/showMessage(Request)
 - document lsp.h and lsp.c.
diff --git a/ted.c b/ted.c
index fdc3c4b..bbb83ee 100644
--- a/ted.c
+++ b/ted.c
@@ -170,7 +170,7 @@ LSP *ted_get_lsp(Ted *ted, const char *path, Language language) {
 	for (i = 0; i < TED_LSP_MAX; ++i) {
 		LSP *lsp = ted->lsps[i];
 		if (!lsp) break;
-		if (lsp->language != language) continue;
+		if (!streq(lsp->command, settings->lsp)) continue;
 		
 		if (!lsp->initialized) {
 			// withhold judgement until this server initializes.
@@ -190,7 +190,7 @@ LSP *ted_get_lsp(Ted *ted, const char *path, Language language) {
 	if (*settings->lsp) {
 		// start up this LSP
 		char *root_dir = settings_get_root_dir(settings, path);
-		ted->lsps[i] = lsp_create(root_dir, language, settings->lsp);
+		ted->lsps[i] = lsp_create(root_dir, settings->lsp);
 		free(root_dir);
 		// don't actually return it yet, since it's still initializing (see above)
 	}
-- 
cgit v1.2.3