Merge lp:~julien-spautz/scratch/comment-plugin into lp:~elementary-apps/scratch/scratch

Proposed by Julien Spautz
Status: Work in progress
Proposed branch: lp:~julien-spautz/scratch/comment-plugin
Merge into: lp:~elementary-apps/scratch/scratch
Diff against target: 368 lines (+281/-5)
8 files modified
CMakeLists.txt (+1/-1)
plugins/CMakeLists.txt (+1/-0)
plugins/comment/CMakeLists.txt (+24/-0)
plugins/comment/CommentPlugin.vala (+65/-0)
plugins/comment/Commentator.vala (+175/-0)
plugins/comment/comment.plugin (+10/-0)
plugins/vim-emulation/vim-emulation.vala (+1/-1)
src/Widgets/SourceView.vala (+4/-3)
To merge this branch: bzr merge lp:~julien-spautz/scratch/comment-plugin
Reviewer Review Type Date Requested Status
Mario Guerriero (community) Needs Fixing
Review via email: mp+216878@code.launchpad.net

Description of the change

Implemented a comment/uncomment shortcut as plugin. Not sure what shortcut to use so I'm using Ctrl M and Ctrl Shift M for commenting and uncommenting respectively which I've used in some other editor, don't know which one.

The plugin gets the comment tokens for the current language from gtk.sourceview. It will either comment the current line or the selected text. If only block or only line comments are supported, they are used as a substitute for the other. Un commenting simply removes all comment tokens in the current line or selection, doesn't watch for strings or other exceptions yet.

To post a comment you must log in.
1288. By Julien Spautz

undo deleted line

Revision history for this message
Julien Spautz (julien-spautz) wrote :

Undoing is also kind of fucked up, no idea why.

Revision history for this message
Mario Guerriero (mefrio-g) wrote :

Apart for the bug you mentioned I noticed another little bug. Do the following steps to reproduce:

 - Create a new document
 - Write something
 - Change language highlighting to Vala (or any other language)
 - Try to comment some lines with CTRL+M

It just does not watch for the language_changed signal emitted by the Scratch.Widgets.SourceView class.

Revision history for this message
Mario Guerriero (mefrio-g) :
review: Needs Fixing
1289. By Julien Spautz

watch for language changes

Revision history for this message
Mario Guerriero (mefrio-g) wrote :

I still experience the bug I described above.

Unmerged revisions

1289. By Julien Spautz

watch for language changes

1288. By Julien Spautz

undo deleted line

1287. By Julien Spautz

implemented uncommenting

1286. By Julien Spautz

