|
|
|
|
@ -213,6 +213,7 @@ Source::View::View(const boost::filesystem::path &file_path, const Glib::RefPtr<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string comment_characters; |
|
|
|
|
std::string comment_end_characters; |
|
|
|
|
if(is_bracket_language) |
|
|
|
|
comment_characters = "//"; |
|
|
|
|
else { |
|
|
|
|
@ -226,9 +227,13 @@ Source::View::View(const boost::filesystem::path &file_path, const Glib::RefPtr<
|
|
|
|
|
comment_characters = "//"; |
|
|
|
|
else if(language_id == "lua") |
|
|
|
|
comment_characters = "--"; |
|
|
|
|
else if(language_id == "markdown") { |
|
|
|
|
comment_characters = "<!--"; |
|
|
|
|
comment_end_characters = "-->"; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if(!comment_characters.empty()) { |
|
|
|
|
toggle_comments = [this, comment_characters = std::move(comment_characters)] { |
|
|
|
|
toggle_comments = [this, comment_characters = std::move(comment_characters), comment_end_characters = std::move(comment_end_characters)] { |
|
|
|
|
std::vector<int> lines; |
|
|
|
|
Gtk::TextIter selection_start, selection_end; |
|
|
|
|
get_buffer()->get_selection_bounds(selection_start, selection_end); |
|
|
|
|
@ -238,12 +243,14 @@ Source::View::View(const boost::filesystem::path &file_path, const Glib::RefPtr<
|
|
|
|
|
--line_end; |
|
|
|
|
bool lines_commented = true; |
|
|
|
|
bool extra_spaces = true; |
|
|
|
|
bool extra_end_spaces = true; |
|
|
|
|
int min_indentation = std::numeric_limits<int>::max(); |
|
|
|
|
for(auto line = line_start; line <= line_end; ++line) { |
|
|
|
|
auto iter = get_buffer()->get_iter_at_line(line); |
|
|
|
|
bool line_added = false; |
|
|
|
|
bool line_commented = false; |
|
|
|
|
bool extra_space = false; |
|
|
|
|
bool extra_end_space = false; |
|
|
|
|
int indentation = 0; |
|
|
|
|
for(;;) { |
|
|
|
|
if(iter.ends_line()) |
|
|
|
|
@ -256,39 +263,36 @@ Source::View::View(const boost::filesystem::path &file_path, const Glib::RefPtr<
|
|
|
|
|
else { |
|
|
|
|
lines.emplace_back(line); |
|
|
|
|
line_added = true; |
|
|
|
|
for(size_t c = 0; c < comment_characters.size(); ++c) { |
|
|
|
|
if(iter.ends_line()) { |
|
|
|
|
break; |
|
|
|
|
auto comment_end = iter; |
|
|
|
|
comment_end.forward_chars(comment_characters.size()); |
|
|
|
|
if(comment_end.get_line() == line && get_buffer()->get_text(iter, comment_end) == comment_characters) { |
|
|
|
|
if(!comment_end_characters.empty()) { |
|
|
|
|
auto end = get_iter_at_line_end(line); |
|
|
|
|
auto start = end; |
|
|
|
|
start.backward_chars(comment_end_characters.size()); |
|
|
|
|
if(start.get_line() == line && get_buffer()->get_text(start, end) == comment_end_characters) { |
|
|
|
|
start.backward_char(); |
|
|
|
|
if(*start == ' ') |
|
|
|
|
extra_end_space = true; |
|
|
|
|
} |
|
|
|
|
else if(*iter == static_cast<unsigned int>(comment_characters[c])) { |
|
|
|
|
if(c < comment_characters.size() - 1) { |
|
|
|
|
iter.forward_char(); |
|
|
|
|
continue; |
|
|
|
|
else |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
line_commented = true; |
|
|
|
|
if(!iter.ends_line()) { |
|
|
|
|
iter.forward_char(); |
|
|
|
|
if(*iter == ' ') |
|
|
|
|
if(*comment_end == ' ') |
|
|
|
|
extra_space = true; |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if(line_added) { |
|
|
|
|
lines_commented &= line_commented; |
|
|
|
|
extra_spaces &= extra_space; |
|
|
|
|
extra_end_spaces &= extra_end_space; |
|
|
|
|
min_indentation = std::min(min_indentation, indentation); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if(lines.size()) { |
|
|
|
|
auto comment_characters_and_space = comment_characters + ' '; |
|
|
|
|
if(!lines.empty()) { |
|
|
|
|
get_buffer()->begin_user_action(); |
|
|
|
|
for(auto &line : lines) { |
|
|
|
|
auto iter = get_buffer()->get_iter_at_line(line); |
|
|
|
|
@ -296,15 +300,30 @@ Source::View::View(const boost::filesystem::path &file_path, const Glib::RefPtr<
|
|
|
|
|
iter.forward_chars(min_indentation); |
|
|
|
|
if(lines_commented) { |
|
|
|
|
auto end_iter = iter; |
|
|
|
|
end_iter.forward_chars(comment_characters.size() + static_cast<int>(extra_spaces)); |
|
|
|
|
end_iter.forward_chars(comment_characters.size() + static_cast<size_t>(extra_spaces)); |
|
|
|
|
while(*iter == ' ' || *iter == '\t') { |
|
|
|
|
iter.forward_char(); |
|
|
|
|
end_iter.forward_char(); |
|
|
|
|
} |
|
|
|
|
get_buffer()->erase(iter, end_iter); |
|
|
|
|
|
|
|
|
|
if(!comment_end_characters.empty()) { |
|
|
|
|
auto end = get_iter_at_line_end(line); |
|
|
|
|
auto start = end; |
|
|
|
|
start.backward_chars(comment_end_characters.size()); |
|
|
|
|
if(start.get_line() == line && get_buffer()->get_text(start, end) == comment_end_characters) { |
|
|
|
|
if(extra_end_spaces) |
|
|
|
|
start.backward_char(); |
|
|
|
|
get_buffer()->erase(start, end); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
get_buffer()->insert(iter, comment_characters + ' '); |
|
|
|
|
|
|
|
|
|
if(!comment_end_characters.empty()) |
|
|
|
|
get_buffer()->insert(get_iter_at_line_end(line), ' ' + comment_end_characters); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
get_buffer()->insert(iter, comment_characters_and_space); |
|
|
|
|
} |
|
|
|
|
get_buffer()->end_user_action(); |
|
|
|
|
} |
|
|
|
|
|