~dreampie-devel/dreampie/trunk

« back to all changes in this revision

Viewing changes to dreampielib/gui/autocomplete.py

  • Committer: Noam Yorav-Raphael
  • Date: 2012-01-05 08:33:39 UTC
  • Revision ID: noamraph@gmail.com-20120105083339-f59y4peyp6r5x1ys
Calls to the subprocess will be canceled if a timeout has passed. This is needed when the subprocess is handling GUI events while idle.
Also, you can now press ctrl-c to break idle GUI handling.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
from .hyper_parser import HyperParser
24
24
from .autocomplete_window import AutocompleteWindow, find_prefix_range
25
 
from .common import beep, get_text
 
25
from .common import beep, get_text, TimeoutError
26
26
 
27
27
# This string includes all chars that may be in an identifier
28
28
ID_CHARS = string.ascii_letters + string.digits + "_"
29
29
ID_CHARS_DOT = ID_CHARS + '.'
30
30
 
31
31
class Autocomplete(object):
32
 
    def __init__(self, sourceview, window_main,
 
32
    def __init__(self, sourceview, window_main, get_is_executing,
33
33
                 complete_attributes, complete_firstlevels, get_func_args,
34
34
                 find_modules, get_module_members, complete_filenames,
35
35
                 complete_dict_keys,
36
36
                 INDENT_WIDTH):
37
37
        self.sourceview = sourceview
38
38
        self.sourcebuffer = sourceview.get_buffer()
 
39
        self.get_is_executing = get_is_executing
39
40
        self.complete_attributes = complete_attributes
40
41
        self.complete_firstlevels = complete_firstlevels
41
42
        self.get_func_args = get_func_args
50
51
 
51
52
    def show_completions(self, is_auto, complete):
52
53
        """
53
 
        If complete is False, just show the comopletion list.
 
54
        If complete is False, just show the completion list.
54
55
        If complete is True, complete as far as possible. If there's only
55
56
        one completion, don't show the window.
56
57
 
57
58
        If is_auto is True, don't beep if can't find completions.
58
59
        """
 
60
        if self.get_is_executing():
 
61
            if not is_auto:
 
62
                beep()
 
63
            return
 
64
        
59
65
        sb = self.sourcebuffer
60
66
        text = get_text(sb, sb.get_start_iter(), sb.get_end_iter())
61
67
        index = sb.get_iter_at_mark(sb.get_insert()).get_offset()
147
153
        if is_auto and '(' in comp_what:
148
154
            # Don't evaluate expressions which may contain a function call.
149
155
            return
150
 
        key_reprs = self.complete_dict_keys(comp_what)
151
 
        if key_reprs is None:
 
156
        try:
 
157
            key_reprs = self.complete_dict_keys(comp_what)
 
158
        except TimeoutError:
152
159
            return
153
160
        if text[index:index+1] != ']':
154
161
            key_reprs = [x+']' for x in key_reprs]
181
188
            if is_auto and '(' in comp_what:
182
189
                # Don't evaluate expressions which may contain a function call.
183
190
                return
184
 
            public_and_private = self.complete_attributes(comp_what)
185
 
            if public_and_private is None: # The subprocess is busy
 
191
            try:
 
192
                public, private = self.complete_attributes(comp_what)
 
193
            except TimeoutError:
186
194
                return
187
 
            public, private = public_and_private
188
195
        else:
189
 
            public_and_private = self.complete_firstlevels()
190
 
            if public_and_private is None: # The subprocess is busy
 
196
            try:
 
197
                public, private = self.complete_firstlevels()
 
198
            except TimeoutError:
191
199
                return
192
 
            public, private = public_and_private
193
200
            
194
201
            # If we are inside a function call after a ',' or '(',
195
202
            # get argument names.
200
207
                    expr = hp.get_expression()
201
208
                    if expr and '(' not in expr:
202
209
                        # Don't need to execute a function just to get arguments
203
 
                        args = self.get_func_args(expr)
204
 
                        if args is not None:
 
210
                        try:
 
211
                            args = self.get_func_args(expr)
 
212
                        except TimeoutError:
 
213
                            pass
 
214
                        else:
205
215
                            public.extend(args)
206
216
                            public.sort()
207
217
        
246
256
        else:
247
257
            comp_what = u''
248
258
        
249
 
        modules = self.find_modules(comp_what)
250
 
        if modules is None:
 
259
        try:
 
260
            modules = self.find_modules(comp_what)
 
261
        except TimeoutError:
251
262
            return None
252
263
        
253
264
        public = [s for s in modules if s[0] != '_']
276
287
            return
277
288
        comp_what = m.group(1)
278
289
        
279
 
        public_and_private = self.get_module_members(comp_what)
280
 
        if public_and_private is None:
 
290
        try:
 
291
            public, private = self.get_module_members(comp_what)
 
292
        except TimeoutError:
281
293
            return
282
 
        public, private = public_and_private
283
294
        is_case_insen = False
284
295
        return comp_prefix, public, private, is_case_insen
285
296
        
330
341
        
331
342
        add_quote = not (len(text) > index and text[index] == str_char)
332
343
        
333
 
        res = self.complete_filenames(
334
 
            str_prefix, text[str_start:comp_prefix_index], str_char, add_quote)
335
 
        if res is None:
 
344
        try:
 
345
            public, private, is_case_insen = self.complete_filenames(
 
346
                str_prefix, text[str_start:comp_prefix_index], str_char,
 
347
                add_quote)
 
348
        except TimeoutError:
336
349
            return
337
 
        public, private, is_case_insen = res
338
350
        
339
351
        return comp_prefix, public, private, is_case_insen
340
352