23 import rope.base.libutils |
23 import rope.base.libutils |
24 import rope.contrib.codeassist |
24 import rope.contrib.codeassist |
25 |
25 |
26 from PyQt5.QtCore import QObject |
26 from PyQt5.QtCore import QObject |
27 |
27 |
|
28 import Globals |
|
29 |
28 class CodeAssist(QObject): |
30 class CodeAssist(QObject): |
29 """ |
31 """ |
30 Class implementing the autocompletion interface to rope. |
32 Class implementing the autocompletion interface to rope. |
31 """ |
33 """ |
32 def __init__(self, refactoring, editor, plugin, parent = None): |
34 def __init__(self, plugin, parent = None): |
33 """ |
35 """ |
34 Constructor |
36 Constructor |
35 |
37 |
36 @param refactoring reference to the rope refactoring object |
|
37 (Refactoring) |
|
38 @param editor reference to the editor object, that called this method |
|
39 QScintilla.Editor) |
|
40 @param plugin reference to the plugin object |
38 @param plugin reference to the plugin object |
41 @param parent parent (QObject) |
39 @param parent parent (QObject) |
42 """ |
40 """ |
43 QObject.__init__(self, parent) |
41 QObject.__init__(self, parent) |
44 |
42 |
45 self.__refactoring = refactoring |
|
46 self.__editor = editor |
|
47 self.__plugin = plugin |
43 self.__plugin = plugin |
|
44 |
|
45 self.__project = rope.base.project.Project(Globals.getConfigDir()) |
|
46 self.__project.validate(self.__project.root) |
48 |
47 |
49 def getCompletions(self): |
48 def getCompletions(self, editor): |
50 """ |
49 """ |
51 Public method to calculate the possible completions. |
50 Public method to calculate the possible completions. |
52 |
51 |
|
52 @param editor reference to the editor object, that called this method |
|
53 QScintilla.Editor) |
53 @return list of proposals (QStringList) |
54 @return list of proposals (QStringList) |
54 """ |
55 """ |
55 project = self.__refactoring.getProject() |
56 filename = editor.getFileName() |
56 project.validate(project.root) |
|
57 filename = self.__editor.getFileName() |
|
58 if filename: |
57 if filename: |
59 resource = rope.base.libutils.path_to_resource(project, filename) |
58 resource = rope.base.libutils.path_to_resource( |
|
59 self.__project, filename) |
60 else: |
60 else: |
61 resource = None |
61 resource = None |
62 line, index = self.__editor.getCursorPosition() |
62 line, index = editor.getCursorPosition() |
63 source = self.__editor.text() |
63 source = editor.text() |
64 offset = len("".join(source.splitlines(True)[:line])) + index |
64 offset = len("".join(source.splitlines(True)[:line])) + index |
65 maxfixes = self.__plugin.getPreferences("MaxFixes") |
65 maxfixes = self.__plugin.getPreferences("MaxFixes") |
66 try: |
66 try: |
67 proposals = rope.contrib.codeassist.code_assist( |
67 proposals = rope.contrib.codeassist.code_assist( |
68 project, source, offset, resource, maxfixes = maxfixes) |
68 self.__project, source, offset, resource, maxfixes = maxfixes) |
69 proposals = rope.contrib.codeassist.sorted_proposals(proposals) |
69 proposals = rope.contrib.codeassist.sorted_proposals(proposals) |
70 names = [proposal.name for proposal in proposals] |
70 names = [proposal.name for proposal in proposals] |
71 return names |
71 return names |
72 except Exception: |
72 except Exception: |
73 return [] |
73 return [] |
74 |
74 |
75 def getCallTips(self, pos): |
75 def getCallTips(self, pos, editor): |
76 """ |
76 """ |
77 Public method to calculate calltips. |
77 Public method to calculate calltips. |
78 |
78 |
|
79 @param editor reference to the editor object, that called this method |
|
80 QScintilla.Editor) |
79 @param pos position in the text for the calltip (integer) |
81 @param pos position in the text for the calltip (integer) |
80 @return list of possible calltips (list of strings) |
82 @return list of possible calltips (list of strings) |
81 """ |
83 """ |
82 project = self.__refactoring.getProject() |
84 filename = editor.getFileName() |
83 project.validate(project.root) |
|
84 filename = self.__editor.getFileName() |
|
85 if filename: |
85 if filename: |
86 resource = rope.base.libutils.path_to_resource(project, filename) |
86 resource = rope.base.libutils.path_to_resource( |
|
87 self.__project, filename) |
87 else: |
88 else: |
88 resource = None |
89 resource = None |
89 source = self.__editor.text() |
90 source = editor.text() |
90 maxfixes = self.__plugin.getPreferences("CalltipsMaxFixes") |
91 maxfixes = self.__plugin.getPreferences("CalltipsMaxFixes") |
91 try: |
92 try: |
|
93 if pos >= len(source) or source[pos] != "(": |
|
94 offset = source.rindex("(", 0, pos) |
|
95 else: |
|
96 offset = pos |
92 cts = rope.contrib.codeassist.get_calltip( |
97 cts = rope.contrib.codeassist.get_calltip( |
93 project, source, pos, resource, maxfixes = maxfixes, |
98 self.__project, source, offset, resource, maxfixes = maxfixes, |
94 remove_self = True) |
99 remove_self = True) |
95 if cts is not None: |
100 if cts is not None: |
96 cts = [cts] |
101 cts = [cts] |
97 else: |
102 else: |
98 cts = [] |
103 cts = [] |
99 except Exception: |
104 except Exception: |
100 cts = [] |
105 cts = [] |
101 return cts |
106 return cts |
|
107 |
|
108 def reportChanged(self, filename, oldSource): |
|
109 """ |
|
110 Public slot to report some changed sources. |
|
111 |
|
112 @param filename file name of the changed source (string) |
|
113 @param oldSource source code before the change (string) |
|
114 """ |
|
115 import rope.base.libutils |
|
116 |
|
117 try: |
|
118 rope.base.libutils.report_change( |
|
119 self.__project, filename, oldSource) |
|
120 except RuntimeError: |
|
121 # this could come from trying to do PyQt4/PyQt5 mixed stuff |
|
122 # simply ignore it |
|
123 pass |