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

Method python_func_kw_matches

IPython/core/completer.py:1490–1564  ·  view source on GitHub ↗

Match named parameters (kwargs) of the last open function

(self,text)

Source from the content-addressed store, hash-verified

1488 return list(set(ret))
1489
1490 def python_func_kw_matches(self,text):
1491 """Match named parameters (kwargs) of the last open function"""
1492
1493 if "." in text: # a parameter cannot be dotted
1494 return []
1495 try: regexp = self.__funcParamsRegex
1496 except AttributeError:
1497 regexp = self.__funcParamsRegex = re.compile(r'''
1498 '.*?(?<!\\)' | # single quoted strings or
1499 ".*?(?<!\\)" | # double quoted strings or
1500 \w+ | # identifier
1501 \S # other characters
1502 ''&#x27;, re.VERBOSE | re.DOTALL)
1503 # 1. find the nearest identifier that comes before an unclosed
1504 # parenthesis before the cursor
1505 # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo"
1506 tokens = regexp.findall(self.text_until_cursor)
1507 iterTokens = reversed(tokens); openPar = 0
1508
1509 for token in iterTokens:
1510 if token == ')':
1511 openPar -= 1
1512 elif token == '(':
1513 openPar += 1
1514 if openPar > 0:
1515 # found the last unclosed parenthesis
1516 break
1517 else:
1518 return []
1519 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
1520 ids = []
1521 isId = re.compile(r'\w+$').match
1522
1523 while True:
1524 try:
1525 ids.append(next(iterTokens))
1526 if not isId(ids[-1]):
1527 ids.pop(); break
1528 if not next(iterTokens) == '.':
1529 break
1530 except StopIteration:
1531 break
1532
1533 # Find all named arguments already assigned to, as to avoid suggesting
1534 # them again
1535 usedNamedArgs = set()
1536 par_level = -1
1537 for token, next_token in zip(tokens, tokens[1:]):
1538 if token == '(':
1539 par_level += 1
1540 elif token == ')':
1541 par_level -= 1
1542
1543 if par_level != 0:
1544 continue
1545
1546 if next_token != '=':
1547 continue

Callers

nothing calls this directly

Calls 3

_default_argumentsMethod · 0.95
popMethod · 0.80
addMethod · 0.45

Tested by

no test coverage detected