From e63b4e974db296819fcd474e5cec4d4344d89cdc Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 9 Jun 2021 09:15:19 +0200 Subject: [PATCH] Cleanup of source language checks --- src/directories.cpp | 11 +-- src/notebook.cpp | 29 ++++---- src/project.cpp | 31 ++++----- src/source.cpp | 98 +++++++++++---------------- src/source.hpp | 5 -- src/source_base.cpp | 112 +++++++++++++++++-------------- src/source_base.hpp | 32 ++++++--- src/source_clang.cpp | 12 ++-- src/source_language_protocol.cpp | 2 +- src/tooltips.cpp | 8 +-- tests/source_clang_test.cpp | 2 +- 11 files changed, 166 insertions(+), 176 deletions(-) diff --git a/src/directories.cpp b/src/directories.cpp index fd154d7..1297b8d 100644 --- a/src/directories.cpp +++ b/src/directories.cpp @@ -402,14 +402,9 @@ Directories::Directories() : Gtk::ListViewText(1) { else if(view->file_path == source_path) { view->rename(target_path); - std::string old_language_id; - if(view->language) - old_language_id = view->language->get_id(); - view->language = Source::guess_language(target_path); - std::string new_language_id; - if(view->language) - new_language_id = view->language->get_id(); - if(new_language_id != old_language_id) + auto old_language_id = view->language_id; + view->set_language(Source::guess_language(target_path)); + if(view->language_id != old_language_id) Terminal::get().print("\e[33mWarning\e[m: language for " + filesystem::get_short_path(target_path).string() + " has changed.\nPlease reopen the file.\n"); } } diff --git a/src/notebook.cpp b/src/notebook.cpp index cd57c82..75403f1 100644 --- a/src/notebook.cpp +++ b/src/notebook.cpp @@ -156,22 +156,20 @@ bool Notebook::open(const boost::filesystem::path &file_path_, Position position auto last_view = get_current_view(); auto language = Source::guess_language(file_path); - - std::string language_protocol_language_id; - if(language) { - language_protocol_language_id = language->get_id(); - if(language_protocol_language_id == "js") { - if(file_path.extension() == ".ts") - language_protocol_language_id = "typescript"; - else if(file_path.extension() == ".tsx") - language_protocol_language_id = "typescriptreact"; - else - language_protocol_language_id = "javascript"; - } + std::string language_id = language ? language->get_id() : ""; + std::string language_protocol_language_id = language_id; + + if(language_protocol_language_id == "js") { + if(file_path.extension() == ".ts") + language_protocol_language_id = "typescript"; + else if(file_path.extension() == ".tsx") + language_protocol_language_id = "typescriptreact"; + else + language_protocol_language_id = "javascript"; } size_t source_views_previous_size = source_views.size(); - if(language && (language->get_id() == "chdr" || language->get_id() == "cpphdr" || language->get_id() == "c" || language->get_id() == "cpp" || language->get_id() == "objc")) + if(language_id == "chdr" || language_id == "cpphdr" || language_id == "c" || language_id == "cpp" || language_id == "objc") source_views.emplace_back(new Source::ClangView(file_path, language)); else if(language && !language_protocol_language_id.empty() && !filesystem::find_executable(language_protocol_language_id + "-language-server").empty()) source_views.emplace_back(new Source::LanguageProtocolView(file_path, language, language_protocol_language_id, language_protocol_language_id + "-language-server")); @@ -189,9 +187,8 @@ bool Notebook::open(const boost::filesystem::path &file_path_, Position position } } if(source_views_previous_size == source_views.size()) { - if(language) { + if(!language_id.empty()) { static std::set shown; - std::string language_id = language->get_id(); if(shown.find(language_id) == shown.end()) { if(language_id == "js") { Terminal::get().print("\e[33mWarning\e[m: could not find JavaScript/TypeScript language server.\n"); @@ -451,7 +448,7 @@ bool Notebook::open(const boost::filesystem::path &file_path_, Position position }); #ifdef JUCI_ENABLE_DEBUG - if(dynamic_cast(view) || (view->language && view->language->get_id() == "rust")) { + if(dynamic_cast(view) || view->language_id == "rust") { view->toggle_breakpoint = [view](int line_nr) { if(view->get_source_buffer()->get_source_marks_at_line(line_nr, "debug_breakpoint").size() > 0) { auto start_iter = view->get_buffer()->get_iter_at_line(line_nr); diff --git a/src/project.cpp b/src/project.cpp index 206f206..b4db0b5 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -77,13 +77,13 @@ void Project::on_save(size_t index) { Commands::get().load(); boost::filesystem::path build_path; - if(view->language && view->language->get_id() == "cmake") { + if(view->language_id == "cmake") { if(view->file_path.filename() == "CMakeLists.txt") build_path = view->file_path; else build_path = filesystem::find_file_in_path_parents("CMakeLists.txt", view->file_path.parent_path()); } - else if(view->language && view->language->get_id() == "meson") { + else if(view->language_id == "meson") { if(view->file_path.filename() == "meson.build") build_path = view->file_path; else @@ -172,21 +172,18 @@ std::shared_ptr Project::create() { if(auto view = Notebook::get().get_current_view()) { build = Build::create(view->file_path); - if(view->language) { - auto language_id = view->language->get_id(); - if(language_id == "markdown") - return std::shared_ptr(new Project::Markdown(std::move(build))); - if(language_id == "js") - return std::shared_ptr(new Project::JavaScript(std::move(build))); - if(language_id == "python") - return std::shared_ptr(new Project::Python(std::move(build))); - if(language_id == "html") - return std::shared_ptr(new Project::HTML(std::move(build))); - if(language_id == "go") - return std::shared_ptr(new Project::Go(std::move(build))); - if(language_id == "julia") - return std::shared_ptr(new Project::Julia(std::move(build))); - } + if(view->language_id == "markdown") + return std::shared_ptr(new Project::Markdown(std::move(build))); + if(view->language_id == "js") + return std::shared_ptr(new Project::JavaScript(std::move(build))); + if(view->language_id == "python") + return std::shared_ptr(new Project::Python(std::move(build))); + if(view->language_id == "html") + return std::shared_ptr(new Project::HTML(std::move(build))); + if(view->language_id == "go") + return std::shared_ptr(new Project::Go(std::move(build))); + if(view->language_id == "julia") + return std::shared_ptr(new Project::Julia(std::move(build))); } else build = Build::create(Directories::get().path); diff --git a/src/source.cpp b/src/source.cpp index 679b647..80073b8 100644 --- a/src/source.cpp +++ b/src/source.cpp @@ -89,9 +89,8 @@ Glib::RefPtr Source::guess_language(const boost::filesystem::path else language = language_manager->get_language("cpp"); } - else if(language->get_id() == "opencl") { + else if(language->get_id() == "opencl") language = language_manager->get_language("cpp"); - } return language; } @@ -170,46 +169,36 @@ Source::View::View(const boost::filesystem::path &file_path, const Glib::RefPtr< hide_tag = get_buffer()->create_tag(); hide_tag->property_scale() = 0.25; - if(language) { - auto language_id = language->get_id(); - if(language_id == "chdr" || language_id == "c") - is_c = true; - else if(language_id == "cpphdr" || language_id == "cpp") - is_cpp = true; - else if(language_id == "js" || language_id == "html") - is_js = true; - - if(is_c || is_cpp) { - use_fixed_continuation_indenting = false; - // TODO 2019: check if clang-format has improved... - // boost::filesystem::path clang_format_file; - // auto search_path=file_path.parent_path(); - // boost::system::error_code ec; - // while(true) { - // clang_format_file=search_path/".clang-format"; - // if(boost::filesystem::exists(clang_format_file, ec)) - // break; - // clang_format_file=search_path/"_clang-format"; - // if(boost::filesystem::exists(clang_format_file, ec)) - // break; - // clang_format_file.clear(); - - // if(search_path==search_path.root_directory()) - // break; - // search_path=search_path.parent_path(); - // } - // if(!clang_format_file.empty()) { - // auto lines=filesystem::read_lines(clang_format_file); - // for(auto &line: lines) { - // std::cout << "1" << std::endl; - // if(!line.empty() && line.compare(0, 23, "ContinuationIndentWidth")==0) { - // std::cout << "2" << std::endl; - // use_continuation_indenting=true; - // break; - // } - // } - // } - } + if(is_c || is_cpp) { + use_fixed_continuation_indenting = false; + // TODO 2019: check if clang-format has improved... + // boost::filesystem::path clang_format_file; + // auto search_path=file_path.parent_path(); + // boost::system::error_code ec; + // while(true) { + // clang_format_file=search_path/".clang-format"; + // if(boost::filesystem::exists(clang_format_file, ec)) + // break; + // clang_format_file=search_path/"_clang-format"; + // if(boost::filesystem::exists(clang_format_file, ec)) + // break; + // clang_format_file.clear(); + + // if(search_path==search_path.root_directory()) + // break; + // search_path=search_path.parent_path(); + // } + // if(!clang_format_file.empty()) { + // auto lines=filesystem::read_lines(clang_format_file); + // for(auto &line: lines) { + // std::cout << "1" << std::endl; + // if(!line.empty() && line.compare(0, 23, "ContinuationIndentWidth")==0) { + // std::cout << "2" << std::endl; + // use_continuation_indenting=true; + // break; + // } + // } + // } } setup_signals(); @@ -219,15 +208,10 @@ Source::View::View(const boost::filesystem::path &file_path, const Glib::RefPtr< std::string comment_characters; if(is_bracket_language) comment_characters = "//"; - else if(language) { - auto language_id = language->get_id(); - if(language_id == "cmake" || language_id == "makefile" || language_id == "python" || - language_id == "python3" || language_id == "sh" || language_id == "perl" || - language_id == "ruby" || language_id == "r" || language_id == "asm" || - language_id == "automake" || language_id == "yaml" || language_id == "docker" || - language_id == "julia") + else { + if(is_language({"cmake", "makefile", "python", "python3", "sh", "perl", "ruby", "r", "asm", "automake", "yaml", "docker", "julia"})) comment_characters = "#"; - else if(language_id == "latex" || language_id == "matlab" || language_id == "octave" || language_id == "bibtex") + else if(is_language({"latex", "matlab", "octave", "bibtex"})) comment_characters = "%"; else if(language_id == "fortran") comment_characters = "!"; @@ -451,11 +435,10 @@ void Source::View::configure() { set_draw_spaces(parse_show_whitespace_characters(Config::get().source.show_whitespace_characters)); { // Set Word Wrap - auto language_id = language ? language->get_id() : ""; namespace qi = boost::spirit::qi; std::set word_wrap_language_ids; qi::phrase_parse(Config::get().source.word_wrap.begin(), Config::get().source.word_wrap.end(), (+(~qi::char_(','))) % ',', qi::space, word_wrap_language_ids); - if(std::any_of(word_wrap_language_ids.begin(), word_wrap_language_ids.end(), [&language_id](const std::string &word_wrap_language_id) { + if(std::any_of(word_wrap_language_ids.begin(), word_wrap_language_ids.end(), [this](const std::string &word_wrap_language_id) { return word_wrap_language_id == language_id || word_wrap_language_id == "all"; })) set_wrap_mode(Gtk::WrapMode::WRAP_WORD_CHAR); @@ -730,8 +713,7 @@ void Source::View::setup_signals() { void Source::View::setup_format_style(bool is_generic_view) { static auto prettier = filesystem::find_executable("prettier"); - auto prefer_prettier = language && (language->get_id() == "js" || language->get_id() == "json" || language->get_id() == "css" || language->get_id() == "html" || - language->get_id() == "markdown" || language->get_id() == "yaml"); + auto prefer_prettier = is_language({"js", "json", "css", "html", "markdown", "yaml"}); if(prettier.empty() && prefer_prettier) { static bool shown = false; if(!shown) { @@ -1566,7 +1548,7 @@ void Source::View::extend_selection() { // Attempt to select a sentence, for instance: int a = 2; if(!is_bracket_language) { // If for instance cmake, meson or python if(!select_matching_brackets) { - bool select_end_block = language->get_id() == "cmake" || language->get_id() == "meson"; + bool select_end_block = is_language({"cmake", "meson"}); auto get_tabs = [this](Gtk::TextIter iter) -> boost::optional { iter = get_buffer()->get_iter_at_line(iter.get_line()); @@ -1665,7 +1647,7 @@ void Source::View::extend_selection() { } // Select no_spellcheck_tag block if markdown - if(no_spellcheck_tag && language->get_id() == "markdown" && start_stored.has_tag(no_spellcheck_tag) && end_stored.has_tag(no_spellcheck_tag) && + if(no_spellcheck_tag && language_id == "markdown" && start_stored.has_tag(no_spellcheck_tag) && end_stored.has_tag(no_spellcheck_tag) && (!start.has_tag(no_spellcheck_tag) || !end.has_tag(no_spellcheck_tag))) { start = start_stored; end = end_stored; @@ -2372,7 +2354,7 @@ bool Source::View::on_key_press_event_basic(GdkEventKey *event) { auto tabs = get_line_before(tabs_end_iter); // Python indenting after : - if(*condition_iter == ':' && language && language->get_id() == "python") { + if(*condition_iter == ':' && language_id == "python") { get_buffer()->insert_at_cursor('\n' + tabs + tab); scroll_to(get_buffer()->get_insert()); return true; @@ -3241,7 +3223,7 @@ bool Source::View::on_key_press_event_smart_inserts(GdkEventKey *event) { left = "/*"; right = "*/"; } - else if((language && language->get_id() == "markdown") || + else if(language_id == "markdown" || !is_code_iter(get_buffer()->get_insert()->get_iter()) || !is_code_iter(get_buffer()->get_selection_bound()->get_iter())) { // Insert `` around selection if(event->keyval == GDK_KEY_dead_grave) { diff --git a/src/source.hpp b/src/source.hpp index c81bc42..1369486 100644 --- a/src/source.hpp +++ b/src/source.hpp @@ -173,11 +173,6 @@ namespace Source { bool interactive_completion = true; - bool is_c = false; - bool is_cpp = false; - /// Set to true if language is html or js (including typescript) - bool is_js = false; - private: void setup_signals(); void setup_format_style(bool is_generic_view); diff --git a/src/source_base.cpp b/src/source_base.cpp index f5b9003..9aacbc8 100644 --- a/src/source_base.cpp +++ b/src/source_base.cpp @@ -10,7 +10,15 @@ #include #include -Source::CommonView::CommonView(const Glib::RefPtr &language) : Gsv::View(), language(language) { +Source::CommonView::CommonView(const Glib::RefPtr &language) : Gsv::View() { + set_language(language); + if(is_language({"chdr", "c"})) + is_c = true; + else if(is_language({"cpphdr", "cpp"})) + is_cpp = true; + else if(is_language({"js", "html"})) + is_js = true; + search_settings = gtk_source_search_settings_new(); gtk_source_search_settings_set_wrap_around(search_settings, true); search_context = gtk_source_search_context_new(get_source_buffer()->gobj(), search_settings); @@ -23,6 +31,49 @@ Source::CommonView::~CommonView() { g_clear_object(&search_settings); } +void Source::CommonView::set_language(const Glib::RefPtr &language) { + this->language = language; + language_id = language ? language->get_id() : ""; +} + +bool Source::CommonView::is_language(const std::initializer_list &languages) { + if(!language) + return false; + return std::any_of(languages.begin(), languages.end(), [this](const std::string &e) { + return e == language_id; + }); +} + +bool Source::CommonView::on_key_press_event(GdkEventKey *event) { + if(event->keyval == GDK_KEY_Home && event->state & GDK_CONTROL_MASK) { + auto iter = get_buffer()->begin(); + scroll_to(iter); // Home key should always scroll to start, even though cursor does not move + return Gsv::View::on_key_press_event(event); + } + else if(event->keyval == GDK_KEY_End && event->state & GDK_CONTROL_MASK) { + auto iter = get_buffer()->end(); + scroll_to(iter); // End key should always scroll to start, even though cursor does not move + return Gsv::View::on_key_press_event(event); + } + return Gsv::View::on_key_press_event(event); +} + +bool Source::CommonView::on_motion_notify_event(GdkEventMotion *event) { + // Workaround for drag-and-drop crash on MacOS + // TODO 2023: check if this bug has been fixed +#ifdef __APPLE__ + if((event->state & GDK_BUTTON1_MASK) > 0 && (event->state & GDK_SHIFT_MASK) == 0) { + int x, y; + window_to_buffer_coords(Gtk::TextWindowType::TEXT_WINDOW_TEXT, event->x, event->y, x, y); + Gtk::TextIter iter; + get_iter_at_location(iter, x, y); + get_buffer()->select_range(iter, get_buffer()->get_selection_bound()->get_iter()); + return true; + } +#endif + return Gsv::View::on_motion_notify_event(event); +} + void Source::CommonView::search_highlight(const std::string &text, bool case_sensitive, bool regex) { gtk_source_search_settings_set_case_sensitive(search_settings, case_sensitive); gtk_source_search_settings_set_regex_enabled(search_settings, regex); @@ -122,36 +173,6 @@ void Source::CommonView::search_occurrences_updated(GtkWidget *widget, GParamSpe view->update_search_occurrences(gtk_source_search_context_get_occurrences_count(view->search_context)); } -bool Source::CommonView::on_key_press_event(GdkEventKey *event) { - if(event->keyval == GDK_KEY_Home && event->state & GDK_CONTROL_MASK) { - auto iter = get_buffer()->begin(); - scroll_to(iter); // Home key should always scroll to start, even though cursor does not move - return Gsv::View::on_key_press_event(event); - } - else if(event->keyval == GDK_KEY_End && event->state & GDK_CONTROL_MASK) { - auto iter = get_buffer()->end(); - scroll_to(iter); // End key should always scroll to start, even though cursor does not move - return Gsv::View::on_key_press_event(event); - } - return Gsv::View::on_key_press_event(event); -} - -bool Source::CommonView::on_motion_notify_event(GdkEventMotion *event) { - // Workaround for drag-and-drop crash on MacOS - // TODO 2023: check if this bug has been fixed -#ifdef __APPLE__ - if((event->state & GDK_BUTTON1_MASK) > 0 && (event->state & GDK_SHIFT_MASK) == 0) { - int x, y; - window_to_buffer_coords(Gtk::TextWindowType::TEXT_WINDOW_TEXT, event->x, event->y, x, y); - Gtk::TextIter iter; - get_iter_at_location(iter, x, y); - get_buffer()->select_range(iter, get_buffer()->get_selection_bound()->get_iter()); - return true; - } -#endif - return Gsv::View::on_motion_notify_event(event); -} - void Source::CommonView::cut() { if(!get_editable()) return copy(); @@ -163,7 +184,7 @@ void Source::CommonView::cut() { if(!get_buffer()->get_has_selection()) cut_lines(); - else if(language && language->get_id() == "diff") { + else if(language_id == "diff") { Gtk::TextIter start, end; get_buffer()->get_selection_bounds(start, end); Glib::ustring selection; @@ -201,7 +222,7 @@ void Source::CommonView::cut_lines() { end.forward_to_line_end(); end.forward_char(); - if(language && language->get_id() == "diff") { + if(language_id == "diff") { Glib::ustring selection; selection.reserve(end.get_offset() - start.get_offset()); for(auto iter = start; iter < end; ++iter) { @@ -230,7 +251,7 @@ void Source::CommonView::cut_lines() { void Source::CommonView::copy() { if(!get_buffer()->get_has_selection()) copy_lines(); - else if(language && language->get_id() == "diff") { + else if(language_id == "diff") { Gtk::TextIter start, end; get_buffer()->get_selection_bounds(start, end); Glib::ustring selection; @@ -258,7 +279,7 @@ void Source::CommonView::copy_lines() { end.forward_to_line_end(); end.forward_char(); - if(language && language->get_id() == "diff") { + if(language_id == "diff") { Glib::ustring selection; selection.reserve(end.get_offset() - start.get_offset()); for(auto iter = start; iter < end; ++iter) { @@ -292,14 +313,8 @@ Source::BaseView::BaseView(const boost::filesystem::path &file_path, const Glib: if(language) { get_source_buffer()->set_language(language); get_source_buffer()->set_highlight_syntax(true); - auto language_id = language->get_id(); - if(language_id == "chdr" || language_id == "cpphdr" || language_id == "c" || - language_id == "cpp" || language_id == "objc" || language_id == "java" || - language_id == "js" || language_id == "ts" || language_id == "proto" || - language_id == "c-sharp" || language_id == "html" || language_id == "cuda" || - language_id == "php" || language_id == "rust" || language_id == "swift" || - language_id == "go" || language_id == "scala" || language_id == "opencl" || - language_id == "json" || language_id == "css" || language_id == "glsl") + if(is_language({"chdr", "cpphdr", "c", "cpp", "objc", "java", "js", "proto", "c-sharp", "html", "cuda", + "php", "rust", "swift", "go", "scala", "opencl", "json", "css", "glsl"})) is_bracket_language = true; } @@ -307,8 +322,7 @@ Source::BaseView::BaseView(const boost::filesystem::path &file_path, const Glib: tab_char = Config::get().source.default_tab_char; tab_size = Config::get().source.default_tab_size; if(language) { - auto language_id = language->get_id(); - if(language_id == "python" || language_id == "rust" || language_id == "julia") { + if(is_language({"python", "rust", "julia"})) { tab_char = ' '; tab_size = 4; } @@ -564,7 +578,7 @@ std::pair Source::BaseView::find_tab_char_and_size() { bool single_quoted = false; bool double_quoted = false; //For bracket languages, TODO: add more language ids - if(is_bracket_language && !(language && language->get_id() == "html")) { + if(is_bracket_language && language_id != "html") { bool line_comment = false; bool comment = false; bool bracket_last_line = false; @@ -993,7 +1007,7 @@ void Source::BaseView::paste() { first_paste_line_has_tabs = true; paste_line_tabs = tabs; } - else if(language && language->get_id() == "python") { // Special case for Python code where the first line ends with ':' + else if(language_id == "python") { // Special case for Python code where the first line ends with ':' char last_char = 0; for(auto &chr : line) { if(chr != ' ' && chr != '\t') @@ -1505,10 +1519,10 @@ void Source::BaseView::set_snippets() { snippets = nullptr; - if(language) { + if(!language_id.empty()) { for(auto &pair : Snippets::get().snippets) { std::smatch sm; - if(std::regex_match(language->get_id().raw(), sm, pair.first)) { + if(std::regex_match(language_id, sm, pair.first)) { snippets = &pair.second; break; } diff --git a/src/source_base.hpp b/src/source_base.hpp index d957fde..43741f5 100644 --- a/src/source_base.hpp +++ b/src/source_base.hpp @@ -32,19 +32,35 @@ namespace Source { public: CommonView(const Glib::RefPtr &language = {}); ~CommonView() override; - void search_highlight(const std::string &text, bool case_sensitive, bool regex); - void search_forward(); - void search_backward(); - void replace_forward(const std::string &replacement); - void replace_backward(const std::string &replacement); - void replace_all(const std::string &replacement); + protected: Glib::RefPtr language; + public: + std::string language_id; + void set_language(const Glib::RefPtr &language); + /// Checks if parameter languages contain language_id + bool is_language(const std::initializer_list &languages); + protected: + bool is_c = false; + bool is_cpp = false; + /// Set to true if language is html or js (including typescript) + bool is_js = false; + bool keep_clipboard = false; + bool on_key_press_event(GdkEventKey *event) override; + bool on_motion_notify_event(GdkEventMotion *motion_event) override; + public: + void search_highlight(const std::string &text, bool case_sensitive, bool regex); + void search_forward(); + void search_backward(); + void replace_forward(const std::string &replacement); + void replace_backward(const std::string &replacement); + void replace_all(const std::string &replacement); + void cut(); void cut_lines(); void copy(); @@ -53,10 +69,6 @@ namespace Source { std::function update_search_occurrences; - protected: - bool on_key_press_event(GdkEventKey *event) override; - bool on_motion_notify_event(GdkEventMotion *motion_event) override; - private: GtkSourceSearchContext *search_context; GtkSourceSearchSettings *search_settings; diff --git a/src/source_clang.cpp b/src/source_clang.cpp index 29e618c..87d52c4 100644 --- a/src/source_clang.cpp +++ b/src/source_clang.cpp @@ -30,7 +30,7 @@ Source::ClangViewParse::ClangViewParse(const boost::filesystem::path &file_path, syntax_tags.emplace(item.first, tag); } - if(get_buffer()->size() == 0 && (language->get_id() == "chdr" || language->get_id() == "cpphdr")) { + if(get_buffer()->size() == 0 && is_language({"chdr", "cpphdr"})) { disable_spellcheck = true; get_buffer()->insert_at_cursor("#pragma once\n"); disable_spellcheck = false; @@ -53,7 +53,7 @@ bool Source::ClangViewParse::save() { if(!Source::View::save()) return false; - if(language->get_id() == "chdr" || language->get_id() == "cpphdr") { + if(is_language({"chdr", "cpphdr"})) { for(auto &view : views) { if(auto clang_view = dynamic_cast(view)) { if(this != clang_view) @@ -115,7 +115,7 @@ void Source::ClangViewParse::parse_initialize() { } } - if(language && (language->get_id() == "chdr" || language->get_id() == "cpphdr")) + if(is_language({"chdr", "cpphdr"})) clangmm::remove_include_guard(buffer_raw); auto build = Project::Build::create(file_path); @@ -161,7 +161,7 @@ void Source::ClangViewParse::parse_initialize() { } else if(parse_process_state == ParseProcessState::processing && parse_mutex.try_lock()) { auto &parse_thread_buffer_raw = const_cast(parse_thread_buffer.raw()); - if(this->language && (this->language->get_id() == "chdr" || this->language->get_id() == "cpphdr")) + if(is_language({"chdr", "cpphdr"})) clangmm::remove_include_guard(parse_thread_buffer_raw); auto status = clang_tu->reparse(parse_thread_buffer_raw); if(status == 0) { @@ -949,7 +949,7 @@ Source::ClangViewAutocomplete::ClangViewAutocomplete(const boost::filesystem::pa }; autocomplete.add_rows = [this](std::string &buffer, int line_number, int column) { - if(this->language && (this->language->get_id() == "chdr" || this->language->get_id() == "cpphdr")) + if(is_language({"chdr", "cpphdr"})) clangmm::remove_include_guard(buffer); code_complete_results = std::make_unique(clang_tu->get_code_completions(buffer, line_number, column)); if(!code_complete_results->cx_results) @@ -2141,7 +2141,7 @@ void Source::ClangView::async_delete() { if(stream) { std::string buffer; buffer.assign(std::istreambuf_iterator(stream), std::istreambuf_iterator()); - if(language && (language->get_id() == "chdr" || language->get_id() == "cpphdr")) + if(is_language({"chdr", "cpphdr"})) clangmm::remove_include_guard(buffer); clang_tu->reparse(buffer); clang_tokens = clang_tu->get_tokens(); diff --git a/src/source_language_protocol.cpp b/src/source_language_protocol.cpp index 3a483ba..748892e 100644 --- a/src/source_language_protocol.cpp +++ b/src/source_language_protocol.cpp @@ -568,7 +568,7 @@ void Source::LanguageProtocolView::setup_navigation_and_refactoring() { style_file_search_path = style_file_search_path.parent_path(); } - if(!has_style_file && language && language->get_id() == "rust") { + if(!has_style_file && language_id == "rust") { auto style_file_search_path = file_path.parent_path(); while(true) { if(boost::filesystem::exists(style_file_search_path / "rustfmt.toml", ec) || diff --git a/src/tooltips.cpp b/src/tooltips.cpp index 8f2c3eb..08f2f98 100644 --- a/src/tooltips.cpp +++ b/src/tooltips.cpp @@ -953,7 +953,7 @@ void Tooltip::insert_code(const std::string &code, boost::variant(view)) - language = source_view->language; + language = Source::LanguageManager::get_default()->get_language(source_view->language_id); } } } @@ -1225,10 +1225,8 @@ void Tooltip::insert_doxygen(const std::string &input_, bool remove_delimiters) auto token = get_token(); if(token == "endcode") { if(language_id.empty() && view) { - if(auto source_view = dynamic_cast(view)) { - if(source_view->language) - language_id = source_view->language->get_id(); - } + if(auto source_view = dynamic_cast(view)) + language_id = source_view->language_id; } markdown += "```" + language_id + '\n' + input.substr(start, end - start) + "\n```\n"; ++i; diff --git a/tests/source_clang_test.cpp b/tests/source_clang_test.cpp index 46f931b..32e23fb 100644 --- a/tests/source_clang_test.cpp +++ b/tests/source_clang_test.cpp @@ -88,7 +88,7 @@ int main() { // test remove_include_guard { - clang_view->language = Source::LanguageManager::get_default()->get_language("chdr"); + clang_view->set_language(Source::LanguageManager::get_default()->get_language("chdr")); std::string source = "#ifndef F\n#define F\n#endif // F"; clangmm::remove_include_guard(source); g_assert_cmpstr(source.c_str(), ==, " \n \n ");