Browse Source

Added line comment for markdown

merge-requests/413/head
eidheim 3 years ago
parent
commit
6def62ad21
  1. 67
      src/source.cpp

67
src/source.cpp

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

Loading…
Cancel
Save