AssistantEric/Assistant.py

changeset 119
263a95431e41
parent 118
67d952a9036e
child 121
edad4b3132fd
equal deleted inserted replaced
118:67d952a9036e 119:263a95431e41
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 language = editor.getLanguage()
127 if language == "": 127 if language:
128 return 128 self.__apisManager.getAPIs(language)
129 self.__apisManager.getAPIs(language)
130 129
131 def __editorClosed(self, editor): 130 def __editorClosed(self, editor):
132 """ 131 """
133 Private slot called, when an editor was closed. 132 Private slot called, when an editor was closed.
134 133
136 """ 135 """
137 if editor in self.__editors: 136 if editor in self.__editors:
138 editor.editorSaved.disconnect( 137 editor.editorSaved.disconnect(
139 self.__apisManager.getAPIs(ApisNameProject).editorSaved) 138 self.__apisManager.getAPIs(ApisNameProject).editorSaved)
140 self.__editors.remove(editor) 139 self.__editors.remove(editor)
141 if editor.autoCompletionHook() == self.autocomplete: 140 try:
142 self.__unsetAutoCompletionHook(editor) 141 if editor.getCompletionListHook("Assistant"):
143 if editor.callTipHook() == self.calltips: 142 self.__unsetAutoCompletionHook(editor)
144 self.__unsetCalltipsHook(editor) 143 except AttributeError:
144 # old interface (before 6.1.0)
145 if editor.autoCompletionHook() == self.autocomplete:
146 self.__unsetAutoCompletionHook(editor)
147 try:
148 if editor.getCallTipHook("Assistant"):
149 self.__unsetCalltipsHook(editor)
150 except AttributeError:
151 # old interface (before 6.1.0)
152 if editor.callTipHook() == self.calltips:
153 self.__unsetCalltipsHook(editor)
145 154
146 def __preferencesChanged(self): 155 def __preferencesChanged(self):
147 """ 156 """
148 Private method to handle a change of the global configuration. 157 Private method to handle a change of the global configuration.
149 """ 158 """
150 self.__apisManager.reloadAPIs() 159 self.__apisManager.reloadAPIs()
151 160
152 def __getCharacter(self, pos, editor):
153 """
154 Private method to get the character to the left of the current position
155 in the current line.
156
157 @param pos position to get character at (integer)
158 @param editor reference to the editor object to work with
159 (QScintilla.Editor)
160 @return requested character or "", if there are no more (string) and
161 the next position (i.e. pos - 1)
162 """
163 if pos <= 0:
164 return "", pos
165
166 pos -= 1
167 ch = editor.charAt(pos)
168
169 # Don't go past the end of the previous line
170 if ch == '\n' or ch == '\r':
171 return "", pos
172
173 return ch, pos
174
175 ################################# 161 #################################
176 ## autocompletion methods below 162 ## auto-completion methods below
177 ################################# 163 #################################
178 164
179 def __completionListSelected(self, id, txt): 165 def __completionListSelected(self, id, txt):
180 """ 166 """
181 Private slot to handle the selection from the completion list. 167 Private slot to handle the selection from the completion list.
208 elif wLeft: 194 elif wLeft:
209 txt = txt[len(wLeft):] 195 txt = txt[len(wLeft):]
210 editor.insert(txt) 196 editor.insert(txt)
211 editor.setCursorPosition(line, col + len(txt)) 197 editor.setCursorPosition(line, col + len(txt))
212 198
199 def __recordSelectedContext(self, id, txt):
200 """
201 Private slot to handle the selection from the completion list to
202 record the selected completion context.
203
204 @param id the ID of the user list (should be 1) (integer)
205 @param txt the selected text (string)
206 """
207 from QScintilla.Editor import EditorAutoCompletionListID
208
209 if id == EditorAutoCompletionListID:
210 lst = txt.split()
211 if len(lst) > 1:
212 self.__lastFullContext = lst[1][1:].split(")")[0]
213 else:
214 self.__lastFullContext = None
215
213 def __setAutoCompletionHook(self, editor): 216 def __setAutoCompletionHook(self, editor):
214 """ 217 """
215 Private method to set the autocompletion hook. 218 Private method to set the autocompletion hook.
216 219
217 @param editor reference to the editor (QScintilla.Editor) 220 @param editor reference to the editor (QScintilla.Editor)
218 """ 221 """
219 editor.userListActivated.connect(self.__completionListSelected) 222 try:
220 editor.setAutoCompletionHook(self.autocomplete) 223 editor.userListActivated.connect(self.__recordSelectedContext)
224 editor.addCompletionListHook("Assistant", self.getCompletionsList)
225 except AttributeError:
226 # old interface (before 6.1.0)
227 editor.userListActivated.connect(self.__completionListSelected)
228 editor.setAutoCompletionHook(self.autocomplete)
221 229
222 def __unsetAutoCompletionHook(self, editor): 230 def __unsetAutoCompletionHook(self, editor):
223 """ 231 """
224 Private method to unset the autocompletion hook. 232 Private method to unset the autocompletion hook.
225 233
226 @param editor reference to the editor (QScintilla.Editor) 234 @param editor reference to the editor (QScintilla.Editor)
227 """ 235 """
228 editor.unsetAutoCompletionHook() 236 try:
229 editor.userListActivated.disconnect(self.__completionListSelected) 237 editor.userListActivated.disconnect(self.__recordSelectedContext)
238 editor.removeCompletionListHook("Assistant")
239 except AttributeError:
240 # old interface (before 6.1.0)
241 editor.unsetAutoCompletionHook()
242 editor.userListActivated.disconnect(self.__completionListSelected)
230 243
231 def autocomplete(self, editor, context): 244 def autocomplete(self, editor, context):
232 """ 245 """
233 Public method to determine the autocompletion proposals. 246 Public method to determine the autocompletion proposals.
234 247
695 """ 708 """
696 Private method to set the calltip hook. 709 Private method to set the calltip hook.
697 710
698 @param editor reference to the editor (QScintilla.Editor) 711 @param editor reference to the editor (QScintilla.Editor)
699 """ 712 """
700 editor.setCallTipHook(self.calltips) 713 try:
714 editor.addCallTipHook("Assistant", self.calltips)
715 except AttributeError:
716 # old interface (before 6.1.0)
717 editor.setCallTipHook(self.calltips)
701 718
702 def __unsetCalltipsHook(self, editor): 719 def __unsetCalltipsHook(self, editor):
703 """ 720 """
704 Private method to unset the calltip hook. 721 Private method to unset the calltip hook.
705 722
706 @param editor reference to the editor (QScintilla.Editor) 723 @param editor reference to the editor (QScintilla.Editor)
707 """ 724 """
708 editor.unsetCallTipHook() 725 try:
726 editor.removeCallTipHook("Assistant")
727 except AttributeError:
728 # old interface (before 6.1.0)
729 editor.unsetCallTipHook()
709 730
710 def calltips(self, editor, pos, commas): 731 def calltips(self, editor, pos, commas):
711 """ 732 """
712 Public method to return a list of calltips. 733 Public method to return a list of calltips.
713 734

eric ide

mercurial