diff options
Diffstat (limited to 'plugins/snippets/snippets/Helper.py')
-rw-r--r--[-rwxr-xr-x] | plugins/snippets/snippets/Helper.py | 244 |
1 files changed, 122 insertions, 122 deletions
diff --git a/plugins/snippets/snippets/Helper.py b/plugins/snippets/snippets/Helper.py index d8a19673..6d440d03 100755..100644 --- a/plugins/snippets/snippets/Helper.py +++ b/plugins/snippets/snippets/Helper.py @@ -23,164 +23,164 @@ import re from gi.repository import Gtk def message_dialog(par, typ, msg): - d = Gtk.MessageDialog(par, Gtk.DialogFlags.MODAL, typ, Gtk.ButtonsType.OK, msg) - d.set_property('use-markup', True) + d = Gtk.MessageDialog(par, Gtk.DialogFlags.MODAL, typ, Gtk.ButtonsType.OK, msg) + d.set_property('use-markup', True) - d.run() - d.destroy() + d.run() + d.destroy() def compute_indentation(view, piter): - line = piter.get_line() - start = view.get_buffer().get_iter_at_line(line) - end = start.copy() - + line = piter.get_line() + start = view.get_buffer().get_iter_at_line(line) + end = start.copy() + + ch = end.get_char() + + while (ch.isspace() and ch != '\r' and ch != '\n' and \ + end.compare(piter) < 0): + if not end.forward_char(): + break; + ch = end.get_char() - - while (ch.isspace() and ch != '\r' and ch != '\n' and \ - end.compare(piter) < 0): - if not end.forward_char(): - break; - - ch = end.get_char() - - if start.equal(end): - return '' - - return start.get_slice(end) + + if start.equal(end): + return '' + + return start.get_slice(end) def markup_escape(text): - return saxutils.escape(text) + return saxutils.escape(text) def spaces_instead_of_tabs(view, text): - if not view.get_insert_spaces_instead_of_tabs(): - return text + if not view.get_insert_spaces_instead_of_tabs(): + return text - return text.replace("\t", view.get_tab_width() * ' ') + return text.replace("\t", view.get_tab_width() * ' ') def insert_with_indent(view, piter, text, indentfirst = True, context = None): - text = spaces_instead_of_tabs(view, text) - lines = text.split('\n') - buf = view.get_buffer() + text = spaces_instead_of_tabs(view, text) + lines = text.split('\n') + buf = view.get_buffer() + + buf._snippets_context = context - buf._snippets_context = context + if len(lines) == 1: + view.get_buffer().insert(piter, text) + else: + # Compute indentation + indent = compute_indentation(view, piter) + text = '' - if len(lines) == 1: - view.get_buffer().insert(piter, text) - else: - # Compute indentation - indent = compute_indentation(view, piter) - text = '' + for i in range(0, len(lines)): + if indentfirst or i > 0: + text += indent + lines[i] + '\n' + else: + text += lines[i] + '\n' - for i in range(0, len(lines)): - if indentfirst or i > 0: - text += indent + lines[i] + '\n' - else: - text += lines[i] + '\n' - - buf.insert(piter, text[:-1]) + buf.insert(piter, text[:-1]) - buf._snippets_context = None + buf._snippets_context = None def get_buffer_context(buf): - if hasattr(buf, "_snippets_context"): - return buf._snippets_context - return None + if hasattr(buf, "_snippets_context"): + return buf._snippets_context + return None def snippets_debug(*s): - return + return def write_xml(node, f, cdata_nodes=()): - assert node is not None + assert node is not None - if not hasattr(f, "write"): - f = open(f, "wb") + if not hasattr(f, "write"): + f = open(f, "wb") - # Encoding - f.write("<?xml version='1.0' encoding='utf-8'?>\n") + # Encoding + f.write("<?xml version='1.0' encoding='utf-8'?>\n") - _write_node(node, f, cdata_nodes) + _write_node(node, f, cdata_nodes) def _write_indent(file, text, indent): - file.write(' ' * indent + text) + file.write(' ' * indent + text) def _write_node(node, file, cdata_nodes=(), indent=0): - # write XML to file - tag = node.tag - - if node is Comment: - _write_indent(file, "<!-- %s -->\n" % saxutils.escape(node.text.encode('utf-8')), indent) - elif node is ProcessingInstruction: - _write_indent(file, "<?%s?>\n" % saxutils.escape(node.text.encode('utf-8')), indent) - else: - items = node.items() - - if items or node.text or len(node): - _write_indent(file, "<" + tag.encode('utf-8'), indent) - - if items: - items.sort() # lexical order - for k, v in items: - file.write(" %s=%s" % (k.encode('utf-8'), saxutils.quoteattr(v.encode('utf-8')))) - if node.text or len(node): - file.write(">") - if node.text and node.text.strip() != "": - if tag in cdata_nodes: - file.write(_cdata(node.text)) - else: - file.write(saxutils.escape(node.text.encode('utf-8'))) - else: - file.write("\n") - - for n in node: - _write_node(n, file, cdata_nodes, indent + 1) - - if not len(node): - file.write("</" + tag.encode('utf-8') + ">\n") - else: - _write_indent(file, "</" + tag.encode('utf-8') + ">\n", \ - indent) - else: - file.write(" />\n") - - if node.tail and node.tail.strip() != "": - file.write(saxutils.escape(node.tail.encode('utf-8'))) + # write XML to file + tag = node.tag + + if node is Comment: + _write_indent(file, "<!-- %s -->\n" % saxutils.escape(node.text.encode('utf-8')), indent) + elif node is ProcessingInstruction: + _write_indent(file, "<?%s?>\n" % saxutils.escape(node.text.encode('utf-8')), indent) + else: + items = node.items() + + if items or node.text or len(node): + _write_indent(file, "<" + tag.encode('utf-8'), indent) + + if items: + items.sort() # lexical order + for k, v in items: + file.write(" %s=%s" % (k.encode('utf-8'), saxutils.quoteattr(v.encode('utf-8')))) + if node.text or len(node): + file.write(">") + if node.text and node.text.strip() != "": + if tag in cdata_nodes: + file.write(_cdata(node.text)) + else: + file.write(saxutils.escape(node.text.encode('utf-8'))) + else: + file.write("\n") + + for n in node: + _write_node(n, file, cdata_nodes, indent + 1) + + if not len(node): + file.write("</" + tag.encode('utf-8') + ">\n") + else: + _write_indent(file, "</" + tag.encode('utf-8') + ">\n", \ + indent) + else: + file.write(" />\n") + + if node.tail and node.tail.strip() != "": + file.write(saxutils.escape(node.tail.encode('utf-8'))) def _cdata(text, replace=string.replace): - text = text.encode('utf-8') - return '<![CDATA[' + replace(text, ']]>', ']]]]><![CDATA[>') + ']]>' + text = text.encode('utf-8') + return '<![CDATA[' + replace(text, ']]>', ']]]]><![CDATA[>') + ']]>' def buffer_word_boundary(buf): - iter = buf.get_iter_at_mark(buf.get_insert()) - start = iter.copy() - - if not iter.starts_word() and (iter.inside_word() or iter.ends_word()): - start.backward_word_start() - - if not iter.ends_word() and iter.inside_word(): - iter.forward_word_end() - - return (start, iter) + iter = buf.get_iter_at_mark(buf.get_insert()) + start = iter.copy() + + if not iter.starts_word() and (iter.inside_word() or iter.ends_word()): + start.backward_word_start() + + if not iter.ends_word() and iter.inside_word(): + iter.forward_word_end() + + return (start, iter) def buffer_line_boundary(buf): - iter = buf.get_iter_at_mark(buf.get_insert()) - start = iter.copy() - start.set_line_offset(0) - - if not iter.ends_line(): - iter.forward_to_line_end() - - return (start, iter) + iter = buf.get_iter_at_mark(buf.get_insert()) + start = iter.copy() + start.set_line_offset(0) + + if not iter.ends_line(): + iter.forward_to_line_end() + + return (start, iter) def drop_get_uris(selection): - uris = [] - if selection.targets_include_uri(): - data = selection.get_data() - lines = re.split('\\s*[\\n\\r]+\\s*', data.strip()) + uris = [] + if selection.targets_include_uri(): + data = selection.get_data() + lines = re.split('\\s*[\\n\\r]+\\s*', data.strip()) - for line in lines: - if not line.startswith('#'): - uris.append(line) + for line in lines: + if not line.startswith('#'): + uris.append(line) - return uris + return uris -# ex:ts=8:et: +# ex:ts=4:et: |