~brian-sidebotham/wxwidgets-cmake/wxpython-2.9.4

« back to all changes in this revision

Viewing changes to wxPython/wx/tools/Editra/src/autocomp/htmlcomp.py

  • Committer: Brian Sidebotham
  • Date: 2013-08-03 14:30:08 UTC
  • Revision ID: brian.sidebotham@gmail.com-20130803143008-c7806tkych1tp6fc
Initial import into Bazaar

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
###############################################################################
 
2
# Name: htmlcomp.py                                                           #
 
3
# Purpose: Simple input assistant for html and xml.                           #
 
4
# Author: Cody Precord                                                        #
 
5
# Copyright: (c) 2009 Cody Precord <staff@editra.org>                         #
 
6
# License: wxWindows License                                                  #
 
7
###############################################################################
 
8
 
 
9
"""
 
10
Simple autocompletion support for HTML and XML documents.
 
11
 
 
12
"""
 
13
 
 
14
__author__ = "Cody Precord <cprecord@editra.org>"
 
15
__cvsid__ = "$Id: htmlcomp.py 70229 2012-01-01 01:27:10Z CJP $"
 
16
__revision__ = "$Revision: 70229 $"
 
17
 
 
18
#--------------------------------------------------------------------------#
 
19
# Imports
 
20
import re
 
21
import wx
 
22
import wx.stc
 
23
 
 
24
# Local Imports
 
25
import completer
 
26
 
 
27
#--------------------------------------------------------------------------#
 
28
# Standard Html Tags
 
29
TAGS = ['!--', 'a', 'abbr', 'accept', 'accesskey', 'acronym', 'action',
 
30
        'address', 'align', 'alink', 'alt', 'applet', 'archive', 'area',
 
31
        'article', 'aside', 'audio', 'axis', 'b', 'background', 'base',
 
32
        'basefont', 'bdo', 'bgcolor', 'big', 'blockquote', 'body', 'border',
 
33
        'bordercolor', 'br', 'button', 'canvas', 'caption', 'cellpadding',
 
34
        'cellspacing', 'center', 'char', 'charoff', 'charset', 'checked',
 
35
        'cite', 'cite', 'class', 'classid', 'clear', 'code', 'codebase',
 
36
        'codetype', 'col', 'colgroup', 'color', 'cols', 'colspan', 'command',
 
37
        'compact', 'content', 'coords', 'data', 'datetime', 'datalist', 'dd',
 
38
        'declare', 'defer', 'del', 'details', 'dfn', 'dialog', 'dir', 'dir',
 
39
        'disabled', 'div', 'dl', 'dt', 'dtml-call', 'dtml-comment', 'dtml-if',
 
40
        'dtml-in', 'dtml-let', 'dtml-raise', 'dtml-tree', 'dtml-try',
 
41
        'dtml-unless', 'dtml-var', 'dtml-with', 'em', 'embed', 'enctype',
 
42
        'face', 'fieldset', 'figcaption', 'figure', 'font', 'for', 'form',
 
43
        'footer', 'frame', 'gutter', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head',
 
44
        'header', 'headers', 'height', 'hgroup', 'hr', 'href', 'hreflang',
 
45
        'hspace', 'html', 'http-equiv', 'i', 'id', 'iframe', 'img', 'input',
 
46
        'ins', 'isindex', 'ismap', 'kbd', 'keygen', 'label', 'lang', 'language',
 
47
        'legend', 'li', 'link', 'link', 'longdesc', 'lowsrc', 'map',
 
48
        'marginheight', 'marginwidth', 'mark', 'maxlength', 'menu', 'meta',
 
49
        'meter', 'method', 'multiple', 'name', 'nav', 'nohref', 'noscript',
 
50
        'nowrap', 'object', 'ol', 'optgroup', 'option', 'output', 'p', 'param',
 
51
        'pre', 'profile', 'progress', 'prompt', 'q', 'readonly', 'rel', 'rev',
 
52
        'rows', 'rowspan', 'rp', 'rt', 'ruby', 'rules', 's', 'samp', 'scheme',
 
53
        'scope', 'script', 'scrolling', 'section', 'select', 'selected',
 
54
        'shape', 'size', 'small', 'source', 'span', 'src', 'standby', 'start',
 
55
        'strike', 'strong', 'style', 'sub', 'summary', 'sup', 'tabindex',
 
56
        'table', 'target', 'tbody', 'td', 'text', 'textarea', 'tfoot', 'th',
 
57
        'thead', 'time', 'title', 'tr', 'tt', 'type', 'u', 'ul', 'url',
 
58
        'usemap', 'valign', 'value', 'valuetype', 'var', 'version', 'video',
 
59
        'vlink', 'vspace', 'width', 'wrap', 'xmp']
 
