csscomp.py
6.22 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
###############################################################################
# Name: csscomp.py #
# Purpose: Simple input assistant for CSS #
# Author: Cody Precord #
# Copyright: (c) 2009 Cody Precord <staff@editra.org> #
# License: wxWindows License #
###############################################################################
"""
Simple autocompletion support for Cascading Style Sheets.
"""
__author__ = "Cody Precord <cprecord@editra.org>"
__cvsid__ = "$Id: csscomp.py 70229 2012-01-01 01:27:10Z CJP $"
__revision__ = "$Revision: 70229 $"
#--------------------------------------------------------------------------#
# Imports
import re
import wx
import wx.stc
# Local Imports
import completer
#--------------------------------------------------------------------------#
# Regular Expressions
RE_LINK_PSEUDO = re.compile("a:(link|visited|active|hover|focus)*")
RE_CSS_COMMENT = re.compile("\/\*[^*]*\*+([^/][^*]*\*+)*\/")
RE_CSS_BLOCK = re.compile("\{[^}]*\}")
PSUEDO_SYMBOLS = completer.CreateSymbols([ u'active', u'focus', u'hover',
u'link', u'visited' ],
)
#--------------------------------------------------------------------------#
class Completer(completer.BaseCompleter):
"""CSS Code completion provider"""
def __init__(self, stc_buffer):
super(Completer, self).__init__(stc_buffer)
# Setup
self.SetAutoCompKeys([ord(':'), ord('.') ])
self.SetAutoCompStops(' {}#')
self.SetAutoCompFillups('')
self.SetCallTipKeys([ord('('), ])
self.SetCallTipCancel([ord(')'), wx.WXK_RETURN])
def GetAutoCompList(self, command):
"""Returns the list of possible completions for a command string.
@param command: command lookup is done on
"""
buff = self.GetBuffer()
keywords = buff.GetKeywords()
if command in [None, u'']:
return completer.CreateSymbols(keywords, completer.TYPE_UNKNOWN)
cpos = buff.GetCurrentPos()
cline = buff.GetCurrentLine()
lstart = buff.PositionFromLine(cline)
tmp = buff.GetTextRange(lstart, cpos).rstrip()
# Check for the case of a pseudo class
if IsPsuedoClass(command, tmp):
return PSUEDO_SYMBOLS
# Give some help on some common properties
if tmp.endswith(u':'):
word = GetWordLeft(tmp.rstrip().rstrip(u':'))
comps = PROP_OPTS.get(word, list())
comps = list(set(comps))
comps.sort()
return completer.CreateSymbols(comps, completer.TYPE_PROPERTY)
# Look for if we are completing a tag class
if tmp.endswith(u'.'):
classes = list()
if not buff.IsString(cpos):
txt = buff.GetText()
txt = RE_CSS_COMMENT.sub(u'', txt)
txt = RE_CSS_BLOCK.sub(u' ', txt)
for token in txt.split():
if u'.' in token:
classes.append(token.split(u'.', 1)[-1])
classes = list(set(classes))
classes.sort()
return completer.CreateSymbols(classes, completer.TYPE_CLASS)
return completer.CreateSymbols(keywords, completer.TYPE_UNKNOWN)
def GetCallTip(self, command):
"""Returns the formated calltip string for the command."""
if command == u'url':
return u'url(\'../path\')'
else:
return u''
def ShouldCheck(self, cpos):
"""Should completions be attempted
@param cpos: current buffer position
@return: bool
"""
buff = self.GetBuffer()
rval = True
if buff is not None:
if buff.IsComment(cpos):
rval = False
return rval
#--------------------------------------------------------------------------#
def IsPsuedoClass(cmd, line):
"""Check the line to see if its a link pseudo class
@param cmd: current command
@param line: line of the command
@return: bool
"""
if cmd.endswith(u':'):
token = line.split()[-1]
pieces = token.split(u":")
if pieces[0] == 'a' or pieces[0].startswith('a.'):
return True
return False
def GetWordLeft(line):
"""Get the first valid word to the left of the end of line
@param line: Line text
@return: string
"""
for idx in range(1, len(line)+1):
ch = line[idx*-1]
if ch.isspace() or ch in u'{;':
return line[-1*idx:].strip()
else:
return u''
#--------------------------------------------------------------------------#
# Properties to provide some input help on
PROP_OPTS = { u'border-style' : [u'none', u'hidden', u'dotted', u'dashed',
u'solid', u'double', u'groove', u'ridge',
u'inset', u'outset'],
u'float' : [u'left', u'right', u'none'],
u'font-style' : [u'normal', u'italic', u'oblique'],
u'font-weight' : [u'normal', u'bold', u'lighter', u'bolder'],
u'list-style-type' : [u'none', u'disc', u'circle', u'square',
u'decimal', u'decimal-leading-zero',
u'lower-roman', u'upper-roman',
u'lower-alpha', u'upper-alpha',
u'lower-greek', u'lower-latin', u'hebrew',
u'armenian', u'georgian', u'cjk-ideographic',
u'hiragana', u'katakana',
u'hiragana-iroha', u'katakana-iroha'],
u'text-decoration' : [u'none', u'underline', u'line-through',
u'overline', u'blink'],
u'text-align' : [u'left', u'right', u'center', u'justify'],
u'vertical-align' : [u'baseline', u'sub', u'super', u'top',
u'text-top', u'middle', u'bottom',
u'text-bottom', ]
}