Anonymous avatar Anonymous committed cda8433

Pushing up Highlighting.highlights

Comments (0)

Files changed (4)

docs/workingon.txt

 * 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)
   
 
 
 
 Small Features For 0.2 Release
 ==============================
-- Numerical lists
-- Fields
 * Better color selection for reST highlighting
+* Better change editor dialog
 
 
 
         fileMenu.add_command(label='Open Project ...',
                              command=self._open_project_dialog, underline=6)
         fileMenu.add_command(label='Close Project',
-                             command=self.close_project, underline=3)
+                             command=self.close_project, underline=1)
         fileMenu.add_separator()
         fileMenu.add_command(label='New File ...',
                              command=self._create_new_file_dialog, underline=0)
         fileMenu.add_command(label='Open File ...',
                              command=self._open_file_dialog, underline=0)
         fileMenu.add_separator()
+        fileMenu.add_command(label='Save Editor',
+                             command=self.save_file, underline=0)
+        fileMenu.add_command(label='Close Editor',
+                             command=self.close_active_editor, underline=0)
+        fileMenu.add_separator()
         fileMenu.add_command(label='Exit',
                              command=self.exit, underline=1)
         

rope/highlight.py

 
 
 class Highlighting(object):
+    
+    def __init__(self):
+        self.pattern = None
 
     def get_styles(self):
         """Returns the dictionary of styles used in highlighting texts by this highlighting"""
 
-    def highlights(self, editor, startIndex, endIndex):
+    def highlights(self, editor, start, end):
         """Generates highlighted ranges as (start, end, style) tuples"""
+        text = editor.get(start, end)
+        for match in self._get_pattern().finditer(text):
+            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)
+    
+    def _get_pattern(self):
+        if not self.pattern:
+            self.pattern = self._make_pattern()
+        return self.pattern
+    
+    def _make_pattern(self):
+        """Returns highlighting patterns"""
 
 
 class HighlightingStyle(object):
 
 class PythonHighlighting(Highlighting):
 
-    def __init__(self):
-        kw = r"\b" + PythonHighlighting.any("keyword", keyword.kwlist) + r"\b"
+    @staticmethod
+    def any(name, list):
+        return "(?P<%s>" % name + "|".join(list) + ")"
+    
+    def _make_pattern(self):
+        keyword_pattern = r"\b" + PythonHighlighting.any("keyword", keyword.kwlist) + r"\b"
         import __builtin__
         builtinlist = [str(name) for name in dir(__builtin__)
                        if not name.startswith('_')]
-        builtin = r"([^.'\"\\]\b|^)" + PythonHighlighting.any("builtin", builtinlist) + r"\b"
-        comment = PythonHighlighting.any("comment", [r"#[^\n]*"])
+        builtin_pattern = r"([^.'\"\\]\b|^)" + \
+                          PythonHighlighting.any("builtin", builtinlist) + r"\b"
+        comment_pattern = PythonHighlighting.any("comment", [r"#[^\n]*"])
         sqstring = r"(\b[rR])?'[^'\\\n]*(\\.[^'\\\n]*)*'?"
         dqstring = r'(\b[rR])?"[^"\\\n]*(\\.[^"\\\n]*)*"?'
         sq3string = r"(\b[rR])?'''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?"
         dq3string = r'(\b[rR])?"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?'
-        string = PythonHighlighting.any("string", [sq3string, dq3string, sqstring, dqstring])
-        self.pattern = re.compile(kw + "|" + builtin + '|' + comment + "|" + string, re.S)
-
-    @staticmethod
-    def any(name, list):
-        return "(?P<%s>" % name + "|".join(list) + ")"
+        string_pattern = PythonHighlighting.any("string", [sq3string, dq3string, sqstring, dqstring])
+        definition_pattern = r'^\s*(?P<defkeyword>def|class)\s+(?P<definition>\w+)'
+        all_patterns = definition_pattern + '|' + keyword_pattern + '|' + \
+                       builtin_pattern + '|' + comment_pattern + '|' + string_pattern
+        return re.compile(all_patterns, re.S | re.M)
 
     def get_styles(self):
         return {'keyword': HighlightingStyle(color='blue', bold=True),
+                'defkeyword': HighlightingStyle(color='blue', bold=True),
                 'string' : HighlightingStyle(color='#004080'),
                 'comment' : HighlightingStyle(color='#008000', italic=True),
                 'builtin' : HighlightingStyle(color='#908080'),
                 'definition' : HighlightingStyle(color='purple', bold=True)}
 
