From 6c6dce47a1c1feb143e1edf5e1fbc40eb9076397 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 8 Jan 2015 06:15:41 +0530 Subject: [PATCH] Wire up the snippet manager Its implementation is not yet complete --- src/calibre/gui2/tweak_book/{ => editor}/snippets.py | 8 ++++---- src/calibre/gui2/tweak_book/editor/text.py | 4 ++++ 2 files changed, 8 insertions(+), 4 deletions(-) rename src/calibre/gui2/tweak_book/{ => editor}/snippets.py (98%) diff --git a/src/calibre/gui2/tweak_book/snippets.py b/src/calibre/gui2/tweak_book/editor/snippets.py similarity index 98% rename from src/calibre/gui2/tweak_book/snippets.py rename to src/calibre/gui2/tweak_book/editor/snippets.py index 8c725d18d1..c790ffef19 100644 --- a/src/calibre/gui2/tweak_book/snippets.py +++ b/src/calibre/gui2/tweak_book/editor/snippets.py @@ -23,7 +23,7 @@ SnipKey = namedtuple('SnipKey', 'trigger syntaxes') def snip_key(trigger, *syntaxes): if '*' in syntaxes: syntaxes = all_text_syntaxes - return SnipKey(trigger, frozenset(*syntaxes)) + return SnipKey(trigger, frozenset(syntaxes)) builtin_snippets = { # {{{ snip_key('<<', 'html', 'xml'): { @@ -234,7 +234,7 @@ def expand_template(editor, trigger, template, selected_text=''): left = right - string_length(trigger) text, tab_stops = parse_template(template) c.setPosition(left), c.setPosition(right, c.KeepAnchor), c.insertText(text) - editor_tab_stops = [EditorTabStop(c, ts) for ts in tab_stops] + editor_tab_stops = [EditorTabStop(c, ts) for ts in tab_stops.itervalues()] if selected_text: for ts in editor_tab_stops: @@ -273,10 +273,10 @@ class SnippetManager(QObject): self.active_templates.remove(template) return at - def handle_keypress(self, ev): + def handle_key_press(self, ev): editor = self.parent() if ev.key() == Qt.Key_Tab and ev.modifiers() & Qt.CTRL: - at = self.get_active_template(editor.cursor()) + at = self.get_active_template(editor.textCursor()) if at is not None: if at.jump_to_next(editor) is None: self.active_templates.remove(at) diff --git a/src/calibre/gui2/tweak_book/editor/text.py b/src/calibre/gui2/tweak_book/editor/text.py index 1c6e5ff3c2..a84a4f22d0 100644 --- a/src/calibre/gui2/tweak_book/editor/text.py +++ b/src/calibre/gui2/tweak_book/editor/text.py @@ -25,6 +25,7 @@ from calibre.gui2.tweak_book.editor import ( from calibre.gui2.tweak_book.editor.themes import get_theme, theme_color, theme_format from calibre.gui2.tweak_book.editor.syntax.base import SyntaxHighlighter from calibre.gui2.tweak_book.editor.smarts import NullSmarts +from calibre.gui2.tweak_book.editor.snippets import SnippetManager from calibre.spell.break_iterator import index_of from calibre.utils.icu import safe_chr, string_length, capitalize, upper, lower, swapcase from calibre.utils.titlecase import titlecase @@ -142,6 +143,7 @@ class TextEdit(PlainTextEdit): def __init__(self, parent=None, expected_geometry=(100, 50)): PlainTextEdit.__init__(self, parent) + self.snippet_manager = SnippetManager(self) self.completion_popup = CompletionPopup(self) self.request_completion = self.completion_doc_name = None self.clear_completion_cache_timer = t = QTimer(self) @@ -783,6 +785,8 @@ class TextEdit(PlainTextEdit): # https://bugreports.qt-project.org/browse/QTBUG-36281 ev.setAccepted(False) return + if self.snippet_manager.handle_key_press(ev): + return if self.smarts.handle_key_press(ev, self): self.handle_keypress_completion(ev) return