Browse Source

Merge branch 'master' of bitbucket.org:cppit/juci

master
oyvang 11 years ago
parent
commit
9980889d1e
  1. 2
      juci/CMakeLists.txt
  2. 9
      juci/config.cc
  3. 19
      juci/directories.cc
  4. 9
      juci/directories.h
  5. 17
      juci/notebook.cc
  6. 8
      juci/notebook.h
  7. 320
      juci/source.cc
  8. 99
      juci/source.h

2
juci/CMakeLists.txt

@ -3,7 +3,7 @@ set(project_name juci)
set(module juci_to_python_api) set(module juci_to_python_api)
project (${project_name}) project (${project_name})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_LIST_DIR}/cmake/Modules/") set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_LIST_DIR}/cmake/Modules/")
INCLUDE(FindPkgConfig) INCLUDE(FindPkgConfig)

9
juci/config.cc

@ -15,11 +15,14 @@ void MainConfig::GenerateSource() {
boost::property_tree::ptree source_json = cfg_.get_child("source"); boost::property_tree::ptree source_json = cfg_.get_child("source");
boost::property_tree::ptree syntax_json = source_json.get_child("syntax"); boost::property_tree::ptree syntax_json = source_json.get_child("syntax");
boost::property_tree::ptree colors_json = source_json.get_child("colors"); boost::property_tree::ptree colors_json = source_json.get_child("colors");
for ( auto &i : syntax_json ) for ( auto &i : colors_json ) {
source_cfg_.InsertTag(i.first, i.second.get_value<std::string>()); source_cfg_.InsertTag(i.first, i.second.get_value<std::string>());
std::cout << "inserting tag, key: " << i.first << " value: " << i.second.get_value<std::string>() << std::endl;
for ( auto &i : colors_json ) }
for ( auto &i : syntax_json ) {
source_cfg_.InsertType(i.first, i.second.get_value<std::string>()); source_cfg_.InsertType(i.first, i.second.get_value<std::string>());
std::cout << "inserting type, key: " << i.first << " value: " << i.second.get_value<std::string>() << std::endl;
}
} }
void MainConfig::GenerateKeybindings() { void MainConfig::GenerateKeybindings() {

19
juci/directories.cc

@ -6,7 +6,7 @@ Directories::Controller::Controller(Directories::Config& cfg) :
m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
} }
bool Directories::Controller:: void Directories::Controller::
open_folder(const boost::filesystem::path& dir_path) { open_folder(const boost::filesystem::path& dir_path) {
m_refTreeModel = Gtk::TreeStore::create(view()); m_refTreeModel = Gtk::TreeStore::create(view());
m_TreeView.set_model(m_refTreeModel); m_TreeView.set_model(m_refTreeModel);
@ -95,20 +95,31 @@ get_project_name(const boost::filesystem::path& dir_path) {
size_t variabel_end = line.find("}", variabel_start); size_t variabel_end = line.find("}", variabel_start);
project_name_var = line.substr(variabel_start+1, project_name_var = line.substr(variabel_start+1,
(variabel_end)-variabel_start-1); (variabel_end)-variabel_start-1);
boost::algorithm::trim(project_name_var);
if (variabel_start == std::string::npos) { // not a variabel
variabel_start = line.find("(", 0);
variabel_end = line.find(")", variabel_start);
return line.substr(variabel_start+1,
(variabel_end)-variabel_start-1);
}
break; break;
} }
} }
std::ifstream ifs2(itr->path().string()); std::ifstream ifs2(itr->path().string());
while (std::getline(ifs2, line)) { while (std::getline(ifs2, line)) {
if (line.find("set("+project_name_var, 0) != std::string::npos) { if (line.find("set(", 0) != std::string::npos
|| line.find("set (", 0) != std::string::npos) {
if( line.find(project_name_var, 0) != std::string::npos) {
size_t variabel_start = line.find(project_name_var, 0) size_t variabel_start = line.find(project_name_var, 0)
+project_name_var.length(); +project_name_var.length();
size_t variabel_end = line.find(")", variabel_start); size_t variabel_end = line.find(")", variabel_start);
project_name = line.substr(variabel_start, project_name = line.substr(variabel_start+1,
variabel_end-variabel_start); variabel_end-variabel_start-1);
boost::algorithm::trim(project_name);
return project_name; return project_name;
} }
} }
}
break; break;
} }
} }

