PageRenderTime 44ms CodeModel.GetById 33ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 1ms

/tortoisehg/hgqt/lexers.py

https://bitbucket.org/tortoisehg/hgtk/
Python | 161 lines | 114 code | 29 blank | 18 comment | 23 complexity | 429fe578a7e936c75c30b4cfb2ab49be MD5 | raw file
  1# lexers.py - select Qsci lexer for a filename and contents
  2#
  3# Copyright 2010 Steve Borho <steve@borho.org>
  4#
  5# This software may be used and distributed according to the terms of the
  6# GNU General Public License version 2, incorporated herein by reference.
  7
  8import re
  9
 10from PyQt4 import Qsci
 11from PyQt4.QtGui import *
 12
 13from tortoisehg.hgqt import qtlib
 14
 15class _LexerSelector(object):
 16    _lexer = None
 17    def match(self, filename, filedata):
 18        return False
 19
 20    def lexer(self, parent):
 21        """
 22        Return a configured instance of the lexer
 23        """
 24        return self.cfg_lexer(self._lexer(parent))
 25
 26    def cfg_lexer(self, lexer):
 27        font = qtlib.getfont('fontlog').font()
 28        lexer.setFont(font, -1)
 29        return lexer
 30
 31class _FilenameLexerSelector(_LexerSelector):
 32    """
 33    Base class for lexer selector based on file name matching
 34    """
 35    extensions = ()
 36    def match(self, filename, filedata):
 37        filename = filename.lower()
 38        for ext in self.extensions:
 39            if filename.endswith(ext):
 40                return True
 41        return False
 42
 43class _ScriptLexerSelector(_FilenameLexerSelector):
 44    """
 45    Base class for lexer selector based on content pattern matching
 46    """
 47    regex = None
 48    headersize = 3
 49    def match(self, filename, filedata):
 50        if super(_ScriptLexerSelector, self).match(filename, filedata):
 51            return True
 52        if self.regex and filedata:
 53            for line in filedata.splitlines()[:self.headersize]:
 54                if len(line)<1000 and self.regex.match(line):
 55                    return True
 56        return False
 57
 58class PythonLexerSelector(_ScriptLexerSelector):
 59    extensions = ('.py', '.pyw')
 60    _lexer = Qsci.QsciLexerPython
 61    regex = re.compile(r'^#[!].*python')
 62
 63class BashLexerSelector(_ScriptLexerSelector):
 64    extensions = ('.sh', '.bash')
 65    _lexer = Qsci.QsciLexerBash
 66    regex = re.compile(r'^#[!].*sh')
 67
 68class PerlLexerSelector(_ScriptLexerSelector):
 69    extensions = ('.pl', '.perl')
 70    _lexer = Qsci.QsciLexerPerl
 71    regex = re.compile(r'^#[!].*perl')
 72
 73class RubyLexerSelector(_ScriptLexerSelector):
 74    extensions = ('.rb', '.ruby')
 75    _lexer = Qsci.QsciLexerRuby
 76    regex = re.compile(r'^#[!].*ruby')
 77
 78class LuaLexerSelector(_ScriptLexerSelector):
 79    extensions = ('.lua', )
 80    _lexer = Qsci.QsciLexerLua
 81    regex = None
 82
 83class CppLexerSelector(_FilenameLexerSelector):
 84    extensions = ('.c', '.cpp', '.cxx', '.h', '.hpp', '.hxx')
 85    _lexer = Qsci.QsciLexerCPP
 86
 87class CSSLexerSelector(_FilenameLexerSelector):
 88    extensions = ('.css',)
 89    _lexer = Qsci.QsciLexerCSS
 90
 91class HTMLLexerSelector(_FilenameLexerSelector):
 92    extensions = ('.htm', '.html', '.xhtml', '.xml')
 93    _lexer = Qsci.QsciLexerHTML
 94
 95class VerilogLexerSelector(_FilenameLexerSelector):
 96    extensions = ('.v', '.vh')
 97    _lexer = Qsci.QsciLexerVerilog
 98
 99class VHDLLexerSelector(_FilenameLexerSelector):
100    extensions = ('.vhd', '.vhdl')
101    _lexer = Qsci.QsciLexerVHDL
102
103class BatchLexerSelector(_FilenameLexerSelector):
104    extensions = ('.cmd', '.bat')
105    _lexer = Qsci.QsciLexerBatch
106
107class MakeLexerSelector(_FilenameLexerSelector):
108    extensions = ('.mk', 'makefile')
109    _lexer = Qsci.QsciLexerMakefile
110
111class SQLLexerSelector(_FilenameLexerSelector):
112    extensions = ('.sql',)
113    _lexer = Qsci.QsciLexerSQL
114
115class JSLexerSelector(_FilenameLexerSelector):
116    extensions = ('.js',)
117    _lexer = Qsci.QsciLexerJavaScript
118
119class JavaLexerSelector(_FilenameLexerSelector):
120    extensions = ('.java',)
121    _lexer = Qsci.QsciLexerJava
122
123class TeXLexerSelector(_FilenameLexerSelector):
124    extensions = ('.tex', '.latex',)
125    _lexer = Qsci.QsciLexerTeX
126
127class DiffLexerSelector(_ScriptLexerSelector):
128    extensions = ()
129    _lexer = Qsci.QsciLexerDiff
130    regex = re.compile(r'^@@ [-]\d+,\d+ [+]\d+,\d+ @@$')
131    def cfg_lexer(self, lexer):
132        #lexer.setDefaultPaper(QtGui.QColor(cfg.getDiffBGColor()))
133        #lexer.setColor(QtGui.QColor(cfg.getDiffFGColor()), -1)
134        for label, i in (('diff.inserted', 6),
135                         ('diff.deleted', 5),
136                         ('diff.hunk', 4)):
137            effect = qtlib.geteffect(label)
138            for e in effect.split(';'):
139                if e.startswith('color:'):
140                    lexer.setColor(QColor(e[7:]), i)
141        font = qtlib.getfont('fontdiff').font()
142        lexer.setFont(font, -1)
143        return lexer
144
145
146lexers = []
147for clsname, cls in globals().items():
148    if clsname.startswith('_'):
149        continue
150    if isinstance(cls, type) and issubclass(cls, _LexerSelector):
151        #print clsname
152        lexers.append(cls())
153
154def get_diff_lexer(parent):
155    return DiffLexerSelector().lexer(parent)
156
157def get_lexer(filename, filedata, parent):
158    for lselector in lexers:
159        if lselector.match(filename, filedata):
160            return lselector.lexer(parent)
161    return None