示例#1
0
    def _check_refresh_patterns(self, info, ctx, force=False):

        # Concept of ctx['match_end']:
        #   for cm_refresh_pattern `\/`, and word pattern `[a-z/]`
        #   foo/bar gets `foo/`
        #   foo/bar/baz gets `foo/bar/`
        # It is useful for trigerring cm_refresh in the middle of a word

        patterns = info.get('cm_refresh_patterns', None)
        typed = ctx['typed']

        word_pattern = info.get('word_pattern',
                                None) or cm_default.word_pattern(ctx)

        # remove the last word, check whether the special pattern matches
        # word_removed
        end_word_matched = re.search(word_pattern + "$", typed)
        if end_word_matched:
            ctx['base'] = end_word_matched.group()
            ctx['startcol'] = ctx['col'] - len(ctx['base'].encode('utf-8'))
            word_removed = typed[:end_word_matched.start()]
            word_len = len(ctx['base'])
        else:
            ctx['base'] = ''
            ctx['startcol'] = ctx['col']
            word_removed = typed
            word_len = 0

        ctx['match_end'] = len(word_removed)

        # check source extra patterns
        if patterns:
            for pattern in patterns:
                # use greedy match '.*', to push the match to the last occurance
                # pattern
                if not pattern.startswith("^"):
                    pattern = '.*' + pattern

                matched = re.search(pattern, typed)
                if matched and matched.end() >= len(typed) - word_len:
                    ctx['match_end'] = matched.end()
                    return True

        min_len = info['cm_refresh_length']

        # always match
        if min_len == 0:
            return True

        if force and word_len > 0:
            return True

        if min_len > 0 and word_len >= min_len:
            return True

        return False
示例#2
0
    def _check_refresh_patterns(self,info,ctx,force=False):

        # Concept of ctx['match_end']:
        #   for cm_refresh_pattern `\/`, and word pattern `[a-z/]`
        #   foo/bar gets `foo/`
        #   foo/bar/baz gets `foo/bar/`
        # It is useful for trigerring cm_refresh in the middle of a word

        patterns = info.get('cm_refresh_patterns',None)
        typed = ctx['typed']

        word_pattern = info.get('word_pattern', None) or cm_default.word_pattern(ctx)

        # remove the last word, check whether the special pattern matches
        # word_removed
        end_word_matched = re.search(word_pattern + "$",typed)
        if end_word_matched:
            ctx['base']       = end_word_matched.group()
            ctx['startcol']   = ctx['col'] - len(ctx['base'].encode('utf-8'))
            word_removed      = typed[:end_word_matched.start()]
            word_len          = len(ctx['base'])
        else:
            ctx['base']       = ''
            ctx['startcol']   = ctx['col']
            word_removed      = typed
            word_len          = 0

        ctx['match_end'] = len(word_removed)

        # check source extra patterns
        if patterns:
            for pattern in patterns:
                # use greedy match '.*', to push the match to the last occurance
                # pattern
                if not pattern.startswith("^"):
                    pattern = '.*' + pattern

                matched = re.search(pattern, typed)
                if matched and matched.end() >= len(typed)-word_len:
                    ctx['match_end'] = matched.end()
                    return True

        min_len = info['cm_refresh_length']

        # always match
        if min_len==0:
            return True

        if force and word_len>0:
            return True

        if min_len > 0 and word_len >= min_len:
            return True

        return False
示例#3
0
    def _check_refresh_patterns(self, info, ctx, force=False):

        patterns = info.get('cm_refresh_patterns', None)
        typed = ctx['typed']
        is_matched = False

        word_pattern = info.get('word_pattern',
                                None) or cm_default.word_pattern(ctx)

        # check source specific patterns
        if patterns:
            for pattern in patterns:
                matched = re.search(pattern, typed)
                if matched:
                    groups = matched.groups()
                    if groups and matched.end(len(groups)) == len(typed):
                        # last group at the end, calculate startcol for it
                        ctx['startcol'] = ctx['col'] - len(groups[-1])
                        ctx['base'] = groups[-1]
                    else:
                        m = re.search(word_pattern + "$", typed)
                        if m:
                            span = m.span()
                            ctx['base'] = ctx['typed'][span[0]:span[1]]
                            ctx['startcol'] = ctx['col'] - len(ctx['base'])
                        else:
                            # this is a source specific match, keep going with empty base
                            ctx['base'] = ''
                            ctx['startcol'] = ctx['col']
                    return True

        m = re.search(word_pattern + "$", typed)

        if not m:
            return False

        span = m.span()
        ctx['base'] = ctx['typed'][span[0]:span[1]]
        ctx['startcol'] = ctx['col'] - len(ctx['base'])

        minimum_length = info['cm_refresh_min_word_len']
        if len(ctx['base']) < minimum_length and not force:
            return False
        else:
            return True
