From 8e7e455f4327317fe711f086fbf315698324df30 Mon Sep 17 00:00:00 2001 From: Andrew Fowlie Date: Wed, 16 Jan 2019 15:11:59 +0800 Subject: add snippet to toggle comment/uncomment --- plugins/snippets/data/python.xml | 11 +++++++ plugins/snippets/snippets/Makefile.am | 3 +- plugins/snippets/snippets/__init__.py | 1 + plugins/snippets/snippets/comment.py | 57 +++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 plugins/snippets/snippets/comment.py (limited to 'plugins/snippets') diff --git a/plugins/snippets/data/python.xml b/plugins/snippets/data/python.xml index 3829c3a0..14fc1b4e 100644 --- a/plugins/snippets/data/python.xml +++ b/plugins/snippets/data/python.xml @@ -109,4 +109,15 @@ $0]]> main main + + + ]]> + Toggle comment + m]]> + + diff --git a/plugins/snippets/snippets/Makefile.am b/plugins/snippets/snippets/Makefile.am index 086b5968..2568707f 100644 --- a/plugins/snippets/snippets/Makefile.am +++ b/plugins/snippets/snippets/Makefile.am @@ -15,7 +15,8 @@ plugin_PYTHON = \ Importer.py \ Exporter.py \ LanguageManager.py \ - Completion.py + Completion.py \ + comment.py uidir = $(PLUMA_PLUGINS_DATA_DIR)/snippets/ui ui_DATA = snippets.ui diff --git a/plugins/snippets/snippets/__init__.py b/plugins/snippets/snippets/__init__.py index ada586c2..9e7f717d 100644 --- a/plugins/snippets/snippets/__init__.py +++ b/plugins/snippets/snippets/__init__.py @@ -21,6 +21,7 @@ from gi.repository import GObject, GLib, Gtk, Peas, Pluma from .WindowHelper import WindowHelper from .Library import Library from .Manager import Manager +from .comment import toggle_lines class SnippetsPlugin(GObject.Object, Peas.Activatable): __gtype_name__ = "SnippetsPlugin" diff --git a/plugins/snippets/snippets/comment.py b/plugins/snippets/snippets/comment.py new file mode 100644 index 00000000..e8e457cc --- /dev/null +++ b/plugins/snippets/snippets/comment.py @@ -0,0 +1,57 @@ +# Pluma comment functions +# Copyright (C) 2019 Andrew Fowlie +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + + +def split_leading_space(txt): + """ + @returns Text split into leading spaces and content + """ + index = len(txt.lstrip()) + spaces = txt[:-index] + content = txt[-index:] + return spaces, content + +def toggle_line(line, comment="#", after_leading_space=True, comment_empty=False): + """ + @param line Line of text to toggle comments + @param comment String used for inline comments + @param after_leading_space Whether to comment after any leading white space + @param comment_empty Whether to comment empty lines + + @returns Text with comments toggled + """ + if not comment_empty and not line.strip(): + return line + + spaces, content = split_leading_space(line) + + if content.startswith(comment): + return "{}{}".format(spaces, content[len(comment):]) + elif after_leading_space: + return "{}{}{}".format(spaces, comment, content) + else: + return "{}{}{}".format(comment, spaces, content) + +def toggle_lines(lines, **kwargs): + """ + @param lines Multiple lines of text + @type lines str + @returns Text with comments toggled + """ + lines = [toggle_line(line, **kwargs) for line in lines.split("\n")] + return "\n".join(lines) -- cgit v1.2.1