MCPcopy Index your code
hub / github.com/python/cpython / get_surrounding_brackets

Method get_surrounding_brackets

Lib/idlelib/hyperparser.py:116–156  ·  view source on GitHub ↗

Return bracket indexes or None. If the index given to the HyperParser is surrounded by a bracket defined in openers (or at least has one before it), return the indices of the opening bracket and the closing bracket (or the end of line, whichever comes first).

(self, openers='([{', mustclose=False)

Source from the content-addressed store, hash-verified

114 not in ('#', '"', "'"))
115
116 def get_surrounding_brackets(self, openers='([{', mustclose=False):
117 """Return bracket indexes or None.
118
119 If the index given to the HyperParser is surrounded by a
120 bracket defined in openers (or at least has one before it),
121 return the indices of the opening bracket and the closing
122 bracket (or the end of line, whichever comes first).
123
124 If it is not surrounded by brackets, or the end of line comes
125 before the closing bracket and mustclose is True, returns None.
126 """
127
128 bracketinglevel = self.bracketing[self.indexbracket][1]
129 before = self.indexbracket
130 while (not self.isopener[before] or
131 self.rawtext[self.bracketing[before][0]] not in openers or
132 self.bracketing[before][1] > bracketinglevel):
133 before -= 1
134 if before < 0:
135 return None
136 bracketinglevel = min(bracketinglevel, self.bracketing[before][1])
137 after = self.indexbracket + 1
138 while (after < len(self.bracketing) and
139 self.bracketing[after][1] >= bracketinglevel):
140 after += 1
141
142 beforeindex = self.text.index("%s-%dc" %
143 (self.stopatindex, len(self.rawtext)-self.bracketing[before][0]))
144 if (after >= len(self.bracketing) or
145 self.bracketing[after][0] > len(self.rawtext)):
146 if mustclose:
147 return None
148 afterindex = self.stopatindex
149 else:
150 # We are after a real char, so it is a ')' and we give the
151 # index before it.
152 afterindex = self.text.index(
153 "%s-%dc" % (self.stopatindex,
154 len(self.rawtext)-(self.bracketing[after][0]-1)))
155
156 return beforeindex, afterindex
157
158 # the set of built-in identifiers which are also keywords,
159 # i.e. keyword.iskeyword() returns True for them

Callers 5

open_calltipMethod · 0.95
paren_closed_eventMethod · 0.95
flash_paren_eventMethod · 0.80
without_mustcloseMethod · 0.80
with_mustcloseMethod · 0.80

Calls 1

indexMethod · 0.45

Tested by 2

without_mustcloseMethod · 0.64
with_mustcloseMethod · 0.64