Commits

Ali Gholami Rudi  committed 53478d4

Making Highlighting independent of Editor

  • Participants
  • Parent commits 7757b89

Comments (0)

Files changed (4)

File docs/workingon.txt

 * Use Modified event for updating highlighting only when necessary
 
 Possible solutions
-* Making Highlighting independent of the editor
 * Adding Highlighting.get_highlighting_region(text, index)
-* Pushing up Highlighting.highlights()
-* Experiment about memory usage of Text.get(START, END)
+- Making Highlighting independent of the editor
+- Pushing up Highlighting.highlights()
+- Experiment about memory usage of Text.get(START, END)
   
 
 

File rope/editor.py

     def add_modification_observer(self, observer):
         self.modification_observers.append(observer)
 
-    def _highlight_range(self, startIndex, endIndex):
+    def _highlight_range(self, start_index, end_index):
         for style in self.highlighting.get_styles().keys():
-            self.text.tag_remove(style, startIndex, endIndex)
-        for start, end, kind in self.highlighting.highlights(self, 
-                                                             GraphicalTextIndex(self, startIndex),
-                                                             GraphicalTextIndex(self, endIndex)):
-            self.text.tag_add(kind, start._getIndex(), end._getIndex())
+            self.text.tag_remove(style, start_index, end_index)
+        start_offset = self._get_offset(start_index)
+        end_offset = self._get_offset(end_index)
+        for start, end, kind in self.highlighting.highlights(self.get_text(),
+                                                             start_offset,
+                                                             end_offset):
+            tag_start = '1.0 +%dc' % start
+            tag_end = '1.0 +%dc' % end
+            self.text.tag_add(kind, tag_start, tag_end)
 
     def _bind_keys(self):
         self.text.bind('<Alt-f>', lambda event: self.next_word())
         return self._get_line_from_index(INSERT)
 
     def get_current_offset(self):
-        result = self._get_column_from_index(INSERT)
-        current_line = self._get_line_from_index(INSERT)
+        return self._get_offset(INSERT)
+    
+    def _get_offset(self, index):
+        result = self._get_column_from_index(index)
+        current_line = self._get_line_from_index(index)
         current_pos = '1.0 lineend'
         for x in range(current_line - 1):
             result += self._get_column_from_index(current_pos) + 1
             current_pos = str(self.text.index(current_pos + ' +1l lineend'))
         return result
-
+    
     def set_status_bar_manager(self, manager):
         self.status_bar_manager = manager
 

File rope/highlight.py

     def get_styles(self):
         """Returns the dictionary of styles used in highlighting texts by this highlighting"""
 
-    def highlights(self, editor, start, end):
+    def highlights(self, text, start, end):
         """Generates highlighted ranges as (start, end, style) tuples"""
-        text = editor.get(start, end)
-        for match in self._get_pattern().finditer(text):
+        if end == None:
+            end = len(text)
+        for match in self._get_pattern().finditer(text[start:end]):
             for key, value in match.groupdict().items():
                 if value:
                     a, b = match.span(key)
-                    yield (editor.get_relative(start, a),
-                           editor.get_relative(start, b), key)
+#                    print a, b, key
+                    yield (start + a,
+                           start + b, key)
     
     def _get_pattern(self):
         if not self.pattern:
         interpreted_pattern = '(?P<interpreted>`[^`]+`)(?P<role>:\\w+:)?'
         hyperlink_target_pattern = '(?P<hyperlink_target>\\w+://[^\\s]+)'
         hyperlink_pattern = '(?P<hyperlink>[\\w]+_|`[^`]+`_)\\b'
-        hyperlink_definition_pattern = '(?P<hyperlink_definition>\\.\\. _[^\n:]+:)'
+        hyperlink_definition_pattern = '(?P<hyperlink_definition>\\.\\. _([^`\n:]|`.+`)+:)'
         field_pattern = '^\\s*(?P<field>:[^\n:]+:)'
