MCPcopy Create free account
hub / github.com/python/cpython / getwords

Method getwords

Lib/idlelib/autoexpand.py:54–83  ·  view source on GitHub ↗

Return a list of words that match the prefix before the cursor.

(self)

Source from the content-addressed store, hash-verified

52 return "break"
53
54 def getwords(self):
55 "Return a list of words that match the prefix before the cursor."
56 word = self.getprevword()
57 if not word:
58 return []
59 before = self.text.get("1.0", "insert wordstart")
60 wbefore = re.findall(r"\b" + word + r"\w+\b", before)
61 del before
62 after = self.text.get("insert wordend", "end")
63 wafter = re.findall(r"\b" + word + r"\w+\b", after)
64 del after
65 if not wbefore and not wafter:
66 return []
67 words = []
68 dict = {}
69 # search backwards through words before
70 wbefore.reverse()
71 for w in wbefore:
72 if dict.get(w):
73 continue
74 words.append(w)
75 dict[w] = w
76 # search onwards through words after
77 for w in wafter:
78 if dict.get(w):
79 continue
80 words.append(w)
81 dict[w] = w
82 words.append(word)
83 return words
84
85 def getprevword(self):
86 "Return the word prefix before the cursor."

Callers 5

expand_word_eventMethod · 0.95
test_before_onlyMethod · 0.80
test_after_onlyMethod · 0.80

Calls 5

getprevwordMethod · 0.95
getMethod · 0.45
findallMethod · 0.45
reverseMethod · 0.45
appendMethod · 0.45

Tested by 4

test_before_onlyMethod · 0.64
test_after_onlyMethod · 0.64