Exemple #1
0
 def load(self, dataHash):
     PMXBundleItem.load(self, dataHash)
     for key in PMXCommand.KEYS:
         value = dataHash.get(key, None)
         if value != None and key in ['capturePattern']:
             value = compileRegexp(value)
         setattr(self, key, value)
Exemple #2
0
 def load(self, dataHash):
     PMXBundleItem.load(self, dataHash)
     for key in PMXCommand.KEYS:
         value = dataHash.get(key, None)
         if value != None and key in [    'capturePattern' ]:
             value = compileRegexp( value )
         setattr(self, key, value)
Exemple #3
0
 def transform(self, text, processor):
     flags = [OPTION_CAPTURE_GROUP]
     if self.option_multiline:
         flags.append(OPTION_MULTILINE)
     pattern = compileRegexp(unicode(self.pattern), flags)
     result = ""
     for child in self:
         if isinstance(child, TextNode):
             repl = self.prepare_replacement(unicode(child))
             result += pattern.sub(repl, text)
         elif isinstance(child, Condition):
             for match in pattern.finditer(text):
                 repl = child.index <= len(match.groups(
                 )) != None and child.insertion or child.otherwise
                 if repl != None:
                     repl = self.prepare_replacement(repl)
                     result += pattern.sub(repl, match.group(0))
                 if not self.option_global:
                     break
     if any(map(lambda r: result.find(r) != -1, ['\u', '\U'])):
         result = Regexp.uppercase(result)
     if any(map(lambda r: result.find(r) != -1, ['\L'])):
         result = Regexp.lowercase(result)
     return result.replace('\n', '\n' + processor.indentation).replace(
         '\t', processor.tabreplacement)
Exemple #4
0
 def update(self, dataHash):
     for key in self.KEYS:
         value = dataHash.get(key, None)
         if value != None:
             if key in [ 'decreaseIndentPattern', 'increaseIndentPattern', 'indentNextLinePattern', 'unIndentedLinePattern' ]:
                 value = compileRegexp( value )
             elif key in [ 'shellVariables' ]:
                 value = dict(map(lambda d: (d['name'], d['value']), value))
             elif key in [ 'symbolTransformation' ]:
                 value = map(lambda value: value.strip(), value.split(";"))
             elif key in [ 'showInSymbolList' ]:
                 value = bool(int(value))
             elif key in [ 'spellChecking' ]:
                 value = bool(int(value))
         setattr(self, key, value)
Exemple #5
0
 def transform(self, text, processor):
     flags = [OPTION_CAPTURE_GROUP]
     if self.option_multiline:
         flags.append(OPTION_MULTILINE)
     pattern = compileRegexp(unicode(self.pattern), flags)
     result = ""
     for child in self:
         if isinstance(child, TextNode):
             repl = self.prepare_replacement(unicode(child))
             result += pattern.sub(repl, text)
         elif isinstance(child, Condition):
             for match in pattern.finditer(text):
                 repl = child.index <= len(match.groups()) != None and child.insertion or child.otherwise
                 if repl != None:
                     repl = self.prepare_replacement(repl)
                     result += pattern.sub(repl, match.group(0))
                 if not self.option_global:
                     break
     if any(map(lambda r: result.find(r) != -1, ['\u', '\U'])):
         result = Regexp.uppercase(result)
     if any(map(lambda r: result.find(r) != -1, ['\L'])):
         result = Regexp.lowercase(result)
     return result.replace('\n', '\n' + processor.indentation).replace('\t', processor.tabreplacement)
Exemple #6
0
class Regexp(NodeList):
    _repl_re = compileRegexp(u"\$(?:(\d+)|g<(.+?)>)")

    def __init__(self, scope, parent=None):
        super(Regexp, self).__init__(scope, parent)
        self.pattern = ""
        self.options = None

    def open(self, scope, text):
        node = self
        if scope == 'string.regexp.format':
            self.pattern += text[:-1]
        elif scope == 'meta.structure.condition.regexp':
            self.append(text.replace('\\n', '\n').replace('\\t', '\t'))
            node = Condition(scope, self)
            self.append(node)
        elif scope == 'constant.character.escape.regexp':
            #Escape in pattern
            if isinstance(self.pattern, basestring):
                self.pattern += text
            else:
                self.append(text.replace('\\n', '\n').replace('\\t', '\t'))
        else:
            return super(Regexp, self).open(scope, text)
        return node

    def close(self, scope, text):
        node = self
        if scope == 'string.regexp.format':
            self.append(text)
        elif scope == 'string.regexp.options':
            self.options = text
        elif scope == 'constant.character.escape.regexp':
            #Escape in pattern
            if isinstance(self.pattern, basestring):
                self.pattern += text
            else:
                self.append(text)
        else:
            return super(Regexp, self).close(scope, text)
        return node

    @staticmethod
    def uppercase(text):
        titles = text.split('\u')
        if len(titles) > 1:
            text = "".join(
                [titles[0]] +
                map(lambda txt: txt[0].upper() + txt[1:], titles[1:]))
        uppers = text.split('\U')
        if len(uppers) > 1:
            text = "".join([uppers[0]] + map(
                lambda txt: txt.find('\E') != -1 and txt[:txt.find('\E')].
                upper() + txt[txt.find('\E') + 2:] or txt.upper(), uppers))
        return text

    @staticmethod
    def lowercase(text):
        lowers = text.split('\L')
        if len(lowers) > 1:
            text = "".join([lowers[0]] + map(
                lambda txt: txt.find('\E') != -1 and txt[:txt.find('\E')].
                lower() + txt[txt.find('\E') + 2:] or txt.lower(), lowers))
        return text

    @staticmethod
    def prepare_replacement(text):
        repl = None

        def expand(m, template):
            def handle(match):
                numeric, named = match.groups()
                if numeric:
                    return m.group(int(numeric)) or ""
                return m.group(named) or ""

            return Regexp._repl_re.sub(handle, template)

        if '$' in text:
            repl = lambda m, r=text: expand(m, r)
        else:
            repl = lambda m, r=text: r
        return repl

    def transform(self, text, processor):
        flags = [OPTION_CAPTURE_GROUP]
        if self.option_multiline:
            flags.append(OPTION_MULTILINE)
        pattern = compileRegexp(unicode(self.pattern), flags)
        result = ""
        for child in self:
            if isinstance(child, TextNode):
                repl = self.prepare_replacement(unicode(child))
                result += pattern.sub(repl, text)
            elif isinstance(child, Condition):
                for match in pattern.finditer(text):
                    repl = child.index <= len(match.groups(
                    )) != None and child.insertion or child.otherwise
                    if repl != None:
                        repl = self.prepare_replacement(repl)
                        result += pattern.sub(repl, match.group(0))
                    if not self.option_global:
                        break
        if any(map(lambda r: result.find(r) != -1, ['\u', '\U'])):
            result = Regexp.uppercase(result)
        if any(map(lambda r: result.find(r) != -1, ['\L'])):
            result = Regexp.lowercase(result)
        return result.replace('\n', '\n' + processor.indentation).replace(
            '\t', processor.tabreplacement)

    @property
    def option_global(self):
        return self.options != None and 'g' in self.options or False

    @property
    def option_multiline(self):
        return self.options != None and 'm' in self.options or False