-        all_patterns = literal_pattern + '|' + hyperlink_pattern + '|' + \
+        escaped_pattern = '(?P<escaped>\\\\.)'
+        all_patterns = literal_pattern + '|' + escaped_pattern + '|' + hyperlink_pattern + '|' + \
                        interpreted_pattern + '|' + \
                        title_pattern + '|' + listsign_pattern + '|' + \
                        directive_pattern + '|' + emphasis_pattern + '|' + \
                 'hyperlink_target' : HighlightingStyle(color='blue'),
                 'hyperlink' : HighlightingStyle(color='blue'),
                 'hyperlink_definition' : HighlightingStyle(color='blue'),
-                'field' : HighlightingStyle(color='purple')}
+                'field' : HighlightingStyle(color='purple'),
+                'escaped' : HighlightingStyle()}
 

File ropetest/highlighttest.py

 import unittest
 
-from ropetest.mockeditortest import GraphicalEditorFactory, MockEditorFactory
 from rope.highlight import PythonHighlighting, HighlightingStyle, ReSTHighlighting
 
 class HighlightTest(unittest.TestCase):
 
-    __factory = MockEditorFactory()
-    
     def setUp(self):
-        self.editor = self.__factory.create()
         self.highlighting = PythonHighlighting()
         unittest.TestCase.setUp(self)
     
         unittest.TestCase.tearDown(self)
 
     def _assertOutcomesEquals(self, text, expected, not_expected=[], start=None, end=None):
-        self.editor.set_text(text)
-        startIndex = self.editor.get_start()
-        if start is not None:
-            startIndex = self.editor.get_index(start)
-        endIndex = self.editor.get_end()
-        if end is not None:
-            endIndex = self.editor.get_index(end)
+        if start is None:
+            start = 0
+        if end is None:
+            end = len(text)
         highlights = []
-        for result in self.highlighting.highlights(self.editor, startIndex, endIndex):
+        for result in self.highlighting.highlights(text, start, end):
             highlights.append(result)
         for highlight in expected:
-            current = (self.editor.get_index(highlight[0]),
-                       self.editor.get_index(highlight[1]), highlight[2])
-            self.assertTrue(current in highlights)
+            self.assertTrue(highlight in highlights)
         for highlight in not_expected:
-            current = (self.editor.get_index(highlight[0]),
-                       self.editor.get_index(highlight[1]), highlight[2])
-            self.assertTrue(current not in highlights)
+            self.assertTrue(highlight not in highlights)
 
     def testKeywordHighlighting(self):
         text = 'def sample_function():\n    pass\n'
         noHigh = NoHighlighting()
         text = 'def sample_function():\n    pass\n'
         expected = []
-        for result in noHigh.highlights(self.editor, None, None):
+        for result in noHigh.highlights(text, None, None):
             self.assertEquals(expected[0], result)
             del expected[0]
         self.assertFalse(expected)
 
 class ReSTHighlightTest(unittest.TestCase):
 
-    __factory = MockEditorFactory()
-    
     def setUp(self):
-        self.editor = self.__factory.create()
         self.highlighting = ReSTHighlighting()
         unittest.TestCase.setUp(self)
     
         unittest.TestCase.tearDown(self)
     
     def in_highlights(self, text, expected):
-        self.editor.set_text(text)
-        start = self.editor.get_start()
-        end = self.editor.get_end()
         highlights = []
-        for result in self.highlighting.highlights(self.editor, start, end):
+        for result in self.highlighting.highlights(text, 0, len(text)):
             highlights.append(result)
-        expected = (self.editor.get_index(expected[0]), self.editor.get_index(expected[1]),
-                    expected[2])
         return expected in highlights
     
     def test_highlighting_section_titles(self):
         self.assertTrue('field' in self.highlighting.get_styles())
         self.assertTrue(self.in_highlights(':Age: 3 months', (0, 5, 'field')))
 
+    def test_escaping(self):
+        self.assertFalse(self.in_highlights('\\`Age\\` 3 months', (1, 7, 'interpreted')))
+
 
 def suite():
     result = unittest.TestSuite()