MCPcopy Create free account
hub / github.com/ipython/ipython / file_matches

Method file_matches

IPython/core/completer.py:1160–1240  ·  view source on GitHub ↗

Match filenames, expanding ~USER type strings. Most of the seemingly convoluted logic in this completer is an attempt to handle filenames with spaces in them. And yet it's not quite perfect, because Python's readline doesn't expose all of the GNU readline details ne

(self, text)

Source from the content-addressed store, hash-verified

1158 for f in self.glob("%s*" % text)]
1159
1160 def file_matches(self, text):
1161 """Match filenames, expanding ~USER type strings.
1162
1163 Most of the seemingly convoluted logic in this completer is an
1164 attempt to handle filenames with spaces in them. And yet it's not
1165 quite perfect, because Python's readline doesn't expose all of the
1166 GNU readline details needed for this to be done correctly.
1167
1168 For a filename with a space in it, the printed completions will be
1169 only the parts after what's already been typed (instead of the
1170 full completions, as is normally done). I don't think with the
1171 current (as of Python 2.3) Python readline it's possible to do
1172 better."""
1173
1174 # chars that require escaping with backslash - i.e. chars
1175 # that readline treats incorrectly as delimiters, but we
1176 # don't want to treat as delimiters in filename matching
1177 # when escaped with backslash
1178 if text.startswith('!'):
1179 text = text[1:]
1180 text_prefix = u'!'
1181 else:
1182 text_prefix = u''
1183
1184 text_until_cursor = self.text_until_cursor
1185 # track strings with open quotes
1186 open_quotes = has_open_quotes(text_until_cursor)
1187
1188 if '(' in text_until_cursor or '[' in text_until_cursor:
1189 lsplit = text
1190 else:
1191 try:
1192 # arg_split ~ shlex.split, but with unicode bugs fixed by us
1193 lsplit = arg_split(text_until_cursor)[-1]
1194 except ValueError:
1195 # typically an unmatched ", or backslash without escaped char.
1196 if open_quotes:
1197 lsplit = text_until_cursor.split(open_quotes)[-1]
1198 else:
1199 return []
1200 except IndexError:
1201 # tab pressed on empty line
1202 lsplit = ""
1203
1204 if not open_quotes and lsplit != protect_filename(lsplit):
1205 # if protectables are found, do matching on the whole escaped name
1206 has_protectables = True
1207 text0,text = text,lsplit
1208 else:
1209 has_protectables = False
1210 text = os.path.expanduser(text)
1211
1212 if text == "":
1213 return [text_prefix + protect_filename(f) for f in self.glob("*")]
1214
1215 # Compute the matches from the filesystem
1216 if sys.platform == 'win32':
1217 m0 = self.clean_glob(text)

Callers

nothing calls this directly

Calls 3

arg_splitFunction · 0.90
has_open_quotesFunction · 0.85
protect_filenameFunction · 0.85

Tested by

no test coverage detected