| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent acde8ab commit f1edce9
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,7 +25,7 @@ | |||
| 25 | 25 | import __builtin__ | |
| 26 | 26 | import __main__ | |
| 27 | 27 | import rlcompleter | |
| 28 | - import line | ||
| 28 | + import line as lineparts | ||
| 29 | 29 | import re | |
| 30 | 30 | import os | |
| 31 | 31 | from glob import glob | |
@@ -210,6 +210,10 @@ def get_completer(cursor_offset, current_line, locals_, argspec, config, magic_m | |||
| 210 | 210 | if matches is not None: | |
| 211 | 211 | return sorted(set(matches)), FilenameCompletion | |
| 212 | 212 | ||
| 213 | + matches = DictKeyCompletion.matches(cursor_offset, current_line, locals_=locals_, config=config) | ||
| 214 | + if matches is not None: | ||
| 215 | + return sorted(set(matches)), DictKeyCompletion | ||
| 216 | + | ||
| 213 | 217 | matches = AttrCompletion.matches(cursor_offset, current_line, locals_=locals_, config=config) | |
| 214 | 218 | if matches is not None: | |
| 215 | 219 | cw = AttrCompletion.locate(cursor_offset, current_line)[2] | |
@@ -264,18 +268,18 @@ def substitute(cls, cursor_offset, line, match): | |||
| 264 | 268 | ||
| 265 | 269 | class ImportCompletion(BaseCompletionType): | |
| 266 | 270 | matches = staticmethod(importcompletion.complete) | |
| 267 | - locate = staticmethod(line.current_word) | ||
| 271 | + locate = staticmethod(lineparts.current_word) | ||
| 268 | 272 | format = staticmethod(after_last_dot) | |
| 269 | 273 | ||
| 270 | 274 | class FilenameCompletion(BaseCompletionType): | |
| 271 | 275 | shown_before_tab = False | |
| 272 | 276 | @classmethod | |
| 273 | 277 | def matches(cls, cursor_offset, current_line): | |
| 274 | - cs = line.current_string(cursor_offset, current_line) | ||
| 278 | + cs = lineparts.current_string(cursor_offset, current_line) | ||
| 275 | 279 | if cs is None: | |
| 276 | 280 | return None | |
| 277 | 281 | return filename_matches(cs[2]) | |
| 278 | - locate = staticmethod(line.current_string) | ||
| 282 | + locate = staticmethod(lineparts.current_string) | ||
| 279 | 283 | format = staticmethod(last_part_of_filename) | |
| 280 | 284 | ||
| 281 | 285 | class AttrCompletion(BaseCompletionType): | |
@@ -292,9 +296,25 @@ def matches(cls, cursor_offset, line, locals_, config): | |||
| 292 | 296 | # possibly be raised here, so if anyone wants to do that, feel free to send me | |
| 293 | 297 | # a patch. XXX: Make sure you raise here if you're debugging the completion | |
| 294 | 298 | # stuff ! | |
| 295 | - e = True | ||
| 296 | - raise | ||
| 297 | - else: | ||
| 298 | - e = False | ||
| 299 | - locate = staticmethod(line.current_word) | ||
| 299 | + pass | ||
| 300 | + return None | ||
| 301 | + locate = staticmethod(lineparts.current_word) | ||
| 300 | 302 | format = staticmethod(after_last_dot) | |
| 303 | + | ||
| 304 | + class DictKeyCompletion(BaseCompletionType): | ||
| 305 | + locate = staticmethod(lineparts.current_dict_key) | ||
| 306 | + @classmethod | ||
| 307 | + def matches(cls, cursor_offset, line, locals_, config): | ||
| 308 | + r = cls.locate(cursor_offset, line) | ||
| 309 | + if r is None: | ||
| 310 | + return None | ||
| 311 | + start, end, orig = r | ||
| 312 | + _, _, dexpr = lineparts.current_dict(cursor_offset, line) | ||
| 313 | + obj = eval(dexpr, locals_) | ||
| 314 | + if obj and isinstance(obj, type({})) and obj.keys(): | ||
| 315 | + return ["{!r}]".format(k) for k in obj.keys() if repr(k).startswith(orig)] | ||
| 316 | + else: | ||
| 317 | + return [] | ||
| 318 | + @classmethod | ||
| 319 | + def format(cls, match): | ||
| 320 | + return match[:-1] | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1436,7 +1436,12 @@ def tab(self, back=False): | |||
| 1436 | 1436 | ||
| 1437 | 1437 | # 3. check to see if we can expand the current word | |
| 1438 | 1438 | if self.matches_iter.is_cseq(): | |
| 1439 | - self.cursor_offset, self.s = self.matches_iter.substitute_cseq() | ||
| 1439 | + #TODO resolve this error-prone situation: | ||
| 1440 | + # can't assign at same time to self.s and self.cursor_offest | ||
| 1441 | + # because for cursor_offset | ||
| 1442 | + # property to work correctly, self.s must already be set | ||
| 1443 | + temp_cursor_offset, self.s = self.matches_iter.substitute_cseq() | ||
| 1444 | + self.cursor_offset = temp_cursor_offset | ||
| 1440 | 1445 | self.print_line(self.s) | |
| 1441 | 1446 | if not self.matches_iter: | |
| 1442 | 1447 | self.complete() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -738,7 +738,7 @@ def paint(self, about_to_exit=False, user_quit=False): | |||
| 738 | 738 | visible_space_below = min_height - current_line_end_row - 1 | |
| 739 | 739 | ||
| 740 | 740 | info_max_rows = max(visible_space_above, visible_space_below) | |
| 741 | - infobox = paint.paint_infobox(info_max_rows, int(width * self.config.cli_suggestion_width), self.matches_iter.matches, self.argspec, self.current_match, self.docstring, self.config) | ||
| 741 | + infobox = paint.paint_infobox(info_max_rows, int(width * self.config.cli_suggestion_width), self.matches_iter.matches, self.argspec, self.current_match, self.docstring, self.config, self.matches_iter.completer.format) | ||
| 742 | 742 | ||
| 743 | 743 | if visible_space_above >= infobox.height and self.config.curtsies_list_above: | |
| 744 | 744 | arr[current_line_start_row - infobox.height:current_line_start_row, 0:infobox.width] = infobox | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -39,22 +39,17 @@ def paint_current_line(rows, columns, current_display_line): | |||
| 39 | 39 | lines = display_linize(current_display_line, columns, True) | |
| 40 | 40 | return fsarray(lines, width=columns) | |
| 41 | 41 | ||
| 42 | - def matches_lines(rows, columns, matches, current, config): | ||
| 42 | + def matches_lines(rows, columns, matches, current, config, format): | ||
| 43 | 43 | highlight_color = func_for_letter(config.color_scheme['operator'].lower()) | |
| 44 | 44 | ||
| 45 | 45 | if not matches: | |
| 46 | 46 | return [] | |
| 47 | 47 | color = func_for_letter(config.color_scheme['main']) | |
| 48 | 48 | max_match_width = max(len(m) for m in matches) | |
| 49 | 49 | words_wide = max(1, (columns - 1) // (max_match_width + 1)) | |
| 50 | - if os.path.sep in matches[0]: # filename completion | ||
| 51 | - pass | ||
| 52 | - elif '.' in matches[0]: | ||
| 53 | - matches = [m.rstrip('.').rsplit('.')[-1] for m in matches] | ||
| 54 | - if current: | ||
| 55 | - current = current.rstrip('.').rsplit('.')[-1] | ||
| 56 | - else: | ||
| 57 | - pass | ||
| 50 | + matches = [format(m) for m in matches] | ||
| 51 | + if current: | ||
| 52 | + current = format(current) | ||
| 58 | 53 | ||
| 59 | 54 | matches_lines = [fmtstr(' ').join(color(m.ljust(max_match_width)) | |
| 60 | 55 | if m != current | |
@@ -147,13 +142,13 @@ def formatted_docstring(docstring, columns, config): | |||
| 147 | 142 | return sum(([color(x) for x in (display_linize(line, columns) if line else fmtstr(''))] | |
| 148 | 143 | for line in docstring.split('\n')), []) | |
| 149 | 144 | ||
| 150 | - def paint_infobox(rows, columns, matches, argspec, match, docstring, config): | ||
| 145 | + def paint_infobox(rows, columns, matches, argspec, match, docstring, config, format): | ||
| 151 | 146 | """Returns painted completions, argspec, match, docstring etc.""" | |
| 152 | 147 | if not (rows and columns): | |
| 153 | 148 | return fsarray(0, 0) | |
| 154 | 149 | width = columns - 4 | |
| 155 | 150 | lines = ((formatted_argspec(argspec, width, config) if argspec else []) + | |
| 156 | - (matches_lines(rows, width, matches, match, config) if matches else []) + | ||
| 151 | + (matches_lines(rows, width, matches, match, config, format) if matches else []) + | ||
| 157 | 152 | (formatted_docstring(docstring, width, config) if docstring else [])) | |
| 158 | 153 | ||
| 159 | 154 | output_lines = [] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -308,8 +308,6 @@ def cur_line(self): | |||
| 308 | 308 | ||
| 309 | 309 | def substitute(self, match): | |
| 310 | 310 | """Returns a cursor offset and line with match substituted in""" | |
| 311 | - if match.startswith("'"): | ||
| 312 | - raise ValueError(match) | ||
| 313 | 311 | start, end, word = self.completer.locate(self.orig_cursor_offset, self.orig_line) | |
| 314 | 312 | result = start + len(match), self.orig_line[:start] + match + self.orig_line[end:] | |
| 315 | 313 | return result | |
@@ -321,9 +319,12 @@ def substitute_cseq(self): | |||
| 321 | 319 | """Returns a new line by substituting a common sequence in, and update matches""" | |
| 322 | 320 | cseq = os.path.commonprefix(self.matches) | |
| 323 | 321 | new_cursor_offset, new_line = self.substitute(cseq) | |
| 324 | - self.update(new_cursor_offset, new_line, self.matches, self.completer) | ||
| 325 | 322 | if len(self.matches) == 1: | |
| 326 | 323 | self.clear() | |
| 324 | + else: | ||
| 325 | + self.update(new_cursor_offset, new_line, self.matches, self.completer) | ||
| 326 | + if len(self.matches) == 1: | ||
| 327 | + self.clear() | ||
| 327 | 328 | return new_cursor_offset, new_line | |
| 328 | 329 | ||
| 329 | 330 | def update(self, cursor_offset, current_line, matches, completer): | |
@@ -335,7 +336,7 @@ def update(self, cursor_offset, current_line, matches, completer): | |||
| 335 | 336 | assert matches is not None | |
| 336 | 337 | self.matches = matches | |
| 337 | 338 | self.completer = completer | |
| 338 | - assert self.completer.locate(self.orig_cursor_offset, self.orig_line) is not None | ||
| 339 | + assert self.completer.locate(self.orig_cursor_offset, self.orig_line) is not None, (self.completer.locate, self.orig_cursor_offset, self.orig_line) | ||
| 339 | 340 | self.index = -1 | |
| 340 | 341 | self.start, self.end, self.current_word = self.completer.locate(self.orig_cursor_offset, self.orig_line) | |
| 341 | 342 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -131,6 +131,7 @@ def test_simple(self): | |||
| 131 | 131 | self.assertAccess('asdf[<(1,>|]') | |
| 132 | 132 | self.assertAccess('asdf[<(1, >|]') | |
| 133 | 133 | self.assertAccess('asdf[<(1, 2)>|]') | |
| 134 | + self.assertAccess('d[d[<12|>') | ||
| 134 | 135 | ||
| 135 | 136 | class TestCurrentDict(LineTestCase): | |
| 136 | 137 | def setUp(self): | |
| Back | FazBrowse Home | New Git URL |
0 commit comments