-    def highlights(self, editor, start, end):
-        text = editor.get(start, end)
-        for match in self.pattern.finditer(text):
-            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)
-                    if value in ("def", "class"):
-                        idprog = re.compile(r"\s+(\w+)", re.S)
-                        m1 = idprog.match(text, b)
-                        if m1:
-                            a, b = m1.span(1)
-                            yield (editor.get_relative(start, a),
-                                   editor.get_relative(start, b), 'definition')
-
 
 class NoHighlighting(Highlighting):
 
         if False:
             yield None
 
+
 class ReSTHighlighting(Highlighting):
     
-    def __init__(self):
-        self.pattern = None
-        
-    def _get_pattern(self):
-        if not self.pattern:
-            self.pattern = self._make_pattern()
-        return self.pattern
-    
     def _make_pattern(self):
         title_pattern = '(?P<overline>^(([^\\w\\s\\d]+)\n))?' + \
                         '(?P<title>.+)\n' + \
         emphasis_pattern = '(?P<emphasis>\\*[^*\n]+\\*)'
         strongemphasis_pattern = '(?P<strongemphasis>\\*\\*[^*\n]+\\*\\*)'
         literal_pattern = '(?P<literal>``[^`]+``)'
-        interpreted_pattern = '(?P<interpreted>`.+`)(?P<role>:\\w+:)?'
+        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' : HighlightingStyle(color='blue'),
                 'field' : HighlightingStyle(color='purple')}
 
-    def highlights(self, editor, start, end):
-        text = editor.get(start, end)
-        for match in self._get_pattern().finditer(text):
-            for key, value in match.groupdict().items():
-                if value:
-                    a, b = match.span(key)
-#                    print a, b, key
-                    yield (editor.get_relative(start, a),
-                           editor.get_relative(start, b), key)
-

ropetest/highlighttest.py

 
     def testKeywordHighlighting(self):
         text = 'def sample_function():\n    pass\n'
-        highs = [(0, 3, 'keyword'), (27, 31, 'keyword')]
+        highs = [(0, 3, 'defkeyword'), (27, 31, 'keyword')]
         self._assertOutcomesEquals(text, highs)
 
     def testKeywordHighlighting2(self):
-        text = 'import re\nclass Test(object):    def f(self):\npass\n'
-        highs = [(0, 6, 'keyword'), (10, 15, 'keyword'),
-                 (33, 36, 'keyword'), (46, 50, 'keyword')]
+        text = 'import re\nclass Test(object):\n    def f(self):\npass\n'
+        highs = [(0, 6, 'keyword'), (10, 15, 'defkeyword'),
+                 (34, 37, 'defkeyword'), (47, 51, 'keyword')]
         self._assertOutcomesEquals(text, highs)
 
     def testKeywordHighlighting3(self):
 
     def test_function_definition(self):
         text = 'def func(args):'
-        highs = [(0, 3, 'keyword'), (4, 8, 'definition')]
+        highs = [(0, 3, 'defkeyword'), (4, 8, 'definition')]
         self._assertOutcomesEquals(text, highs)
         
     def test_class_definition(self):
         self.assertTrue('definition' in self.highlighting.get_styles())
         text = 'class Sample(object):'
-        highs = [(0, 5, 'keyword'), (6, 12, 'definition')]
+        highs = [(0, 5, 'defkeyword'), (6, 12, 'definition')]
         self._assertOutcomesEquals(text, highs)
 
     def test_comments(self):
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.