|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2008 - 2015 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the autocompletion interface to rope. |
|
8 """ |
|
9 |
|
10 import os |
|
11 import sys |
|
12 |
|
13 sys.path.insert(0, os.path.dirname(__file__)) |
|
14 if sys.version_info[0] >= 3: |
|
15 path = os.path.join(os.path.dirname(__file__), 'rope_py3') |
|
16 else: |
|
17 path = os.path.join(os.path.dirname(__file__), 'rope_py2') |
|
18 str = unicode # __IGNORE_WARNING__ |
|
19 sys.path.insert(0, path) |
|
20 |
|
21 import rope.base.libutils |
|
22 import rope.contrib.codeassist |
|
23 |
|
24 from PyQt5.QtCore import QObject |
|
25 #from PyQt5.QtGui import * |
|
26 |
|
27 class CodeAssist(QObject): |
|
28 """ |
|
29 Class implementing the autocompletion interface to rope. |
|
30 """ |
|
31 def __init__(self, refactoring, editor, plugin, parent = None): |
|
32 """ |
|
33 Constructor |
|
34 |
|
35 @param refactoring reference to the rope refactoring object |
|
36 (Refactoring) |
|
37 @param editor reference to the editor object, that called this method |
|
38 QScintilla.Editor) |
|
39 @param plugin reference to the plugin object |
|
40 @param parent parent (QObject) |
|
41 """ |
|
42 QObject.__init__(self, parent) |
|
43 |
|
44 self.__refactoring = refactoring |
|
45 self.__editor = editor |
|
46 self.__plugin = plugin |
|
47 |
|
48 def getCompletions(self): |
|
49 """ |
|
50 Public method to calculate the possible completions. |
|
51 |
|
52 @return list of proposals (QStringList) |
|
53 """ |
|
54 project = self.__refactoring.getProject() |
|
55 project.validate(project.root) |
|
56 filename = self.__editor.getFileName() |
|
57 if filename: |
|
58 resource = rope.base.libutils.path_to_resource(project, filename) |
|
59 else: |
|
60 resource = None |
|
61 line, index = self.__editor.getCursorPosition() |
|
62 offset = self.__editor.positionFromLineIndex(line, index) |
|
63 source = self.__editor.text() |
|
64 maxfixes = self.__plugin.getPreferences("MaxFixes") |
|
65 try: |
|
66 proposals = rope.contrib.codeassist.code_assist( |
|
67 project, source, offset, resource, maxfixes = maxfixes) |
|
68 proposals = rope.contrib.codeassist.sorted_proposals(proposals) |
|
69 names = [proposal.name for proposal in proposals] |
|
70 return names |
|
71 except Exception: |
|
72 return [] |
|
73 |
|
74 def getCallTips(self, pos): |
|
75 """ |
|
76 Public method to calculate calltips. |
|
77 |
|
78 @param pos position in the text for the calltip (integer) |
|
79 @return list of possible calltips (list of strings) |
|
80 """ |
|
81 project = self.__refactoring.getProject() |
|
82 project.validate(project.root) |
|
83 filename = self.__editor.getFileName() |
|
84 if filename: |
|
85 resource = rope.base.libutils.path_to_resource(project, filename) |
|
86 else: |
|
87 resource = None |
|
88 source = self.__editor.text() |
|
89 maxfixes = self.__plugin.getPreferences("CalltipsMaxFixes") |
|
90 try: |
|
91 cts = rope.contrib.codeassist.get_calltip( |
|
92 project, source, pos, resource, maxfixes = maxfixes, |
|
93 remove_self = True) |
|
94 if cts is not None: |
|
95 cts = [cts] |
|
96 else: |
|
97 cts = [] |
|
98 except Exception: |
|
99 cts = [] |
|
100 return cts |