60
 
 
61
# Tags that usually have a new line inbetween them
 
62
NLINE_TAGS = ('body', 'head', 'html', 'ol', 'style', 'table', 'tbody', 'ul')
 
63
 
 
64
TAG_RE = re.compile("\<\s*([a-zA-Z][a-zA-Z0-9]*)")
 
65
 
 
66
PHP_AREA = [wx.stc.STC_HPHP_COMMENT, wx.stc.STC_HPHP_COMMENTLINE,
 
67
            wx.stc.STC_HPHP_COMPLEX_VARIABLE, wx.stc.STC_HPHP_DEFAULT,
 
68
            wx.stc.STC_HPHP_HSTRING, wx.stc.STC_HPHP_HSTRING_VARIABLE,
 
69
            wx.stc.STC_HPHP_NUMBER, wx.stc.STC_HPHP_OPERATOR,
 
70
            wx.stc.STC_HPHP_SIMPLESTRING,
 
71
            wx.stc.STC_HPHP_VARIABLE, wx.stc.STC_HPHP_WORD]
 
72
 
 
73
HTML_AREA = [wx.stc.STC_H_ASP, wx.stc.STC_H_ASPAT, wx.stc.STC_H_ATTRIBUTE,
 
74
             wx.stc.STC_H_ATTRIBUTEUNKNOWN, wx.stc.STC_H_CDATA,
 
75
             wx.stc.STC_H_COMMENT, wx.stc.STC_H_DEFAULT,
 
76
             wx.stc.STC_H_DOUBLESTRING, wx.stc.STC_H_ENTITY,
 
77
             wx.stc.STC_H_NUMBER, wx.stc.STC_H_OTHER, wx.stc.STC_H_QUESTION,
 
78
             wx.stc.STC_H_SCRIPT, wx.stc.STC_H_SGML_1ST_PARAM,
 
79
             wx.stc.STC_H_SGML_1ST_PARAM_COMMENT,
 
80
             wx.stc.STC_H_SGML_BLOCK_DEFAULT, wx.stc.STC_H_SGML_COMMAND,
 
81
             wx.stc.STC_H_SGML_COMMENT, wx.stc.STC_H_SGML_DEFAULT,
 
82
             wx.stc.STC_H_SGML_DOUBLESTRING, wx.stc.STC_H_SGML_ENTITY,
 
83
             wx.stc.STC_H_SGML_ERROR, wx.stc.STC_H_SGML_SIMPLESTRING,
 
84
             wx.stc.STC_H_SGML_SPECIAL, wx.stc.STC_H_SINGLESTRING,
 
85
             wx.stc.STC_H_TAG, wx.stc.STC_H_TAGEND,
 
86
             wx.stc.STC_H_TAGUNKNOWN, wx.stc.STC_H_VALUE,
 
87
             wx.stc.STC_H_XCCOMMENT, wx.stc.STC_H_XMLEND,
 
88
             wx.stc.STC_H_XMLSTART]
 
89
 
 
90
#--------------------------------------------------------------------------#
 
91
 
 
92
class Completer(completer.BaseCompleter):
 
93
    """HTML/XML Code completion provider"""
 
94
    def __init__(self, stc_buffer):
 
95
        super(Completer, self).__init__(stc_buffer)
 
96
 
 
97
        # Setup
 
98
        self.SetAutoCompKeys([ord('>'), ord('<')])
 
