simplecomp.py
3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
###############################################################################
# Name: simplecomp.py #
# Purpose: Simple autocompletion based on buffer words (SciTE docet) #
# Author: Giuseppe "Cowo" Corbelli #
# Copyright: (c) 2009 Giuseppe "Cowo" Corbelli #
# License: wxWindows License #
###############################################################################
"""
Simple Generic autocompleter for completing words found in the current buffer.
"""
__author__ = "Giuseppe \"Cowo\" Corbelli"
__cvsid__ = "$Id: simplecomp.py 72222 2012-07-28 15:43:38Z CJP $"
__revision__ = "$Revision: 72222 $"
#--------------------------------------------------------------------------#
# Imports
import string
import wx.stc as stc
# Local Imports
import completer
#--------------------------------------------------------------------------#
class Completer(completer.BaseCompleter):
"""Generic word completer provider"""
wordCharacters = "".join(['_', string.letters])
def __init__(self, stc_buffer):
super(Completer, self).__init__(stc_buffer)
# Setup
self.SetAutoCompKeys([])
self.SetAutoCompStops(' \'"\\`):')
self.SetAutoCompFillups('.,:;([]){}<>%^&+-=*/|$@')
self.SetCallTipKeys([])
self.SetCallTipCancel([])
self.SetCaseSensitive(False)
def _GetCompletionInfo(self, command, calltip=False):
"""Get Completion list or Calltip
@return: list or string
"""
bf = self.GetBuffer()
# A list of Symbol(keyword, TYPE_UNKNOWN)
kwlst = map(
lambda kw: completer.Symbol(kw, completer.TYPE_UNKNOWN),
bf.GetKeywords()
)
if command in (None, u''):
return kwlst
fillups = self.GetAutoCompFillups()
if command[0].isdigit() or (command[-1] in fillups):
return list()
currentPos = bf.GetCurrentPos()
# Get the real word: segment using autocompFillup
tmp = command
for ch in fillups:
tmp = command.strip(ch)
ls = list(tmp)
ls.reverse()
idx = 0
for c in ls:
if c in fillups:
break
idx += 1
ls2 = ls[:idx]
ls2.reverse()
command = u"".join(ls2)
# Available completions so far
wordsNear = []
maxWordLength = 0
nWords = 0
minPos = 0
maxPos = bf.GetLength()
flags = stc.STC_FIND_WORDSTART
if self.GetCaseSensitive():
flags |= stc.STC_FIND_MATCHCASE
posFind = bf.FindText(minPos, maxPos, command, flags)
while posFind >= 0 and posFind < maxPos:
wordEnd = posFind + len(command)
if posFind != currentPos:
while -1 != Completer.wordCharacters.find(chr(bf.GetCharAt(wordEnd))):
wordEnd += 1
wordLength = wordEnd - posFind
if wordLength > len(command):
word = bf.GetTextRange(posFind, wordEnd)
sym = completer.Symbol(word, completer.TYPE_UNKNOWN)
if not wordsNear.count(sym):
wordsNear.append(sym)
maxWordLength = max(maxWordLength, wordLength)
nWords += 1
minPos = wordEnd
posFind = bf.FindText(minPos, maxPos, command, flags)
if len(wordsNear) > 0 and (maxWordLength > len(command)):
return wordsNear
return kwlst
def GetAutoCompList(self, command):
"""Returns the list of possible completions for a command string.
@param command: command lookup is done on
"""
rlist = self._GetCompletionInfo(command)
return sorted(list(set(rlist)))