<commit_before>#include "notebook.h"
#include "logging.h"
#include "sourcefile.h"
#include "singletons.h"
#include <fstream>
#include <regex>
#include "cmake.h"

#if GTK_VERSION_GE(3, 18)
#include "gtksourceview-3.0/gtksourceview/gtksourcemap.h"
#endif

#include <iostream> //TODO: remove
using namespace std; //TODO: remove

namespace sigc {
#ifndef SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE
  template <typename Functor>
  struct functor_trait<Functor, false> {
    typedef decltype (::sigc::mem_fun(std::declval<Functor&>(),
                                      &Functor::operator())) _intermediate;
    typedef typename _intermediate::result_type result_type;
    typedef Functor functor_type;
  };
#else
  SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE
#endif
}

Notebook::Notebook(Directories &directories) : Gtk::Notebook(), directories(directories) {
  Gsv::init();
}

int Notebook::size() {
  return get_n_pages();
}

Source::View* Notebook::get_view(int page) {
  return source_views.at(get_index(page));
}

size_t Notebook::get_index(int page) {
  for(size_t c=0;c<hboxes.size();c++) {
    if(page_num(*hboxes.at(c))==page)
      return c;
  }
  return -1;
}

Source::View* Notebook::get_current_view() {
  return get_view(get_current_page());
}