99
        self.SetAutoCompStops(' ')
 
100
        self.SetAutoCompFillups('')
 
101
 
 
102
    def GetAutoCompList(self, command):
 
103
        """Returns the list of possible completions for a
 
104
        command string.
 
105
        @param command: command lookup is done on
 
106
 
 
107
        """
 
108
        if command in [None, u'']:
 
109
            return list()
 
110
 
 
111
        buff = self.GetBuffer()
 
112
        cpos = buff.GetCurrentPos()
 
113
 
 
114
        # Check if we are in a php region or not
 
115
        if buff.GetStyleAt(cpos) not in HTML_AREA:
 
116
            return list()
 
117
 
 
118
        # Get current context
 
119
        cline = buff.GetCurrentLine()
 
120
        ccol = buff.GetColumn(cpos)
 
121
        tmp = buff.GetLine(cline).rstrip()
 
122
        if ccol < len(tmp):
 
123
            tmp = tmp[:ccol].rstrip()
 
124
 
 
125
        # Check if we are completing an open tag (i.e < was typed)
 
126
        if tmp.endswith('<'):
 
127
            if buff.GetLexer() == wx.stc.STC_LEX_XML:
 
128
                taglst = _FindXmlTags(buff.GetText())
 
129
            else:
 
130
                taglst = TAGS
 
131
            return completer.CreateSymbols(taglst, completer.TYPE_ELEMENT)
 
132
 
 
133
        # Check for a self closing tag (i.e />)
 
134
        endchk = tmp.strip().replace(u" ", u"").replace(u"\t", u"")
 
135
        if endchk.endswith(u"/>"):
 
136
            return list()
 
137
 
 
138
        # Try to autocomplete a closing tag (if necessary)
 
139
        tmp = tmp.rstrip('>').rstrip()
 
140
        if len(tmp) and (tmp[-1] in '"\' \t' or tmp[-1].isalpha()):
 
141
            # Walk backwards from the current line
 
142
            for line in range(cline, -1, -1):
 
143
                txt = buff.GetLine(line)
 
144
                if line == cline:
 
145
                    txt = txt[:buff.GetColumn(cpos)]
 
146
 
 
147
                idx = txt.rfind('<')
 
148
                if idx != -1:
 
149
                    parts = txt[idx:].lstrip('<').strip().split()
 
150
                    if len(parts):
 
151
                        tag = parts[0].rstrip('>')
 
152
                        if len(tag) and \
 
153
                           tag not in ('img', 'br', '?php', '?xml', '?') and \
 
154
                           not tag[0] in ('!', '/'):
 
155
                            rtag = u"</" + tag + u">"
 
156
 
 
157
                            if not parts[-1].endswith('>'):
 
158
                                rtag = u">" + rtag
 
159
                            return [ completer.Symbol(rtag, completer.TYPE_ELEMENT) ]
 
160
                    break
 
161
 
 
162
        return list()
 
163
 
 
164
    def OnCompletionInserted(self, pos, text):
 
165
        """Handle adjusting caret position after some insertions.
 
166
        @param pos: position caret was at before insertion
 
167
        @param text: text that was inserted
 
168
 
 
169
        """
 
170
        buff = self.GetBuffer()
 
171
        if text.strip().startswith(u"</"):
 
172
            buff.SetCurrentPos(pos) # move caret back between the tags
 
173
            # HACK: SetCurrentPos causes text to be selected
 
174
            buff.SetSelection(pos, pos)
 
175
 
 
176
#--------------------------------------------------------------------------#
 
177
 
 
178
def _FindXmlTags(text):
 
179
    """Dynamically generate a list of possible xml tags based on tags found in
 
180
    the given text.
 
181
    @param text: string
 
182
    @return: sorted list
 
183
 
 
184
    """
 
185
    matches = TAG_RE.findall(text)
 
186
    if len(matches):
 
187
        matches.append(u'!--')
 
188
        matches = list(set(matches))
 
189
        matches.sort()
 
190
    else:
 
191
        matches = [u'!--', ]
 
192
    return matches