From 03461615de5299cc2b97c6e84802325f678f27b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Wed, 18 Mar 2015 15:27:48 +0100 Subject: [PATCH 1/7] Fix imports --- juci/CMakeLists.txt | 49 +++++++---- juci/cmake/Modules/FindLibClang.cmake | 82 +++++++++++++++++++ ...FindTestlcl.cmake => FindLibClangmm.cmake} | 12 +-- 3 files changed, 118 insertions(+), 25 deletions(-) create mode 100644 juci/cmake/Modules/FindLibClang.cmake rename juci/cmake/Modules/{FindTestlcl.cmake => FindLibClangmm.cmake} (64%) diff --git a/juci/CMakeLists.txt b/juci/CMakeLists.txt index ec9f2ac..a6b3ab4 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 @@ -73,16 +86,22 @@ endif() add_executable(${project_name} #list of every needed file to create the executable juci.cc - keybindings - menu - source + keybindings.h + keybindings.cc + menu.h + menu.cc + source.h + source.cc sourcefile.h sourcefile.cc - window - api - notebook - entry - + window.cc + window.h + api.h + api.cc + notebook.cc + notebook.h + entry.h + entry.cc #there is no need for extentions ) @@ -92,29 +111,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/") 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} ) From 4d215386fffcdc3286f568ea19e3d187cf5f6827 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Fri, 20 Mar 2015 15:43:03 +0100 Subject: [PATCH 2/7] Fix namespace and add proof of concept --- juci/source.cc | 49 +++++++++++++++++++++++++++++++++---------------- juci/source.h | 10 +++++----- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/juci/source.cc b/juci/source.cc index d2634b3..a6a4270 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -15,7 +15,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 +37,9 @@ void Source::View::ApplyTheme(const Source::Theme &theme) { } } -void Source::View::OnOpenFile(std::vector &locations, +void Source::View::OnOpenFile(std::vector &locations, const Source::Theme &theme) { - ApplyTheme(theme); + /* ApplyTheme(theme); Glib::RefPtr buffer = get_buffer(); for (auto &loc : locations) { string type = std::to_string(loc.kind()); @@ -69,7 +69,7 @@ void Source::View::OnOpenFile(std::vector &locations, buffer->apply_tag_by_name(theme.typetable().at(type), begin_iter, end_iter); } - } + }*/ } // Source::View::Theme::tagtable() // returns a const refrence to the tagtable @@ -108,7 +108,8 @@ void Source::Theme::SetTagTable( //// Model //// /////////////// Source::Model::Model() : - theme_() {/* + theme_() { + /* std::cout << "Model constructor run" << std::endl; boost::property_tree::ptree pt; boost::property_tree::json_parser::read_json("config.json", pt); @@ -140,7 +141,7 @@ void Source::Model::SetFilePath(const string &filepath) { } void Source::Model:: -SetSourceLocations(const std::vector &locations) { +SetSourceLocations(const std::vector &locations) { locations_ = locations; } //////////////////// @@ -150,7 +151,7 @@ SetSourceLocations(const std::vector &locations) { // Source::Controller::Controller() // Constructor for Controller Source::Controller::Controller() { - //std::cout << "Controller constructor run" << std::endl; + // std::cout << "Controller constructor run" << std::endl; view().get_buffer()->signal_changed().connect([this](){ this->OnLineEdit(); }); @@ -178,16 +179,32 @@ 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().theme()); +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(); + + clang::TranslationUnit tu(true, filepath); + 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 << "Token line: " << line; + std::cout << ", column: " << column; + std::cout << ", kind: " << t.kind() << 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 13b59ba..e5ba70d 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; @@ -30,7 +30,7 @@ namespace Source { View(); string UpdateLine(); void ApplyTheme(const Theme &theme); - void OnOpenFile(std::vector &locations, + void OnOpenFile(std::vector &locations, const Theme &theme); private: string GetLine(const Gtk::TextIter &begin); @@ -43,15 +43,15 @@ namespace Source { const string filepath(); void SetFilePath(const string &filepath); void SetSourceLocations( - const std::vector &locations); - std::vector& getSourceLocations() { + const std::vector &locations); + std::vector& getSourceLocations() { return locations_; } private: Theme theme_; string filepath_; - std::vector locations_; + std::vector locations_; }; class Controller { From e4a8c50bb54fb0f5dff52e27e09b75da1baa54ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Fri, 20 Mar 2015 15:44:14 +0100 Subject: [PATCH 3/7] Edit out python module from cmake --- juci/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/juci/CMakeLists.txt b/juci/CMakeLists.txt index a6b3ab4..ea09d3c 100644 --- a/juci/CMakeLists.txt +++ b/juci/CMakeLists.txt @@ -126,8 +126,8 @@ link_directories( ${LIBCLANG_LIBRARY_DIRS} ) #module: -set_target_properties(${module} PROPERTIES PREFIX "" - LIBRARY_OUTPUT_DIRECTORY "/usr/lib/python2.7/dist-packages/") +#set_target_properties(${module} PROPERTIES PREFIX "" +# LIBRARY_OUTPUT_DIRECTORY "/usr/lib/python2.7/dist-packages/") target_link_libraries(${module} ${PYTHON_LIBRARIES} ${Boost_LIBRARIES}) #executable: target_link_libraries(${project_name} ${LIVCLANG_LIBRARIES} ${LCL_LIBRARIES} ${GTKMM_LIBRARIES} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES}) From 6c6458209099ae56b7cbd24b54e79fc0d5693573 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Sun, 22 Mar 2015 22:17:19 +0100 Subject: [PATCH 4/7] BAB-38 working tokens --- juci/source.cc | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/juci/source.cc b/juci/source.cc index a6a4270..ed2e263 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -185,7 +185,20 @@ void Source::Controller::OnOpenFile(const string &filepath) { int start_offset = buffer()->begin().get_offset(); int end_offset = buffer()->end().get_offset(); - clang::TranslationUnit tu(true, filepath); + 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); @@ -197,10 +210,8 @@ void Source::Controller::OnOpenFile(const string &filepath) { unsigned line; unsigned column; loc.get_location_info(NULL, &line, &column, NULL); - std::cout << "Token line: " << line; - std::cout << ", column: " << column; - std::cout << ", kind: " << t.kind() << std::endl; } + // model().SetSourceLocations(tu.getSourceLocations()); // view().OnOpenFile(model().getSourceLocations(), model().theme()); } From 406959004864e6410fb476db2b4b26d908ba2d5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Mon, 23 Mar 2015 10:06:37 +0100 Subject: [PATCH 5/7] Add timer --- juci/CMakeLists.txt | 2 +- juci/source.cc | 9 +++++++++ juci/source.h | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/juci/CMakeLists.txt b/juci/CMakeLists.txt index ea09d3c..5995127 100644 --- a/juci/CMakeLists.txt +++ b/juci/CMakeLists.txt @@ -52,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}) diff --git a/juci/source.cc b/juci/source.cc index ed2e263..b315dc0 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -3,6 +3,7 @@ #include "sourcefile.h" #include #include +#include ////////////// //// View //// @@ -185,9 +186,12 @@ void Source::Controller::OnOpenFile(const string &filepath) { 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(); @@ -198,7 +202,10 @@ void Source::Controller::OnOpenFile(const string &filepath) { arguments.emplace_back(lol[a].c_str()); } } + boost::timer::auto_cpu_timer timer; clang::TranslationUnit tu(true, filepath, arguments); + timer.~auto_cpu_timer(); + boost::timer::auto_cpu_timer timer2; clang::SourceLocation start(&tu, filepath, start_offset); clang::SourceLocation end(&tu, filepath, end_offset); clang::SourceRange range(&start, &end); @@ -212,6 +219,8 @@ void Source::Controller::OnOpenFile(const string &filepath) { loc.get_location_info(NULL, &line, &column, NULL); } + timer2.~auto_cpu_timer(); + // std::cout << t.elapsed().user << std::endl; // model().SetSourceLocations(tu.getSourceLocations()); // view().OnOpenFile(model().getSourceLocations(), model().theme()); } diff --git a/juci/source.h b/juci/source.h index e5ba70d..726f9a3 100644 --- a/juci/source.h +++ b/juci/source.h @@ -24,6 +24,24 @@ namespace Source { std::unordered_map typetable_; string background_; }; // class Theme + /* + 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: From 3313af46a5cd253cebabe56af4ed661c8aab3f01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Mon, 23 Mar 2015 11:01:04 +0100 Subject: [PATCH 6/7] add trans --- juci/CMakeLists.txt | 4 ++-- juci/source.cc | 9 +++------ juci/source.h | 6 +++--- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/juci/CMakeLists.txt b/juci/CMakeLists.txt index bffb5f8..d4f326d 100644 --- a/juci/CMakeLists.txt +++ b/juci/CMakeLists.txt @@ -127,8 +127,8 @@ link_directories( ${LIBCLANG_LIBRARY_DIRS} ) #module: -#set_target_properties(${module} PROPERTIES PREFIX "" -# LIBRARY_OUTPUT_DIRECTORY "/usr/lib/python2.7/dist-packages/") +set_target_properties(${module} PROPERTIES PREFIX "" + LIBRARY_OUTPUT_DIRECTORY "/usr/lib/python2.7/dist-packages/") target_link_libraries(${module} ${PYTHON_LIBRARIES} ${Boost_LIBRARIES}) #executable: target_link_libraries(${project_name} ${LIVCLANG_LIBRARIES} ${LCL_LIBRARIES} ${GTKMM_LIBRARIES} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES}) diff --git a/juci/source.cc b/juci/source.cc index 670a647..95307e2 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -38,9 +38,9 @@ void Source::View::ApplyConfig(const Source::Config &config) { } } -void Source::View::OnOpenFile(std::vector &locations, +void Source::View::OnOpenFile(std::vector &locations, const Source::Config &config) { - ApplyConfig(config); + /* ApplyConfig(config); Glib::RefPtr buffer = get_buffer(); for (auto &loc : locations) { string type = std::to_string(loc.kind()); @@ -71,6 +71,7 @@ void Source::View::OnOpenFile(std::vector &locations, begin_iter, end_iter); } } + */ } @@ -194,10 +195,7 @@ void Source::Controller::OnOpenFile(const string &filepath) { arguments.emplace_back(lol[a].c_str()); } } - boost::timer::auto_cpu_timer timer; clang::TranslationUnit tu(true, filepath, arguments); - timer.~auto_cpu_timer(); - boost::timer::auto_cpu_timer timer2; clang::SourceLocation start(&tu, filepath, start_offset); clang::SourceLocation end(&tu, filepath, end_offset); clang::SourceRange range(&start, &end); @@ -211,7 +209,6 @@ void Source::Controller::OnOpenFile(const string &filepath) { loc.get_location_info(NULL, &line, &column, NULL); } - timer2.~auto_cpu_timer(); // std::cout << t.elapsed().user << std::endl; // model().SetSourceLocations(tu.getSourceLocations()); // view().OnOpenFile(model().getSourceLocations(), model().theme()); diff --git a/juci/source.h b/juci/source.h index 2457556..9b5406d 100644 --- a/juci/source.h +++ b/juci/source.h @@ -52,7 +52,7 @@ namespace Source { 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); @@ -65,8 +65,8 @@ 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_; } From 64508d0ee4b4aa369c15131961db8291628a2755 Mon Sep 17 00:00:00 2001 From: oyvang Date: Tue, 24 Mar 2015 14:39:18 +0100 Subject: [PATCH 7/7] Fully working linenumbers --- juci/api.cc | 1 - juci/juci.cc | 2 +- juci/notebook.cc | 99 ++++++++++++++++++++---------------------------- juci/notebook.h | 4 ++ 4 files changed, 46 insertions(+), 60 deletions(-) diff --git a/juci/api.cc b/juci/api.cc index 995d344..83745cc 100644 --- a/juci/api.cc +++ b/juci/api.cc @@ -1,4 +1,3 @@ - #include "api.h" Menu::Controller* PluginApi::menu_; 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 bdc0c30..63e3759 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -1,10 +1,12 @@ - +#include #include "notebook.h" + + Notebook::Model::Model() { cc_extension_ = ".cc"; h_extension_ = ".h"; - scrollvalue_ = 20; + scrollvalue_ = 50; }; Notebook::View::View(){ view_.pack_start(notebook_); @@ -94,13 +96,6 @@ Notebook::Controller::Controller(Keybindings::Controller& keybindings) { [this]() { Search(false); }); - - text_vec_.back()->view(). - signal_scroll_event().connect(sigc::mem_fun( - this, - &Notebook::Controller:: - scroll_event_callback)); - }//Constructor @@ -116,19 +111,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; @@ -144,9 +134,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); @@ -163,21 +153,19 @@ void Notebook::Controller::OnOpenFile(std::string path) { Notebook().show_all_children(); Notebook().set_current_page(Pages()-1); Notebook().set_focus_child(text_vec_.back()->view()); + lines = Buffer(linenumbers_vec_.back())->get_text(); OnBufferChange(); } void Notebook::Controller::OnCreatePage(){ text_vec_.push_back(new Source::Controller); - std::string temp = "TEXT "; - temp += text_vec_.size(); - text_vec_.back()->view().set_name(temp); linenumbers_vec_.push_back(new Source::Controller); scrolledline_vec_.push_back(new Gtk::ScrolledWindow()); scrolledtext_vec_.push_back(new Gtk::ScrolledWindow()); 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); @@ -186,6 +174,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()); } @@ -267,7 +256,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_. @@ -288,42 +277,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; + } } @@ -350,7 +332,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 ccfdbb6..e95e0a0 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -65,10 +65,14 @@ namespace Notebook { Gtk::TextIter search_match_end_; Gtk::TextIter search_match_start_; Glib::RefPtr refClipboard_; + std::string + lines="1 "; }; // class controller } // namespace Notebook + + #endif // JUCI_NOTEBOOK_H_