AssistantEric/Assistant.py

branch
eric7
changeset 206
0e83bc0cc7fd
parent 201
1770e4b702a9
child 208
5c77d52e97cd
equal deleted inserted replaced
205:900e71dd7e81 206:0e83bc0cc7fd
6 """ 6 """
7 Module implementing the eric assistant, an alternative autocompletion and 7 Module implementing the eric assistant, an alternative autocompletion and
8 calltips system. 8 calltips system.
9 """ 9 """
10 10
11 import imp
12 import re 11 import re
13 12
14 from PyQt6.QtCore import QObject 13 from PyQt6.QtCore import QObject
15 14
16 from eric7.EricWidgets.EricApplication import ericApp 15 from eric7.EricWidgets.EricApplication import ericApp
16 from eric7.QScintilla.Editor import Editor
17 from eric7.Utilities.ModuleParser import PY_SOURCE, Module
17 18
18 from .APIsManager import APIsManager, ApisNameProject 19 from .APIsManager import APIsManager, ApisNameProject
19 20
20 AcsAPIs = 0x0001 21 AcsAPIs = 0x0001
21 AcsDocument = 0x0002 22 AcsDocument = 0x0002
48 49
49 self.__editors = [] 50 self.__editors = []
50 self.__lastContext = None 51 self.__lastContext = None
51 self.__lastFullContext = None 52 self.__lastFullContext = None
52 53
53 from eric7.QScintilla.Editor import Editor
54
55 self.__fromDocumentID = Editor.FromDocumentID 54 self.__fromDocumentID = Editor.FromDocumentID
56 55
57 def activate(self): 56 def activate(self):
58 """ 57 """
59 Public method to perform actions upon activation. 58 Public method to perform actions upon activation.
91 for editor in self.__editors[:]: 90 for editor in self.__editors[:]:
92 self.__editorClosed(editor) 91 self.__editorClosed(editor)
93 92
94 self.__apisManager.deactivate() 93 self.__apisManager.deactivate()
95 94
96 def setEnabled(self, key, enabled): 95 def setEnabled(
96 self,
97 key, # noqa: U100
98 enabled, # noqa: U100
99 ):
97 """ 100 """
98 Public method to enable or disable a feature. 101 Public method to enable or disable a feature.
99 102
100 @param key feature to set 103 @param key feature to set
101 @type str 104 @type str
239 projectType = self.__getProjectType(editor) 242 projectType = self.__getProjectType(editor)
240 243
241 line, col = editor.getCursorPosition() 244 line, col = editor.getCursorPosition()
242 sep = "" 245 sep = ""
243 if language and context: 246 if language and context:
244 wc = re.sub("\w", "", editor.wordCharacters()) 247 wc = re.sub(r"\w", "", editor.wordCharacters())
245 pat = re.compile("\w{0}".format(re.escape(wc))) 248 pat = re.compile(r"\w{0}".format(re.escape(wc)))
246 text = editor.text(line) 249 text = editor.text(line)
247 250
248 beg = text[:col] 251 beg = text[:col]
249 for wsep in editor.getLexer().autoCompletionWordSeparators(): 252 for wsep in editor.getLexer().autoCompletionWordSeparators():
250 if beg.endswith(wsep): 253 if beg.endswith(wsep):
296 col -= 1 299 col -= 1
297 if col > 0 and beg[col - 1] != " ": 300 if col > 0 and beg[col - 1] != " ":
298 col -= 1 301 col -= 1
299 prefix = editor.getWordLeft(line, col) 302 prefix = editor.getWordLeft(line, col)
300 if editor.isPyFile(): 303 if editor.isPyFile():
301 from eric7.Utilities.ModuleParser import Module
302
303 src = editor.text() 304 src = editor.text()
304 fn = editor.getFileName() 305 fn = editor.getFileName()
305 if fn is None: 306 if fn is None:
306 fn = "" 307 fn = ""
307 mod = Module("", fn, imp.PY_SOURCE) 308 mod = Module("", fn, PY_SOURCE)
308 mod.scan(src) 309 mod.scan(src)
309 310
310 importCompletion = False 311 importCompletion = False
311 if editor.isPyFile(): 312 if editor.isPyFile():
312 # check, if we are completing a from import statement 313 # check, if we are completing a from import statement
532 completionsList.remove(entry) 533 completionsList.remove(entry)
533 if completion["pictureId"]: 534 if completion["pictureId"]:
534 entry += "?{0}".format(completion["pictureId"]) 535 entry += "?{0}".format(completion["pictureId"])
535 else: 536 else:
536 cont = False 537 cont = False
537 regexp = re.compile(re.escape(entry) + "\?\d{,2}") 538 regexp = re.compile(re.escape(entry) + r"\?\d{,2}")
538 for comp in completionsList: 539 for comp in completionsList:
539 if regexp.fullmatch(comp): 540 if regexp.fullmatch(comp):
540 cont = True 541 cont = True
541 break 542 break
542 if cont: 543 if cont:
570 """ 571 """
571 completionsList = [] 572 completionsList = []
572 573
573 prefixFound = False 574 prefixFound = False
574 if prefix and module: 575 if prefix and module:
575 from eric7.QScintilla.Editor import Editor
576
577 line, col = editor.getCursorPosition() 576 line, col = editor.getCursorPosition()
578 if prefix in ["cls", "self"]: 577 if prefix in ["cls", "self"]:
579 prefixFound = True 578 prefixFound = True
580 for cl in module.classes.values(): 579 for cl in module.classes.values():
581 if line >= cl.lineno and ( 580 if line >= cl.lineno and (
802 return [] 801 return []
803 802
804 projectType = self.__getProjectType(editor) 803 projectType = self.__getProjectType(editor)
805 804
806 line, col = editor.lineIndexFromPosition(pos) 805 line, col = editor.lineIndexFromPosition(pos)
807 wc = re.sub("\w", "", editor.wordCharacters()) 806 wc = re.sub(r"\w", "", editor.wordCharacters())
808 pat = re.compile("\w{0}".format(re.escape(wc))) 807 pat = re.compile(r"\w{0}".format(re.escape(wc)))
809 text = editor.text(line) 808 text = editor.text(line)
810 while col > 0 and not pat.match(text[col - 1]): 809 while col > 0 and not pat.match(text[col - 1]):
811 col -= 1 810 col -= 1
812 word = editor.getWordLeft(line, col) 811 word = editor.getWordLeft(line, col)
813 812
824 col -= 1 823 col -= 1
825 if col >= 0: 824 if col >= 0:
826 col -= 1 825 col -= 1
827 prefix = editor.getWordLeft(line, col) 826 prefix = editor.getWordLeft(line, col)
828 if editor.isPyFile(): 827 if editor.isPyFile():
829 from eric7.Utilities.ModuleParser import Module
830
831 src = editor.text() 828 src = editor.text()
832 fn = editor.getFileName() 829 fn = editor.getFileName()
833 if fn is None: 830 if fn is None:
834 fn = "" 831 fn = ""
835 mod = Module("", fn, imp.PY_SOURCE) 832 mod = Module("", fn, PY_SOURCE)
836 mod.scan(src) 833 mod.scan(src)
837 834
838 apiCalltips = [] 835 apiCalltips = []
839 projectCalltips = [] 836 projectCalltips = []
840 documentCalltips = [] 837 documentCalltips = []

eric ide

mercurial