|
|
|
@ -6,6 +6,7 @@ |
|
|
|
#include "logging.h" |
|
|
|
#include "logging.h" |
|
|
|
#include <algorithm> |
|
|
|
#include <algorithm> |
|
|
|
#include <regex> |
|
|
|
#include <regex> |
|
|
|
|
|
|
|
#include "selectiondialog.h" |
|
|
|
|
|
|
|
|
|
|
|
bool Source::Config::legal_extension(std::string e) const { |
|
|
|
bool Source::Config::legal_extension(std::string e) const { |
|
|
|
std::transform(e.begin(), e.end(),e.begin(), ::tolower); |
|
|
|
std::transform(e.begin(), e.end(),e.begin(), ::tolower); |
|
|
|
@ -20,8 +21,17 @@ bool Source::Config::legal_extension(std::string e) const { |
|
|
|
//////////////
|
|
|
|
//////////////
|
|
|
|
//// View ////
|
|
|
|
//// View ////
|
|
|
|
//////////////
|
|
|
|
//////////////
|
|
|
|
Source::View::View() { |
|
|
|
Source::View::View(const Source::Config& config, const std::string& file_path, const std::string& project_path): |
|
|
|
|
|
|
|
config(config), file_path(file_path), project_path(project_path) { |
|
|
|
Gsv::init(); |
|
|
|
Gsv::init(); |
|
|
|
|
|
|
|
set_smart_home_end(Gsv::SMART_HOME_END_BEFORE); |
|
|
|
|
|
|
|
set_show_line_numbers(config.show_line_numbers); |
|
|
|
|
|
|
|
set_highlight_current_line(config.highlight_current_line); |
|
|
|
|
|
|
|
sourcefile s(file_path); |
|
|
|
|
|
|
|
get_source_buffer()->get_undo_manager()->begin_not_undoable_action(); |
|
|
|
|
|
|
|
get_source_buffer()->set_text(s.get_content()); |
|
|
|
|
|
|
|
get_source_buffer()->get_undo_manager()->end_not_undoable_action(); |
|
|
|
|
|
|
|
search_start = search_end = this->get_buffer()->end(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
string Source::View::get_line(size_t line_number) { |
|
|
|
string Source::View::get_line(size_t line_number) { |
|
|
|
@ -40,60 +50,222 @@ string Source::View::get_line_before_insert() { |
|
|
|
return line; |
|
|
|
return line; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
///////////////
|
|
|
|
//Basic indentation
|
|
|
|
//// Parser ///
|
|
|
|
bool Source::View::on_key_press(GdkEventKey* key) { |
|
|
|
///////////////
|
|
|
|
const std::regex spaces_regex(std::string("^(")+config.tab_char+"*).*$"); |
|
|
|
clang::Index Source::Parser::clang_index(0, 1); |
|
|
|
//Indent as in next or previous line
|
|
|
|
|
|
|
|
if(key->keyval==GDK_KEY_Return && key->state==0) { |
|
|
|
|
|
|
|
int line_nr=get_source_buffer()->get_insert()->get_iter().get_line(); |
|
|
|
|
|
|
|
string line(get_line_before_insert()); |
|
|
|
|
|
|
|
std::smatch sm; |
|
|
|
|
|
|
|
if(std::regex_match(line, sm, spaces_regex)) { |
|
|
|
|
|
|
|
if((line_nr+1)<get_source_buffer()->get_line_count()) { |
|
|
|
|
|
|
|
string next_line=get_line(line_nr+1); |
|
|
|
|
|
|
|
std::smatch sm2; |
|
|
|
|
|
|
|
if(std::regex_match(next_line, sm2, spaces_regex)) { |
|
|
|
|
|
|
|
if(sm2[1].str().size()>sm[1].str().size()) { |
|
|
|
|
|
|
|
get_source_buffer()->insert_at_cursor("\n"+sm2[1].str()); |
|
|
|
|
|
|
|
scroll_to(get_source_buffer()->get_insert()); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
get_source_buffer()->insert_at_cursor("\n"+sm[1].str()); |
|
|
|
|
|
|
|
scroll_to(get_source_buffer()->get_insert()); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//Indent right when clicking tab, no matter where in the line the cursor is. Also works on selected text.
|
|
|
|
|
|
|
|
else if(key->keyval==GDK_KEY_Tab && key->state==0) { |
|
|
|
|
|
|
|
Gtk::TextIter selection_start, selection_end; |
|
|
|
|
|
|
|
get_source_buffer()->get_selection_bounds(selection_start, selection_end); |
|
|
|
|
|
|
|
int line_start=selection_start.get_line(); |
|
|
|
|
|
|
|
int line_end=selection_end.get_line(); |
|
|
|
|
|
|
|
for(int line=line_start;line<=line_end;line++) { |
|
|
|
|
|
|
|
Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(line); |
|
|
|
|
|
|
|
get_source_buffer()->insert(line_it, config.tab); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//Indent left when clicking shift-tab, no matter where in the line the cursor is. Also works on selected text.
|
|
|
|
|
|
|
|
else if((key->keyval==GDK_KEY_ISO_Left_Tab || key->keyval==GDK_KEY_Tab) && key->state==GDK_SHIFT_MASK) { |
|
|
|
|
|
|
|
Gtk::TextIter selection_start, selection_end; |
|
|
|
|
|
|
|
get_source_buffer()->get_selection_bounds(selection_start, selection_end); |
|
|
|
|
|
|
|
int line_start=selection_start.get_line(); |
|
|
|
|
|
|
|
int line_end=selection_end.get_line(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(int line_nr=line_start;line_nr<=line_end;line_nr++) { |
|
|
|
|
|
|
|
string line=get_line(line_nr); |
|
|
|
|
|
|
|
if(!(line.size()>=config.tab_size && line.substr(0, config.tab_size)==config.tab)) |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(int line_nr=line_start;line_nr<=line_end;line_nr++) { |
|
|
|
|
|
|
|
Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(line_nr); |
|
|
|
|
|
|
|
Gtk::TextIter line_plus_it=line_it; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(unsigned c=0;c<config.tab_size;c++) |
|
|
|
|
|
|
|
line_plus_it++; |
|
|
|
|
|
|
|
get_source_buffer()->erase(line_it, line_plus_it); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//"Smart" backspace key
|
|
|
|
|
|
|
|
else if(key->keyval==GDK_KEY_BackSpace) { |
|
|
|
|
|
|
|
Gtk::TextIter insert_it=get_source_buffer()->get_insert()->get_iter(); |
|
|
|
|
|
|
|
int line_nr=insert_it.get_line(); |
|
|
|
|
|
|
|
if(line_nr>0) { |
|
|
|
|
|
|
|
string line=get_line(line_nr); |
|
|
|
|
|
|
|
string previous_line=get_line(line_nr-1); |
|
|
|
|
|
|
|
smatch sm; |
|
|
|
|
|
|
|
if(std::regex_match(previous_line, sm, spaces_regex)) { |
|
|
|
|
|
|
|
if(line==sm[1] || line==(std::string(sm[1])+config.tab) || (line+config.tab==sm[1])) { |
|
|
|
|
|
|
|
Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(line_nr); |
|
|
|
|
|
|
|
get_source_buffer()->erase(line_it, insert_it); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////
|
|
|
|
|
|
|
|
//// ClangView ///
|
|
|
|
|
|
|
|
//////////////////
|
|
|
|
|
|
|
|
clang::Index Source::ClangView::clang_index(0, 0); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Source::ClangView::ClangView(const Source::Config& config, const std::string& file_path, const std::string& project_path, Terminal::Controller& terminal): |
|
|
|
|
|
|
|
Source::View(config, file_path, project_path), terminal(terminal), |
|
|
|
|
|
|
|
parse_thread_go(true), parse_thread_mapped(false), parse_thread_stop(false) { |
|
|
|
|
|
|
|
override_font(Pango::FontDescription(config.font)); |
|
|
|
|
|
|
|
override_background_color(Gdk::RGBA(config.background)); |
|
|
|
|
|
|
|
for (auto &item : config.tags) { |
|
|
|
|
|
|
|
get_source_buffer()->create_tag(item.first)->property_foreground() = item.second; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int start_offset = get_source_buffer()->begin().get_offset(); |
|
|
|
|
|
|
|
int end_offset = get_source_buffer()->end().get_offset(); |
|
|
|
|
|
|
|
auto buffer_map=get_buffer_map(); |
|
|
|
|
|
|
|
//Remove includes for first parse for initial syntax highlighting
|
|
|
|
|
|
|
|
auto& str=buffer_map[file_path]; |
|
|
|
|
|
|
|
std::size_t pos=0; |
|
|
|
|
|
|
|
while((pos=str.find("#include", pos))!=std::string::npos) { |
|
|
|
|
|
|
|
auto start_pos=pos; |
|
|
|
|
|
|
|
pos=str.find('\n', pos+8); |
|
|
|
|
|
|
|
if(pos==std::string::npos) |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
if(start_pos==0 || str[start_pos-1]=='\n') { |
|
|
|
|
|
|
|
str.replace(start_pos, pos-start_pos, pos-start_pos, ' '); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
pos++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
init_syntax_highlighting(buffer_map, |
|
|
|
|
|
|
|
start_offset, |
|
|
|
|
|
|
|
end_offset, |
|
|
|
|
|
|
|
&ClangView::clang_index); |
|
|
|
|
|
|
|
update_syntax(extract_tokens(0, get_source_buffer()->get_text().size())); //TODO: replace get_source_buffer()->get_text().size()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//GTK-calls must happen in main thread, so the parse_thread
|
|
|
|
|
|
|
|
//sends signals to the main thread that it is to call the following functions:
|
|
|
|
|
|
|
|
parse_start.connect([this]{ |
|
|
|
|
|
|
|
if(parse_thread_buffer_map_mutex.try_lock()) { |
|
|
|
|
|
|
|
parse_thread_buffer_map=get_buffer_map(); |
|
|
|
|
|
|
|
parse_thread_mapped=true; |
|
|
|
|
|
|
|
parse_thread_buffer_map_mutex.unlock(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
parse_thread_go=true; |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parsing_in_progress=this->terminal.print_in_progress("Parsing "+file_path); |
|
|
|
|
|
|
|
parse_done.connect([this](){ |
|
|
|
|
|
|
|
if(parse_thread_mapped) { |
|
|
|
|
|
|
|
INFO("Updating syntax"); |
|
|
|
|
|
|
|
update_syntax(extract_tokens(0, get_source_buffer()->get_text().size())); |
|
|
|
|
|
|
|
parsing_in_progress->done("done"); |
|
|
|
|
|
|
|
INFO("Syntax updated"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
parse_thread_go=true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parse_thread=std::thread([this]() { |
|
|
|
|
|
|
|
while(true) { |
|
|
|
|
|
|
|
while(!parse_thread_go && !parse_thread_stop) |
|
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
|
|
|
|
|
|
|
if(parse_thread_stop) |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
if(!parse_thread_mapped) { |
|
|
|
|
|
|
|
parse_thread_go=false; |
|
|
|
|
|
|
|
parse_start(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (parse_thread_mapped && parsing_mutex.try_lock() && parse_thread_buffer_map_mutex.try_lock()) { |
|
|
|
|
|
|
|
reparse(parse_thread_buffer_map); |
|
|
|
|
|
|
|
parse_thread_go=false; |
|
|
|
|
|
|
|
parsing_mutex.unlock(); |
|
|
|
|
|
|
|
parse_thread_buffer_map_mutex.unlock(); |
|
|
|
|
|
|
|
parse_done(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get_source_buffer()->signal_changed().connect([this]() { |
|
|
|
|
|
|
|
parse_thread_mapped=false; |
|
|
|
|
|
|
|
parse_thread_go=true; |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
signal_key_press_event().connect(sigc::mem_fun(*this, &Source::ClangView::on_key_press), false); |
|
|
|
|
|
|
|
signal_key_release_event().connect(sigc::mem_fun(*this, &Source::ClangView::on_key_release), false); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Source::Parser::~Parser() { |
|
|
|
Source::ClangView::~ClangView() { |
|
|
|
|
|
|
|
//TODO: Is it possible to stop the clang-process in progress?
|
|
|
|
|
|
|
|
parsing_in_progress->cancel("canceled"); |
|
|
|
|
|
|
|
parse_thread_stop=true; |
|
|
|
|
|
|
|
if(parse_thread.joinable()) |
|
|
|
|
|
|
|
parse_thread.join(); |
|
|
|
parsing_mutex.lock(); //Be sure not to destroy while still parsing with libclang
|
|
|
|
parsing_mutex.lock(); //Be sure not to destroy while still parsing with libclang
|
|
|
|
parsing_mutex.unlock(); |
|
|
|
parsing_mutex.unlock(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Source::Parser:: |
|
|
|
void Source::ClangView:: |
|
|
|
init_syntax_highlighting(const std::string &filepath, |
|
|
|
init_syntax_highlighting(const std::map<std::string, std::string> |
|
|
|
const std::string &project_path, |
|
|
|
&buffers, |
|
|
|
const std::map<std::string, std::string> |
|
|
|
int start_offset, |
|
|
|
&buffers, |
|
|
|
int end_offset, |
|
|
|
int start_offset, |
|
|
|
clang::Index *index) { |
|
|
|
int end_offset, |
|
|
|
|
|
|
|
clang::Index *index) { |
|
|
|
|
|
|
|
this->project_path=project_path; |
|
|
|
|
|
|
|
std::vector<string> arguments = get_compilation_commands(); |
|
|
|
std::vector<string> arguments = get_compilation_commands(); |
|
|
|
tu_ = std::unique_ptr<clang::TranslationUnit>(new clang::TranslationUnit(index, |
|
|
|
tu_ = std::unique_ptr<clang::TranslationUnit>(new clang::TranslationUnit(index, |
|
|
|
filepath, |
|
|
|
file_path, |
|
|
|
arguments, |
|
|
|
arguments, |
|
|
|
buffers)); |
|
|
|
buffers)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::map<std::string, std::string> Source::Parser:: |
|
|
|
std::map<std::string, std::string> Source::ClangView:: |
|
|
|
get_buffer_map() const { |
|
|
|
get_buffer_map() const { |
|
|
|
std::map<std::string, std::string> buffer_map; |
|
|
|
std::map<std::string, std::string> buffer_map; |
|
|
|
for (auto &controller : controllers) { |
|
|
|
buffer_map[file_path]=get_source_buffer()->get_text().raw(); |
|
|
|
buffer_map.operator[](controller->parser.file_path) = |
|
|
|
|
|
|
|
controller->buffer()->get_text().raw(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return buffer_map; |
|
|
|
return buffer_map; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Source::Model::UpdateLine
|
|
|
|
int Source::ClangView:: |
|
|
|
int Source::Parser:: |
|
|
|
|
|
|
|
reparse(const std::map<std::string, std::string> &buffer) { |
|
|
|
reparse(const std::map<std::string, std::string> &buffer) { |
|
|
|
return tu_->ReparseTranslationUnit(file_path, buffer); |
|
|
|
return tu_->ReparseTranslationUnit(file_path, buffer); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::vector<Source::AutoCompleteData> Source::Parser:: |
|
|
|
std::vector<Source::AutoCompleteData> Source::ClangView:: |
|
|
|
get_autocomplete_suggestions(int line_number, |
|
|
|
get_autocomplete_suggestions(int line_number, int column) { |
|
|
|
int column) { |
|
|
|
|
|
|
|
INFO("Getting auto complete suggestions"); |
|
|
|
INFO("Getting auto complete suggestions"); |
|
|
|
std::vector<Source::AutoCompleteData> suggestions; |
|
|
|
std::vector<Source::AutoCompleteData> suggestions; |
|
|
|
auto buffer_map=get_buffer_map(); |
|
|
|
std::map<std::string, std::string> buffer_map; |
|
|
|
|
|
|
|
buffer_map[file_path]=get_source_buffer()->get_text(get_source_buffer()->begin(), get_source_buffer()->get_insert()->get_iter()); |
|
|
|
|
|
|
|
buffer_map[file_path]+="\n"; |
|
|
|
parsing_mutex.lock(); |
|
|
|
parsing_mutex.lock(); |
|
|
|
clang::CodeCompleteResults results(tu_.get(), |
|
|
|
clang::CodeCompleteResults results(tu_.get(), |
|
|
|
file_path, |
|
|
|
file_path, |
|
|
|
buffer_map, |
|
|
|
buffer_map, |
|
|
|
line_number, |
|
|
|
line_number, |
|
|
|
column); |
|
|
|
column-1); |
|
|
|
for (int i = 0; i < results.size(); i++) { |
|
|
|
for (int i = 0; i < results.size(); i++) { |
|
|
|
const vector<clang::CompletionChunk> chunks_ = results.get(i).get_chunks(); |
|
|
|
const vector<clang::CompletionChunk> chunks_ = results.get(i).get_chunks(); |
|
|
|
std::vector<AutoCompleteChunk> chunks; |
|
|
|
std::vector<AutoCompleteChunk> chunks; |
|
|
|
@ -108,22 +280,24 @@ get_autocomplete_suggestions(int line_number, |
|
|
|
return suggestions; |
|
|
|
return suggestions; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> Source::Parser:: |
|
|
|
std::vector<std::string> Source::ClangView:: |
|
|
|
get_compilation_commands() { |
|
|
|
get_compilation_commands() { |
|
|
|
clang::CompilationDatabase db(project_path+"/"); |
|
|
|
clang::CompilationDatabase db(project_path); |
|
|
|
clang::CompileCommands commands(file_path, &db); |
|
|
|
clang::CompileCommands commands(file_path, &db); |
|
|
|
std::vector<clang::CompileCommand> cmds = commands.get_commands(); |
|
|
|
std::vector<clang::CompileCommand> cmds = commands.get_commands(); |
|
|
|
std::vector<std::string> arguments; |
|
|
|
std::vector<std::string> arguments; |
|
|
|
for (auto &i : cmds) { |
|
|
|
for (auto &i : cmds) { |
|
|
|
std::vector<std::string> lol = i.get_command_as_args(); |
|
|
|
std::vector<std::string> lol = i.get_command_as_args(); |
|
|
|
for (int a = 1; a < lol.size()-4; a++) { |
|
|
|
for (size_t a = 1; a < lol.size()-4; a++) { |
|
|
|
arguments.emplace_back(lol[a]); |
|
|
|
arguments.emplace_back(lol[a]); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if(boost::filesystem::path(file_path).extension()==".h") //TODO: temporary fix for .h-files (parse as c++)
|
|
|
|
|
|
|
|
arguments.emplace_back("-xc++"); |
|
|
|
return arguments; |
|
|
|
return arguments; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::vector<Source::Range> Source::Parser:: |
|
|
|
std::vector<Source::Range> Source::ClangView:: |
|
|
|
extract_tokens(int start_offset, int end_offset) { |
|
|
|
extract_tokens(int start_offset, int end_offset) { |
|
|
|
std::vector<Source::Range> ranges; |
|
|
|
std::vector<Source::Range> ranges; |
|
|
|
clang::SourceLocation start(tu_.get(), file_path, start_offset); |
|
|
|
clang::SourceLocation start(tu_.get(), file_path, start_offset); |
|
|
|
@ -143,7 +317,36 @@ extract_tokens(int start_offset, int end_offset) { |
|
|
|
return ranges; |
|
|
|
return ranges; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Source::Parser:: |
|
|
|
void Source::ClangView::update_syntax(const std::vector<Source::Range> &ranges) { |
|
|
|
|
|
|
|
if (ranges.empty() || ranges.size() == 0) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
auto buffer = get_source_buffer(); |
|
|
|
|
|
|
|
buffer->remove_all_tags(buffer->begin(), buffer->end()); |
|
|
|
|
|
|
|
for (auto &range : ranges) { |
|
|
|
|
|
|
|
std::string type = std::to_string(range.kind); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
config.types.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.types.at(type), |
|
|
|
|
|
|
|
begin_iter, end_iter); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Source::ClangView:: |
|
|
|
highlight_cursor(clang::Token *token, |
|
|
|
highlight_cursor(clang::Token *token, |
|
|
|
std::vector<Source::Range> *source_ranges) { |
|
|
|
std::vector<Source::Range> *source_ranges) { |
|
|
|
clang::SourceLocation location = token->get_source_location(tu_.get()); |
|
|
|
clang::SourceLocation location = token->get_source_location(tu_.get()); |
|
|
|
@ -159,7 +362,7 @@ highlight_cursor(clang::Token *token, |
|
|
|
Source::Location(end_line_num, |
|
|
|
Source::Location(end_line_num, |
|
|
|
end_offset), (int) cursor.kind()); |
|
|
|
end_offset), (int) cursor.kind()); |
|
|
|
} |
|
|
|
} |
|
|
|
void Source::Parser:: |
|
|
|
void Source::ClangView:: |
|
|
|
highlight_token(clang::Token *token, |
|
|
|
highlight_token(clang::Token *token, |
|
|
|
std::vector<Source::Range> *source_ranges, |
|
|
|
std::vector<Source::Range> *source_ranges, |
|
|
|
int token_kind) { |
|
|
|
int token_kind) { |
|
|
|
@ -175,264 +378,179 @@ highlight_token(clang::Token *token, |
|
|
|
end_offset), token_kind); |
|
|
|
end_offset), token_kind); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
////////////////////
|
|
|
|
bool Source::ClangView::on_key_release(GdkEventKey* key) { |
|
|
|
//// Controller ////
|
|
|
|
INFO("Source::ClangView::on_key_release getting iters"); |
|
|
|
////////////////////
|
|
|
|
// Get function to fill popup with suggests item vector under is for testing
|
|
|
|
|
|
|
|
Gtk::TextIter beg = get_source_buffer()->get_insert()->get_iter(); |
|
|
|
// Source::Controller::Controller()
|
|
|
|
Gtk::TextIter end = get_source_buffer()->get_insert()->get_iter(); |
|
|
|
// Constructor for Controller
|
|
|
|
Gtk::TextIter tmp = get_source_buffer()->get_insert()->get_iter(); |
|
|
|
Source::Controller::Controller(const Source::Config &config, |
|
|
|
Gtk::TextIter tmp1 = get_source_buffer()->get_insert()->get_iter(); |
|
|
|
const std::vector<std::unique_ptr<Source::Controller> > &controllers) : |
|
|
|
Gtk::TextIter line = get_source_buffer()->get_iter_at_line(tmp.get_line()); |
|
|
|
config(config), parser(controllers), parse_thread_go(false), parse_thread_mapped(false), parse_thread_stop(false) { |
|
|
|
if (end.backward_char() && end.backward_char()) { |
|
|
|
INFO("Source Controller with childs constructed"); |
|
|
|
bool illegal_chars = |
|
|
|
view.signal_key_press_event().connect(sigc::mem_fun(*this, &Source::Controller::on_key_press), false); |
|
|
|
end.backward_search("\"", Gtk::TEXT_SEARCH_VISIBLE_ONLY, tmp, tmp1, line) |
|
|
|
view.set_smart_home_end(Gsv::SMART_HOME_END_BEFORE); |
|
|
|
|| |
|
|
|
view.override_font(Pango::FontDescription(config.font)); |
|
|
|
end.backward_search("//", Gtk::TEXT_SEARCH_VISIBLE_ONLY, tmp, tmp1, line); |
|
|
|
view.set_show_line_numbers(config.show_line_numbers); |
|
|
|
INFO("Source::ClangView::on_key_release checking key->keyval"); |
|
|
|
view.set_highlight_current_line(config.highlight_current_line); |
|
|
|
if (illegal_chars) { |
|
|
|
view.override_background_color(Gdk::RGBA(config.background)); |
|
|
|
return false; |
|
|
|
for (auto &item : config.tags) { |
|
|
|
|
|
|
|
buffer()->create_tag(item.first)->property_foreground() = item.second; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
buffer()->signal_changed().connect([this]() { |
|
|
|
|
|
|
|
if(signal_buffer_changed) |
|
|
|
|
|
|
|
signal_buffer_changed(is_saved); |
|
|
|
|
|
|
|
is_saved=false; |
|
|
|
|
|
|
|
parse_thread_mapped=false; |
|
|
|
|
|
|
|
parse_thread_go=true; |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Source::Controller::~Controller() { |
|
|
|
|
|
|
|
parse_thread_stop=true; |
|
|
|
|
|
|
|
if(parse_thread.joinable()) |
|
|
|
|
|
|
|
parse_thread.join(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Source::Controller::update_syntax(const std::vector<Source::Range> &ranges) { |
|
|
|
|
|
|
|
if (ranges.empty() || ranges.size() == 0) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
auto buffer = view.get_buffer(); |
|
|
|
|
|
|
|
buffer->remove_all_tags(buffer->begin(), buffer->end()); |
|
|
|
|
|
|
|
for (auto &range : ranges) { |
|
|
|
|
|
|
|
std::string type = std::to_string(range.kind); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
config.types.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.types.at(type), |
|
|
|
|
|
|
|
begin_iter, end_iter); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Source::Controller::on_new_empty_file() { |
|
|
|
|
|
|
|
string filename("/tmp/untitled"); |
|
|
|
|
|
|
|
sourcefile s(filename); |
|
|
|
|
|
|
|
parser.file_path=filename; |
|
|
|
|
|
|
|
parser.project_path=filename; |
|
|
|
|
|
|
|
s.save(""); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Source::Controller::on_open_file(const string &filepath) { |
|
|
|
|
|
|
|
parser.file_path=filepath; |
|
|
|
|
|
|
|
sourcefile s(filepath); |
|
|
|
|
|
|
|
auto buffer_map=parser.get_buffer_map(); |
|
|
|
|
|
|
|
buffer_map[filepath] = s.get_content(); |
|
|
|
|
|
|
|
buffer()->get_undo_manager()->begin_not_undoable_action(); |
|
|
|
|
|
|
|
buffer()->set_text(s.get_content()); |
|
|
|
|
|
|
|
is_saved=true; |
|
|
|
|
|
|
|
buffer()->get_undo_manager()->end_not_undoable_action(); |
|
|
|
|
|
|
|
int start_offset = buffer()->begin().get_offset(); |
|
|
|
|
|
|
|
int end_offset = buffer()->end().get_offset(); |
|
|
|
|
|
|
|
if (config.legal_extension(filepath.substr(filepath.find_last_of(".") + 1))) { |
|
|
|
|
|
|
|
parser.init_syntax_highlighting(filepath, |
|
|
|
|
|
|
|
parser.file_path.substr(0, parser.file_path.find_last_of('/')), |
|
|
|
|
|
|
|
buffer_map, |
|
|
|
|
|
|
|
start_offset, |
|
|
|
|
|
|
|
end_offset, |
|
|
|
|
|
|
|
&Parser::clang_index); |
|
|
|
|
|
|
|
update_syntax(parser.extract_tokens(start_offset, end_offset)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//GTK-calls must happen in main thread, so the parse_thread
|
|
|
|
|
|
|
|
//sends signals to the main thread that it is to call the following functions:
|
|
|
|
|
|
|
|
parse_start.connect([this]{ |
|
|
|
|
|
|
|
if(parse_thread_buffer_map_mutex.try_lock()) { |
|
|
|
|
|
|
|
this->parse_thread_buffer_map=parser.get_buffer_map(); |
|
|
|
|
|
|
|
parse_thread_mapped=true; |
|
|
|
|
|
|
|
parse_thread_buffer_map_mutex.unlock(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
parse_thread_go=true; |
|
|
|
std::string c = get_source_buffer()->get_text(end, beg); |
|
|
|
}); |
|
|
|
switch (key->keyval) { |
|
|
|
|
|
|
|
case 46: |
|
|
|
parse_done.connect([this](){ |
|
|
|
break; |
|
|
|
if(parse_thread_mapped) { |
|
|
|
case 58: |
|
|
|
INFO("Updating syntax"); |
|
|
|
if (c != "::") return false; |
|
|
|
update_syntax(parser.extract_tokens(0, buffer()->get_text().size())); |
|
|
|
break; |
|
|
|
INFO("Syntax updated"); |
|
|
|
case 60: |
|
|
|
|
|
|
|
if (c != "->") return false; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case 62: |
|
|
|
|
|
|
|
if (c != "->") return false; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
} else { |
|
|
|
parse_thread_go=true; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
INFO("Source::ClangView::on_key_release getting autocompletions"); |
|
|
|
|
|
|
|
std::vector<Source::AutoCompleteData> acdata=get_autocomplete_suggestions(beg.get_line()+1, |
|
|
|
parse_thread=std::thread([this]() { |
|
|
|
beg.get_line_offset()+2); |
|
|
|
while(true) { |
|
|
|
std::map<std::string, std::string> rows; |
|
|
|
while(!parse_thread_go && !parse_thread_stop) |
|
|
|
for (auto &data : acdata) { |
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
|
|
|
std::stringstream ss; |
|
|
|
if(parse_thread_stop) |
|
|
|
std::string return_value; |
|
|
|
break; |
|
|
|
for (auto &chunk : data.chunks) { |
|
|
|
if(!parse_thread_mapped) { |
|
|
|
switch (chunk.kind) { |
|
|
|
parse_thread_go=false; |
|
|
|
case clang::CompletionChunk_ResultType: |
|
|
|
parse_start(); |
|
|
|
return_value = chunk.chunk; |
|
|
|
} |
|
|
|
break; |
|
|
|
else if (parse_thread_mapped && parser.parsing_mutex.try_lock() && parse_thread_buffer_map_mutex.try_lock()) { |
|
|
|
case clang::CompletionChunk_Informative: break; |
|
|
|
parser.reparse(this->parse_thread_buffer_map); |
|
|
|
default: ss << chunk.chunk; break; |
|
|
|
parse_thread_go=false; |
|
|
|
|
|
|
|
parser.parsing_mutex.unlock(); |
|
|
|
|
|
|
|
parse_thread_buffer_map_mutex.unlock(); |
|
|
|
|
|
|
|
parse_done(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
if (ss.str().length() > 0) { // if length is 0 the result is empty
|
|
|
|
|
|
|
|
rows[ss.str() + " --> " + return_value] = ss.str(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (rows.empty()) { |
|
|
|
|
|
|
|
rows["No suggestions found..."] = ""; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SelectionDialog selection_dialog(*this); |
|
|
|
|
|
|
|
selection_dialog.on_select=[this, &rows](Gtk::ListViewText& list_view_text){ |
|
|
|
|
|
|
|
std::string selected = rows.at(list_view_text.get_text(list_view_text.get_selected()[0])); |
|
|
|
|
|
|
|
get_source_buffer()->insert_at_cursor(selected); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
selection_dialog.show(rows); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Glib::RefPtr<Gsv::Buffer> Source::Controller::buffer() { |
|
|
|
//Clang indentation
|
|
|
|
return view.get_source_buffer(); |
|
|
|
//TODO: replace indentation methods with a better implementation or maybe use libclang
|
|
|
|
} |
|
|
|
bool Source::ClangView::on_key_press(GdkEventKey* key) { |
|
|
|
|
|
|
|
const std::regex bracket_regex(std::string("^(")+config.tab_char+"*).*\\{ *$"); |
|
|
|
//TODO: move indentation to Parser, replace indentation methods with a better implementation or
|
|
|
|
const std::regex no_bracket_statement_regex(std::string("^(")+config.tab_char+"*)(if|for|else if|catch|while) *\\(.*[^;}] *$"); |
|
|
|
//maybe use libclang
|
|
|
|
const std::regex no_bracket_no_para_statement_regex(std::string("^(")+config.tab_char+"*)(else|try|do) *$"); |
|
|
|
bool Source::Controller::on_key_press(GdkEventKey* key) { |
|
|
|
const std::regex spaces_regex(std::string("^(")+config.tab_char+"*).*$"); |
|
|
|
const std::regex bracket_regex("^( *).*\\{ *$"); |
|
|
|
|
|
|
|
const std::regex no_bracket_statement_regex("^( *)(if|for|else if|catch|while) *\\(.*[^;}] *$"); |
|
|
|
//Indent depending on if/else/etc and brackets
|
|
|
|
const std::regex no_bracket_no_para_statement_regex("^( *)(else|try|do) *$"); |
|
|
|
|
|
|
|
const std::regex spaces_regex("^( *).*$"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Indent as in previous line, and indent right after if/else/etc
|
|
|
|
|
|
|
|
if(key->keyval==GDK_KEY_Return && key->state==0) { |
|
|
|
if(key->keyval==GDK_KEY_Return && key->state==0) { |
|
|
|
string line(view.get_line_before_insert()); |
|
|
|
string line(get_line_before_insert()); |
|
|
|
std::smatch sm; |
|
|
|
std::smatch sm; |
|
|
|
if(std::regex_match(line, sm, bracket_regex)) { |
|
|
|
if(std::regex_match(line, sm, bracket_regex)) { |
|
|
|
buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab+"\n"+sm[1].str()+"}"); |
|
|
|
int line_nr=get_source_buffer()->get_insert()->get_iter().get_line(); |
|
|
|
auto insert_it = buffer()->get_insert()->get_iter(); |
|
|
|
if((line_nr+1)<get_source_buffer()->get_line_count()) { |
|
|
|
|
|
|
|
string next_line=get_line(line_nr+1); |
|
|
|
|
|
|
|
std::smatch sm2; |
|
|
|
|
|
|
|
if(std::regex_match(next_line, sm2, spaces_regex)) { |
|
|
|
|
|
|
|
if(sm2[1].str()==sm[1].str()+config.tab) { |
|
|
|
|
|
|
|
get_source_buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab); |
|
|
|
|
|
|
|
scroll_to(get_source_buffer()->get_insert()); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
get_source_buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab+"\n"+sm[1].str()+"}"); |
|
|
|
|
|
|
|
auto insert_it = get_source_buffer()->get_insert()->get_iter(); |
|
|
|
for(size_t c=0;c<config.tab_size+sm[1].str().size();c++) |
|
|
|
for(size_t c=0;c<config.tab_size+sm[1].str().size();c++) |
|
|
|
insert_it--; |
|
|
|
insert_it--; |
|
|
|
view.scroll_to(buffer()->get_insert()); |
|
|
|
scroll_to(get_source_buffer()->get_insert()); |
|
|
|
buffer()->place_cursor(insert_it); |
|
|
|
get_source_buffer()->place_cursor(insert_it); |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
else if(std::regex_match(line, sm, no_bracket_statement_regex)) { |
|
|
|
else if(std::regex_match(line, sm, no_bracket_statement_regex)) { |
|
|
|
buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab); |
|
|
|
get_source_buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab); |
|
|
|
view.scroll_to(buffer()->get_insert()); |
|
|
|
scroll_to(get_source_buffer()->get_insert()); |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
else if(std::regex_match(line, sm, no_bracket_no_para_statement_regex)) { |
|
|
|
else if(std::regex_match(line, sm, no_bracket_no_para_statement_regex)) { |
|
|
|
buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab); |
|
|
|
get_source_buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab); |
|
|
|
view.scroll_to(buffer()->get_insert()); |
|
|
|
scroll_to(get_source_buffer()->get_insert()); |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
else if(std::regex_match(line, sm, spaces_regex)) { |
|
|
|
else if(std::regex_match(line, sm, spaces_regex)) { |
|
|
|
std::smatch sm2; |
|
|
|
std::smatch sm2; |
|
|
|
size_t line_nr=buffer()->get_insert()->get_iter().get_line(); |
|
|
|
size_t line_nr=get_source_buffer()->get_insert()->get_iter().get_line(); |
|
|
|
if(line_nr>0 && sm[1].str().size()>=config.tab_size) { |
|
|
|
if(line_nr>0 && sm[1].str().size()>=config.tab_size) { |
|
|
|
string previous_line=view.get_line(line_nr-1); |
|
|
|
string previous_line=get_line(line_nr-1); |
|
|
|
if(!std::regex_match(previous_line, sm2, bracket_regex)) { |
|
|
|
if(!std::regex_match(previous_line, sm2, bracket_regex)) { |
|
|
|
if(std::regex_match(previous_line, sm2, no_bracket_statement_regex)) { |
|
|
|
if(std::regex_match(previous_line, sm2, no_bracket_statement_regex)) { |
|
|
|
buffer()->insert_at_cursor("\n"+sm2[1].str()); |
|
|
|
get_source_buffer()->insert_at_cursor("\n"+sm2[1].str()); |
|
|
|
view.scroll_to(buffer()->get_insert()); |
|
|
|
scroll_to(get_source_buffer()->get_insert()); |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
else if(std::regex_match(previous_line, sm2, no_bracket_no_para_statement_regex)) { |
|
|
|
else if(std::regex_match(previous_line, sm2, no_bracket_no_para_statement_regex)) { |
|
|
|
buffer()->insert_at_cursor("\n"+sm2[1].str()); |
|
|
|
get_source_buffer()->insert_at_cursor("\n"+sm2[1].str()); |
|
|
|
view.scroll_to(buffer()->get_insert()); |
|
|
|
scroll_to(get_source_buffer()->get_insert()); |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
buffer()->insert_at_cursor("\n"+sm[1].str()); |
|
|
|
|
|
|
|
view.scroll_to(buffer()->get_insert()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//Indent right when clicking tab, no matter where in the line the cursor is. Also works on selected text.
|
|
|
|
|
|
|
|
if(key->keyval==GDK_KEY_Tab && key->state==0) { |
|
|
|
|
|
|
|
Gtk::TextIter selection_start, selection_end; |
|
|
|
|
|
|
|
buffer()->get_selection_bounds(selection_start, selection_end); |
|
|
|
|
|
|
|
int line_start=selection_start.get_line(); |
|
|
|
|
|
|
|
int line_end=selection_end.get_line(); |
|
|
|
|
|
|
|
for(int line=line_start;line<=line_end;line++) { |
|
|
|
|
|
|
|
Gtk::TextIter line_it = buffer()->get_iter_at_line(line); |
|
|
|
|
|
|
|
buffer()->insert(line_it, config.tab); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//Indent left when clicking shift-tab, no matter where in the line the cursor is. Also works on selected text.
|
|
|
|
|
|
|
|
else if((key->keyval==GDK_KEY_ISO_Left_Tab || key->keyval==GDK_KEY_Tab) && key->state==GDK_SHIFT_MASK) { |
|
|
|
|
|
|
|
Gtk::TextIter selection_start, selection_end; |
|
|
|
|
|
|
|
buffer()->get_selection_bounds(selection_start, selection_end); |
|
|
|
|
|
|
|
int line_start=selection_start.get_line(); |
|
|
|
|
|
|
|
int line_end=selection_end.get_line(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(int line_nr=line_start;line_nr<=line_end;line_nr++) { |
|
|
|
|
|
|
|
string line=view.get_line(line_nr); |
|
|
|
|
|
|
|
if(!(line.size()>=config.tab_size && line.substr(0, config.tab_size)==config.tab)) |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(int line_nr=line_start;line_nr<=line_end;line_nr++) { |
|
|
|
|
|
|
|
Gtk::TextIter line_it = buffer()->get_iter_at_line(line_nr); |
|
|
|
|
|
|
|
Gtk::TextIter line_plus_it=line_it; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(unsigned c=0;c<config.tab_size;c++) |
|
|
|
|
|
|
|
line_plus_it++; |
|
|
|
|
|
|
|
buffer()->erase(line_it, line_plus_it); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
//Indent left when writing } on a new line
|
|
|
|
//Indent left when writing } on a new line
|
|
|
|
else if(key->keyval==GDK_KEY_braceright) { |
|
|
|
else if(key->keyval==GDK_KEY_braceright) { |
|
|
|
string line=view.get_line_before_insert(); |
|
|
|
string line=get_line_before_insert(); |
|
|
|
if(line.size()>=config.tab_size) { |
|
|
|
if(line.size()>=config.tab_size) { |
|
|
|
for(auto c: line) { |
|
|
|
for(auto c: line) { |
|
|
|
if(c!=' ') |
|
|
|
if(c!=config.tab_char) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
Gtk::TextIter insert_it = buffer()->get_insert()->get_iter(); |
|
|
|
Gtk::TextIter insert_it = get_source_buffer()->get_insert()->get_iter(); |
|
|
|
Gtk::TextIter line_it = buffer()->get_iter_at_line(insert_it.get_line()); |
|
|
|
Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(insert_it.get_line()); |
|
|
|
Gtk::TextIter line_plus_it=line_it; |
|
|
|
Gtk::TextIter line_plus_it=line_it; |
|
|
|
for(unsigned c=0;c<config.tab_size;c++) |
|
|
|
for(unsigned c=0;c<config.tab_size;c++) |
|
|
|
line_plus_it++; |
|
|
|
line_plus_it++; |
|
|
|
|
|
|
|
|
|
|
|
buffer()->erase(line_it, line_plus_it); |
|
|
|
get_source_buffer()->erase(line_it, line_plus_it); |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
//"Smart" backspace key
|
|
|
|
|
|
|
|
else if(key->keyval==GDK_KEY_BackSpace) { |
|
|
|
return Source::View::on_key_press(key); |
|
|
|
Gtk::TextIter insert_it=buffer()->get_insert()->get_iter(); |
|
|
|
} |
|
|
|
int line_nr=insert_it.get_line(); |
|
|
|
|
|
|
|
if(line_nr>0) { |
|
|
|
////////////////////
|
|
|
|
string line=view.get_line(line_nr); |
|
|
|
//// Controller ////
|
|
|
|
string previous_line=view.get_line(line_nr-1); |
|
|
|
////////////////////
|
|
|
|
smatch sm; |
|
|
|
|
|
|
|
if(std::regex_match(previous_line, sm, spaces_regex)) { |
|
|
|
// Source::Controller::Controller()
|
|
|
|
if(line==sm[1]) { |
|
|
|
// Constructor for Controller
|
|
|
|
Gtk::TextIter line_it = buffer()->get_iter_at_line(line_nr); |
|
|
|
Source::Controller::Controller(const Source::Config &config, |
|
|
|
buffer()->erase(line_it, insert_it); |
|
|
|
const std::string& file_path, std::string project_path, Terminal::Controller& terminal) { |
|
|
|
} |
|
|
|
if(project_path=="") { |
|
|
|
} |
|
|
|
project_path=boost::filesystem::path(file_path).parent_path().string(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
if (config.legal_extension(file_path.substr(file_path.find_last_of(".") + 1))) |
|
|
|
|
|
|
|
view=std::unique_ptr<View>(new ClangView(config, file_path, project_path, terminal)); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
view=std::unique_ptr<View>(new GenericView(config, file_path, project_path)); |
|
|
|
|
|
|
|
INFO("Source Controller with childs constructed"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Glib::RefPtr<Gsv::Buffer> Source::Controller::buffer() { |
|
|
|
|
|
|
|
return view->get_source_buffer(); |
|
|
|
} |
|
|
|
} |
|
|
|
|