void Notebook::open(const boost::filesystem::path &file_path) {
  DEBUG("start");
  for(int c=0;c<size();c++) {
    if(file_path==get_view(c)->file_path) {
      set_current_page(c);
      get_current_view()->grab_focus();
      return;
    }
  }
  
  std::ifstream can_read(file_path.string());
  if(!can_read) {
    Singleton::terminal()->print("Error: could not open "+file_path.string()+"\n");
    return;
  }
  can_read.close();
  
  auto language=Source::guess_language(file_path);
  if(language && (language->get_id()=="chdr" || language->get_id()=="cpphdr" || language->get_id()=="c" || language->get_id()=="cpp" || language->get_id()=="objc")) {
    boost::filesystem::path project_path;
    if(directories.cmake && directories.cmake->project_path!="" && file_path.generic_string().substr(0, directories.cmake->project_path.generic_string().size()+1)==directories.cmake->project_path.generic_string()+'/') {
      project_path=directories.cmake->project_path;
      if(boost::filesystem::exists(project_path.string()+"/CMakeLists.txt") && !boost::filesystem::exists(project_path.string()+"/compile_commands.json"))
        CMake::create_compile_commands(project_path);
    }
    else {
      project_path=file_path.parent_path();
      CMake cmake(project_path);
      if(cmake.project_path!="") {
        project_path=cmake.project_path;
        Singleton::terminal()->print("Project path for "+file_path.string()+" set to "+project_path.string()+"\n");
      }
      else
        Singleton::terminal()->print("Error: could not find project path for "+file_path.string()+"\n");
    }
    source_views.emplace_back(new Source::ClangView(file_path, project_path, language));
  }
  else
    source_views.emplace_back(new Source::GenericView(file_path, "", language));
  
  source_views.back()->on_update_status=[this](Source::View* view, const std::string &status) {
    if(get_current_page()!=-1 && get_current_view()==view)
      Singleton::status()->set_text(status+" ");
  };
  source_views.back()->on_update_info=[this](Source::View* view, const std::string &info) {
    if(get_current_page()!=-1 && get_current_view()==view)
      Singleton::info()->set_text(" "+info);
  };
  
  scrolled_windows.emplace_back(new Gtk::ScrolledWindow());
  hboxes.emplace_back(new Gtk::HBox());
  scrolled_windows.back()->add(*source_views.back());
  hboxes.back()->pack_start(*scrolled_windows.back());

#if GTK_VERSION_GE(3, 18)
  source_maps.emplace_back(Glib::wrap(gtk_source_map_new()));
  gtk_source_map_set_view(GTK_SOURCE_MAP(source_maps.back()->gobj()), source_views.back()->gobj());
#endif
  configure(source_views.size()-1);
  
  std::string title=file_path.filename().string();
  append_page(*hboxes.back(), title);
  
  set_tab_reorderable(*hboxes.back(), true);
  show_all_children();
  set_current_page(size()-1);
  set_focus_child(*source_views.back());
  get_current_view()->get_buffer()->set_modified(false);<commit_msg>Now adding project_path if possible to all file types.<commit_after>#include "notebook.h"
#include "logging.h"
#include "sourcefile.h"
#include "singletons.h"
#include <fstream>
#include <regex>
#include "cmake.h"

#if GTK_VERSION_GE(3, 18)
#include "gtksourceview-3.0/gtksourceview/gtksourcemap.h"
#endif

#include <iostream> //TODO: remove
using namespace std; //TODO: remove

namespace sigc {
#ifndef SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE
  template <typename Functor>
  struct functor_trait<Functor, false> {
    typedef decltype (::sigc::mem_fun(std::declval<Functor&>(),
                                      &Functor::operator())) _intermediate;
    typedef typename _intermediate::result_type result_type;
    typedef Functor functor_type;
  };
#else
  SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE
#endif
}

Notebook::Notebook(Directories &directories) : Gtk::Notebook(), directories(directories) {
  Gsv::init();
}

int Notebook::size() {
  return get_n_pages();
}

Source::View* Notebook::get_view(int page) {
  return source_views.at(get_index(page));
}

size_t Notebook::get_index(int page) {
  for(size_t c=0;c<hboxes.size();c++) {
    if(page_num(*hboxes.at(c))==page)
      return c;
  }
  return -1;
}

Source::View* Notebook::get_current_view() {
  return get_view(get_current_page());
}

void Notebook::open(const boost::filesystem::path &file_path) {
  DEBUG("start");
  for(int c=0;c<size();c++) {
    if(file_path==get_view(c)->file_path) {
      set_current_page(c);
      get_current_view()->grab_focus();
      return;
    }
  }
  
  std::ifstream can_read(file_path.string());
  if(!can_read) {
    Singleton::terminal()->print("Error: could not open "+file_path.string()+"\n");
    return;
  }
  can_read.close();
  
  auto language=Source::guess_language(file_path);
  if(language && (language->get_id()=="chdr" || language->get_id()=="cpphdr" || language->get_id()=="c" || language->get_id()=="cpp" || language->get_id()=="objc")) {
    boost::filesystem::path project_path;
    if(directories.cmake && directories.cmake->project_path!="" && file_path.generic_string().substr(0, directories.cmake->project_path.generic_string().size()+1)==directories.cmake->project_path.generic_string()+'/') {
      project_path=directories.cmake->project_path;
      if(boost::filesystem::exists(project_path.string()+"/CMakeLists.txt") && !boost::filesystem::exists(project_path.string()+"/compile_commands.json"))
        CMake::create_compile_commands(project_path);
    }
    else {
      project_path=file_path.parent_path();
      CMake cmake(project_path);
      if(cmake.project_path!="") {
        project_path=cmake.project_path;
        Singleton::terminal()->print("Project path for "+file_path.string()+" set to "+project_path.string()+"\n");
      }
      else
        Singleton::terminal()->print("Error: could not find project path for "+file_path.string()+"\n");
    }
    source_views.emplace_back(new Source::ClangView(file_path, project_path, language));
  }
  else {
    boost::filesystem::path project_path;
    if(directories.cmake && directories.cmake->project_path!="" && file_path.generic_string().substr(0, directories.cmake->project_path.generic_string().size()+1)==directories.cmake->project_path.generic_string()+'/')
      project_path=directories.cmake->project_path;
    source_views.emplace_back(new Source::GenericView(file_path, project_path, language));
  }
  
  source_views.back()->on_update_status=[this](Source::View* view, const std::string &status) {
    if(get_current_page()!=-1 && get_current_view()==view)
      Singleton::status()->set_text(status+" ");
  };
  source_views.back()->on_update_info=[this](Source::View* view, const std::string &info) {
    if(get_current_page()!=-1 && get_current_view()==view)
      Singleton::info()->set_text(" "+info);
  };
  
  scrolled_windows.emplace_back(new Gtk::ScrolledWindow());
  hboxes.emplace_back(new Gtk::HBox());
  scrolled_windows.back()->add(*source_views.back());
  hboxes.back()->pack_start(*scrolled_windows.back());

#if GTK_VERSION_GE(3, 18)
  source_maps.emplace_back(Glib::wrap(gtk_source_map_new()));
  gtk_source_map_set_view(GTK_SOURCE_MAP(source_maps.back()->gobj()), source_views.back()->gobj());
#endif
  configure(source_views.size()-1);
  
  std::string title=file_path.filename().string();
  append_page(*hboxes.back(), title);
  
  set_tab_reorderable(*hboxes.back(), true);
  show_all_children();
  set_current_page(size()-1);
  set_focus_child(*source_views.back());
  get_current_view()->get_buffer()->set_modified(false);
{'fraction_non_alphanumeric': 0.17432972047917855, 'fraction_numerical': 0.0041072447233314316, 'mean_word_length': 5.417276720351391, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7271438', 'n_tokens_mistral': 3087, 'n_tokens_neox': 3054, 'n_words': 399}