implemented keyboard shortcut for commenting lines or blocks, uncommenting not yet supported

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'CMakeLists.txt'
2--- CMakeLists.txt 2014-01-31 14:58:18 +0000
3+++ CMakeLists.txt 2014-04-27 15:17:23 +0000
4@@ -100,4 +100,4 @@
5 add_subdirectory (po)
6
7 # Data
8-install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/data/scratch-text-editor.desktop DESTINATION share/applications)
9+install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/data/scratch-text-editor.desktop DESTINATION share/applications)
10\ No newline at end of file
11
12=== modified file 'plugins/CMakeLists.txt'
13--- plugins/CMakeLists.txt 2013-07-08 00:40:39 +0000
14+++ plugins/CMakeLists.txt 2014-04-27 15:17:23 +0000
15@@ -12,3 +12,4 @@
16 add_subdirectory (source-tree)
17 add_subdirectory (outline)
18 add_subdirectory (vim-emulation)
19+add_subdirectory (comment)
20\ No newline at end of file
21
22=== added directory 'plugins/comment'
23=== added file 'plugins/comment/CMakeLists.txt'
24--- plugins/comment/CMakeLists.txt 1970-01-01 00:00:00 +0000
25+++ plugins/comment/CMakeLists.txt 2014-04-27 15:17:23 +0000
26@@ -0,0 +1,24 @@
27+add_definitions(${NORMAL_CFLAGS})
28+link_directories(${NORMAL_LINK_DIRS})
29+vala_precompile(VALA_C
30+ CommentPlugin.vala
31+ Commentator.vala
32+PACKAGES
33+ gtk+-3.0
34+ gee-0.8
35+ granite
36+ scratchcore
37+ libpeas-1.0
38+ gtksourceview-3.0
39+ ${ZEITGEIST_DEPS}
40+OPTIONS
41+ --vapidir=${CMAKE_BINARY_DIR}
42+ --vapidir=${CMAKE_BINARY_DIR}/src/
43+ --vapidir=${CMAKE_BINARY_DIR}/scratchcore/
44+)
45+add_library(comment MODULE ${VALA_C})
46+target_link_libraries(comment ${NORMAL_LIBRARIES})
47+install(TARGETS comment DESTINATION lib/scratch/plugins/comment/)
48+install(FILES comment.plugin DESTINATION lib/scratch/plugins/comment/)
49+add_dependencies(comment scratchcore scratch)
50+message("-- File Manager plugin will be compiled")
51\ No newline at end of file
52
53=== added file 'plugins/comment/CommentPlugin.vala'
54--- plugins/comment/CommentPlugin.vala 1970-01-01 00:00:00 +0000
55+++ plugins/comment/CommentPlugin.vala 2014-04-27 15:17:23 +0000
56@@ -0,0 +1,65 @@
57+// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
58+/***
59+ BEGIN LICENSE
60+
61+ Copyright (C) 2013 Julien Spautz <spautz.julien@gmail.com>
62+ This program is free software: you can redistribute it and/or modify it
63+ under the terms of the GNU Lesser General Public License version 3, as published
64+ by the Free Software Foundation.
65+
66+ This program is distributed in the hope that it will be useful, but
67+ WITHOUT ANY WARRANTY; without even the implied warranties of
68+ MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
69+ PURPOSE. See the GNU General Public License for more details.
70+
71+ You should have received a copy of the GNU General Public License along
72+ with this program. If not, see <http://www.gnu.org/licenses/>
73+
74+ END LICENSE
75+***/
76+
77+public const string NAME = N_("Comments");
78+public const string DESCRIPTION = N_("Comment and uncomment selected code");
79+
80+namespace Scratch.Plugins {
81+
82+ public Scratch.Services.Interface plugins;
83+
84+ Gee.HashMap <Scratch.Services.Document, Comment.Commentator> commentators;
85+
86+ public class CommentPlugin : Peas.ExtensionBase, Peas.Activatable {
87+
88+ public Object object { owned get; construct; }
89+
90+ public Scratch.Services.Interface plugins {
91+ owned get { return object as Scratch.Services.Interface; }
92+ }
93+
94+ public CommentPlugin () {
95+ message ("Starting Comment Plugin");
96+ }
97+
98+ public void activate () {
99+ commentators = new Gee.HashMap <Scratch.Services.Document, Comment.Commentator> ();
100+ plugins.hook_document.connect (on_hook_document);
101+ }
102+
103+ public void deactivate () {
104+ }
105+
106+ public void update_state () {
107+ }
108+
109+ void on_hook_document (Scratch.Services.Document document) {
110+ if (commentators.has_key (document) == false)
111+ commentators [document] = new Comment.Commentator (document);
112+ }
113+ }
114+}
115+
116+[ModuleInit]
117+public void peas_register_types (GLib.TypeModule module) {
118+ var objmodule = module as Peas.ObjectModule;
119+ objmodule.register_extension_type (typeof (Peas.Activatable),
120+ typeof (Scratch.Plugins.CommentPlugin));
121+}
122\ No newline at end of file
123
124=== added file 'plugins/comment/Commentator.vala'
125--- plugins/comment/Commentator.vala 1970-01-01 00:00:00 +0000
126+++ plugins/comment/Commentator.vala 2014-04-27 15:17:23 +0000
127@@ -0,0 +1,175 @@
128+// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
129+/***
130+ BEGIN LICENSE
131+
132+ Copyright (C) 2013 Julien Spautz <spautz.julien@gmail.com>
133+ This program is free software: you can redistribute it and/or modify it
134+ under the terms of the GNU Lesser General Public License version 3, as published
135+ by the Free Software Foundation.
136+
137+ This program is distributed in the hope that it will be useful, but
138+ WITHOUT ANY WARRANTY; without even the implied warranties of
139+ MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
140+ PURPOSE. See the GNU General Public License for more details.
141+
142+ You should have received a copy of the GNU General Public License along
143+ with this program. If not, see <http://www.gnu.org/licenses/>
144+
145+ END LICENSE
146+***/
147+
148+namespace Scratch.Plugins.Comment {
149+
150+ class Commentator : Object {
151+
152+ string line_comment_start;
153+ string block_comment_start;
154+ string block_comment_end;
155+
156+ bool supports_comments {
157+ get { return supports_line_comments || supports_block_comments; }
158+ }
159+
160+ bool supports_line_comments {
161+ get { return line_comment_start != ""; }
162+ }
163+
164+ bool supports_block_comments {
165+ get { return block_comment_start != ""
166+ && block_comment_end != ""; }
167+ }
168+
169+ public Scratch.Services.Document document { get; construct; }
170+
171+ public Scratch.Widgets.SourceView source_view {
172+ get { return document.source_view; }
173+ }
174+
175+ public Gtk.SourceBuffer buffer {
176+ get { return source_view.buffer; }
177+ }
178+
179+ public Commentator (Scratch.Services.Document document) {
180+ Object (document: document);
181+ get_comment_tokens ();
182+ connect_signals ();
183+ }
184+
185+ void connect_signals () {
186+ source_view.key_press_event.connect (handle_key_press);
187+ buffer.notify["language"].connect (get_comment_tokens);
188+ }
189+
190+ bool handle_key_press (Object sender, Gdk.EventKey event) {
191+ var ctrl = (event.state & Gdk.ModifierType.CONTROL_MASK) != 0;
192+ var shift = (event.state & Gdk.ModifierType.SHIFT_MASK) != 0;
193+
194+ if (ctrl && event.keyval == Gdk.Key.m) {
195+ comment ();
196+ return true;
197+ } else if (ctrl && shift && event.keyval == Gdk.Key.M) {
198+ uncomment ();
199+ }
200+
201+ return false;
202+ }
203+
204+ void get_comment_tokens () {
205+ var source_language = buffer.language;
206+ line_comment_start = source_language.get_metadata ("line-comment-start") ?? "";
207+ block_comment_start = source_language.get_metadata ("block-comment-start") ?? "";
208+ block_comment_end = source_language.get_metadata ("block-comment-end") ?? "";
209+ }
210+
211+ void comment () {
212+ if (supports_comments == false)
213+ return;
214+
215+ if (buffer.has_selection)
216+ comment_block ();
217+ else
218+ comment_line ();
219+ }
220+
221+ void uncomment () {
222+ if (supports_comments == false)
223+ return;
224+
225+ if (buffer.has_selection)
226+ uncomment_block ();
227+ else
228+ uncomment_line ();
229+ }
230+
231+ void comment_line () {
232+ if (supports_line_comments == false) {
233+ comment_line_with_block_comments ();
234+ return;
235+ }
236+
237+ Gtk.TextIter start;
238+ buffer.get_iter_at_mark (out start, buffer.get_insert ());
239+ start.set_line_offset (0);
240+ buffer.insert (ref start, line_comment_start, -1);
241+ }
242+
243+ void comment_line_with_block_comments () {
244+ Gtk.TextIter start;
245+ buffer.get_iter_at_mark (out start, buffer.get_insert ());
246+ start.set_line_offset (0);
247+ buffer.insert (ref start, block_comment_start, -1);
248+ start.forward_to_line_end ();
249+ buffer.insert (ref start, block_comment_end, -1);
250+ }
251+
252+ void comment_block () {
253+ if (supports_block_comments == false) {
254+ comment_block_with_line_comments ();
255+ return;
256+ }
257+
258+ Gtk.TextIter start, end;
259+ buffer.get_selection_bounds (out start, out end);
260+ buffer.insert (ref start, block_comment_start, -1);
261+ buffer.get_selection_bounds (out start, out end);
262+ buffer.insert (ref end, block_comment_end, -1);
263+ }
264+
265+ void comment_block_with_line_comments () {
266+ Gtk.TextIter start, end;
267+ buffer.get_selection_bounds (out start, out end);
268+
269+ var start_line = start.get_line ();
270+ var end_line = end.get_line ();
271+
272+ for (var line = start_line; line <= end_line; line++) {
273+ start.set_line (line);
274+ buffer.insert (ref start, line_comment_start, -1);
275+ }
276+ }
277+
278+ void uncomment_block () {
279+ Gtk.TextIter start, end;
280+ buffer.get_selection_bounds (out start, out end);
281+ var selection = buffer.get_text (start, end, true);
282+ var uncommented = selection.replace (line_comment_start, "")
283+ .replace (block_comment_start, "")
284+ .replace (block_comment_end, "");
285+ buffer.delete_selection (false, false);
286+ buffer.insert_at_cursor (uncommented, -1);
287+ }
288+
289+ void uncomment_line () {
290+ Gtk.TextIter start, end;
291+ buffer.get_iter_at_mark (out start, buffer.get_insert ());
292+ buffer.get_iter_at_mark (out end, buffer.get_insert ());
293+ start.set_line_offset (0);
294+ end.forward_to_line_end ();
295+
296+ var line_text = buffer.get_text (start, end, true);
297+ var uncommented = line_text.replace (line_comment_start, "");
298+ buffer.delete_interactive (ref start, ref end, true);
299+ buffer.insert_at_cursor (uncommented, -1);
300+ }
301+ }
302+}
303\ No newline at end of file
304
305=== added file 'plugins/comment/comment.plugin'
306--- plugins/comment/comment.plugin 1970-01-01 00:00:00 +0000
307+++ plugins/comment/comment.plugin 2014-04-27 15:17:23 +0000
308@@ -0,0 +1,10 @@
309+[Plugin]
310+Module=comment
311+Loader=C
312+IAge=2
313+Name=Comments
314+Description=Comment and uncomment selected code
315+Icon=system-
316+Authors=Julien Spautz
317+Copyright=Copyright © 2013 Scratch Developers
318+Website=http://launchpad.net/scratch
319\ No newline at end of file
320
321=== modified file 'plugins/vim-emulation/vim-emulation.vala'
322--- plugins/vim-emulation/vim-emulation.vala 2013-07-12 21:56:40 +0000
323+++ plugins/vim-emulation/vim-emulation.vala 2014-04-27 15:17:23 +0000
324@@ -253,4 +253,4 @@
325 var objmodule = module as Peas.ObjectModule;
326 objmodule.register_extension_type (typeof (Peas.Activatable),
327 typeof (Scratch.Plugins.VimEmulation));
328-}
329+}
330\ No newline at end of file
331
332=== modified file 'src/Widgets/SourceView.vala'
333--- src/Widgets/SourceView.vala 2014-03-27 07:56:52 +0000
334+++ src/Widgets/SourceView.vala 2014-04-27 15:17:23 +0000
335@@ -28,6 +28,7 @@
336
337 public new SourceBuffer buffer;
338 public Gtk.TextMark mark;
339+
340 public SourceLanguageManager manager;
341 public SourceStyleSchemeManager style_scheme_manager;
342
343@@ -37,7 +38,7 @@
344
345 // Properties
346 private string font;
347-
348+
349 // Signals
350 public signal void style_changed (SourceStyleScheme style);
351
352@@ -189,7 +190,7 @@
353 override_font (Pango.FontDescription.from_string (this.font));
354
355 buffer.style_scheme = style_scheme_manager.get_scheme (Scratch.settings.style_scheme);
356-
357+
358 this.style_changed (buffer.style_scheme);
359 }
360
361@@ -203,7 +204,7 @@
362 Scratch.settings.indent_width = (int) tab_width;
363 Scratch.settings.font = this.font;
364 Scratch.settings.style_scheme = buffer.style_scheme.id;
365-
366+
367 this.style_changed (buffer.style_scheme);
368 }
369

Subscribers

People subscribed via source and target branches