autoexpand.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. '''Complete the current word before the cursor with words in the editor.
  2. Each menu selection or shortcut key selection replaces the word with a
  3. different word with the same prefix. The search for matches begins
  4. before the target and moves toward the top of the editor. It then starts
  5. after the cursor and moves down. It then returns to the original word and
  6. the cycle starts again.
  7. Changing the current text line or leaving the cursor in a different
  8. place before requesting the next selection causes AutoExpand to reset
  9. its state.
  10. There is only one instance of Autoexpand.
  11. '''
  12. import re
  13. import string
  14. class AutoExpand:
  15. wordchars = string.ascii_letters + string.digits + "_"
  16. def __init__(self, editwin):
  17. self.text = editwin.text
  18. self.bell = self.text.bell
  19. self.state = None
  20. def expand_word_event(self, event):
  21. "Replace the current word with the next expansion."
  22. curinsert = self.text.index("insert")
  23. curline = self.text.get("insert linestart", "insert lineend")
  24. if not self.state:
  25. words = self.getwords()
  26. index = 0
  27. else:
  28. words, index, insert, line = self.state
  29. if insert != curinsert or line != curline:
  30. words = self.getwords()
  31. index = 0
  32. if not words:
  33. self.bell()
  34. return "break"
  35. word = self.getprevword()
  36. self.text.delete("insert - %d chars" % len(word), "insert")
  37. newword = words[index]
  38. index = (index + 1) % len(words)
  39. if index == 0:
  40. self.bell() # Warn we cycled around
  41. self.text.insert("insert", newword)
  42. curinsert = self.text.index("insert")
  43. curline = self.text.get("insert linestart", "insert lineend")
  44. self.state = words, index, curinsert, curline
  45. return "break"
  46. def getwords(self):
  47. "Return a list of words that match the prefix before the cursor."
  48. word = self.getprevword()
  49. if not word:
  50. return []
  51. before = self.text.get("1.0", "insert wordstart")
  52. wbefore = re.findall(r"\b" + word + r"\w+\b", before)
  53. del before
  54. after = self.text.get("insert wordend", "end")
  55. wafter = re.findall(r"\b" + word + r"\w+\b", after)
  56. del after
  57. if not wbefore and not wafter:
  58. return []
  59. words = []
  60. dict = {}
  61. # search backwards through words before
  62. wbefore.reverse()
  63. for w in wbefore:
  64. if dict.get(w):
  65. continue
  66. words.append(w)
  67. dict[w] = w
  68. # search onwards through words after
  69. for w in wafter:
  70. if dict.get(w):
  71. continue
  72. words.append(w)
  73. dict[w] = w
  74. words.append(word)
  75. return words
  76. def getprevword(self):
  77. "Return the word prefix before the cursor."
  78. line = self.text.get("insert linestart", "insert")
  79. i = len(line)
  80. while i > 0 and line[i-1] in self.wordchars:
  81. i = i-1
  82. return line[i:]
  83. if __name__ == '__main__':
  84. from unittest import main
  85. main('idlelib.idle_test.test_autoexpand', verbosity=2)