AssistantEric/Assistant.py

changeset 131
7d868e8e1cfb
parent 122
d746710bfe2e
child 132
eb12cd27384f
equal deleted inserted replaced
130:08c7aece376c 131:7d868e8e1cfb
121 editor.editorSaved.connect( 121 editor.editorSaved.connect(
122 self.__apisManager.getAPIs(ApisNameProject).editorSaved) 122 self.__apisManager.getAPIs(ApisNameProject).editorSaved)
123 self.__editors.append(editor) 123 self.__editors.append(editor)
124 124
125 # preload the api to give the manager a chance to prepare the database 125 # preload the api to give the manager a chance to prepare the database
126 language = editor.getLanguage() 126 try:
127 language = editor.getApiLanguage()
128 except AttributeError:
129 # backward compatibility
130 language = editor.apiLanguage
127 if language: 131 if language:
128 self.__apisManager.getAPIs(language) 132 projectType = self.__getProjectType(editor)
133 self.__apisManager.getAPIs(language, projectType=projectType)
129 134
130 def __editorClosed(self, editor): 135 def __editorClosed(self, editor):
131 """ 136 """
132 Private slot called, when an editor was closed. 137 Private slot called, when an editor was closed.
133 138
156 """ 161 """
157 Private method to handle a change of the global configuration. 162 Private method to handle a change of the global configuration.
158 """ 163 """
159 self.__apisManager.reloadAPIs() 164 self.__apisManager.reloadAPIs()
160 165
166 def __getProjectType(self, editor):
167 """
168 Private method to determine the project type to be used.
169
170 @param editor reference to the editor to check
171 @type Editor
172 @return project type
173 @rtype str
174 """
175 filename = editor.getFileName()
176 if self.__project.isOpen() and filename and \
177 self.__project.isProjectFile(filename):
178 projectType = self.__project.getProjectType()
179 else:
180 projectType = ""
181
182 return projectType
183
161 ################################# 184 #################################
162 ## auto-completion methods below 185 ## auto-completion methods below
163 ################################# 186 #################################
164 187
165 def __completionListSelected(self, id, txt): 188 def __completionListSelected(self, id, txt):
267 @param editor reference to the editor object, that called this method 290 @param editor reference to the editor object, that called this method
268 (QScintilla.Editor) 291 (QScintilla.Editor)
269 @param context flag indicating to autocomplete a context (boolean) 292 @param context flag indicating to autocomplete a context (boolean)
270 @return list of possible completions (list of strings) 293 @return list of possible completions (list of strings)
271 """ 294 """
272 language = editor.getLanguage() 295 try:
273 if language == "": 296 language = editor.getApiLanguage()
274 return [] 297 except AttributeError:
298 # backward compatibility
299 language = editor.apiLanguage
300
301 completeFromDocumentOnly = False
302 if language in ["", "Guessed"] or language.startswith("Pygments|"):
303 if self.__plugin.getPreferences("AutoCompletionSource") & \
304 AcsDocument:
305 completeFromDocumentOnly = True
306 else:
307 return []
308
309 projectType = self.__getProjectType(editor)
275 310
276 line, col = editor.getCursorPosition() 311 line, col = editor.getCursorPosition()
277 self.__completingContext = context 312 self.__completingContext = context
278 sep = "" 313 sep = ""
279 if context: 314 if context:
372 if word == tokens[2]: 407 if word == tokens[2]:
373 word = "" 408 word = ""
374 409
375 if word or importCompletion: 410 if word or importCompletion:
376 completionsList = self.__getCompletions( 411 completionsList = self.__getCompletions(
377 word, context, prefix, language, mod, editor, 412 word, context, prefix, language, projectType, mod, editor,
378 importCompletion, sep) 413 importCompletion, completeFromDocumentOnly, sep)
379 if len(completionsList) == 0 and prefix: 414 if len(completionsList) == 0 and prefix:
380 # searching with prefix didn't return anything, try without 415 # searching with prefix didn't return anything, try without
381 completionsList = self.__getCompletions( 416 completionsList = self.__getCompletions(
382 word, context, "", language, mod, editor, importCompletion, 417 word, context, "", language, projectType, mod, editor,
383 sep) 418 importCompletion, completeFromDocumentOnly, sep)
384 return completionsList 419 return completionsList
385 420
386 return [] 421 return []
387 422
388 def __getCompletions(self, word, context, prefix, language, module, editor, 423 def __getCompletions(self, word, context, prefix, language, projectType,
389 importCompletion, sep): 424 module, editor, importCompletion, documentOnly, sep):
390 """ 425 """
391 Private method to get the list of possible completions. 426 Private method to get the list of possible completions.
392 427
393 @param word word (or wordpart) to complete (string) 428 @param word word (or wordpart) to complete (string)
394 @param context flag indicating to autocomplete a context (boolean) 429 @param context flag indicating to autocomplete a context (boolean)
395 @param prefix prefix of the word to be completed (string) 430 @param prefix prefix of the word to be completed (string)
396 @param language programming language of the source (string) 431 @param language programming language of the source (string)
432 @param projectType type of the project (string)
397 @param module reference to the scanned module info (Module) 433 @param module reference to the scanned module info (Module)
398 @param editor reference to the editor object (QScintilla.Editor.Editor) 434 @param editor reference to the editor object (QScintilla.Editor.Editor)
399 @param importCompletion flag indicating an import completion (boolean) 435 @param importCompletion flag indicating an import completion (boolean)
436 @param documentOnly flag indicating to complete from the document only
437 (boolean)
400 @param sep separator string (string) 438 @param sep separator string (string)
401 @return list of possible completions (list of strings) 439 @return list of possible completions (list of strings)
402 """ 440 """
403 apiCompletionsList = [] 441 apiCompletionsList = []
404 docCompletionsList = [] 442 docCompletionsList = []
405 projectCompletionList = [] 443 projectCompletionList = []
406 444
407 if self.__plugin.getPreferences("AutoCompletionSource") & AcsAPIs: 445 if not documentOnly:
408 api = self.__apisManager.getAPIs(language) 446 if self.__plugin.getPreferences("AutoCompletionSource") & AcsAPIs:
409 apiCompletionsList = self.__getApiCompletions( 447 api = self.__apisManager.getAPIs(
410 api, word, context, prefix, module, editor) 448 language, projectType=projectType)
411 449 apiCompletionsList = self.__getApiCompletions(
412 if self.__plugin.getPreferences("AutoCompletionSource") & AcsProject: 450 api, word, context, prefix, module, editor)
413 api = self.__apisManager.getAPIs(ApisNameProject) 451
414 projectCompletionList = self.__getApiCompletions( 452 if self.__plugin.getPreferences("AutoCompletionSource") & \
415 api, word, context, prefix, module, editor) 453 AcsProject:
454 api = self.__apisManager.getAPIs(ApisNameProject)
455 projectCompletionList = self.__getApiCompletions(
456 api, word, context, prefix, module, editor)
416 457
417 if self.__plugin.getPreferences("AutoCompletionSource") & AcsDocument \ 458 if self.__plugin.getPreferences("AutoCompletionSource") & AcsDocument \
418 and not importCompletion: 459 and not importCompletion:
419 docCompletionsList = self.__getDocumentCompletions( 460 docCompletionsList = self.__getDocumentCompletions(
420 editor, word, context, sep, prefix, module) 461 editor, word, context, sep, prefix, module)
736 @param pos position in the text for the calltip (integer) 777 @param pos position in the text for the calltip (integer)
737 @param commas minimum number of commas contained in the calltip 778 @param commas minimum number of commas contained in the calltip
738 (integer) 779 (integer)
739 @return list of possible calltips (list of strings) 780 @return list of possible calltips (list of strings)
740 """ 781 """
741 language = editor.getLanguage() 782 try:
742 if language == "": 783 language = editor.getApiLanguage()
743 return 784 except AttributeError:
785 # backward compatibility
786 language = editor.apiLanguage
787
788 completeFromDocumentOnly = False
789 if language in ["", "Guessed"] or language.startswith("Pygments|"):
790 if self.__plugin.getPreferences("AutoCompletionSource") & \
791 AcsDocument:
792 completeFromDocumentOnly = True
793 else:
794 return []
795
796 projectType = self.__getProjectType(editor)
744 797
745 line, col = editor.lineIndexFromPosition(pos) 798 line, col = editor.lineIndexFromPosition(pos)
746 wc = re.sub("\w", "", editor.wordCharacters()) 799 wc = re.sub("\w", "", editor.wordCharacters())
747 pat = re.compile("\w{0}".format(re.escape(wc))) 800 pat = re.compile("\w{0}".format(re.escape(wc)))
748 text = editor.text(line) 801 text = editor.text(line)
776 829
777 apiCalltips = [] 830 apiCalltips = []
778 projectCalltips = [] 831 projectCalltips = []
779 documentCalltips = [] 832 documentCalltips = []
780 833
781 if self.__plugin.getPreferences("AutoCompletionSource") & AcsAPIs: 834 if not completeFromDocumentOnly:
782 api = self.__apisManager.getAPIs(language) 835 if self.__plugin.getPreferences("AutoCompletionSource") & AcsAPIs:
783 if api is not None: 836 api = self.__apisManager.getAPIs(
784 apiCalltips = self.__getApiCalltips( 837 language, projectType=projectType)
838 if api is not None:
839 apiCalltips = self.__getApiCalltips(
840 api, word, commas, prefix, mod, editor)
841
842 if self.__plugin.getPreferences("AutoCompletionSource") & \
843 AcsProject:
844 api = self.__apisManager.getAPIs(ApisNameProject)
845 projectCalltips = self.__getApiCalltips(
785 api, word, commas, prefix, mod, editor) 846 api, word, commas, prefix, mod, editor)
786
787 if self.__plugin.getPreferences("AutoCompletionSource") & AcsProject:
788 api = self.__apisManager.getAPIs(ApisNameProject)
789 projectCalltips = self.__getApiCalltips(
790 api, word, commas, prefix, mod, editor)
791 847
792 if self.__plugin.getPreferences("AutoCompletionSource") & AcsDocument: 848 if self.__plugin.getPreferences("AutoCompletionSource") & AcsDocument:
793 documentCalltips = self.__getDocumentCalltips( 849 documentCalltips = self.__getDocumentCalltips(
794 word, prefix, mod, editor) 850 word, prefix, mod, editor)
795 851

eric ide

mercurial