9
juci/directories.h

@ -4,12 +4,12 @@
#include <gtkmm.h> #include <gtkmm.h>
#include <glib.h> #include <glib.h>
#include "boost/filesystem.hpp" #include "boost/filesystem.hpp"
#include "boost/algorithm/string.hpp"
#include <utility> #include <utility>
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
namespace Directories { namespace Directories {
class Config { class Config {
@ -38,8 +38,7 @@ namespace Directories {
Gtk::TreeModelColumn<Glib::ustring> m_col_path; Gtk::TreeModelColumn<Glib::ustring> m_col_path;
}; };
class Model { class Model { };
};
class Controller { class Controller {
public: public:
@ -49,7 +48,7 @@ namespace Directories {
Model& model() { return model_;} Model& model() { return model_;}
Directories::Config& config() { return config_;} Directories::Config& config() { return config_;}
Gtk::ScrolledWindow& widget() {return m_ScrolledWindow;} Gtk::ScrolledWindow& widget() {return m_ScrolledWindow;}
bool open_folder (const boost::filesystem::path& dir_path); void open_folder(const boost::filesystem::path& dir_path);
void list_dirs(const boost::filesystem::path& dir_path, void list_dirs(const boost::filesystem::path& dir_path,
Gtk::TreeModel::Row &row, unsigned depth); Gtk::TreeModel::Row &row, unsigned depth);
std::string get_project_name(const boost::filesystem::path& dir_path); std::string get_project_name(const boost::filesystem::path& dir_path);
@ -61,10 +60,12 @@ namespace Directories {
Gtk::TreeView m_TreeView; Gtk::TreeView m_TreeView;
Glib::RefPtr<Gtk::TreeStore> m_refTreeModel; Glib::RefPtr<Gtk::TreeStore> m_refTreeModel;
bool IsIgnored(std::string path); bool IsIgnored(std::string path);
private: private:
View view_; View view_;
Model model_; Model model_;
Directories::Config config_; Directories::Config config_;
protected: protected:
void on_treeview_row_activated(const Gtk::TreeModel::Path& path, void on_treeview_row_activated(const Gtk::TreeModel::Path& path,
Gtk::TreeViewColumn* column); Gtk::TreeViewColumn* column);

17
juci/notebook.cc

@ -4,7 +4,7 @@ Notebook::Model::Model() {
cc_extension_ = ".cc"; cc_extension_ = ".cc";
h_extension_ = ".h"; h_extension_ = ".h";
scrollvalue_ = 50; scrollvalue_ = 50;
}; }
Notebook::View::View() { Notebook::View::View() {
view_.pack2(notebook_); view_.pack2(notebook_);
@ -20,7 +20,6 @@ Notebook::Controller::Controller(Keybindings::Controller& keybindings,
refClipboard_ = Gtk::Clipboard::get(); refClipboard_ = Gtk::Clipboard::get();
view().pack1(directories_.widget(), true, true); view().pack1(directories_.widget(), true, true);
CreateKeybindings(keybindings); CreateKeybindings(keybindings);
} // Constructor } // Constructor
void Notebook::Controller::CreateKeybindings(Keybindings::Controller void Notebook::Controller::CreateKeybindings(Keybindings::Controller
@ -74,7 +73,7 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller
[this]() { [this]() {
is_new_file_ = false; is_new_file_ = false;
OnEditSearch(); OnEditSearch();
//TODO(Oyvang, Zalox, Forgi)Create function OnEditFind(); // TODO(Oyvang) Zalox, Forgi)Create function OnEditFind();
}); });
keybindings.action_group_menu()-> keybindings.action_group_menu()->
add(Gtk::Action::create("EditCopy", add(Gtk::Action::create("EditCopy",
@ -256,7 +255,8 @@ void Notebook::Controller::OnCreatePage(){
} }
void Notebook::Controller::OnCloseCurrentPage() { void Notebook::Controller::OnCloseCurrentPage() {
//TODO (oyvang, zalox, forgi) Save a temp file, in case you close one you dont want to close? // TODO(oyvang) zalox, forgi)
// Save a temp file, in case you close one you dont want to close?
if (Pages() != 0) { if (Pages() != 0) {
int page = CurrentPage(); int page = CurrentPage();
Notebook().remove_page(page); Notebook().remove_page(page);
@ -365,7 +365,8 @@ void Notebook::Controller::OnBufferChange() {
"\n"+std::to_string(line_nr)+" "); "\n"+std::to_string(line_nr)+" ");
} }
while (line_nr > text_nr) { while (line_nr > text_nr) {
Gtk::TextIter iter = Buffer(linenumbers_vec_.at(page))->get_iter_at_line(line_nr); Gtk::TextIter iter =
Buffer(linenumbers_vec_.at(page))->get_iter_at_line(line_nr);
iter.backward_char(); iter.backward_char();
line_nr--; line_nr--;
Buffer(linenumbers_vec_.at(page))-> Buffer(linenumbers_vec_.at(page))->
@ -375,7 +376,6 @@ void Notebook::Controller::OnBufferChange() {
if (Buffer(text_vec_.at(page))->get_insert()->get_iter().starts_line() && 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))->get_insert()->get_iter().get_line() ==
Buffer(text_vec_.at(page))->end().get_line()) { Buffer(text_vec_.at(page))->end().get_line()) {
GdkEventScroll* scroll = new GdkEventScroll; GdkEventScroll* scroll = new GdkEventScroll;
scroll->delta_y = 1.0; scroll->delta_y = 1.0;
scroll->delta_x = 0.0; scroll->delta_x = 0.0;
@ -389,7 +389,7 @@ void Notebook::Controller::OnBufferChange() {
start.backward_char(); start.backward_char();
word = Buffer(text_vec_.at(page))->get_text(start, end); word = Buffer(text_vec_.at(page))->get_text(start, end);
if (word == ".") { if (word == ".") {
//TODO(Oyvang,Zalox,Forgie) Remove TEST // TODO(Forgie) Zalox,Forgie) Remove TEST
std::vector<std::string> TEST; std::vector<std::string> TEST;
TEST.push_back("toString()"); TEST.push_back("toString()");
TEST.push_back("toLower()"); TEST.push_back("toLower()");
@ -405,7 +405,8 @@ void Notebook::Controller
Gtk::TreeModel::iterator iter = directories().m_refTreeModel->get_iter(path); Gtk::TreeModel::iterator iter = directories().m_refTreeModel->get_iter(path);
if (iter) { if (iter) {
Gtk::TreeModel::Row row = *iter; Gtk::TreeModel::Row row = *iter;
boost::filesystem::path fs_path(Glib::ustring(row[directories().view().m_col_path])); std::string upath = Glib::ustring(row[directories().view().m_col_path]);
boost::filesystem::path fs_path(upath);
if (boost::filesystem::is_directory(fs_path)) { if (boost::filesystem::is_directory(fs_path)) {
directories().m_TreeView.row_expanded(path) ? directories().m_TreeView.row_expanded(path) ?
directories().m_TreeView.collapse_row(path) : directories().m_TreeView.collapse_row(path) :

8
juci/notebook.h

@ -5,15 +5,11 @@
#include "gtkmm.h" #include "gtkmm.h"
#include "entry.h" #include "entry.h"
#include "source.h" #include "source.h"
#include "directories.h" #include "directories.h"
#include <boost/algorithm/string/case_conv.hpp> #include <boost/algorithm/string/case_conv.hpp>
#include <type_traits> #include <type_traits>
#include <sigc++/sigc++.h> #include <sigc++/sigc++.h>
namespace Notebook { namespace Notebook {
class Model { class Model {
public: public:
@ -33,7 +29,6 @@ namespace Notebook {
}; };
class Controller { class Controller {
public: public:
Controller(Keybindings::Controller& keybindings, Controller(Keybindings::Controller& keybindings,
Source::Config& config, Source::Config& config,
Directories::Config& dir_cfg); Directories::Config& dir_cfg);
@ -70,8 +65,10 @@ namespace Notebook {
void Search(bool forward); void Search(bool forward);
const Source::Config& source_config() { return source_config_; } const Source::Config& source_config() { return source_config_; }
protected: protected:
void BufferChangeHandler(Glib::RefPtr<Gtk::TextBuffer> buffer); void BufferChangeHandler(Glib::RefPtr<Gtk::TextBuffer> buffer);
private: private:
void CreateKeybindings(Keybindings::Controller& keybindings); void CreateKeybindings(Keybindings::Controller& keybindings);
Glib::RefPtr<Gtk::Builder> m_refBuilder; Glib::RefPtr<Gtk::Builder> m_refBuilder;
@ -91,5 +88,4 @@ namespace Notebook {
Glib::RefPtr<Gtk::Clipboard> refClipboard_; Glib::RefPtr<Gtk::Clipboard> refClipboard_;
}; // class controller }; // class controller
} // namespace Notebook } // namespace Notebook
#endif // JUCI_NOTEBOOK_H_ #endif // JUCI_NOTEBOOK_H_

320
juci/source.cc

@ -5,21 +5,33 @@
#include <fstream> #include <fstream>
#include <boost/timer/timer.hpp> #include <boost/timer/timer.hpp>
#define log( var ) \
std::cout << "source.cc (" << __LINE__ << ") " << #var << std::endl
Source::Location::
Location(int line_number, int column_offset) :
line_number_(line_number), column_offset_(column_offset) { }
Source::Location::
Location(const Source::Location &org) :
line_number_(org.line_number_), column_offset_(org.column_offset_) { }
Source::Range::
Range(const Location &start, const Location &end, int kind) :
start_(start), end_(end), kind_(kind) { }
Source::Range::
Range(const Source::Range &org) :
start_(org.start_), end_(org.end_), kind_(org.kind_) { }
////////////// //////////////
//// View //// //// View ////
////////////// //////////////
Source::View::View() { Source::View::View() {
// std::cout << "View constructor run" << std::endl;
override_font(Pango::FontDescription("Monospace")); override_font(Pango::FontDescription("Monospace"));
} }
// Source::View::UpdateLine
// returns the new line
string Source::View::UpdateLine() {
Gtk::TextIter line(get_buffer()->get_insert()->get_iter());
// std::cout << line.get_line() << std::endl;
// for each word --> check what it is --> apply appropriate tag
return "";
}
string Source::View::GetLine(const Gtk::TextIter &begin) { string Source::View::GetLine(const Gtk::TextIter &begin) {
Gtk::TextIter end(begin); Gtk::TextIter end(begin);
@ -32,47 +44,11 @@ string Source::View::GetLine(const Gtk::TextIter &begin) {
// Applies theme in textview // Applies theme in textview
void Source::View::ApplyConfig(const Source::Config &config) { void Source::View::ApplyConfig(const Source::Config &config) {
for (auto &item : config.tagtable()) { for (auto &item : config.tagtable()) {
// std::cout << "Apply: f: " << item.first << ", s: " <<
// item.second << std::endl;
get_buffer()->create_tag(item.first)->property_foreground() = item.second; get_buffer()->create_tag(item.first)->property_foreground() = item.second;
} }
} }
void Source::View::OnOpenFile(std::vector<clang::SourceLocation> &locations,
const Source::Config &config) {
/* ApplyConfig(config);
Glib::RefPtr<Gtk::TextBuffer> buffer = get_buffer();
for (auto &loc : locations) {
string type = std::to_string(loc.kind());
try {
config.typetable().at(type);
} catch (std::exception) {
continue;
}
int linum_start = loc.line_number_begin();
int linum_end = loc.line_number_end();
int begin = loc.begin();
int end = loc.end();
if (end < 0) end = 0;
if (begin < 0) begin = 0;
// std::cout << "Libc: ";
// std::cout << "type: " << type;
// std::cout << " linum_s: " << linum_start+1 << " linum_e: " << linum_end+1;
// std::cout << ", begin: " << begin << ", end: " << end << std::endl;
Gtk::TextIter begin_iter = buffer->get_iter_at_line_offset(linum_start,
begin);
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);
}
}
*/
}
// Source::View::Config::Config(Config &config) // Source::View::Config::Config(Config &config)
@ -96,14 +72,13 @@ const std::unordered_map<string, string>& Source::Config::typetable() const {
return typetable_; return typetable_;
} }
void Source::Config::InsertTag(const string &key, const string &value) { void Source::Config::InsertTag(const string &key, const string &value) {
tagtable_[key] = value; tagtable_[key] = value;
} }
// Source::View::Config::SetTagTable() // Source::View::Config::SetTagTable()
// sets the tagtable for the view // sets the tagtable for the view
void Source::Config::SetTypeTable( void Source::Config::
const std::unordered_map<string, string> &typetable) { SetTypeTable(const std::unordered_map<string, string> &typetable) {
typetable_ = typetable; typetable_ = typetable;
} }
@ -112,8 +87,8 @@ void Source::Config::InsertType(const string &key, const string &value) {
} }
// Source::View::Config::SetTagTable() // Source::View::Config::SetTagTable()
// sets the tagtable for the view // sets the tagtable for the view
void Source::Config::SetTagTable( void Source::Config::
const std::unordered_map<string, string> &tagtable) { SetTagTable(const std::unordered_map<string, string> &tagtable) {
tagtable_ = tagtable; tagtable_ = tagtable;
} }
@ -124,22 +99,129 @@ Source::Model::Model(const Source::Config &config) :
config_(config) { config_(config) {
} }
Source::Config& Source::Model::config() { void Source::Model::
InitSyntaxHighlighting(const std::string &filepath,
const std::string &project_path,
const std::string &text,
int start_offset,
int end_offset) {
set_file_path(filepath);
set_project_path(project_path);
std::vector<const char*> arguments = get_compilation_commands();
tu_ = clang::TranslationUnit(true,
filepath,
arguments,
text);
}
// Source::View::UpdateLine
void Source::View::
OnLineEdit(const std::vector<Source::Range> &locations,
const Source::Config &config) {
OnUpdateSyntax(locations, config);
}
// Source::Model::UpdateLine
int Source::Model::
ReParse(const std::string &buffer) {
return tu_.ReparseTranslationUnit(file_path(), buffer);
}
// Source::Controller::OnLineEdit()
// fired when a line in the buffer is edited
void Source::Controller::OnLineEdit() { }
// sets the filepath for this mvc
void Source::Model::
set_file_path(const std::string &file_path) {
file_path_ = file_path;
}
// sets the project path for this mvc
void Source::Model::
set_project_path(const std::string &project_path) {
project_path_ = project_path;
}
// gets the file_path member
const std::string& Source::Model::file_path() const {
return file_path_;
}
// gets the project_path member
const std::string& Source::Model::project_path() const {
return project_path_;
}
// gets the config member
const Source::Config& Source::Model::config() const {
return config_; return config_;
} }
const string Source::Model::filepath() { std::vector<const char*> Source::Model::
return filepath_; get_compilation_commands() {
clang::CompilationDatabase db(project_path()+"/");
clang::CompileCommands commands(file_path(), &db);
std::vector<clang::CompileCommand> cmds = commands.get_commands();
std::vector<const char*> arguments;
for (auto &i : cmds) {
std::vector<std::string> lol = i.get_command_as_args();
for (int a = 1; a < lol.size()-4; a++) {
arguments.emplace_back(lol[a].c_str());
}
}
return arguments;
} }
void Source::Model::SetFilePath(const string &filepath) { std::vector<Source::Range> Source::Model::
filepath_ = filepath; ExtractTokens(int start_offset, int end_offset) {
std::vector<Source::Range> ranges;
clang::SourceLocation start(&tu_, file_path(), start_offset);
clang::SourceLocation end(&tu_, file_path(), end_offset);
clang::SourceRange range(&start, &end);
clang::Tokens tokens(&tu_, &range);
std::vector<clang::Token> tks = tokens.tokens();
for (auto &token : tks) {
switch (token.kind()) {
case 0: HighlightCursor(&token, &ranges); break; // PunctuationToken
case 1: HighlightToken(&token, &ranges, 702); break; // KeywordToken
case 2: HighlightCursor(&token, &ranges); break; // IdentifierToken
case 3: HighlightToken(&token, &ranges, 109); break; // LiteralToken
case 4: HighlightToken(&token, &ranges, 705); break; // CommentToken
}
}
return ranges;
} }
void Source::Model:: void Source::Model::
SetSourceLocations(const std::vector<clang::SourceLocation> &locations) { HighlightCursor(clang::Token *token,
locations_ = locations; std::vector<Source::Range> *source_ranges) {
clang::SourceLocation location = token->get_source_location(&tu_);
clang::Cursor cursor(&tu_, &location);
clang::SourceRange range(&cursor);
clang::SourceLocation begin(&range, true);
clang::SourceLocation end(&range, false);
unsigned begin_line_num, begin_offset, end_line_num, end_offset;
begin.get_location_info(NULL, &begin_line_num, &begin_offset, NULL);
end.get_location_info(NULL, &end_line_num, &end_offset, NULL);
source_ranges->emplace_back(Source::Location(begin_line_num,
begin_offset),
Source::Location(end_line_num,
end_offset), (int) cursor.kind());
} }
void Source::Model::
HighlightToken(clang::Token *token,
std::vector<Source::Range> *source_ranges,
int token_kind) {
clang::SourceRange range = token->get_source_range(&tu_);
unsigned begin_line_num, begin_offset, end_line_num, end_offset;
clang::SourceLocation begin(&range, true);
clang::SourceLocation end(&range, false);
begin.get_location_info(NULL, &begin_line_num, &begin_offset, NULL);
end.get_location_info(NULL, &end_line_num, &end_offset, NULL);
source_ranges->emplace_back(Source::Location(begin_line_num,
begin_offset),
Source::Location(end_line_num,
end_offset), token_kind);
}
//////////////////// ////////////////////
//// Controller //// //// Controller ////
//////////////////// ////////////////////
@ -148,9 +230,6 @@ SetSourceLocations(const std::vector<clang::SourceLocation> &locations) {
// Constructor for Controller // Constructor for Controller
Source::Controller::Controller(const Source::Config &config) : Source::Controller::Controller(const Source::Config &config) :
model_(config) { model_(config) {
view().get_buffer()->signal_changed().connect([this](){
this->OnLineEdit();
});
} }
// Source::Controller::view() // Source::Controller::view()
@ -163,57 +242,114 @@ Source::View& Source::Controller::view() {
Source::Model& Source::Controller::model() { Source::Model& Source::Controller::model() {
return model_; return model_;
} }
// Source::Controller::OnLineEdit()
// fired when a line in the buffer is edited
void Source::Controller::OnLineEdit() {
view().UpdateLine();
}
void Source::Controller::OnNewEmptyFile() { void Source::Controller::OnNewEmptyFile() {
string filename("/tmp/juci_t"); string filename("/tmp/juci_t");
sourcefile s(filename); sourcefile s(filename);
model().SetFilePath(filename); model().set_file_path(filename);
model().set_project_path(filename);
s.save(""); s.save("");
} }
string extract_file_path(const std::string &file_path) {
return file_path.substr(0, file_path.find_last_of('/'));
}
std::vector<std::string> extentions() {
return {".h", ".cc", ".cpp", ".hpp"};
}
bool check_extention(const std::string &file_path) {
std::string extention = file_path.substr(file_path.find_last_of('.'),
file_path.size());
for (auto &ex : extentions()) {
if (extention == ex) {
return true;
}
}
return false;
}
void Source::View::OnUpdateSyntax(const std::vector<Source::Range> &ranges,
const Source::Config &config) {
if (ranges.empty() || ranges.size() == 0) {
return;
}
Glib::RefPtr<Gtk::TextBuffer> buffer = get_buffer();
buffer->remove_all_tags(buffer->begin(), buffer->end());
for (auto &range : ranges) {
string type = std::to_string(range.kind());
try {
config.typetable().at(type);
} catch (std::exception) {
continue;
}
int linum_start = range.start().line_number()-1;
int linum_end = range.end().line_number()-1;
int begin = range.start().column_offset()-1;
int end = range.end().column_offset()-1;
if (end < 0) end = 0;
if (begin < 0) begin = 0;
Gtk::TextIter begin_iter =
buffer->get_iter_at_line_offset(linum_start, begin);
Gtk::TextIter end_iter =
buffer->get_iter_at_line_offset(linum_end, end);
buffer->apply_tag_by_name(config.typetable().at(type),
begin_iter, end_iter);
}
}
void Source::Controller::OnOpenFile(const string &filepath) { void Source::Controller::OnOpenFile(const string &filepath) {
sourcefile s(filepath); sourcefile s(filepath);
buffer()->set_text(s.get_content()); buffer()->set_text(s.get_content());
int start_offset = buffer()->begin().get_offset(); int start_offset = buffer()->begin().get_offset();
int end_offset = buffer()->end().get_offset(); int end_offset = buffer()->end().get_offset();
std::string project_path = if (check_extention(filepath)) {
filepath.substr(0, filepath.find_last_of('/')); view().ApplyConfig(model().config());
model().InitSyntaxHighlighting(filepath,
extract_file_path(filepath),
buffer()->get_text().raw(),
start_offset,
end_offset);
view().OnUpdateSyntax(model().ExtractTokens(start_offset, end_offset),
model().config());
}
clang::CompilationDatabase db(project_path); buffer()->signal_end_user_action().connect([this]() {
clang::CompileCommands commands(filepath, &db); if (!go) {
std::vector<clang::CompileCommand> cmds = commands.get_commands(); std::thread parse([this]() {
std::vector<const char*> arguments; if (parsing.try_lock()) {
for (auto &i : cmds) { while (true) {
std::vector<std::string> lol = i.get_command_as_args(); const std::string raw = buffer()->get_text().raw();
for (int a = 1; a < lol.size()-4; a++) { if (model().ReParse(raw) == 0 &&
arguments.emplace_back(lol[a].c_str()); raw == buffer()->get_text().raw()) {
syntax.lock();
go = true;
syntax.unlock();
break;
} }
} }
clang::TranslationUnit tu(true, filepath, arguments); parsing.unlock();
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<clang::Token> 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; parse.detach();
// model().SetSourceLocations(tu.getSourceLocations());
// view().OnOpenFile(model().getSourceLocations(), model().theme());
} }
});
buffer()->signal_begin_user_action().connect([this]() {
if (go) {
syntax.lock();
view().
OnUpdateSyntax(model().ExtractTokens(0, buffer()->get_text().size()),
model().config());
go = false;
syntax.unlock();
}
});
}
Glib::RefPtr<Gtk::TextBuffer> Source::Controller::buffer() { Glib::RefPtr<Gtk::TextBuffer> Source::Controller::buffer() {
return view().get_buffer(); return view().get_buffer();
} }

99
juci/source.h

@ -1,11 +1,13 @@
#ifndef JUCI_SOURCE_H_ #ifndef JUCI_SOURCE_H_
#define JUCI_SOURCE_H_ #define JUCI_SOURCE_H_
#include <iostream>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include <string> #include <string>
#include "gtkmm.h" #include "gtkmm.h"
#include "clangmm.h" #include "clangmm.h"
#include <thread>
#include <mutex>
using std::string; using std::string;
@ -27,58 +29,93 @@ namespace Source {
std::unordered_map<string, string> typetable_; std::unordered_map<string, string> typetable_;
string background_; string background_;
}; // class Config }; // class Config
/*
class BufferLocation { class Location {
BufferLocation(const BufferLocation &location); public:
BufferLocation(int, int); Location(const Location &location);
int line_number() { return line_number_; } Location(int line_number, int column_offset);
int column_number() { return column_offset_; } int line_number() const { return line_number_; }
int column_offset() const { return column_offset_; }
private: private:
int line_number_; int line_number_;
int column_offset_; int column_offset_;
}; };
class BufferRange { class Range {
BufferRange(const BufferLocation &start, const BufferLocation &end) : public:
start_(start), end_(end) { } Range(const Location &start, const Location &end, int kind);
Range(const Range &org);
const Location& start() const { return start_; }
const Location& end() const { return end_; }
int kind() const { return kind_; }
void to_stream() const {
std::cout << "range: [" << start_.line_number()-1;
std::cout << ", " << end_.line_number()-1 << "] ";
std::cout << "<" << start_.column_offset()-1;
std::cout << ", " << end_.column_offset()-1 << ">";
std::cout << std::endl;
}
private: private:
BufferLocation start_; Location start_;
BufferLocation end_; Location end_;
};*/ int kind_;
};
class View : public Gtk::TextView { class View : public Gtk::TextView {
public: public:
View(); View();
string UpdateLine();
void ApplyConfig(const Config &config); void ApplyConfig(const Config &config);
void OnOpenFile(std::vector<clang::SourceLocation> &locations, void OnLineEdit(const std::vector<Range> &locations,
const Config &config); const Config &config);
void OnUpdateSyntax(const std::vector<Range> &locations,
const Config &config);
private: private:
string GetLine(const Gtk::TextIter &begin); string GetLine(const Gtk::TextIter &begin);
}; // class View }; // class View
class Model{ class Model{
public: public:
Model(const Source::Config &config); // constructor for Source::Model
//Model(); explicit Model(const Source::Config &config);
Config& config(); // inits the syntax highligthing on file open
const string filepath(); void InitSyntaxHighlighting(const std::string &filepath,
void SetFilePath(const string &filepath); const std::string &project_path,
void SetSourceLocations( const std::vector<clang::SourceLocation> &locations); const std::string &text,
std::vector<clang::SourceLocation>& getSourceLocations() { int start_offset,
return locations_; int end_offset);
} // sets the filepath for this mvc
void set_file_path(const string &file_path);
// sets the project path for this mvc
void set_project_path(const string &project_path);
// gets the file_path member
const string& file_path() const;
// gets the project_path member
const string& project_path() const;
// gets the config member
const Config& config() const;
~Model() { }
int ReParse(const std::string &buffer);
std::vector<Range> ExtractTokens(int, int);
private: private:
Source::Config config_; Config config_;
string filepath_; string file_path_;
std::vector<clang::SourceLocation> locations_; string project_path_;
clang::TranslationUnit tu_;
void HighlightToken(clang::Token *token,
std::vector<Range> *source_ranges,
int token_kind);
void HighlightCursor(clang::Token *token,
std::vector<Range> *source_ranges);
std::vector<const char*> get_compilation_commands();
}; };
class Controller { class Controller {
public: public:
Controller(const Source::Config &config); explicit Controller(const Source::Config &config);
Controller(); Controller();
View& view(); View& view();
Model& model(); Model& model();
@ -89,6 +126,10 @@ namespace Source {
private: private:
void OnLineEdit(); void OnLineEdit();
void OnSaveFile(); void OnSaveFile();
std::mutex syntax;
std::mutex parsing;
bool go = false;
protected: protected:
View view_; View view_;
Model model_; Model model_;

Loading…
Cancel
Save