Skip to content

Commit

Permalink
Add context menu item for toggle comments (#472)
Browse files Browse the repository at this point in the history
* Add context menu item for toggle comments

* Remove redundant code

* Rename variable to make clearer

* Update label
  • Loading branch information
davidmhewitt authored and danirabbit committed Aug 7, 2018
1 parent d3a45b2 commit 0c570c7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Services/CommentToggler.vala
Expand Up @@ -64,6 +64,15 @@ public class Scratch.CommentToggler {
return CommentType.NONE;
}

public static bool language_has_comments (Gtk.SourceLanguage? lang) {
if (lang == null) {
return false;
}

var type = get_comment_tags_for_lang (lang, CommentType.LINE, null, null);
return type != CommentType.NONE;
}

// Returns whether or not all lines within a region are already commented.
// This is to detect whether to toggle comments on or off. If all lines are commented, then we want to remove
// those comments. If only some are commented, then the user likely selected a chunk of code that already contained
Expand Down
13 changes: 12 additions & 1 deletion src/Widgets/SourceView.vala
Expand Up @@ -329,8 +329,19 @@ namespace Scratch.Widgets {
var sort_item = new Gtk.MenuItem.with_label (_("Sort Selected Lines"));
sort_item.sensitive = get_selected_line_count () > 1;
sort_item.activate.connect (sort_selected_lines);

menu.add (sort_item);

if (buffer is Gtk.SourceBuffer) {
var can_comment = CommentToggler.language_has_comments ((buffer as Gtk.SourceBuffer).get_language ());
var comment_item = new Gtk.MenuItem.with_label (_("Toggle Comment"));
comment_item.sensitive = get_selected_line_count () > 0 && can_comment;
comment_item.activate.connect (() => {
CommentToggler.toggle_comment (buffer as Gtk.SourceBuffer);
});

menu.add (comment_item);
}

menu.show_all ();
}

Expand Down

0 comments on commit 0c570c7

Please sign in to comment.