Anonymous avatar Anonymous committed f827923

Auto completing keywords and builtins

Comments (0)

Files changed (6)

 
 Version Overview
 ----------------
-  We've just started. Right now, rope is a basic editor. One of our goals
-was to develop rope using rope and for achieving this we've implemented
-a basic editor as our first task. We've already started using rope. From
-now on we will move toward the promised features like auto-completion.
 
 
 Keybinding
+- Auto completing keywords and builtins : May 19, 2006
+
+
+- Auto-complete imported objects : May 19, 2006
+
+
 - Show searching status in the status bar : May 18, 2006
 
 
 * Writing ASTs @ 4
 
 
-* Auto-complete imported objects @ 2
+* Auto-complete from-import imported objects @ 2
+
+
+* Auto-completing self. @ 2
 
 
 * Proposing templates in auto-completion proposals @ 2
   * Inserting common prefixes
 
 
-* Auto completing keywords and builtins @ 1
-
-
-* Auto-complete local variable names @ 3
-
-
 * Enhancing auto-completion @ 2
   * What if the statement in current line is spread in more than one line
   * Complete as you type (updating proposal list while typing)
   * What to do when the program has syntax errors
 
 
+* Auto-complete local variable names @ 3
+
+
 > Public release 0.2pre : May 20, 2006
 
 
-* Auto-complete imported objects @ 1
-
-
 --- Remaining Stories ---
 ? Change program goals and description; principles.html
   ? Go toward refactoring and ... library rather than an IDE
 ? roadmap.html
+? Migrating to sourceforge SVN before 0.2 release
+    ? SF repository; SVN or CVS? How to synchronize local SVN with SF?
 ? Should workingon.txt be under version control?
 ? Functional tests
 ? Separate domain and presentation everywhere

docs/workingon.txt

-*** Auto-complete imported objects @ 1 ***
+- Not proposing builtins and keywords when starting is empty
+- Should we have both function and method?
 
-- import name
-- import name as name
-
-* from mod import name
-* from mod import name as name
-* from mod import *
+? Sorting the proposals
+? What to do after dots?
+? Completions are context dependant
 
 ? Separating ui modules and packages
     ? Decide which modules are the domain and which are the presentation
     ? Should editing tools access Editor directly? Which of them?
-? Specifing the type of each story; UI or Core
-? The connection between ASTs and Type Hierarchies
+    ? Specifing the type of each story; UI or Core
+? The connection between ASTs, module hierarchies and type databases
 * Better font selection on windows
-? Think about a library for functional testing
-? SF repository; SVN or CVS? How to synchronize local SVN with SF?

rope/codeassist.py

 import compiler
+import inspect
+import __builtin__
 
 from rope.exceptions import RopeException
 
 
 
 class CodeAssist(ICodeAssist):
+    def __init__(self):
+        self.builtins = [str(name) for name in dir(__builtin__)
+                         if not name.startswith('_')]
+        import keyword
+        self.keywords = keyword.kwlist
+
     def _find_starting_offset(self, source_code, offset):
         current_offset = offset - 1
         while current_offset >= 0 and (source_code[current_offset].isalnum() or
         if line_beginning != -1 and line_beginning < line_ending - 1:
             result = source_code[:line_beginning] + '#' + source_code[line_beginning + 2:]
         return result
+    
+    def _get_matching_builtins(self, starting):
+        result = {}
+        for builtin in self.builtins:
+            if builtin.startswith(starting):
+                obj = getattr(__builtin__, builtin)
+                kind = 'unknown'
+                if inspect.isclass(obj):
+                    kind = 'class'
+                if inspect.isbuiltin(obj):
+                    kind = 'builtin_function'
+                if inspect.ismodule(obj):
+                    kind = 'module'
+                if inspect.ismethod(obj):
+                    kind = 'method'
+                if inspect.isfunction(obj):
+                    kind = 'function'
+                result[builtin] = CompletionProposal(builtin, kind)
+        return result
+
+    def _get_matching_keywords(self, starting):
+        result = {}
+        for kw in self.keywords:
+            if kw.startswith(starting):
+                result[kw] = CompletionProposal(kw, 'keyword')
+        return result
 
     def complete_code(self, source_code, offset):
         if offset > len(source_code):
         visitor = _GlobalVisitor(starting)
         compiler.walk(code_ast, visitor)
         result.update(visitor.result)
+        if len(starting) > 0:
+            result.update(self._get_matching_builtins(starting))
+            result.update(self._get_matching_keywords(starting))
         return CompletionResult(result.values(), starting_offset, offset)
 

ropetest/codeassisttest.py

         result = self.assist.complete_code(code, len(code))
         self.assert_proposal_not_in_result('sys', 'module', result)
 
+    def test_including_matching_builtins_types(self):
+        code = 'my_var = Excep'
+        result = self.assist.complete_code(code, len(code))
+        self.assert_proposal_in_result('Exception', 'class', result)
         
+    def test_including_matching_builtins_functions(self):
+        code = 'my_var = zi'
+        result = self.assist.complete_code(code, len(code))
+        self.assert_proposal_in_result('zip', 'builtin_function', result)
+        
+    def test_including_keywords(self):
+        code = 'fo'
+        result = self.assist.complete_code(code, len(code))
+        self.assert_proposal_in_result('for', 'keyword', result)
+
 
 if __name__ == '__main__':
     unittest.main()
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.