Source

thg-qt-mq / pygments-thread.diff

# HG changeset patch
# Parent b9509533f7710bc912ad8cdab5d3da31ae917cf0
diff --git a/tortoisehg/hgqt/commit.py b/tortoisehg/hgqt/commit.py
--- a/tortoisehg/hgqt/commit.py
+++ b/tortoisehg/hgqt/commit.py
@@ -100,6 +100,9 @@ class CommitWidget(QWidget):
         self.runner.makeLogVisible.connect(self.makeLogVisible)
         self.runner.commandFinished.connect(self.commandFinished)
 
+        self.pygments = _PygmentsThread(self)
+        self.pygments.done.connect(self.xxx)
+
         repo.configChanged.connect(self.configChanged)
         repo.repositoryChanged.connect(self.repositoryChanged)
         repo.workingBranchChanged.connect(self.workingBranchChanged)
@@ -182,20 +185,10 @@ class CommitWidget(QWidget):
         for wfile in self.stwidget.getChecked():
             tokens.add(wfile)
             tokens.add(os.path.basename(wfile))
-        try:
-            from pygments.lexers import guess_lexer_for_filename
-            from pygments.token import Token
-            from pygments.util import ClassNotFound
-            try:
-                contents = unicode(contents)
-                lexer = guess_lexer_for_filename(wfile, contents)
-                for tokentype, value in lexer.get_tokens(contents):
-                    if tokentype is Token.Name and len(value) > 4:
-                        tokens.add(value)
-            except ClassNotFound:
-                pass
-        except ImportError:
-            pass
+        self.pygments.start(wfile, contents)
+
+    @pyqtSlot(object)
+    def xxx(self, tokens):
         for n in sorted(list(tokens)):
             self._apis.add(n)
         self._apis.apiPreparationFinished.connect(self.apiPrepFinished)
@@ -633,6 +626,37 @@ class CommitWidget(QWidget):
             return
         return super(CommitWidget, self).keyPressEvent(event)
 
+class _PygmentsThread(QThread):
+    done = pyqtSignal(object)
+
+    def __init__(self, parent=None):
+        super(_PygmentsThread, self).__init__(parent)
+        self._tokens = set()
+
+    def start(self, wfile, contents):
+        self._wfile = unicode(wfile)
+        self._contents = unicode(contents)
+        self._tokens.clear()
+        super(_PygmentsThread, self).start()
+
+    def run(self):
+        assert self.currentThread() != qApp.thread()
+        try:
+            from pygments.lexers import guess_lexer_for_filename
+            from pygments.token import Token
+            from pygments.util import ClassNotFound
+            try:
+                lexer = guess_lexer_for_filename(self._wfile, self._contents)
+                for tokentype, value in lexer.get_tokens(self._contents):
+                    if tokentype is Token.Name and len(value) > 4:
+                        self._tokens.add(value)
+            except ClassNotFound:
+                pass
+        except ImportError:
+            pass
+        finally:
+            self.done.emit(self._tokens)
+
 class MessageHistoryCombo(QComboBox):
     def __init__(self, parent=None):
         QComboBox.__init__(self, parent)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.