示例#4
0
    def _check_refresh_patterns(self,info,ctx,force=False):

        patterns = info.get('cm_refresh_patterns',None)
        typed = ctx['typed']
        is_matched = False

        word_pattern = info.get('word_pattern',None) or cm_default.word_pattern(ctx)

        # remove the last word, check whether the special pattern matches
        # last_word_removed
        end_word_matched = re.search(word_pattern + "$",typed)
        if end_word_matched:
            ctx['base']       = end_word_matched.group()
            ctx['startcol']   = ctx['col'] - len(ctx['base'].encode('utf-8'))
            last_word_removed = typed[:end_word_matched.start()]
            word_len          = len(ctx['base'])
        else:
            ctx['base']       = ''
            ctx['startcol']   = ctx['col']
            last_word_removed = typed
            word_len          = 0

        minimum_length = info['cm_refresh_min_word_len']

        # always match
        if minimum_length==0:
            return True

        if force and word_len>0:
            return True

        if word_len >= minimum_length:
            return True

        # check source extra patterns
        if patterns:
            for pattern in patterns:
                matched = re.search(pattern, last_word_removed)
                if matched:
                    return True

        return False
    def refresh_keyword(self, ctx, all=True, expand=50):
        word_pattern = cm_default.word_pattern(ctx)
        compiled = re.compile(word_pattern)
        logger.info('refreshing_keyword, word_pattern [%s]', word_pattern)

        buffer = self.nvim.current.buffer

        if all:
            self._words = set()
            begin = 0
            end = len(buffer)
        else:
            begin = max(ctx['lnum'] - 50, 0)
            end = min(ctx['lnum'] + 50, len(buffer))

        logger.info('keyword refresh begin, current count: %s',
                    len(self._words))

        cur_lnum = ctx['lnum']
        cur_col = ctx['col']

        step = 1000
        for num in range(begin, end, step):
            lines = buffer[num:num + step]
            # convert 0 base to 1 base
            lnum = num + 1
            for line in lines:
                if lnum == cur_lnum:
                    for word in compiled.finditer(line):
                        span = word.span()
                        # filter-out the word at current cursor
                        if (cur_col >= span[0] + 1) and (cur_col - 1 <=
                                                         span[1] + 1):
                            continue
                        self._words.add(word.group())
                else:
                    for word in compiled.finditer(line):
                        self._words.add(word.group())
                lnum += 1

        logger.info('keyword refresh complete, count: %s', len(self._words))
示例#6
0
    def refresh_keyword(self,ctx,all=True,expand=50):
        word_pattern = cm_default.word_pattern(ctx)
        compiled = re.compile(word_pattern)
        logger.info('refreshing_keyword, word_pattern [%s]', word_pattern)

        buffer = self.nvim.current.buffer

        if all:
            self._words = set()
            begin = 0
            end = len(buffer)
        else:
            begin = max(ctx['lnum']-50,0)
            end = min(ctx['lnum']+50,len(buffer))

        logger.info('keyword refresh begin, current count: %s', len(self._words))

        cur_lnum = ctx['lnum']
        cur_col = ctx['col']

        step = 1000
        for num in range(begin,end,step):
            lines = buffer[num:num+step]
            # convert 0 base to 1 base
            lnum = num+1
            for line in lines:
                if lnum == cur_lnum:
                    for word in compiled.finditer(line):
                        span = word.span()
                        # filter-out the word at current cursor
                        if (cur_col>=span[0]+1) and (cur_col-1<=span[1]+1):
                            continue
                        self._words.add(word.group())
                else:
                    for word in compiled.finditer(line):
                        self._words.add(word.group())
                lnum += 1

        logger.info('keyword refresh complete, count: %s', len(self._words))