diff --git a/juci/CMakeLists.txt b/juci/CMakeLists.txt index 84e5973..d4f326d 100644 --- a/juci/CMakeLists.txt +++ b/juci/CMakeLists.txt @@ -3,7 +3,6 @@ set(project_name juci) set(module juci_to_python_api) project (${project_name}) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_LIST_DIR}/cmake/Modules/") @@ -15,8 +14,22 @@ message("Searcing for libclang") #LIBCLANG_LIBRARIES The libraries needed to use libclang. #LIBCLANG_LIBRARY_DIR The path to the directory containing libclang. #LIBCLANG_KNOWN_LLVM_VERSIONS Known LLVM release numbers. -find_package(Testlcl) +find_package(LibClangmm) +find_package(LibClang) +if(${LCL_FOUND}) + message("libclangmm libraries found. Continuing") + message("${LCL_INCLUDE_DIRS}") +else() + message(FATAL_ERROR "The libclangmm libraries are required. Quitting.") +endif() + +if(${LIBCLANG_FOUND}) + message("libclangmm libraries found. Continuing") + message("${LIBCLANG_INCLUDE_DIRS}") +else() + message(FATAL_ERROR "The libclangmm libraries are required. Quitting.") +endif() #### Finding boost, the variables below is set ##### #PYTHONLIBS_FOUND - True if headers and requested libraries were found @@ -39,7 +52,7 @@ endif() #Boost_INCLUDE_DIRS - Boost include directories #Boost_LIBRARY_DIRS - Link directories for Boost libraries #Boost_LIBRARIES - Boost component libraries to be linked -find_package(Boost 1.5 REQUIRED COMPONENTS python) +find_package(Boost 1.5 REQUIRED COMPONENTS python timer system) #If boost is not found if(${Boost_FOUND}) @@ -72,20 +85,25 @@ endif() # name of the executable on Windows will be example.exe add_executable(${project_name} #list of every needed file to create the executable - juci.cc - keybindings - menu - source + juci.cc + keybindings.h + keybindings.cc + menu.h + menu.cc + source.h + source.cc config.h config.cc sourcefile.h sourcefile.cc - window - api - notebook - entry - - #there is no need for extentions + window.cc + window.h + api.h + api.cc + notebook.cc + notebook.h + entry.h + entry.cc ) add_library(${module} SHARED @@ -94,29 +112,25 @@ add_library(${module} SHARED ) # dependencies -if(${lcl_NOTFOUND}) - message("FATAL_ERROR ohh lord!") -else() - message("Found libs!") -endif() - include_directories( ${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS} ${GTKMM_INCLUDE_DIRS} ${LCL_INCLUDE_DIRS} + ${LIBCLANG_INCLUDE_DIRS} ) link_directories( ${GTKMM_LIBRARY_DIRS} ${Boost_LIBRARY_DIRS} ${PYTHON_INCLUDE_DIRS} ${LCL_LIBRARY_DIRS} + ${LIBCLANG_LIBRARY_DIRS} ) #module: set_target_properties(${module} PROPERTIES PREFIX "" - LIBRARY_OUTPUT_DIRECTORY "/usr/lib/python2.7/dist-packages/") + LIBRARY_OUTPUT_DIRECTORY "/usr/lib/python2.7/dist-packages/") target_link_libraries(${module} ${PYTHON_LIBRARIES} ${Boost_LIBRARIES}) #executable: -target_link_libraries(${project_name} ${LCL_LIBRARIES} ${GTKMM_LIBRARIES} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES}) +target_link_libraries(${project_name} ${LIVCLANG_LIBRARIES} ${LCL_LIBRARIES} ${GTKMM_LIBRARIES} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES}) diff --git a/juci/cmake/Modules/FindLibClang.cmake b/juci/cmake/Modules/FindLibClang.cmake new file mode 100644 index 0000000..8a4ed46 --- /dev/null +++ b/juci/cmake/Modules/FindLibClang.cmake @@ -0,0 +1,82 @@ +# +# Try to find libclang +# +# Once done this will define: +# - LIBCLANG_FOUND +# System has libclang. +# - LIBCLANG_INCLUDE_DIRS +# The libclang include directories. +# - LIBCLANG_LIBRARIES +# The libraries needed to use libclang. +# - LIBCLANG_LIBRARY_DIR +# The path to the directory containing libclang. +# - LIBCLANG_KNOWN_LLVM_VERSIONS +# Known LLVM release numbers. + +# most recent versions come first +set(LIBCLANG_KNOWN_LLVM_VERSIONS 3.6 + 3.5.1 + 3.5.0 #Arch Linux + 3.5 #LLVM Debian/Ubuntu packages from http://llvm.org/apt/ + 3.4.2 3.4.1 3.4 3.3 3.2 3.1) + +set(libclang_llvm_header_search_paths) +set(libclang_llvm_lib_search_paths + # LLVM Fedora + /usr/lib/llvm + ) + +foreach (version ${LIBCLANG_KNOWN_LLVM_VERSIONS}) + list(APPEND libclang_llvm_header_search_paths + # LLVM Debian/Ubuntu nightly packages: http://llvm.org/apt/ + "/usr/lib/llvm-${version}/include" + # LLVM MacPorts + "/opt/local/libexec/llvm-${version}/include" + # LLVM Homebrew + "/usr/local/Cellar/llvm/${version}/include" + # LLVM Homebrew/versions + "/usr/local/lib/llvm-${version}/include" + ) + + list(APPEND libclang_llvm_lib_search_paths + # LLVM Debian/Ubuntu nightly packages: http://llvm.org/apt/ + "/usr/lib/llvm-${version}/lib/" + # LLVM MacPorts + "/opt/local/libexec/llvm-${version}/lib" + # LLVM Homebrew + "/usr/local/Cellar/llvm/${version}/lib" + # LLVM Homebrew/versions + "/usr/local/lib/llvm-${version}/lib" + ) +endforeach() + +find_path(LIBCLANG_INCLUDE_DIR clang-c/Index.h + PATHS ${libclang_llvm_header_search_paths} + PATH_SUFFIXES LLVM/include #Windows package from http://llvm.org/releases/ + DOC "The path to the directory that contains clang-c/Index.h") + +# On Windows with MSVC, the import library uses the ".imp" file extension +# instead of the comon ".lib" +if (MSVC) + find_file(LIBCLANG_LIBRARY libclang.imp + PATH_SUFFIXES LLVM/lib + DOC "The file that corresponds to the libclang library.") +endif() + +find_library(LIBCLANG_LIBRARY NAMES libclang.imp libclang clang + PATHS ${libclang_llvm_lib_search_paths} + PATH_SUFFIXES LLVM/lib #Windows package from http://llvm.org/releases/ + DOC "The file that corresponds to the libclang library.") + +get_filename_component(LIBCLANG_LIBRARY_DIR ${LIBCLANG_LIBRARY} PATH) + +set(LIBCLANG_LIBRARIES ${LIBCLANG_LIBRARY}) +set(LIBCLANG_INCLUDE_DIRS ${LIBCLANG_INCLUDE_DIR}) + +include(FindPackageHandleStandardArgs) +# handle the QUIETLY and REQUIRED arguments and set LIBCLANG_FOUND to TRUE if +# all listed variables are TRUE +find_package_handle_standard_args(LibClang DEFAULT_MSG + LIBCLANG_LIBRARY LIBCLANG_INCLUDE_DIR) + +mark_as_advanced(LIBCLANG_INCLUDE_DIR LIBCLANG_LIBRARY) diff --git a/juci/cmake/Modules/FindTestlcl.cmake b/juci/cmake/Modules/FindLibClangmm.cmake similarity index 64% rename from juci/cmake/Modules/FindTestlcl.cmake rename to juci/cmake/Modules/FindLibClangmm.cmake index 5a77a83..4940e0c 100644 --- a/juci/cmake/Modules/FindTestlcl.cmake +++ b/juci/cmake/Modules/FindLibClangmm.cmake @@ -9,16 +9,12 @@ find_package(PkgConfig) -find_path(LCL_INCLUDE_DIR headers/TranslationUnit.h - HINTS "/home/gm/bachelor/lib/" - "/home/forgie/code/libclangpp/" - "/home/zalox/bachelor/libclang++/" +find_path(LCL_INCLUDE_DIR clangmm.h + HINTS "/usr/lib/libclangmm/include/" ) -find_library(LCL_LIBRARY NAMES testlcl - HINTS "/home/gm/bachelor/lib/lib/" - "/home/forgie/code/libclangpp/lib/" - "/home/zalox/bachelor/libclang++/lib/" +find_library(LCL_LIBRARY NAMES clangmm + HINTS "/usr/lib/libclangmm/" ) set(LCL_LIBRARIES ${LCL_LIBRARY} ) diff --git a/juci/juci.cc b/juci/juci.cc index 84b1e6f..b2afc98 100644 --- a/juci/juci.cc +++ b/juci/juci.cc @@ -1,11 +1,11 @@ #include "window.h" - int main(int argc, char *argv[]) { Glib::RefPtr app = Gtk::Application::create( argc, argv, "no.sout.juci"); + Window window; //api::LoadPlugin("juci_api_test"); diff --git a/juci/notebook.cc b/juci/notebook.cc index 5b44bd2..675b8d4 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -1,11 +1,14 @@ - +#include #include "notebook.h" + + Notebook::Model::Model() { cc_extension_ = ".cc"; h_extension_ = ".h"; - scrollvalue_ = 20; -} + scrollvalue_ = 50; +}; + Notebook::View::View(){ view_.pack_start(notebook_); } @@ -98,13 +101,6 @@ source_config_(source_cfg) { [this]() { Search(false); }); - - text_vec_.back()->view(). - signal_scroll_event().connect(sigc::mem_fun( - this, - &Notebook::Controller:: - scroll_event_callback)); - }//Constructor @@ -120,19 +116,14 @@ bool Notebook::Controller::scroll_event_callback(GdkEventScroll* scroll_event) { get_vscrollbar()->get_adjustment(); if ( direction_y != 0 ) { + int dir_val = direction_y==-1?-model_.scrollvalue_:+model_.scrollvalue_; adj->set_value(adj->get_value()+dir_val); text_vec_.at(page)->view().set_vadjustment(adj); linenumbers_vec_.at(page)->view().set_vadjustment(adj); } - if ( direction_x != 0 ) { - int dir_val = direction_x==-1?-model_.scrollvalue_:+model_.scrollvalue_; - adj->set_value(adj->get_value()+dir_val); - text_vec_.at(page)->view().set_hadjustment(adj); - } return true; } - Notebook::Controller::~Controller() { for (auto &i : text_vec_) delete i; for (auto &i : linenumbers_vec_) delete i; @@ -148,9 +139,9 @@ Gtk::Box& Notebook::Controller::entry_view() { return entry_.view(); } - void Notebook::Controller::OnNewPage(std::string name) { OnCreatePage(); + std::cout << "oppretta pages" << std::endl; text_vec_.back()->OnNewEmptyFile(); Notebook().append_page(*editor_vec_.back(), name); @@ -178,7 +169,7 @@ void Notebook::Controller::OnCreatePage(){ editor_vec_.push_back(new Gtk::HBox()); scrolledtext_vec_.back()->add(text_vec_.back()->view()); scrolledline_vec_.back()->add(linenumbers_vec_.back()->view()); - linenumbers_vec_.back()->view().get_buffer()->set_text("1 \n"); + linenumbers_vec_.back()->view().get_buffer()->set_text("1 "); linenumbers_vec_.back()->view().override_color(Gdk::RGBA("Black")); linenumbers_vec_.back()-> view().set_justification(Gtk::Justification::JUSTIFY_RIGHT); @@ -187,6 +178,7 @@ void Notebook::Controller::OnCreatePage(){ linenumbers_vec_.back()->view().set_sensitive(false); editor_vec_.back()->pack_start(*scrolledline_vec_.back(),false,false); editor_vec_.back()->pack_start(*scrolledtext_vec_.back(), true, true); + BufferChangeHandler(text_vec_.back()->view().get_buffer()); } @@ -268,7 +260,7 @@ void Notebook::Controller::Search(bool forward){ if ( !forward ) { if ( search_match_start_ == 0 || - search_match_start_.get_line_offset() == 0) { + search_match_start_.get_line_offset() == 0) { search_match_start_= Buffer(text_vec_.at(CurrentPage()))->end(); } search_match_start_. @@ -289,42 +281,35 @@ void Notebook::Controller::Search(bool forward){ search_match_end_); } } -void Notebook::Controller::OnBufferChange() { - int page = CurrentPage(); - int line_nr = Buffer(text_vec_.at(page))->get_line_count(); - - Glib::RefPtr - mark = Gtk::TextBuffer::Mark::create(); - Glib::RefPtr - mark_lines = Gtk::TextBuffer::Mark::create(); - - if(Buffer(text_vec_.at(page))->get_insert()->get_iter().starts_line() && - Buffer(text_vec_.at(page))->get_insert()->get_iter().get_line() == - Buffer(text_vec_.at(page))->end().get_line()) { - std::string lines ="1 "; - for ( int it = 2; it <= line_nr; ++it ) { - lines.append("\n"+ std::to_string(it)+" "); - } - Buffer(linenumbers_vec_.at(page))->set_text(lines); - - Buffer(text_vec_.at(page))-> - add_mark( - mark, - Buffer(text_vec_.at(page))->end()); - Buffer(linenumbers_vec_.at(page))-> - add_mark( - mark_lines, - Buffer(linenumbers_vec_.at(page))->end()); - text_vec_.at(page)->view().scroll_to(mark); - linenumbers_vec_.at(page)->view().scroll_to(mark_lines); - }else{ - Buffer(text_vec_.at(page))-> - add_mark( - mark, - Buffer(text_vec_.at(page))-> - get_insert()->get_iter()); - } +void Notebook::Controller::OnBufferChange() { + int page = CurrentPage(); + int text_nr = Buffer(text_vec_.at(page))->get_line_count(); + int line_nr = Buffer(linenumbers_vec_.at(page))->get_line_count(); + while (line_nr < text_nr ){ + line_nr++; + Buffer(linenumbers_vec_.at(page))-> + insert(Buffer(linenumbers_vec_.at(page))->end(), + "\n"+std::to_string(line_nr)+" "); + } + while (line_nr > text_nr ){ + Gtk::TextIter iter = Buffer(linenumbers_vec_.at(page))->get_iter_at_line(line_nr); + iter.backward_char(); + line_nr--; + Buffer(linenumbers_vec_.at(page))-> + erase(iter, + Buffer(linenumbers_vec_.at(page))->end()); + } + if(Buffer(text_vec_.at(page))->get_insert()->get_iter().starts_line() && + Buffer(text_vec_.at(page))->get_insert()->get_iter().get_line() == + Buffer(text_vec_.at(page))->end().get_line()) { + + GdkEventScroll* scroll = new GdkEventScroll; + scroll->delta_y = 1.0; + scroll->delta_x = 0.0; + scroll_event_callback(scroll); + delete scroll; + } } @@ -351,7 +336,8 @@ Gtk::Notebook& Notebook::Controller::Notebook() { void Notebook::Controller::BufferChangeHandler(Glib::RefPtr buffer) { buffer->signal_changed().connect( - [this]() { - OnBufferChange(); + [this]() { + OnBufferChange(); }); } + diff --git a/juci/notebook.h b/juci/notebook.h index cddf18a..e05dfbc 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -69,4 +69,5 @@ namespace Notebook { Glib::RefPtr refClipboard_; }; // class controller } // namespace Notebook + #endif // JUCI_NOTEBOOK_H_ diff --git a/juci/source.cc b/juci/source.cc index 34ed54d..95307e2 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -3,6 +3,7 @@ #include "sourcefile.h" #include #include +#include ////////////// //// View //// @@ -15,7 +16,7 @@ Source::View::View() { // returns the new line string Source::View::UpdateLine() { Gtk::TextIter line(get_buffer()->get_insert()->get_iter()); - //std::cout << line.get_line() << std::endl; + // std::cout << line.get_line() << std::endl; // for each word --> check what it is --> apply appropriate tag return ""; } @@ -37,9 +38,9 @@ void Source::View::ApplyConfig(const Source::Config &config) { } } -void Source::View::OnOpenFile(std::vector &locations, - const Source::Config &config) { - ApplyConfig(config); +void Source::View::OnOpenFile(std::vector &locations, + const Source::Config &config) { + /* ApplyConfig(config); Glib::RefPtr buffer = get_buffer(); for (auto &loc : locations) { string type = std::to_string(loc.kind()); @@ -66,10 +67,11 @@ void Source::View::OnOpenFile(std::vector &locations, Gtk::TextIter end_iter = buffer->get_iter_at_line_offset(linum_end, end); // std::cout << get_buffer()->get_text(begin_iter, end_iter) << std::endl; if (begin_iter.get_line() == end_iter.get_line()) { - buffer->apply_tag_by_name(config.typetable().at(type), - begin_iter, end_iter); + buffer->apply_tag_by_name(config.typetable().at(type), + begin_iter, end_iter); } } + */ } @@ -101,7 +103,7 @@ void Source::Config::InsertTag(const string &key, const string &value) { // Source::View::Config::SetTagTable() // sets the tagtable for the view void Source::Config::SetTypeTable( - const std::unordered_map &typetable) { + const std::unordered_map &typetable) { typetable_ = typetable; } @@ -111,32 +113,13 @@ void Source::Config::InsertType(const string &key, const string &value) { // Source::View::Config::SetTagTable() // sets the tagtable for the view void Source::Config::SetTagTable( - const std::unordered_map &tagtable) { + const std::unordered_map &tagtable) { tagtable_ = tagtable; } /////////////// //// Model //// /////////////// -/*Source::Model::Model() { - std::cout << "Model constructor run" << std::endl; - boost::property_tree::ptree pt; - boost::property_tree::json_parser::read_json("config.json", pt); - for ( auto &i : pt ) { - boost::property_tree::ptree props = pt.get_child(i.first); - for (auto &pi : props) { - if (i.first.compare("syntax")) { // checks the config-file - config_.InsertTag(pi.first, pi.second.get_value()); - // std::cout << "inserting tag. " << pi.first << pi.second.get_value() << std::endl; - } - if (i.first.compare("colors")) { // checks the config-file - config_.InsertType(pi.first, pi.second.get_value()); - // std::cout << "inserting type. " << pi.first << pi.second.get_value() << std::endl; - } - } - } -}*/ - Source::Model::Model(const Source::Config &config) : config_(config) { } @@ -154,7 +137,7 @@ void Source::Model::SetFilePath(const string &filepath) { } void Source::Model:: -SetSourceLocations(const std::vector &locations) { +SetSourceLocations(const std::vector &locations) { locations_ = locations; } //////////////////// @@ -170,13 +153,6 @@ Source::Controller::Controller(const Source::Config &config) : }); } -/*Source::Controller::Controller(){ - //std::cout << "Controller constructor run" << std::endl; - - view().get_buffer()->signal_changed().connect([this](){ - this->OnLineEdit(); - }); - }*/ // Source::Controller::view() // return shared_ptr to the view Source::View& Source::Controller::view() { @@ -200,16 +176,44 @@ void Source::Controller::OnNewEmptyFile() { s.save(""); } -void Source::Controller::OnOpenFile(const string &filename) { - sourcefile s(filename); - view().get_buffer()->set_text(s.get_content()); - int linums = view().get_buffer()->end().get_line(); - int offset = view().get_buffer()->end().get_line_offset(); - Clang::TranslationUnit tu(filename.c_str(), linums, offset); - model().SetSourceLocations(tu.getSourceLocations()); - view().OnOpenFile(model().getSourceLocations(), model().config()); +void Source::Controller::OnOpenFile(const string &filepath) { + sourcefile s(filepath); + buffer()->set_text(s.get_content()); + int start_offset = buffer()->begin().get_offset(); + int end_offset = buffer()->end().get_offset(); + + std::string project_path = + filepath.substr(0, filepath.find_last_of('/')); + + clang::CompilationDatabase db(project_path); + clang::CompileCommands commands(filepath, &db); + std::vector cmds = commands.get_commands(); + std::vector arguments; + for (auto &i : cmds) { + std::vector lol = i.get_command_as_args(); + for (int a = 1; a < lol.size()-4; a++) { + arguments.emplace_back(lol[a].c_str()); + } + } + clang::TranslationUnit tu(true, filepath, arguments); + clang::SourceLocation start(&tu, filepath, start_offset); + clang::SourceLocation end(&tu, filepath, end_offset); + clang::SourceRange range(&start, &end); + clang::Tokens tokens(&tu, &range); + std::vector tks = tokens.tokens(); + + for (auto &t : tks) { + clang::SourceLocation loc = t.get_source_location(&tu); + unsigned line; + unsigned column; + loc.get_location_info(NULL, &line, &column, NULL); + } + + // std::cout << t.elapsed().user << std::endl; + // model().SetSourceLocations(tu.getSourceLocations()); + // view().OnOpenFile(model().getSourceLocations(), model().theme()); } -Glib::RefPtr Source::Controller::buffer(){ +Glib::RefPtr Source::Controller::buffer() { return view().get_buffer(); } diff --git a/juci/source.h b/juci/source.h index af94a1c..9b5406d 100644 --- a/juci/source.h +++ b/juci/source.h @@ -5,7 +5,7 @@ #include #include #include "gtkmm.h" -#include +#include "clangmm.h" using std::string; @@ -25,14 +25,34 @@ namespace Source { private: std::unordered_map tagtable_; std::unordered_map typetable_; + string background_; }; // class Config + /* + class BufferLocation { + BufferLocation(const BufferLocation &location); + BufferLocation(int, int); + int line_number() { return line_number_; } + int column_number() { return column_offset_; } + private: + int line_number_; + int column_offset_; + }; + + class BufferRange { + BufferRange(const BufferLocation &start, const BufferLocation &end) : + start_(start), end_(end) { } + private: + BufferLocation start_; + BufferLocation end_; + };*/ + class View : public Gtk::TextView { public: View(); string UpdateLine(); void ApplyConfig(const Config &config); - void OnOpenFile(std::vector &locations, + void OnOpenFile(std::vector &locations, const Config &config); private: string GetLine(const Gtk::TextIter &begin); @@ -45,16 +65,15 @@ namespace Source { Config& config(); const string filepath(); void SetFilePath(const string &filepath); - void SetSourceLocations( - const std::vector &locations); - std::vector& getSourceLocations() { + void SetSourceLocations( const std::vector &locations); + std::vector& getSourceLocations() { return locations_; } private: Source::Config config_; string filepath_; - std::vector locations_; + std::vector locations_; }; class Controller {