Browse Source

proof of concept syntax highlighting

merge-requests/365/head
Jørgen Lien Sellæg 11 years ago
parent
commit
1e018e675c
  1. 9
      juci/config.cc
  2. 3
      juci/notebook.cc
  3. 252
      juci/source.cc
  4. 84
      juci/source.h

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() {

3
juci/notebook.cc

@ -33,7 +33,8 @@ source_config_(source_cfg) {
.key_map()["new_cc_file"]), .key_map()["new_cc_file"]),
[this]() { [this]() {
is_new_file_ = true; is_new_file_ = true;
OnFileNewCCFile(); // OnFileNewCCFile();
OnOpenFile("/home/zalox/bachelor/juci/juci/source.cc");
}); });
keybindings.action_group_menu()->add(Gtk::Action::create("FileNewH", keybindings.action_group_menu()->add(Gtk::Action::create("FileNewH",
"New h file"), "New h file"),

252
juci/source.cc

@ -5,19 +5,40 @@
#include <fstream> #include <fstream>
#include <boost/timer/timer.hpp> #include <boost/timer/timer.hpp>
#define DBGVAR( os, var ) \
(os) << "DBG: " << __FILE__ << "(" << __LINE__ << ") " \
<< #var << " = [" << (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"));
DBGVAR(std::cout, "View Constructor");
} }
// Source::View::UpdateLine // Source::View::UpdateLine
// returns the new line // returns the new line
string Source::View::UpdateLine() { string Source::View::UpdateLine() {
Gtk::TextIter line(get_buffer()->get_insert()->get_iter()); Gtk::TextIter line(get_buffer()->get_insert()->get_iter());
// std::cout << line.get_line() << std::endl; DBGVAR(std::cout, line.get_line());
// for each word --> check what it is --> apply appropriate tag DBGVAR(std::cout, line.get_line_offset());
return ""; return "";
} }
@ -31,58 +52,62 @@ string Source::View::GetLine(const Gtk::TextIter &begin) {
// Source::View::ApplyTheme() // Source::View::ApplyTheme()
// 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()) { // DBGVAR(std::cout, "Start of apply config");
// std::cout << "Apply: f: " << item.first << ", s: " << for (auto &item : config.tagtable())
// item.second << std::endl;
get_buffer()->create_tag(item.first)->property_foreground() = item.second; get_buffer()->create_tag(item.first)->property_foreground() = item.second;
} std::cout << "End of apply config" << std::endl;
} }
void Source::View::OnOpenFile(std::vector<clang::SourceLocation> &locations, void Source::View::OnOpenFile(const std::vector<Source::Range> &ranges,
const Source::Config &config) { const Source::Config &config) {
/* ApplyConfig(config); ApplyConfig(config);
Glib::RefPtr<Gtk::TextBuffer> buffer = get_buffer(); Glib::RefPtr<Gtk::TextBuffer> buffer = get_buffer();
for (auto &loc : locations) { for (auto &range : ranges) {
string type = std::to_string(loc.kind()); string type = std::to_string(range.kind());
try { try {
config.typetable().at(type); config.typetable().at(type);
} catch (std::exception) { } catch (std::exception) {
continue; continue;
} }
int linum_start = loc.line_number_begin(); int linum_start = range.start().line_number()-1;
int linum_end = loc.line_number_end(); int linum_end = range.end().line_number()-1;
int begin = loc.begin();
int end = loc.end(); int begin = range.start().column_offset()-1;
int end = range.end().column_offset()-1;
if (end < 0) end = 0; if (end < 0) end = 0;
if (begin < 0) begin = 0; if (begin < 0) begin = 0;
// std::cout << "Libc: "; std::cout << "Libc: ";
// std::cout << "type: " << type; std::cout << "type: " << type;
// std::cout << " linum_s: " << linum_start+1 << " linum_e: " << linum_end+1; std::cout << " linum_s: " << linum_start;
// std::cout << ", begin: " << begin << ", end: " << end << std::endl; std::cout << " linum_e: " << linum_end;
std::cout << ", begin: " << begin << ", end: " << end << std::endl;
Gtk::TextIter begin_iter = buffer->get_iter_at_line_offset(linum_start, Gtk::TextIter begin_iter = buffer->get_iter_at_line_offset(linum_start,
begin); begin);
Gtk::TextIter end_iter = buffer->get_iter_at_line_offset(linum_end, end); 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; std::cout << get_buffer()->get_text(begin_iter, end_iter) << std::endl;
if (begin_iter.get_line() == end_iter.get_line()) { if (begin_iter.get_line() == end_iter.get_line()) {
buffer->apply_tag_by_name(config.typetable().at(type), buffer->apply_tag_by_name(config.typetable().at(type),
begin_iter, end_iter); begin_iter, end_iter);
} }
} }
*/ // DBGVAR(std::cout, "End of OnOpenFile");
} }
// Source::View::Config::Config(Config &config) // Source::View::Config::Config(Config &config)
// copy-constructor // copy-constructor
Source::Config::Config(const Source::Config &original) { Source::Config::Config(const Source::Config &original) {
// DBGVAR(std::cout, "Start of Config constructor");
SetTagTable(original.tagtable()); SetTagTable(original.tagtable());
SetTypeTable(original.typetable()); SetTypeTable(original.typetable());
// DBGVAR(std::cout, "End of Config constructor");
} }
Source::Config::Config(){} Source::Config::Config() {}
// Source::View::Config::tagtable() // Source::View::Config::tagtable()
// returns a const refrence to the tagtable // returns a const refrence to the tagtable
@ -96,14 +121,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 +136,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 +148,137 @@ Source::Model::Model(const Source::Config &config) :
config_(config) { config_(config) {
} }
Source::Config& Source::Model::config() { void Source::Model::
return config_; initSyntaxHighlighting(const std::string &filepath,
const std::string &project_path,
int start_offset,
int end_offset) {
// DBGVAR(std::cout, "Start of initsntax");
set_file_path(filepath);
set_project_path(project_path);
std::vector<const char*> arguments = get_compilation_commands();
clang::TranslationUnit tu(true, filepath, arguments);
// DBGVAR(std::cout, &tu);
// DBGVAR(std::cout, "Start of extract tokens");
// DBGVAR(std::cout, file_path());
// DBGVAR(std::cout, &tu);
clang::SourceLocation start(&tu, file_path(), start_offset);
// DBGVAR(std::cout, "After start");
clang::SourceLocation end(&tu, file_path(), end_offset);
// DBGVAR(std::cout, "After end");
clang::SourceRange range(&start, &end);
// DBGVAR(std::cout, "After range");
clang::Tokens tokens(&tu, &range);
// DBGVAR(std::cout, "After tokens");
std::vector<clang::Token> tks = tokens.tokens();
// DBGVAR(std::cout, "After tokens tokens");
// DBGVAR(std::cout, "before for loop");
for (auto &token : tks) {
// std::cout << token.kind() << std::endl;
switch (token.kind()) {
// case 0: PunctuationToken(&token); break; // A tkn with punctation
// case 1: KeywordToken(&token); break; // A language keyword.
// case 2: IdentifierToken(&token); break; // An identifier, not a keyword
// case 3: LiteralToken(&token); break; // A num, string, or char literal
case 4:
// std::cout << "Comment token" << std::endl;
clang::SourceRange range = token.get_source_range(&tu);
// DBGVAR(std::cout, "etter range");
unsigned begin_line_num, begin_offset, end_line_num, end_offset;
clang::SourceLocation begin(&range, true);
clang::SourceLocation end(&range, false);
// DBGVAR(std::cout, "Herre krasje etter begin");
begin.get_location_info(NULL, &begin_line_num, &begin_offset, NULL);
// DBGVAR(std::cout, "etter utpressing av begin");
end.get_location_info(NULL, &end_line_num, &end_offset, NULL);
// DBGVAR(std::cout, "etter utpressing av end");
source_ranges_.emplace_back(Source::Location(begin_line_num, begin_offset),
Source::Location(end_line_num, end_offset), 705);
std::string k = token.get_token_spelling(&tu);
break; // A comment.
}
}
// DBGVAR(std::cout, "End of extract tokens");
extractTokens(start_offset, end_offset);
// DBGVAR(std::cout, "End of of initsntax");
} }
const string Source::Model::filepath() { // sets the filepath for this mvc
return filepath_; 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;
}
// sets the ranges for keywords in this mvc
void Source::Model::
set_ranges(const std::vector<Source::Range> &source_ranges) {
source_ranges_ = source_ranges;
}
// 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 ranges member
const std::vector<Source::Range>& Source::Model::source_ranges() const {
return source_ranges_;
}
// gets the config member
const Source::Config& Source::Model::config() const {
return config_;
} }
void Source::Model::SetFilePath(const string &filepath) { std::vector<const char*> Source::Model::
filepath_ = filepath; get_compilation_commands() {
// DBGVAR(std::cout, "Start of 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());
}
}
// DBGVAR(std::cout, "End of get compilation commands");
return arguments;
} }
void Source::Model:: void Source::Model::
SetSourceLocations(const std::vector<clang::SourceLocation> &locations) { extractTokens(int start_offset, int end_offset) {
locations_ = locations;
}
void Source::Model::LiteralToken(clang::Token *token) {
std::cout << "Literal token" << std::endl;
}
void Source::Model::CommentToken(clang::Token *token) {
} }
void Source::Model::IdentifierToken(clang::Token *token) {
std::cout << "Identifyer token" << std::endl;
}
void Source::Model::KeywordToken(clang::Token *token) {
std::cout << "Keyword token" << std::endl;
}
void Source::Model::PunctuationToken(clang::Token *token) {
std::cout << "Punctuation token" << std::endl;
}
//////////////////// ////////////////////
//// Controller //// //// Controller ////
//////////////////// ////////////////////
@ -172,46 +311,25 @@ void Source::Controller::OnLineEdit() {
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('/'));
}
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();
model().initSyntaxHighlighting(filepath,
std::string project_path = extract_file_path(filepath),
filepath.substr(0, filepath.find_last_of('/')); start_offset,
end_offset);
clang::CompilationDatabase db(project_path); view().OnOpenFile(model().source_ranges(), model().config());
clang::CompileCommands commands(filepath, &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());
}
}
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<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;
// model().SetSourceLocations(tu.getSourceLocations());
// view().OnOpenFile(model().getSourceLocations(), model().theme());
} }
Glib::RefPtr<Gtk::TextBuffer> Source::Controller::buffer() { Glib::RefPtr<Gtk::TextBuffer> Source::Controller::buffer() {

84
juci/source.h

@ -1,6 +1,6 @@
#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>
@ -27,32 +27,37 @@ 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_; }
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(); string UpdateLine();
void ApplyConfig(const Config &config); void ApplyConfig(const Config &config);
void OnOpenFile(std::vector<clang::SourceLocation> &locations, void OnOpenFile(const std::vector<Range> &locations,
const Config &config); const Config &config);
private: private:
string GetLine(const Gtk::TextIter &begin); string GetLine(const Gtk::TextIter &begin);
@ -60,25 +65,49 @@ namespace Source {
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); int start_offset,
std::vector<clang::SourceLocation>& getSourceLocations() { int end_offset);
return locations_; // 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);
// sets the ranges for keywords in this mvc
void set_ranges(const std::vector<Range> &ranges);
// gets the file_path member
const string& file_path() const;
// gets the project_path member
const string& project_path() const;
// gets the ranges member
const std::vector<Range>& source_ranges() const;
// gets the config member
const Config& config() const;
~Model() {
std::cout << "SNOUTHN" << std::endl;
}
private: private:
Source::Config config_; Source::Config config_;
string filepath_; string file_path_;
std::vector<clang::SourceLocation> locations_; string project_path_;
std::vector<Range> source_ranges_;
clang::TranslationUnit tu_;
void extractTokens(int, int);
void LiteralToken(clang::Token *token);
void CommentToken(clang::Token *token);
void IdentifierToken(clang::Token *token);
void KeywordToken(clang::Token *token);
void PunctuationToken(clang::Token *token);
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 +118,7 @@ namespace Source {
private: private:
void OnLineEdit(); void OnLineEdit();
void OnSaveFile(); void OnSaveFile();
protected: protected:
View view_; View view_;
Model model_; Model model_;

Loading…
Cancel
Save