Browse Source

Refactor: src/project* files

merge-requests/365/head
eidheim 10 years ago
parent
commit
6db5afbb03
  1. 56
      src/project.cc
  2. 30
      src/project.h
  3. 20
      src/project_build.cc
  4. 20
      src/project_build.h
  5. 6
      src/source_clang.cc
  6. 8
      src/terminal.cc
  7. 68
      src/window.cc

56
src/project.cc

@ -16,7 +16,7 @@ std::unordered_map<std::string, std::string> Project::debug_run_arguments;
std::atomic<bool> Project::compiling(false); std::atomic<bool> Project::compiling(false);
std::atomic<bool> Project::debugging(false); std::atomic<bool> Project::debugging(false);
std::pair<boost::filesystem::path, std::pair<int, int> > Project::debug_stop; std::pair<boost::filesystem::path, std::pair<int, int> > Project::debug_stop;
std::unique_ptr<Project::Language> Project::current_language; std::unique_ptr<Project::Base> Project::current;
Gtk::Label &Project::debug_status_label() { Gtk::Label &Project::debug_status_label() {
static Gtk::Label label; static Gtk::Label label;
@ -47,11 +47,11 @@ void Project::on_save(int page) {
cmake_path=filesystem::find_file_in_path_parents("CMakeLists.txt", view->file_path.parent_path()); cmake_path=filesystem::find_file_in_path_parents("CMakeLists.txt", view->file_path.parent_path());
if(!cmake_path.empty()) { if(!cmake_path.empty()) {
auto build=get_build(cmake_path); auto build=Build::create(cmake_path);
if(dynamic_cast<CMake*>(build.get())) { if(dynamic_cast<CMakeBuild*>(build.get())) {
build->update_default_build(true); build->update_default(true);
if(boost::filesystem::exists(build->get_debug_build_path())) if(boost::filesystem::exists(build->get_debug_path()))
build->update_debug_build(true); build->update_debug(true);
for(int c=0;c<Notebook::get().size();c++) { for(int c=0;c<Notebook::get().size();c++) {
auto source_view=Notebook::get().get_view(c); auto source_view=Notebook::get().get_view(c);
@ -105,35 +105,35 @@ void Project::debug_update_stop() {
Notebook::get().get_current_view()->get_buffer()->place_cursor(Notebook::get().get_current_view()->get_buffer()->get_insert()->get_iter()); Notebook::get().get_current_view()->get_buffer()->place_cursor(Notebook::get().get_current_view()->get_buffer()->get_insert()->get_iter());
} }
std::unique_ptr<Project::Language> Project::get_language() { std::unique_ptr<Project::Base> Project::create() {
std::unique_ptr<Project::Build> build; std::unique_ptr<Project::Build> build;
if(Notebook::get().get_current_page()!=-1) { if(Notebook::get().get_current_page()!=-1) {
auto view=Notebook::get().get_current_view(); auto view=Notebook::get().get_current_view();
build=get_build(view->file_path); build=Build::create(view->file_path);
if(view->language) { if(view->language) {
auto language_id=view->language->get_id(); auto language_id=view->language->get_id();
if(language_id=="markdown") if(language_id=="markdown")
return std::unique_ptr<Project::Language>(new Project::Markdown(std::move(build))); return std::unique_ptr<Project::Base>(new Project::Markdown(std::move(build)));
if(language_id=="python") if(language_id=="python")
return std::unique_ptr<Project::Language>(new Project::Python(std::move(build))); return std::unique_ptr<Project::Base>(new Project::Python(std::move(build)));
if(language_id=="js") if(language_id=="js")
return std::unique_ptr<Project::Language>(new Project::JavaScript(std::move(build))); return std::unique_ptr<Project::Base>(new Project::JavaScript(std::move(build)));
if(language_id=="html") if(language_id=="html")
return std::unique_ptr<Project::Language>(new Project::HTML(std::move(build))); return std::unique_ptr<Project::Base>(new Project::HTML(std::move(build)));
} }
} }
else else
build=get_build(Directories::get().path); build=Build::create(Directories::get().path);
if(dynamic_cast<CMake*>(build.get())) if(dynamic_cast<CMakeBuild*>(build.get()))
return std::unique_ptr<Project::Language>(new Project::Clang(std::move(build))); return std::unique_ptr<Project::Base>(new Project::Clang(std::move(build)));
else else
return std::unique_ptr<Project::Language>(new Project::Language(std::move(build))); return std::unique_ptr<Project::Base>(new Project::Base(std::move(build)));
} }
std::pair<std::string, std::string> Project::Clang::get_run_arguments() { std::pair<std::string, std::string> Project::Clang::get_run_arguments() {
if(build->get_default_build_path().empty() || !build->update_default_build()) if(build->get_default_path().empty() || !build->update_default())
return {"", ""}; return {"", ""};
auto project_path=build->project_path.string(); auto project_path=build->project_path.string();
@ -147,7 +147,7 @@ std::pair<std::string, std::string> Project::Clang::get_run_arguments() {
if(executable!="") { if(executable!="") {
auto project_path=build->project_path; auto project_path=build->project_path;
auto build_path=build->get_default_build_path(); auto build_path=build->get_default_path();
if(!build_path.empty()) { if(!build_path.empty()) {
size_t pos=executable.find(project_path.string()); size_t pos=executable.find(project_path.string());
if(pos!=std::string::npos) if(pos!=std::string::npos)
@ -156,15 +156,15 @@ std::pair<std::string, std::string> Project::Clang::get_run_arguments() {
arguments=filesystem::escape_argument(executable); arguments=filesystem::escape_argument(executable);
} }
else else
arguments=filesystem::escape_argument(build->get_default_build_path()); arguments=filesystem::escape_argument(build->get_default_path());
} }
return {project_path, arguments}; return {project_path, arguments};
} }
void Project::Clang::compile() { void Project::Clang::compile() {
auto default_build_path=build->get_default_build_path(); auto default_build_path=build->get_default_path();
if(default_build_path.empty() || !build->update_default_build()) if(default_build_path.empty() || !build->update_default())
return; return;
if(Config::get().project.clear_terminal_on_compile) if(Config::get().project.clear_terminal_on_compile)
@ -178,8 +178,8 @@ void Project::Clang::compile() {
} }
void Project::Clang::compile_and_run() { void Project::Clang::compile_and_run() {
auto default_build_path=build->get_default_build_path(); auto default_build_path=build->get_default_path();
if(default_build_path.empty() || !build->update_default_build()) if(default_build_path.empty() || !build->update_default())
return; return;
auto project_path=build->project_path; auto project_path=build->project_path;
@ -219,7 +219,7 @@ void Project::Clang::compile_and_run() {
#ifdef JUCI_ENABLE_DEBUG #ifdef JUCI_ENABLE_DEBUG
std::pair<std::string, std::string> Project::Clang::debug_get_run_arguments() { std::pair<std::string, std::string> Project::Clang::debug_get_run_arguments() {
if(build->get_default_build_path().empty() || !build->update_default_build()) if(build->get_default_path().empty() || !build->update_default())
return {"", ""}; return {"", ""};
auto project_path=build->project_path.string(); auto project_path=build->project_path.string();
@ -233,7 +233,7 @@ std::pair<std::string, std::string> Project::Clang::debug_get_run_arguments() {
if(executable!="") { if(executable!="") {
auto project_path=build->project_path; auto project_path=build->project_path;
auto build_path=build->get_debug_build_path(); auto build_path=build->get_debug_path();
if(!build_path.empty()) { if(!build_path.empty()) {
size_t pos=executable.find(project_path.string()); size_t pos=executable.find(project_path.string());
if(pos!=std::string::npos) if(pos!=std::string::npos)
@ -242,15 +242,15 @@ std::pair<std::string, std::string> Project::Clang::debug_get_run_arguments() {
arguments=filesystem::escape_argument(executable); arguments=filesystem::escape_argument(executable);
} }
else else
arguments=filesystem::escape_argument(build->get_debug_build_path()); arguments=filesystem::escape_argument(build->get_debug_path());
} }
return {project_path, arguments}; return {project_path, arguments};
} }
void Project::Clang::debug_start() { void Project::Clang::debug_start() {
auto debug_build_path=build->get_debug_build_path(); auto debug_build_path=build->get_debug_path();
if(debug_build_path.empty() || !build->update_debug_build()) if(debug_build_path.empty() || !build->update_debug())
return; return;
auto project_path=build->project_path; auto project_path=build->project_path;

30
src/project.h

@ -25,10 +25,10 @@ namespace Project {
void debug_update_stop(); void debug_update_stop();
void debug_update_status(const std::string &debug_status); void debug_update_status(const std::string &debug_status);
class Language { class Base {
public: public:
Language(std::unique_ptr<Build> &&build): build(std::move(build)) {} Base(std::unique_ptr<Build> &&build): build(std::move(build)) {}
virtual ~Language() {} virtual ~Base() {}
std::unique_ptr<Build> build; std::unique_ptr<Build> build;
@ -55,11 +55,11 @@ namespace Project {
virtual void debug_delete() {} virtual void debug_delete() {}
}; };
class Clang : public Language { class Clang : public Base {
private: private:
Dispatcher dispatcher; Dispatcher dispatcher;
public: public:
Clang(std::unique_ptr<Build> &&build) : Language(std::move(build)) {} Clang(std::unique_ptr<Build> &&build) : Base(std::move(build)) {}
~Clang() { dispatcher.disconnect(); } ~Clang() { dispatcher.disconnect(); }
std::pair<std::string, std::string> get_run_arguments() override; std::pair<std::string, std::string> get_run_arguments() override;
@ -87,38 +87,38 @@ namespace Project {
#endif #endif
}; };
class Markdown : public Language { class Markdown : public Base {
public: public:
Markdown(std::unique_ptr<Build> &&build) : Language(std::move(build)) {} Markdown(std::unique_ptr<Build> &&build) : Base(std::move(build)) {}
~Markdown(); ~Markdown();
boost::filesystem::path last_temp_path; boost::filesystem::path last_temp_path;
void compile_and_run() override; void compile_and_run() override;
}; };
class Python : public Language { class Python : public Base {
public: public:
Python(std::unique_ptr<Build> &&build) : Language(std::move(build)) {} Python(std::unique_ptr<Build> &&build) : Base(std::move(build)) {}
void compile_and_run() override; void compile_and_run() override;
}; };
class JavaScript : public Language { class JavaScript : public Base {
public: public:
JavaScript(std::unique_ptr<Build> &&build) : Language(std::move(build)) {} JavaScript(std::unique_ptr<Build> &&build) : Base(std::move(build)) {}
void compile_and_run() override; void compile_and_run() override;
}; };
class HTML : public Language { class HTML : public Base {
public: public:
HTML(std::unique_ptr<Build> &&build) : Language(std::move(build)) {} HTML(std::unique_ptr<Build> &&build) : Base(std::move(build)) {}
void compile_and_run() override; void compile_and_run() override;
}; };
std::unique_ptr<Language> get_language(); std::unique_ptr<Base> create();
extern std::unique_ptr<Language> current_language; extern std::unique_ptr<Base> current;
}; };
#endif // JUCI_PROJECT_H_ #endif // JUCI_PROJECT_H_

20
src/project_build.cc

@ -2,8 +2,8 @@
#include "config.h" #include "config.h"
#include "info.h" #include "info.h"
std::unique_ptr<Project::Build> Project::get_build(const boost::filesystem::path &path) { std::unique_ptr<Project::Build> Project::Build::create(const boost::filesystem::path &path) {
std::unique_ptr<Project::Build> cmake(new CMake(path)); std::unique_ptr<Project::Build> cmake(new CMakeBuild(path));
if(!cmake->project_path.empty()) if(!cmake->project_path.empty())
return cmake; return cmake;
else { else {
@ -12,7 +12,7 @@ std::unique_ptr<Project::Build> Project::get_build(const boost::filesystem::path
} }
} }
boost::filesystem::path Project::Build::get_default_build_path() { boost::filesystem::path Project::Build::get_default_path() {
if(project_path.empty()) if(project_path.empty())
return boost::filesystem::path(); return boost::filesystem::path();
@ -35,7 +35,7 @@ boost::filesystem::path Project::Build::get_default_build_path() {
return default_build_path; return default_build_path;
} }
boost::filesystem::path Project::Build::get_debug_build_path() { boost::filesystem::path Project::Build::get_debug_path() {
if(project_path.empty()) if(project_path.empty())
return boost::filesystem::path(); return boost::filesystem::path();
@ -69,18 +69,18 @@ boost::filesystem::path Project::Build::get_debug_build_path() {
return debug_build_path; return debug_build_path;
} }
Project::CMake::CMake(const boost::filesystem::path &path) : Project::Build(), cmake(path) { Project::CMakeBuild::CMakeBuild(const boost::filesystem::path &path) : Project::Build(), cmake(path) {
project_path=cmake.project_path; project_path=cmake.project_path;
} }
bool Project::CMake::update_default_build(bool force) { bool Project::CMakeBuild::update_default(bool force) {
return cmake.update_default_build(get_default_build_path(), force); return cmake.update_default_build(get_default_path(), force);
} }
bool Project::CMake::update_debug_build(bool force) { bool Project::CMakeBuild::update_debug(bool force) {
return cmake.update_debug_build(get_debug_build_path(), force); return cmake.update_debug_build(get_debug_path(), force);
} }
boost::filesystem::path Project::CMake::get_executable(const boost::filesystem::path &path) { boost::filesystem::path Project::CMakeBuild::get_executable(const boost::filesystem::path &path) {
return cmake.get_executable(path); return cmake.get_executable(path);
} }

20
src/project_build.h

@ -12,26 +12,26 @@ namespace Project {
boost::filesystem::path project_path; boost::filesystem::path project_path;
boost::filesystem::path get_default_build_path(); boost::filesystem::path get_default_path();
virtual bool update_default_build(bool force=false) {return false;} virtual bool update_default(bool force=false) {return false;}
boost::filesystem::path get_debug_build_path(); boost::filesystem::path get_debug_path();
virtual bool update_debug_build(bool force=false) {return false;} virtual bool update_debug(bool force=false) {return false;}
virtual boost::filesystem::path get_executable(const boost::filesystem::path &path) {return boost::filesystem::path();} virtual boost::filesystem::path get_executable(const boost::filesystem::path &path) {return boost::filesystem::path();}
static std::unique_ptr<Build> create(const boost::filesystem::path &path);
}; };
class CMake : public Build { class CMakeBuild : public Build {
::CMake cmake; ::CMake cmake;
public: public:
CMake(const boost::filesystem::path &path); CMakeBuild(const boost::filesystem::path &path);
bool update_default_build(bool force=false) override; bool update_default(bool force=false) override;
bool update_debug_build(bool force=false) override; bool update_debug(bool force=false) override;
boost::filesystem::path get_executable(const boost::filesystem::path &path) override; boost::filesystem::path get_executable(const boost::filesystem::path &path) override;
}; };
std::unique_ptr<Build> get_build(const boost::filesystem::path &path);
} }
#endif // JUCI_PROJECT_BUILD_H_ #endif // JUCI_PROJECT_BUILD_H_

6
src/source_clang.cc

@ -174,9 +174,9 @@ void Source::ClangViewParse::soft_reparse() {
} }
std::vector<std::string> Source::ClangViewParse::get_compilation_commands() { std::vector<std::string> Source::ClangViewParse::get_compilation_commands() {
auto build=Project::get_build(file_path); auto build=Project::Build::create(file_path);
auto default_build_path=build->get_default_build_path(); auto default_build_path=build->get_default_path();
build->update_default_build(); build->update_default();
clang::CompilationDatabase db(default_build_path.string()); clang::CompilationDatabase db(default_build_path.string());
clang::CompileCommands commands(file_path.string(), db); clang::CompileCommands commands(file_path.string(), db);
std::vector<clang::CompileCommand> cmds = commands.get_commands(); std::vector<clang::CompileCommand> cmds = commands.get_commands();

8
src/terminal.cc

@ -362,8 +362,8 @@ bool Terminal::on_button_press_event(GdkEventButton* button_event) {
auto path=boost::filesystem::path(path_str); auto path=boost::filesystem::path(path_str);
boost::system::error_code ec; boost::system::error_code ec;
if(path.is_relative()) { if(path.is_relative()) {
if(Project::current_language) if(Project::current)
path=boost::filesystem::canonical(Project::current_language->build->get_default_build_path()/path_str, ec); path=boost::filesystem::canonical(Project::current->build->get_default_path()/path_str, ec);
else else
return Gtk::TextView::on_button_press_event(button_event); return Gtk::TextView::on_button_press_event(button_event);
} }
@ -393,7 +393,7 @@ bool Terminal::on_key_press_event(GdkEventKey *event) {
std::unique_lock<std::mutex> lock(processes_mutex); std::unique_lock<std::mutex> lock(processes_mutex);
bool debug_is_running=false; bool debug_is_running=false;
#ifdef JUCI_ENABLE_DEBUG #ifdef JUCI_ENABLE_DEBUG
debug_is_running=Project::current_language?Project::current_language->debug_is_running():false; debug_is_running=Project::current?Project::current->debug_is_running():false;
#endif #endif
if(processes.size()>0 || debug_is_running) { if(processes.size()>0 || debug_is_running) {
get_buffer()->place_cursor(get_buffer()->end()); get_buffer()->place_cursor(get_buffer()->end());
@ -415,7 +415,7 @@ bool Terminal::on_key_press_event(GdkEventKey *event) {
stdin_buffer+='\n'; stdin_buffer+='\n';
if(debug_is_running) { if(debug_is_running) {
#ifdef JUCI_ENABLE_DEBUG #ifdef JUCI_ENABLE_DEBUG
Project::current_language->debug_write(stdin_buffer); Project::current->debug_write(stdin_buffer);
#endif #endif
} }
else else

68
src/window.cc

@ -558,7 +558,7 @@ void Window::set_menu_actions() {
}); });
menu.add_action("project_set_run_arguments", [this]() { menu.add_action("project_set_run_arguments", [this]() {
auto project_language=Project::get_language(); auto project_language=Project::create();
auto run_arguments=std::make_shared<std::pair<std::string, std::string> >(project_language->get_run_arguments()); auto run_arguments=std::make_shared<std::pair<std::string, std::string> >(project_language->get_run_arguments());
if(run_arguments->second.empty()) if(run_arguments->second.empty())
return; return;
@ -587,12 +587,12 @@ void Window::set_menu_actions() {
return; return;
} }
Project::current_language=Project::get_language(); Project::current=Project::create();
if(Config::get().project.save_on_compile_or_run) if(Config::get().project.save_on_compile_or_run)
Project::save_files(Project::current_language->build->project_path); Project::save_files(Project::current->build->project_path);
Project::current_language->compile_and_run(); Project::current->compile_and_run();
}); });
menu.add_action("compile", [this]() { menu.add_action("compile", [this]() {
if(Project::compiling || Project::debugging) { if(Project::compiling || Project::debugging) {
@ -600,12 +600,12 @@ void Window::set_menu_actions() {
return; return;
} }
Project::current_language=Project::get_language(); Project::current=Project::create();
if(Config::get().project.save_on_compile_or_run) if(Config::get().project.save_on_compile_or_run)
Project::save_files(Project::current_language->build->project_path); Project::save_files(Project::current->build->project_path);
Project::current_language->compile(); Project::current->compile();
}); });
menu.add_action("run_command", [this]() { menu.add_action("run_command", [this]() {
@ -645,7 +645,7 @@ void Window::set_menu_actions() {
#ifdef JUCI_ENABLE_DEBUG #ifdef JUCI_ENABLE_DEBUG
menu.add_action("debug_set_run_arguments", [this]() { menu.add_action("debug_set_run_arguments", [this]() {
auto project_language=Project::get_language(); auto project_language=Project::create();
auto run_arguments=std::make_shared<std::pair<std::string, std::string> >(project_language->debug_get_run_arguments()); auto run_arguments=std::make_shared<std::pair<std::string, std::string> >(project_language->debug_get_run_arguments());
if(run_arguments->second.empty()) if(run_arguments->second.empty())
return; return;
@ -674,51 +674,51 @@ void Window::set_menu_actions() {
return; return;
} }
else if(Project::debugging) { else if(Project::debugging) {
Project::current_language->debug_continue(); Project::current->debug_continue();
return; return;
} }
Project::current_language=Project::get_language(); Project::current=Project::create();
if(Config::get().project.save_on_compile_or_run) if(Config::get().project.save_on_compile_or_run)
Project::save_files(Project::current_language->build->project_path); Project::save_files(Project::current->build->project_path);
Project::current_language->debug_start(); Project::current->debug_start();
}); });
menu.add_action("debug_stop", [this]() { menu.add_action("debug_stop", [this]() {
if(Project::current_language) if(Project::current)
Project::current_language->debug_stop(); Project::current->debug_stop();
}); });
menu.add_action("debug_kill", [this]() { menu.add_action("debug_kill", [this]() {
if(Project::current_language) if(Project::current)
Project::current_language->debug_kill(); Project::current->debug_kill();
}); });
menu.add_action("debug_step_over", [this]() { menu.add_action("debug_step_over", [this]() {
if(Project::current_language) if(Project::current)
Project::current_language->debug_step_over(); Project::current->debug_step_over();
}); });
menu.add_action("debug_step_into", [this]() { menu.add_action("debug_step_into", [this]() {
if(Project::current_language) if(Project::current)
Project::current_language->debug_step_into(); Project::current->debug_step_into();
}); });
menu.add_action("debug_step_out", [this]() { menu.add_action("debug_step_out", [this]() {
if(Project::current_language) if(Project::current)
Project::current_language->debug_step_out(); Project::current->debug_step_out();
}); });
menu.add_action("debug_backtrace", [this]() { menu.add_action("debug_backtrace", [this]() {
if(Project::current_language) if(Project::current)
Project::current_language->debug_backtrace(); Project::current->debug_backtrace();
}); });
menu.add_action("debug_show_variables", [this]() { menu.add_action("debug_show_variables", [this]() {
if(Project::current_language) if(Project::current)
Project::current_language->debug_show_variables(); Project::current->debug_show_variables();
}); });
menu.add_action("debug_run_command", [this]() { menu.add_action("debug_run_command", [this]() {
EntryBox::get().clear(); EntryBox::get().clear();
EntryBox::get().entries.emplace_back(last_run_debug_command, [this](const std::string& content){ EntryBox::get().entries.emplace_back(last_run_debug_command, [this](const std::string& content){
if(content!="") { if(content!="") {
if(Project::current_language) if(Project::current)
Project::current_language->debug_run_command(content); Project::current->debug_run_command(content);
last_run_debug_command=content; last_run_debug_command=content;
} }
EntryBox::get().hide(); EntryBox::get().hide();
@ -740,13 +740,13 @@ void Window::set_menu_actions() {
auto end_iter=start_iter; auto end_iter=start_iter;
while(!end_iter.ends_line() && end_iter.forward_char()) {} while(!end_iter.ends_line() && end_iter.forward_char()) {}
view->get_source_buffer()->remove_source_marks(start_iter, end_iter, "debug_breakpoint"); view->get_source_buffer()->remove_source_marks(start_iter, end_iter, "debug_breakpoint");
if(Project::current_language && Project::debugging) if(Project::current && Project::debugging)
Project::current_language->debug_remove_breakpoint(view->file_path, line_nr+1, view->get_buffer()->get_line_count()+1); Project::current->debug_remove_breakpoint(view->file_path, line_nr+1, view->get_buffer()->get_line_count()+1);
} }
else { else {
view->get_source_buffer()->create_source_mark("debug_breakpoint", view->get_buffer()->get_insert()->get_iter()); view->get_source_buffer()->create_source_mark("debug_breakpoint", view->get_buffer()->get_insert()->get_iter());
if(Project::current_language && Project::debugging) if(Project::current && Project::debugging)
Project::current_language->debug_add_breakpoint(view->file_path, line_nr+1); Project::current->debug_add_breakpoint(view->file_path, line_nr+1);
} }
} }
}); });
@ -880,8 +880,8 @@ bool Window::on_delete_event(GdkEventAny *event) {
} }
Terminal::get().kill_async_processes(); Terminal::get().kill_async_processes();
#ifdef JUCI_ENABLE_DEBUG #ifdef JUCI_ENABLE_DEBUG
if(Project::current_language) if(Project::current)
Project::current_language->debug_delete(); Project::current->debug_delete();
#endif #endif
return false; return false;
} }

Loading…
Cancel
Save