From a6a06900bf7206dc86017bbb1895272ebfac5418 Mon Sep 17 00:00:00 2001 From: pommicket Date: Sat, 24 Dec 2022 12:02:40 -0500 Subject: start LSP setting --- buffer.c | 15 ++------------- config.c | 1 + lsp.c | 5 +++++ main.c | 3 +-- syntax.c | 9 +++++++++ ted.c | 37 ++++++++++++++++++++++++++++++++----- ted.cfg | 3 +++ ted.h | 2 ++ util.c | 8 ++++++++ 9 files changed, 63 insertions(+), 20 deletions(-) diff --git a/buffer.c b/buffer.c index d904c38..831bf89 100644 --- a/buffer.c +++ b/buffer.c @@ -307,7 +307,7 @@ static long context_score(const char *path, Language lang, const SettingsContext } if (context->path) { - if (path && str_has_prefix(path, context->path)) { + if (path && str_has_path_prefix(path, context->path)) { score += (long)strlen(context->path); } else { // dont use this. it's path-specific and for the wrong path. @@ -320,18 +320,7 @@ static long context_score(const char *path, Language lang, const SettingsContext // Get the settings used for this buffer. Settings *buffer_settings(TextBuffer *buffer) { - Ted *ted = buffer->ted; - long best_score = 0; - Settings *settings = ted->default_settings; - Language language = buffer_language(buffer); - arr_foreach_ptr(ted->all_settings, Settings, s) { - long score = context_score(buffer->filename, language, &s->context); - if (score > best_score) { - best_score = score; - settings = s; - } - } - return settings; + return ted_get_settings(buffer->ted, buffer->filename, buffer_language(buffer)); } diff --git a/config.c b/config.c index f3b6428..9cfad1e 100644 --- a/config.c +++ b/config.c @@ -275,6 +275,7 @@ static OptionString const options_string[] = { {"bg-shader", options_zero.bg_shader_text, sizeof options_zero.bg_shader_text, true}, {"bg-texture", options_zero.bg_shader_image, sizeof options_zero.bg_shader_image, true}, {"root-identifiers", options_zero.root_identifiers, sizeof options_zero.root_identifiers, true}, + {"lsp", options_zero.lsp, sizeof options_zero.lsp, true}, }; static void option_bool_set(Settings *settings, const OptionBool *opt, bool value) { diff --git a/lsp.c b/lsp.c index a8a2445..7aa4d56 100644 --- a/lsp.c +++ b/lsp.c @@ -295,6 +295,11 @@ LSP *lsp_create(const char *root_dir, Language language, const char *analyzer_co LSP *lsp = calloc(1, sizeof *lsp); if (!lsp) return NULL; + #if DEBUG + printf("Starting up LSP `%s` for language %s in %s\n", + analyzer_command, language_to_str(language), root_dir); + #endif + ProcessSettings settings = { .stdin_blocking = true, .stdout_blocking = false, diff --git a/main.c b/main.c index a93439a..9df6824 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,6 @@ /* @TODO: -- LSP setting - - figure out workspace using root-files variable (also do this for :build) +- why is it creating the LSP on exit - make sure "save as" works - more LSP stuff: - go to definition using LSP diff --git a/syntax.c b/syntax.c index b156451..dfd7035 100644 --- a/syntax.c +++ b/syntax.c @@ -13,6 +13,15 @@ Language language_from_str(char const *str) { return LANG_NONE; } +const char *language_to_str(Language language) { + for (int i = 0; i < LANG_COUNT; ++i) { + if (language_names[i].lang == language) + return language_names[i].name; + } + return "???"; + +} + // start of single line comment for language l -- used for comment/uncomment selection char const *language_comment_start(Language l) { switch (l) { diff --git a/ted.c b/ted.c index 9000676..3472a98 100644 --- a/ted.c +++ b/ted.c @@ -78,14 +78,41 @@ Settings *ted_active_settings(Ted *ted) { return settings; } -LSP *ted_get_lsp(Ted *ted, const char *path, Language lang) { - for (int i = 0; i < TED_LSP_MAX; ++i) { +Settings *ted_get_settings(Ted *ted, const char *path, Language language) { + long best_score = 0; + Settings *settings = ted->default_settings; + arr_foreach_ptr(ted->all_settings, Settings, s) { + long score = context_score(path, language, &s->context); + if (score > best_score) { + best_score = score; + settings = s; + } + } + return settings; +} + +LSP *ted_get_lsp(Ted *ted, const char *path, Language language) { + int i; + for (i = 0; i < TED_LSP_MAX; ++i) { LSP *lsp = ted->lsps[i]; if (!lsp) break; - if (lsp->language != lang) continue; - if (!str_has_prefix(path, lsp->root_dir)) continue; - return lsp; + if (lsp->language != language) continue; + + // check if root matches up + const char *lsp_root = lsp->root_dir; + if (str_has_path_prefix(path, lsp_root)) + return lsp; } + if (i == TED_LSP_MAX) + return NULL; // why are there so many LSP open??? + Settings *settings = ted_get_settings(ted, path, 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); + free(root_dir); + } + return NULL; } diff --git a/ted.cfg b/ted.cfg index ef6f862..f10f4b0 100644 --- a/ted.cfg +++ b/ted.cfg @@ -70,6 +70,9 @@ root-identifiers = .ted-root.out, Cargo.toml, make.bat, CMakeLists.txt, Makefile # bg-texture = "/path/to/my/cool/picture.jpg" (or .png, .gif, .bmp) # bg-shader = "void main() { gl_FragColor = texture2D(t_texture, t_pos) * vec4(1.0,1.0,1.0,0.2); }" +[Rust.core] +lsp = "rust-analyzer" + [keyboard] # motion and selection Left = :left diff --git a/ted.h b/ted.h index 637bed3..55a97eb 100644 --- a/ted.h +++ b/ted.h @@ -199,6 +199,7 @@ typedef struct { char bg_shader_text[4096]; char bg_shader_image[TED_PATH_MAX]; char root_identifiers[4096]; + char lsp[512]; char build_default_command[256]; // [i] = comma-separated string of file extensions for language i, or NULL for none char *language_extensions[LANG_COUNT]; @@ -519,6 +520,7 @@ void command_execute(Ted *ted, Command c, i64 argument); void ted_switch_to_buffer(Ted *ted, TextBuffer *buffer); // the settings of the active buffer, or the default settings if there is no active buffer Settings *ted_active_settings(Ted *ted); +Settings *ted_get_settings(Ted *ted, const char *path, Language lang); void ted_load_configs(Ted *ted, bool reloading); struct LSP *ted_get_lsp(Ted *ted, const char *path, Language lang); static TextBuffer *find_search_buffer(Ted *ted); diff --git a/util.c b/util.c index 32115cf..a87d230 100644 --- a/util.c +++ b/util.c @@ -80,6 +80,14 @@ static bool str_has_prefix(char const *str, char const *prefix) { return strncmp(str, prefix, strlen(prefix)) == 0; } +// e.g. "/usr/share/bla" has the path prefix "/usr/share" but not "/usr/sha" +static bool str_has_path_prefix(const char *path, const char *prefix) { + size_t prefix_len = strlen(prefix); + if (strncmp(path, prefix, prefix_len) != 0) + return false; + return path[prefix_len] == '\0' || strchr(ALL_PATH_SEPARATORS, path[prefix_len]); +} + static bool streq(char const *a, char const *b) { return strcmp(a, b) == 0; } -- cgit v1.2.3