diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8e8e0b1..2539e96 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -36,6 +36,7 @@ set(global_libraries set(project_files config.cc dialogs.cc + dialogs_unix.cc directories.cc entrybox.cc info.cc @@ -47,8 +48,6 @@ set(project_files terminal.cc tooltips.cc window.cc - - ../tiny-process-library/process.cpp ) #Files used both in ../src and ../tests @@ -74,6 +73,8 @@ set(project_shared_files ../libclangmm/src/Tokens.cc ../libclangmm/src/TranslationUnit.cc ../libclangmm/src/Utility.cc + + ../tiny-process-library/process.cpp ) if(LIBLLDB_FOUND) @@ -81,9 +82,9 @@ if(LIBLLDB_FOUND) endif() if(MSYS) - list(APPEND project_files dialogs_unix.cc ../tiny-process-library/process_win.cpp) + list(APPEND project_shared_files ../tiny-process-library/process_win.cpp) else() - list(APPEND project_files dialogs_unix.cc ../tiny-process-library/process_unix.cpp) + list(APPEND project_shared_files ../tiny-process-library/process_unix.cpp) endif() include_directories(${global_includes}) diff --git a/src/source.h b/src/source.h index e29a22f..5acfde1 100644 --- a/src/source.h +++ b/src/source.h @@ -81,7 +81,7 @@ namespace Source { std::function get_token_spelling; std::function >(const std::vector &views, const std::string &text)> rename_similar_tokens; std::function goto_next_diagnostic; - std::function apply_fix_its; + std::function()> get_fix_its; std::unique_ptr autocomplete_dialog; std::unique_ptr selection_dialog; diff --git a/src/source_clang.cc b/src/source_clang.cc index c6e5900..23beff5 100644 --- a/src/source_clang.cc +++ b/src/source_clang.cc @@ -1275,37 +1275,12 @@ Source::ClangViewAutocomplete(file_path, language) { } }; - apply_fix_its=[this]() { + get_fix_its=[this]() { if(!parsed) { Info::get().print("Buffer is parsing"); - return; - } - std::vector, Glib::RefPtr > > fix_it_marks; - for(auto &fix_it: fix_its) { - auto start_iter=get_buffer()->get_iter_at_line_index(fix_it.offsets.first.line-1, fix_it.offsets.first.index-1); - auto end_iter=get_buffer()->get_iter_at_line_index(fix_it.offsets.second.line-1, fix_it.offsets.second.index-1); - fix_it_marks.emplace_back(get_buffer()->create_mark(start_iter), get_buffer()->create_mark(end_iter)); - } - size_t c=0; - get_buffer()->begin_user_action(); - for(auto &fix_it: fix_its) { - if(fix_it.type==FixIt::Type::INSERT) { - get_buffer()->insert(fix_it_marks[c].first->get_iter(), fix_it.source); - } - if(fix_it.type==FixIt::Type::REPLACE) { - get_buffer()->erase(fix_it_marks[c].first->get_iter(), fix_it_marks[c].second->get_iter()); - get_buffer()->insert(fix_it_marks[c].first->get_iter(), fix_it.source); - } - if(fix_it.type==FixIt::Type::ERASE) { - get_buffer()->erase(fix_it_marks[c].first->get_iter(), fix_it_marks[c].second->get_iter()); - } - c++; + return std::vector(); } - for(auto &mark_pair: fix_it_marks) { - get_buffer()->delete_mark(mark_pair.first); - get_buffer()->delete_mark(mark_pair.second); - } - get_buffer()->end_user_action(); + return fix_its; }; } diff --git a/src/window.cc b/src/window.cc index c459829..dc0099c 100644 --- a/src/window.cc +++ b/src/window.cc @@ -552,8 +552,36 @@ void Window::set_menu_actions() { }); menu.add_action("source_apply_fix_its", [this]() { if(notebook.get_current_page()!=-1) { - if(notebook.get_current_view()->apply_fix_its) { - notebook.get_current_view()->apply_fix_its(); + auto view=notebook.get_current_view(); + if(view->get_fix_its) { + auto buffer=view->get_buffer(); + auto fix_its=view->get_fix_its(); + std::vector, Glib::RefPtr > > fix_it_marks; + for(auto &fix_it: fix_its) { + auto start_iter=buffer->get_iter_at_line_index(fix_it.offsets.first.line-1, fix_it.offsets.first.index-1); + auto end_iter=buffer->get_iter_at_line_index(fix_it.offsets.second.line-1, fix_it.offsets.second.index-1); + fix_it_marks.emplace_back(buffer->create_mark(start_iter), buffer->create_mark(end_iter)); + } + size_t c=0; + buffer->begin_user_action(); + for(auto &fix_it: fix_its) { + if(fix_it.type==Source::FixIt::Type::INSERT) { + buffer->insert(fix_it_marks[c].first->get_iter(), fix_it.source); + } + if(fix_it.type==Source::FixIt::Type::REPLACE) { + buffer->erase(fix_it_marks[c].first->get_iter(), fix_it_marks[c].second->get_iter()); + buffer->insert(fix_it_marks[c].first->get_iter(), fix_it.source); + } + if(fix_it.type==Source::FixIt::Type::ERASE) { + buffer->erase(fix_it_marks[c].first->get_iter(), fix_it_marks[c].second->get_iter()); + } + c++; + } + for(auto &mark_pair: fix_it_marks) { + buffer->delete_mark(mark_pair.first); + buffer->delete_mark(mark_pair.second); + } + buffer->end_user_action(); } } }); @@ -810,7 +838,7 @@ void Window::activate_menu_items(bool activate) { menu.actions["source_goto_method"]->set_enabled(activate ? static_cast(notebook.get_current_view()->get_methods) : false); menu.actions["source_rename"]->set_enabled(activate ? static_cast(notebook.get_current_view()->rename_similar_tokens) : false); menu.actions["source_goto_next_diagnostic"]->set_enabled(activate ? static_cast(notebook.get_current_view()->goto_next_diagnostic) : false); - menu.actions["source_apply_fix_its"]->set_enabled(activate ? static_cast(notebook.get_current_view()->apply_fix_its) : false); + menu.actions["source_apply_fix_its"]->set_enabled(activate ? static_cast(notebook.get_current_view()->get_fix_its) : false); #ifdef JUCI_ENABLE_DEBUG if(notebook.get_current_page()!=-1) { auto view=notebook.get_current_view(); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7443baf..31c8f4d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -33,6 +33,11 @@ add_library(stubs OBJECT ${stub_files}) include_directories(${global_includes}) +add_executable(process_test process_test.cc + $ $) +target_link_libraries(process_test ${global_libraries}) +add_test(process_test process_test) + add_executable(cmake_build_test cmake_build_test.cc $ $) target_link_libraries(cmake_build_test ${global_libraries}) diff --git a/tests/process_test.cc b/tests/process_test.cc new file mode 100644 index 0000000..7cd9cb6 --- /dev/null +++ b/tests/process_test.cc @@ -0,0 +1,50 @@ +#include +#include "process.hpp" + +int main() { + auto output=std::make_shared(); + auto error=std::make_shared(); + { + Process process("echo Test", "", [output](const char *bytes, size_t n) { + *output+=std::string(bytes, n); + }); + g_assert(process.get_exit_status()==0); + g_assert(output->substr(0, 4)=="Test"); + output->clear(); + } + + { + Process process("echo Test && ls an_incorrect_path", "", [output](const char *bytes, size_t n) { + *output+=std::string(bytes, n); + }, [error](const char *bytes, size_t n) { + *error+=std::string(bytes, n); + }); + g_assert(process.get_exit_status()>0); + g_assert(output->substr(0, 4)=="Test"); + g_assert(!error->empty()); + output->clear(); + error->clear(); + } + + { + Process process("bash", "", [output](const char *bytes, size_t n) { + *output+=std::string(bytes, n); + }, nullptr, true); + process.write("echo Test\n"); + process.write("exit\n"); + g_assert(process.get_exit_status()==0); + g_assert(output->substr(0, 4)=="Test"); + output->clear(); + } + + { + Process process("cat", "", [output](const char *bytes, size_t n) { + *output+=std::string(bytes, n); + }, nullptr, true); + process.write("Test\n"); + process.close_stdin(); + g_assert(process.get_exit_status()==0); + g_assert(output->substr(0, 4)=="Test"); + output->clear(); + } +} diff --git a/tests/source_clang_test.cc b/tests/source_clang_test.cc index 52d02c1..4ed2e8d 100644 --- a/tests/source_clang_test.cc +++ b/tests/source_clang_test.cc @@ -73,6 +73,7 @@ int main() { while(!clang_view->parsed) flush_events(); g_assert_cmpuint(clang_view->diagnostics.size(), >, 0); + g_assert_cmpuint(clang_view->get_fix_its().size(), >, 0); clang_view->async_delete(); clang_view->delete_thread.join();