AssistantEric/Assistant.py

changeset 140
a0ea7418d433
parent 136
5cfe53b474a9
child 141
ecbf4f8b3a1b
equal deleted inserted replaced
139:fc0bf084f32a 140:a0ea7418d433
17 17
18 from E5Gui.E5Application import e5App 18 from E5Gui.E5Application import e5App
19 19
20 from .APIsManager import APIsManager, ApisNameProject 20 from .APIsManager import APIsManager, ApisNameProject
21 21
22 import Preferences
23
24 AcsAPIs = 0x0001 22 AcsAPIs = 0x0001
25 AcsDocument = 0x0002 23 AcsDocument = 0x0002
26 AcsProject = 0x0004 24 AcsProject = 0x0004
27 AcsOther = 0x1000
28 25
29 26
30 class Assistant(QObject): 27 class Assistant(QObject):
31 """ 28 """
32 Class implementing the autocompletion and calltips system. 29 Class implementing the autocompletion and calltips system.
47 self.__pluginManager = e5App().getObject("PluginManager") 44 self.__pluginManager = e5App().getObject("PluginManager")
48 45
49 self.__apisManager = APIsManager(self.__ui, self) 46 self.__apisManager = APIsManager(self.__ui, self)
50 47
51 self.__editors = [] 48 self.__editors = []
52 self.__completingContext = False
53 self.__lastContext = None 49 self.__lastContext = None
54 self.__lastFullContext = None 50 self.__lastFullContext = None
55 51
56 from QScintilla.Editor import Editor 52 from QScintilla.Editor import Editor
57 self.__fromDocumentID = Editor.FromDocumentID 53 self.__fromDocumentID = Editor.FromDocumentID
124 120
125 # preload the api to give the manager a chance to prepare the database 121 # preload the api to give the manager a chance to prepare the database
126 try: 122 try:
127 language = editor.getApiLanguage() 123 language = editor.getApiLanguage()
128 except AttributeError: 124 except AttributeError:
129 # backward compatibility 125 # backward compatibility < 16.12
130 language = editor.apiLanguage 126 language = editor.apiLanguage
131 if language: 127 if language:
132 projectType = self.__getProjectType(editor) 128 projectType = self.__getProjectType(editor)
133 self.__apisManager.getAPIs(language, projectType=projectType) 129 self.__apisManager.getAPIs(language, projectType=projectType)
134 130
140 """ 136 """
141 if editor in self.__editors: 137 if editor in self.__editors:
142 editor.editorSaved.disconnect( 138 editor.editorSaved.disconnect(
143 self.__apisManager.getAPIs(ApisNameProject).editorSaved) 139 self.__apisManager.getAPIs(ApisNameProject).editorSaved)
144 self.__editors.remove(editor) 140 self.__editors.remove(editor)
145 try: 141 if editor.getCompletionListHook("Assistant"):
146 if editor.getCompletionListHook("Assistant"): 142 self.__unsetAutoCompletionHook(editor)
147 self.__unsetAutoCompletionHook(editor) 143 if editor.getCallTipHook("Assistant"):
148 except AttributeError: 144 self.__unsetCalltipsHook(editor)
149 # old interface (before 6.1.0)
150 if editor.autoCompletionHook() == self.autocomplete:
151 self.__unsetAutoCompletionHook(editor)
152 try:
153 if editor.getCallTipHook("Assistant"):
154 self.__unsetCalltipsHook(editor)
155 except AttributeError:
156 # old interface (before 6.1.0)
157 if editor.callTipHook() == self.calltips:
158 self.__unsetCalltipsHook(editor)
159 145
160 def __preferencesChanged(self): 146 def __preferencesChanged(self):
161 """ 147 """
162 Private method to handle a change of the global configuration. 148 Private method to handle a change of the global configuration.
163 """ 149 """
183 169
184 ################################# 170 #################################
185 ## auto-completion methods below 171 ## auto-completion methods below
186 ################################# 172 #################################
187 173
188 def __completionListSelected(self, userListId, txt):
189 """
190 Private slot to handle the selection from the completion list.
191
192 @param userListId the ID of the user list (should be 1) (integer)
193 @param txt the selected text (string)
194 """
195 from QScintilla.Editor import EditorAutoCompletionListID
196
197 editor = self.sender()
198 if userListId == EditorAutoCompletionListID:
199 lst = txt.split()
200 if len(lst) > 1:
201 txt = lst[0]
202 self.__lastFullContext = lst[1][1:].split(")")[0]
203 else:
204 self.__lastFullContext = None
205
206 if Preferences.getEditor("AutoCompletionReplaceWord"):
207 editor.selectCurrentWord()
208 editor.removeSelectedText()
209 line, col = editor.getCursorPosition()
210 else:
211 line, col = editor.getCursorPosition()
212 wLeft = editor.getWordLeft(line, col)
213 if not txt.startswith(wLeft):
214 editor.selectCurrentWord()
215 editor.removeSelectedText()
216 line, col = editor.getCursorPosition()
217 elif wLeft:
218 txt = txt[len(wLeft):]
219 editor.insert(txt)
220 editor.setCursorPosition(line, col + len(txt))
221
222 def __recordSelectedContext(self, userListId, txt): 174 def __recordSelectedContext(self, userListId, txt):
223 """ 175 """
224 Private slot to handle the selection from the completion list to 176 Private slot to handle the selection from the completion list to
225 record the selected completion context. 177 record the selected completion context.
226 178
240 """ 192 """
241 Private method to set the autocompletion hook. 193 Private method to set the autocompletion hook.
242 194
243 @param editor reference to the editor (QScintilla.Editor) 195 @param editor reference to the editor (QScintilla.Editor)
244 """ 196 """
245 try: 197 editor.userListActivated.connect(self.__recordSelectedContext)
246 editor.userListActivated.connect(self.__recordSelectedContext) 198 editor.addCompletionListHook("Assistant", self.getCompletionsList)
247 editor.addCompletionListHook("Assistant", self.getCompletionsList)
248 except AttributeError:
249 # old interface (before 6.1.0)
250 editor.userListActivated.connect(self.__completionListSelected)
251 editor.setAutoCompletionHook(self.autocomplete)
252 199
253 def __unsetAutoCompletionHook(self, editor): 200 def __unsetAutoCompletionHook(self, editor):
254 """ 201 """
255 Private method to unset the autocompletion hook. 202 Private method to unset the autocompletion hook.
256 203
257 @param editor reference to the editor (QScintilla.Editor) 204 @param editor reference to the editor (QScintilla.Editor)
258 """ 205 """
259 try: 206 editor.userListActivated.disconnect(self.__recordSelectedContext)
260 editor.userListActivated.disconnect(self.__recordSelectedContext) 207 editor.removeCompletionListHook("Assistant")
261 editor.removeCompletionListHook("Assistant")
262 except AttributeError:
263 # old interface (before 6.1.0)
264 editor.unsetAutoCompletionHook()
265 editor.userListActivated.disconnect(self.__completionListSelected)
266
267 def autocomplete(self, editor, context):
268 """
269 Public method to determine the autocompletion proposals.
270
271 @param editor reference to the editor object, that called this method
272 (QScintilla.Editor)
273 @param context flag indicating to autocomplete a context (boolean)
274 """
275 from QScintilla.Editor import EditorAutoCompletionListID
276
277 if editor.isListActive():
278 editor.cancelList()
279
280 completionsList = self.getCompletionsList(editor, context)
281 if len(completionsList) > 0:
282 completionsList.sort()
283 editor.showUserList(EditorAutoCompletionListID,
284 completionsList)
285 208
286 def getCompletionsList(self, editor, context): 209 def getCompletionsList(self, editor, context):
287 """ 210 """
288 Public method to get a list of possible completions. 211 Public method to get a list of possible completions.
289 212
293 @return list of possible completions (list of strings) 216 @return list of possible completions (list of strings)
294 """ 217 """
295 try: 218 try:
296 language = editor.getApiLanguage() 219 language = editor.getApiLanguage()
297 except AttributeError: 220 except AttributeError:
298 # backward compatibility 221 # backward compatibility < 16.12
299 language = editor.apiLanguage 222 language = editor.apiLanguage
300 223
301 completeFromDocumentOnly = False 224 completeFromDocumentOnly = False
302 if language in ["", "Guessed"] or language.startswith("Pygments|"): 225 if language in ["", "Guessed"] or language.startswith("Pygments|"):
303 if self.__plugin.getPreferences("AutoCompletionSource") & \ 226 if self.__plugin.getPreferences("AutoCompletionSource") & \
307 return [] 230 return []
308 231
309 projectType = self.__getProjectType(editor) 232 projectType = self.__getProjectType(editor)
310 233
311 line, col = editor.getCursorPosition() 234 line, col = editor.getCursorPosition()
312 self.__completingContext = context
313 sep = "" 235 sep = ""
314 if language and context: 236 if language and context:
315 wc = re.sub("\w", "", editor.wordCharacters()) 237 wc = re.sub("\w", "", editor.wordCharacters())
316 pat = re.compile("\w{0}".format(re.escape(wc))) 238 pat = re.compile("\w{0}".format(re.escape(wc)))
317 text = editor.text(line) 239 text = editor.text(line)
752 """ 674 """
753 Private method to set the calltip hook. 675 Private method to set the calltip hook.
754 676
755 @param editor reference to the editor (QScintilla.Editor) 677 @param editor reference to the editor (QScintilla.Editor)
756 """ 678 """
757 try: 679 editor.addCallTipHook("Assistant", self.calltips)
758 editor.addCallTipHook("Assistant", self.calltips)
759 except AttributeError:
760 # old interface (before 6.1.0)
761 editor.setCallTipHook(self.calltips)
762 680
763 def __unsetCalltipsHook(self, editor): 681 def __unsetCalltipsHook(self, editor):
764 """ 682 """
765 Private method to unset the calltip hook. 683 Private method to unset the calltip hook.
766 684
767 @param editor reference to the editor (QScintilla.Editor) 685 @param editor reference to the editor (QScintilla.Editor)
768 """ 686 """
769 try: 687 editor.removeCallTipHook("Assistant")
770 editor.removeCallTipHook("Assistant")
771 except AttributeError:
772 # old interface (before 6.1.0)
773 editor.unsetCallTipHook()
774 688
775 def calltips(self, editor, pos, commas): 689 def calltips(self, editor, pos, commas):
776 """ 690 """
777 Public method to return a list of calltips. 691 Public method to return a list of calltips.
778 692
783 @return list of possible calltips (list of strings) 697 @return list of possible calltips (list of strings)
784 """ 698 """
785 try: 699 try:
786 language = editor.getApiLanguage() 700 language = editor.getApiLanguage()
787 except AttributeError: 701 except AttributeError:
788 # backward compatibility 702 # backward compatibility < 16.12
789 language = editor.apiLanguage 703 language = editor.apiLanguage
790 704
791 completeFromDocumentOnly = False 705 completeFromDocumentOnly = False
792 if language in ["", "Guessed"] or language.startswith("Pygments|"): 706 if language in ["", "Guessed"] or language.startswith("Pygments|"):
793 if self.__plugin.getPreferences("AutoCompletionSource") & \ 707 if self.__plugin.getPreferences("AutoCompletionSource") & \

eric ide

mercurial