Browse Source

Moves the clang index to notebook for better parsing between edited files

master
Jørgen Lien Sellæg 11 years ago
parent
commit
5a41847518
  1. 4
      juci/api.h
  2. 4
      juci/config.cc
  3. 18
      juci/notebook.cc
  4. 4
      juci/notebook.h
  5. 46
      juci/source.cc
  6. 65
      juci/source.h

4
juci/api.h

@ -55,9 +55,9 @@ namespace libjuci {
const std::string plugin_path, const std::string plugin_path,
const std::string menu_keybinding); const std::string menu_keybinding);
void AddMenuXml(const std::string plugin_name, void AddMenuXml(const std::string plugin_name,
const string parent_menu); const std::string parent_menu);
void AddSubMenuXml(const std::string plugin_name, void AddSubMenuXml(const std::string plugin_name,
const string parent_menu); const std::string parent_menu);
////////////////////////////// //////////////////////////////
//// Boost.Python methods //// //// Boost.Python methods ////
////////////////////////////// //////////////////////////////

4
juci/config.cc

@ -17,16 +17,14 @@ void MainConfig::GenerateSource() {
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 : colors_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 : syntax_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() {
string line; std::string line;
std::ifstream menu_xml("menu.xml"); std::ifstream menu_xml("menu.xml");
if (menu_xml.is_open()) { if (menu_xml.is_open()) {
while (getline(menu_xml, line)) { while (getline(menu_xml, line)) {

18
juci/notebook.cc

@ -7,7 +7,7 @@ Notebook::Model::Model() {
scrollvalue_ = 50; scrollvalue_ = 50;
} }
Notebook::View::View() { Notebook::View::View() : notebook_() {
view_.pack2(notebook_); view_.pack2(notebook_);
view_.set_position(120); view_.set_position(120);
} }
@ -17,7 +17,8 @@ Notebook::Controller::Controller(Gtk::Window& window,
Source::Config& source_cfg, Source::Config& source_cfg,
Directories::Config& dir_cfg) : Directories::Config& dir_cfg) :
source_config_(source_cfg), source_config_(source_cfg),
directories_(dir_cfg) { directories_(dir_cfg),
index_(0, 1) {
window_ = &window; window_ = &window;
OnNewPage("juCi++"); OnNewPage("juCi++");
refClipboard_ = Gtk::Clipboard::get(); refClipboard_ = Gtk::Clipboard::get();
@ -26,7 +27,6 @@ Notebook::Controller::Controller(Gtk::Window& window,
CreateKeybindings(keybindings); CreateKeybindings(keybindings);
} // Constructor } // Constructor
void Notebook::Controller::CreateKeybindings(Keybindings::Controller void Notebook::Controller::CreateKeybindings(Keybindings::Controller
&keybindings) { &keybindings) {
directories().m_TreeView.signal_row_activated() directories().m_TreeView.signal_row_activated()
@ -275,6 +275,14 @@ void Notebook::Controller::OnNewPage(std::string name) {
Notebook().set_focus_child(text_vec_.at(Pages()-1)->view()); Notebook().set_focus_child(text_vec_.at(Pages()-1)->view());
} }
void Notebook::Controller::
MapBuffers(std::map<std::string, std::string> *buffers) {
for (auto &buffer : text_vec_) {
buffers->operator[](buffer->model().file_path()) =
buffer->buffer()->get_text().raw();
}
}
void Notebook::Controller::OnOpenFile(std::string path) { void Notebook::Controller::OnOpenFile(std::string path) {
OnCreatePage(); OnCreatePage();
text_vec_.back()->OnOpenFile(path); text_vec_.back()->OnOpenFile(path);
@ -287,8 +295,8 @@ void Notebook::Controller::OnOpenFile(std::string path) {
} }
void Notebook::Controller::OnCreatePage() { void Notebook::Controller::OnCreatePage() {
text_vec_.push_back(new Source::Controller(source_config())); text_vec_.push_back(new Source::Controller(source_config(), this));
linenumbers_vec_.push_back(new Source::Controller(source_config())); linenumbers_vec_.push_back(new Source::Controller(source_config(), this));
scrolledline_vec_.push_back(new Gtk::ScrolledWindow()); scrolledline_vec_.push_back(new Gtk::ScrolledWindow());
scrolledtext_vec_.push_back(new Gtk::ScrolledWindow()); scrolledtext_vec_.push_back(new Gtk::ScrolledWindow());
editor_vec_.push_back(new Gtk::HBox()); editor_vec_.push_back(new Gtk::HBox());

4
juci/notebook.h

@ -10,6 +10,7 @@
#include <type_traits> #include <type_traits>
#include <map> #include <map>
#include <sigc++/sigc++.h> #include <sigc++/sigc++.h>
#include "clangmm.h"
namespace Notebook { namespace Notebook {
class Model { class Model {
@ -57,6 +58,8 @@ namespace Notebook {
void OnOpenFile(std::string filename); void OnOpenFile(std::string filename);
void OnCreatePage(); void OnCreatePage();
bool ScrollEventCallback(GdkEventScroll* scroll_event); bool ScrollEventCallback(GdkEventScroll* scroll_event);
void MapBuffers(std::map<std::string, std::string> *buffers);
clang::Index* index() { return &index_; }
int Pages(); int Pages();
Directories::Controller& directories() { return directories_; } Directories::Controller& directories() { return directories_; }
Gtk::Paned& view(); Gtk::Paned& view();
@ -101,6 +104,7 @@ namespace Notebook {
bool ispopup; bool ispopup;
Gtk::Dialog popup_; Gtk::Dialog popup_;
Gtk::Window* window_; Gtk::Window* window_;
clang::Index index_;
}; // class controller }; // class controller
} // namespace Notebook } // namespace Notebook
#endif // JUCI_NOTEBOOK_H_ #endif // JUCI_NOTEBOOK_H_

46
juci/source.cc

@ -4,7 +4,7 @@
#include <boost/property_tree/json_parser.hpp> #include <boost/property_tree/json_parser.hpp>
#include <fstream> #include <fstream>
#include <boost/timer/timer.hpp> #include <boost/timer/timer.hpp>
#include "notebook.h"
#define log( var ) \ #define log( var ) \
std::cout << "source.cc (" << __LINE__ << ") " << #var << std::endl std::cout << "source.cc (" << __LINE__ << ") " << #var << std::endl
@ -102,16 +102,18 @@ Source::Model::Model(const Source::Config &config) :
void Source::Model:: void Source::Model::
InitSyntaxHighlighting(const std::string &filepath, InitSyntaxHighlighting(const std::string &filepath,
const std::string &project_path, const std::string &project_path,
const std::string &text, const std::map<std::string, std::string>
&buffers,
int start_offset, int start_offset,
int end_offset) { int end_offset,
clang::Index *index) {
set_file_path(filepath); set_file_path(filepath);
set_project_path(project_path); set_project_path(project_path);
std::vector<const char*> arguments = get_compilation_commands(); std::vector<const char*> arguments = get_compilation_commands();
tu_ = clang::TranslationUnit(true, tu_ = clang::TranslationUnit(index,
filepath, filepath,
arguments, arguments,
text); buffers);
} }
// Source::View::UpdateLine // Source::View::UpdateLine
@ -123,7 +125,7 @@ OnLineEdit(const std::vector<Source::Range> &locations,
// Source::Model::UpdateLine // Source::Model::UpdateLine
int Source::Model:: int Source::Model::
ReParse(const std::string &buffer) { ReParse(const std::map<std::string, std::string> &buffer) {
return tu_.ReparseTranslationUnit(file_path(), buffer); return tu_.ReparseTranslationUnit(file_path(), buffer);
} }
@ -138,8 +140,9 @@ GetAutoCompleteSuggestions(int line_number,
std::vector<Source::AutoCompleteData> std::vector<Source::AutoCompleteData>
*suggestions) { *suggestions) {
parsing.lock(); parsing.lock();
model().GetAutoCompleteSuggestions(view().get_buffer() std::map<std::string, std::string> buffers;
->get_text().raw(), notebook_->MapBuffers(&buffers);
model().GetAutoCompleteSuggestions(buffers,
line_number, line_number,
column, column,
suggestions); suggestions);
@ -147,14 +150,14 @@ GetAutoCompleteSuggestions(int line_number,
} }
void Source::Model:: void Source::Model::
GetAutoCompleteSuggestions(const std::string& buffer, GetAutoCompleteSuggestions(const std::map<std::string, std::string> &buffers,
int line_number, int line_number,
int column, int column,
std::vector<Source::AutoCompleteData> std::vector<Source::AutoCompleteData>
*suggestions) { *suggestions) {
clang::CodeCompleteResults results(&tu_, clang::CodeCompleteResults results(&tu_,
file_path(), file_path(),
buffer, buffers,
line_number, line_number,
column); column);
for (int i = 0; i < results.size(); i++) { for (int i = 0; i < results.size(); i++) {
@ -263,9 +266,9 @@ HighlightToken(clang::Token *token,
// Source::Controller::Controller() // Source::Controller::Controller()
// Constructor for Controller // Constructor for Controller
Source::Controller::Controller(const Source::Config &config) : Source::Controller::Controller(const Source::Config &config,
model_(config) { Notebook::Controller *notebook) :
} model_(config), notebook_(notebook) { }
// Source::Controller::view() // Source::Controller::view()
// return shared_ptr to the view // return shared_ptr to the view
@ -313,7 +316,7 @@ void Source::View::OnUpdateSyntax(const std::vector<Source::Range> &ranges,
Glib::RefPtr<Gtk::TextBuffer> buffer = get_buffer(); Glib::RefPtr<Gtk::TextBuffer> buffer = get_buffer();
buffer->remove_all_tags(buffer->begin(), buffer->end()); buffer->remove_all_tags(buffer->begin(), buffer->end());
for (auto &range : ranges) { for (auto &range : ranges) {
string type = std::to_string(range.kind()); std::string type = std::to_string(range.kind());
try { try {
config.typetable().at(type); config.typetable().at(type);
} catch (std::exception) { } catch (std::exception) {
@ -337,17 +340,20 @@ void Source::View::OnUpdateSyntax(const std::vector<Source::Range> &ranges,
void Source::Controller::OnOpenFile(const string &filepath) { void Source::Controller::OnOpenFile(const string &filepath) {
sourcefile s(filepath); sourcefile s(filepath);
std::map<std::string, std::string> buffers;
notebook_->MapBuffers(&buffers);
buffers[filepath] = s.get_content();
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();
if (check_extention(filepath)) { if (check_extention(filepath)) {
view().ApplyConfig(model().config()); view().ApplyConfig(model().config());
model().InitSyntaxHighlighting(filepath, model().InitSyntaxHighlighting(filepath,
extract_file_path(filepath), extract_file_path(filepath),
buffer()->get_text().raw(), buffers,
start_offset, start_offset,
end_offset); end_offset,
notebook_->index());
view().OnUpdateSyntax(model().ExtractTokens(start_offset, end_offset), view().OnUpdateSyntax(model().ExtractTokens(start_offset, end_offset),
model().config()); model().config());
} }
@ -358,7 +364,10 @@ void Source::Controller::OnOpenFile(const string &filepath) {
if (parsing.try_lock()) { if (parsing.try_lock()) {
while (true) { while (true) {
const std::string raw = buffer()->get_text().raw(); const std::string raw = buffer()->get_text().raw();
if (model().ReParse(raw) == 0 && std::map<std::string, std::string> buffers;
notebook_->MapBuffers(&buffers);
buffers[model().file_path()] = raw;
if (model().ReParse(buffers) == 0 &&
raw == buffer()->get_text().raw()) { raw == buffer()->get_text().raw()) {
syntax.lock(); syntax.lock();
go = true; go = true;
@ -373,7 +382,6 @@ void Source::Controller::OnOpenFile(const string &filepath) {
} }
}); });
buffer()->signal_begin_user_action().connect([this]() { buffer()->signal_begin_user_action().connect([this]() {
if (go) { if (go) {
syntax.lock(); syntax.lock();

65
juci/source.h

@ -3,31 +3,34 @@
#include <iostream> #include <iostream>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include <string>
#include "gtkmm.h" #include "gtkmm.h"
#include "clangmm.h" #include "clangmm.h"
#include <thread> #include <thread>
#include <mutex> #include <mutex>
#include <string>
using std::string; namespace Notebook {
class Controller;
}
namespace Source { namespace Source {
class Config { class Config {
public: public:
Config(const Config &original); Config(const Config &original);
Config(); Config();
const std::unordered_map<string, string>& tagtable() const; const std::unordered_map<std::string, std::string>& tagtable() const;
const std::unordered_map<string, string>& typetable() const; const std::unordered_map<std::string, std::string>& typetable() const;
void SetTagTable(const std::unordered_map<string, string> &tagtable); void SetTagTable(const std::unordered_map<std::string, std::string>
void InsertTag(const string &key, const string &value); &tagtable);
void SetTypeTable(const std::unordered_map<string, string> &tagtable); void InsertTag(const std::string &key, const std::string &value);
void InsertType(const string &key, const string &value); void SetTypeTable(const std::unordered_map<std::string, std::string>
&tagtable);
void InsertType(const std::string &key, const std::string &value);
private: private:
std::unordered_map<string, string> tagtable_; std::unordered_map<std::string, std::string> tagtable_;
std::unordered_map<string, string> typetable_; std::unordered_map<std::string, std::string> typetable_;
string background_; std::string background_;
}; // class Config }; // class Config
class Location { class Location {
@ -48,13 +51,6 @@ namespace Source {
const Location& start() const { return start_; } const Location& start() const { return start_; }
const Location& end() const { return end_; } const Location& end() const { return end_; }
int kind() const { return kind_; } 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:
Location start_; Location start_;
Location end_; Location end_;
@ -71,7 +67,7 @@ namespace Source {
const Config &config); const Config &config);
private: private:
string GetLine(const Gtk::TextIter &begin); std::string GetLine(const Gtk::TextIter &begin);
}; // class View }; // class View
class AutoCompleteChunk { class AutoCompleteChunk {
@ -99,33 +95,36 @@ namespace Source {
// inits the syntax highligthing on file open // inits the syntax highligthing on file open
void InitSyntaxHighlighting(const std::string &filepath, void InitSyntaxHighlighting(const std::string &filepath,
const std::string &project_path, const std::string &project_path,
const std::string &text, const std::map<std::string, std::string>
&buffers,
int start_offset, int start_offset,
int end_offset); int end_offset,
clang::Index *index);
// sets the filepath for this mvc // sets the filepath for this mvc
void set_file_path(const string &file_path); void set_file_path(const std::string &file_path);
// sets the project path for this mvc // sets the project path for this mvc
void set_project_path(const string &project_path); void set_project_path(const std::string &project_path);
// gets the file_path member // gets the file_path member
const string& file_path() const; const std::string& file_path() const;
// gets the project_path member // gets the project_path member
const string& project_path() const; const std::string& project_path() const;
// gets the config member // gets the config member
const Config& config() const; const Config& config() const;
void GetAutoCompleteSuggestions(const std::string& buffer, void GetAutoCompleteSuggestions(const std::map<std::string, std::string>
&buffers,
int line_number, int line_number,
int column, int column,
std::vector<AutoCompleteData> std::vector<AutoCompleteData>
*suggestions); *suggestions);
~Model() { } ~Model() { }
int ReParse(const std::string &buffer); int ReParse(const std::map<std::string, std::string> &buffers);
std::vector<Range> ExtractTokens(int, int); std::vector<Range> ExtractTokens(int, int);
private: private:
Config config_; Config config_;
string file_path_; std::string file_path_;
string project_path_; std::string project_path_;
clang::TranslationUnit tu_; clang::TranslationUnit tu_;
void HighlightToken(clang::Token *token, void HighlightToken(clang::Token *token,
std::vector<Range> *source_ranges, std::vector<Range> *source_ranges,
@ -137,12 +136,13 @@ namespace Source {
class Controller { class Controller {
public: public:
explicit Controller(const Source::Config &config); Controller(const Source::Config &config,
Notebook::Controller *notebook);
Controller(); Controller();
View& view(); View& view();
Model& model(); Model& model();
void OnNewEmptyFile(); void OnNewEmptyFile();
void OnOpenFile(const string &filename); void OnOpenFile(const std::string &filename);
void GetAutoCompleteSuggestions(int line_number, void GetAutoCompleteSuggestions(int line_number,
int column, int column,
std::vector<AutoCompleteData> std::vector<AutoCompleteData>
@ -159,6 +159,7 @@ namespace Source {
protected: protected:
View view_; View view_;
Model model_; Model model_;
Notebook::Controller *notebook_;
}; // class Controller }; // class Controller
} // namespace Source } // namespace Source
#endif // JUCI_SOURCE_H_ #endif // JUCI_SOURCE_H_

Loading…
Cancel
Save