htmlcomp.py
8.3 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
###############################################################################
# Name: htmlcomp.py #
# Purpose: Simple input assistant for html and xml. #
# Author: Cody Precord #
# Copyright: (c) 2009 Cody Precord <staff@editra.org> #
# License: wxWindows License #
###############################################################################
"""
Simple autocompletion support for HTML and XML documents.
"""
__author__ = "Cody Precord <cprecord@editra.org>"
__cvsid__ = "$Id: htmlcomp.py 72389 2012-08-28 16:53:09Z CJP $"
__revision__ = "$Revision: 72389 $"
#--------------------------------------------------------------------------#
# Imports
import re
import wx
import wx.stc
# Local Imports
import completer
#--------------------------------------------------------------------------#
# Standard Html Tags
TAGS = ['!--', 'a', 'abbr', 'accept', 'accesskey', 'acronym', 'action',
'address', 'align', 'alink', 'alt', 'applet', 'archive', 'area',
'article', 'aside', 'audio', 'axis', 'b', 'background', 'base',
'basefont', 'bdo', 'bgcolor', 'big', 'blockquote', 'body', 'border',
'bordercolor', 'br', 'button', 'canvas', 'caption', 'cellpadding',
'cellspacing', 'center', 'char', 'charoff', 'charset', 'checked',
'cite', 'cite', 'class', 'classid', 'clear', 'code', 'codebase',
'codetype', 'col', 'colgroup', 'color', 'cols', 'colspan', 'command',
'compact', 'content', 'coords', 'data', 'datetime', 'datalist', 'dd',
'declare', 'defer', 'del', 'details', 'dfn', 'dialog', 'dir', 'dir',
'disabled', 'div', 'dl', 'dt', 'dtml-call', 'dtml-comment', 'dtml-if',
'dtml-in', 'dtml-let', 'dtml-raise', 'dtml-tree', 'dtml-try',
'dtml-unless', 'dtml-var', 'dtml-with', 'em', 'embed', 'enctype',
'face', 'fieldset', 'figcaption', 'figure', 'font', 'for', 'form',
'footer', 'frame', 'gutter', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head',
'header', 'headers', 'height', 'hgroup', 'hr', 'href', 'hreflang',
'hspace', 'html', 'http-equiv', 'i', 'id', 'iframe', 'img', 'input',
'ins', 'isindex', 'ismap', 'kbd', 'keygen', 'label', 'lang', 'language',
'legend', 'li', 'link', 'link', 'longdesc', 'lowsrc', 'map',
'marginheight', 'marginwidth', 'mark', 'maxlength', 'menu', 'meta',
'meter', 'method', 'multiple', 'name', 'nav', 'nohref', 'noscript',
'nowrap', 'object', 'ol', 'optgroup', 'option', 'output', 'p', 'param',
'pre', 'profile', 'progress', 'prompt', 'q', 'readonly', 'rel', 'rev',
'rows', 'rowspan', 'rp', 'rt', 'ruby', 'rules', 's', 'samp', 'scheme',
'scope', 'script', 'scrolling', 'section', 'select', 'selected',
'shape', 'size', 'small', 'source', 'span', 'src', 'standby', 'start',
'strike', 'strong', 'style', 'sub', 'summary', 'sup', 'tabindex',
'table', 'target', 'tbody', 'td', 'text', 'textarea', 'tfoot', 'th',
'thead', 'time', 'title', 'tr', 'tt', 'type', 'u', 'ul', 'url',
'usemap', 'valign', 'value', 'valuetype', 'var', 'version', 'video',
'vlink', 'vspace', 'width', 'wrap', 'xmp']
# Tags that usually have a new line inbetween them
NLINE_TAGS = ('body', 'head', 'html', 'ol', 'style', 'table', 'tbody', 'ul')
TAG_RE = re.compile("\<\s*([a-zA-Z][a-zA-Z0-9]*)")
PHP_AREA = [wx.stc.STC_HPHP_COMMENT, wx.stc.STC_HPHP_COMMENTLINE,
wx.stc.STC_HPHP_COMPLEX_VARIABLE, wx.stc.STC_HPHP_DEFAULT,
wx.stc.STC_HPHP_HSTRING, wx.stc.STC_HPHP_HSTRING_VARIABLE,
wx.stc.STC_HPHP_NUMBER, wx.stc.STC_HPHP_OPERATOR,
wx.stc.STC_HPHP_SIMPLESTRING,
wx.stc.STC_HPHP_VARIABLE, wx.stc.STC_HPHP_WORD]
HTML_AREA = [wx.stc.STC_H_ASP, wx.stc.STC_H_ASPAT, wx.stc.STC_H_ATTRIBUTE,
wx.stc.STC_H_ATTRIBUTEUNKNOWN, wx.stc.STC_H_CDATA,
wx.stc.STC_H_COMMENT, wx.stc.STC_H_DEFAULT,
wx.stc.STC_H_DOUBLESTRING, wx.stc.STC_H_ENTITY,
wx.stc.STC_H_NUMBER, wx.stc.STC_H_OTHER, wx.stc.STC_H_QUESTION,
wx.stc.STC_H_SCRIPT, wx.stc.STC_H_SGML_1ST_PARAM,
wx.stc.STC_H_SGML_1ST_PARAM_COMMENT,
wx.stc.STC_H_SGML_BLOCK_DEFAULT, wx.stc.STC_H_SGML_COMMAND,
wx.stc.STC_H_SGML_COMMENT, wx.stc.STC_H_SGML_DEFAULT,
wx.stc.STC_H_SGML_DOUBLESTRING, wx.stc.STC_H_SGML_ENTITY,
wx.stc.STC_H_SGML_ERROR, wx.stc.STC_H_SGML_SIMPLESTRING,
wx.stc.STC_H_SGML_SPECIAL, wx.stc.STC_H_SINGLESTRING,
wx.stc.STC_H_TAG, wx.stc.STC_H_TAGEND,
wx.stc.STC_H_TAGUNKNOWN, wx.stc.STC_H_VALUE,
wx.stc.STC_H_XCCOMMENT, wx.stc.STC_H_XMLEND,
wx.stc.STC_H_XMLSTART]
#--------------------------------------------------------------------------#
class Completer(completer.BaseCompleter):
"""HTML/XML Code completion provider"""
def __init__(self, stc_buffer):
super(Completer, self).__init__(stc_buffer)
# Setup
self.SetAutoCompKeys([ord('>'), ord('<')])
self.SetAutoCompStops(' ')
self.SetAutoCompFillups('')
def GetAutoCompList(self, command):
"""Returns the list of possible completions for a
command string.
@param command: command lookup is done on
"""
if command in [None, u'', u'<']:
return list()
buff = self.GetBuffer()
cpos = buff.GetCurrentPos()
# Check if we are in a php region or not
if buff.GetStyleAt(cpos) not in HTML_AREA:
return list()
# Get current context
cline = buff.GetCurrentLine()
ccol = buff.GetColumn(cpos)
tmp = buff.GetLine(cline).rstrip()
if ccol < len(tmp):
tmp = tmp[:ccol].rstrip()
# Check if we are completing an open tag (i.e < was typed)
if tmp.endswith('<'):
if buff.GetLexer() == wx.stc.STC_LEX_XML:
taglst = _FindXmlTags(buff.GetText())
else:
taglst = TAGS
return completer.CreateSymbols(taglst, completer.TYPE_ELEMENT)
# Check for a self closing tag (i.e />)
endchk = tmp.strip().replace(u" ", u"").replace(u"\t", u"")
if endchk.endswith(u"/>"):
return list()
# Try to autocomplete a closing tag (if necessary)
tmp = tmp.rstrip('>').rstrip()
if len(tmp) and (tmp[-1] in '"\' \t' or tmp[-1].isalpha()):
# Walk backwards from the current line
for line in range(cline, -1, -1):
txt = buff.GetLine(line)
if line == cline:
txt = txt[:buff.GetColumn(cpos)]
idx = txt.rfind('<')
if idx != -1:
parts = txt[idx:].lstrip('<').strip().split()
if len(parts):
tag = parts[0].rstrip('>')
if len(tag) and \
tag not in ('img', 'br', '?php', '?xml', '?') and \
not tag[0] in ('!', '/'):
rtag = u"</" + tag + u">"
if not parts[-1].endswith('>'):
rtag = u">" + rtag
return [ completer.Symbol(rtag, completer.TYPE_ELEMENT) ]
break
return list()
def OnCompletionInserted(self, pos, text):
"""Handle adjusting caret position after some insertions.
@param pos: position caret was at before insertion
@param text: text that was inserted
"""
buff = self.GetBuffer()
if text.strip().startswith(u"</"):
buff.SetCurrentPos(pos) # move caret back between the tags
# HACK: SetCurrentPos causes text to be selected
buff.SetSelection(pos, pos)
#--------------------------------------------------------------------------#
def _FindXmlTags(text):
"""Dynamically generate a list of possible xml tags based on tags found in
the given text.
@param text: string
@return: sorted list
"""
matches = TAG_RE.findall(text)
if len(matches):
matches.append(u'!--')
matches = list(set(matches))
matches.sort()
else:
matches = [u'!--', ]
return matches