def process_csv_buffer(pl, buffer, line, col, display_name): global csv_cache if csv_cache is None: csv_cache = register_buffer_cache( defaultdict(lambda: (None, None, None))) try: cur_first_line = buffer[0] except UnicodeDecodeError: cur_first_line = vim.eval('strtrans(getline(1))') dialect, has_header, first_line = csv_cache[buffer.number] if dialect is None or (cur_first_line != first_line and display_name == 'auto'): try: text = '\n'.join(buffer[:CSV_SNIFF_LINES]) except UnicodeDecodeError: # May happen in Python 3 text = vim.eval( 'join(map(getline(1, {0}), "strtrans(v:val)"), "\\n")'.format( CSV_SNIFF_LINES)) try: dialect, has_header = detect_text_csv_dialect(text, display_name) except csv.Error as e: pl.warn('Failed to detect csv format: {0}', str(e)) # Try detecting using three lines only: if line == 1: rng = (0, line + 2) elif line == len(buffer): rng = (line - 3, line) else: rng = (line - 2, line + 1) try: dialect, has_header = detect_text_csv_dialect( '\n'.join(buffer[rng[0]:rng[1]]), display_name, header_text='\n'.join(buffer[:4]), ) except csv.Error as e: pl.error('Failed to detect csv format: {0}', str(e)) return None, None if len(buffer) > 2: csv_cache[buffer.number] = dialect, has_header, cur_first_line column_number = len( read_csv( buffer[max(0, line - CSV_PARSE_LINES):line - 1] + [buffer[line - 1][:col]], dialect=dialect, fin=list, )[-1]) or 1 if has_header: try: header = read_csv(buffer[0:1], dialect=dialect) except UnicodeDecodeError: header = read_csv([vim.eval('strtrans(getline(1))')], dialect=dialect) column_name = header[column_number - 1] else: column_name = None return unicode(column_number), column_name
def process_csv_buffer(pl, buffer, line, col, display_name): global csv_cache if csv_cache is None: csv_cache = register_buffer_cache(defaultdict(lambda: (None, None, None))) try: cur_first_line = buffer[0] except UnicodeDecodeError: cur_first_line = vim.eval('strtrans(getline(1))') dialect, has_header, first_line = csv_cache[buffer.number] if dialect is None or (cur_first_line != first_line and display_name == 'auto'): try: text = '\n'.join(buffer[:CSV_SNIFF_LINES]) except UnicodeDecodeError: # May happen in Python 3 text = vim.eval('join(map(getline(1, {0}), "strtrans(v:val)"), "\\n")'.format(CSV_SNIFF_LINES)) try: dialect, has_header = detect_text_csv_dialect(text, display_name) except csv.Error as e: pl.warn('Failed to detect csv format: {0}', str(e)) # Try detecting using three lines only: if line == 1: rng = (0, line + 2) elif line == len(buffer): rng = (line - 3, line) else: rng = (line - 2, line + 1) try: dialect, has_header = detect_text_csv_dialect( '\n'.join(buffer[rng[0]:rng[1]]), display_name, header_text='\n'.join(buffer[:4]), ) except csv.Error as e: pl.error('Failed to detect csv format: {0}', str(e)) return None, None if len(buffer) > 2: csv_cache[buffer.number] = dialect, has_header, cur_first_line column_number = len(read_csv( buffer[max(0, line - CSV_PARSE_LINES):line - 1] + [buffer[line - 1][:col]], dialect=dialect, fin=list, )[-1]) or 1 if has_header: try: header = read_csv(buffer[0:1], dialect=dialect) except UnicodeDecodeError: header = read_csv([vim.eval('strtrans(getline(1))')], dialect=dialect) column_name = header[column_number - 1] else: column_name = None return unicode(column_number), column_name
def trailing_whitespace(pl, segment_info): '''Return the line number for trailing whitespaces It is advised not to use this segment in insert mode: in Insert mode it will iterate over all lines in buffer each time you happen to type a character which may cause lags. It will also show you whitespace warning each time you happen to type space. Highlight groups used: ``trailing_whitespace`` or ``warning``. ''' global trailing_whitespace_cache if trailing_whitespace_cache is None: trailing_whitespace_cache = register_buffer_cache( defaultdict(lambda: (0, None))) bufnr = segment_info['bufnr'] changedtick = getbufvar(bufnr, 'changedtick') if trailing_whitespace_cache[bufnr][0] == changedtick: return trailing_whitespace_cache[bufnr][1] else: buf = segment_info['buffer'] bws = b' \t' sws = str(' \t') # Ignore unicode_literals and use native str. for i in range(len(buf)): try: line = buf[i] except UnicodeDecodeError: # May happen in Python 3 if hasattr(vim, 'bindeval'): line = vim.bindeval('getbufline({0}, {1})'.format( bufnr, i + 1)) has_trailing_ws = (line[-1] in bws) else: line = vim.eval('strtrans(getbufline({0}, {1}))'.format( bufnr, i + 1)) has_trailing_ws = (line[-1] in bws) else: has_trailing_ws = (line and line[-1] in sws) if has_trailing_ws: break if has_trailing_ws: ret = [{ 'contents': str(i + 1), 'highlight_groups': ['trailing_whitespace', 'warning'], }] else: ret = None trailing_whitespace_cache[bufnr] = (changedtick, ret) return ret
def trailing_whitespace(pl, segment_info): '''Return the line number for trailing whitespaces It is advised not to use this segment in insert mode: in Insert mode it will iterate over all lines in buffer each time you happen to type a character which may cause lags. It will also show you whitespace warning each time you happen to type space. Highlight groups used: ``trailing_whitespace`` or ``warning``. ''' global trailing_whitespace_cache if trailing_whitespace_cache is None: trailing_whitespace_cache = register_buffer_cache(defaultdict(lambda: (0, None))) bufnr = segment_info['bufnr'] changedtick = getbufvar(bufnr, 'changedtick') if trailing_whitespace_cache[bufnr][0] == changedtick: return trailing_whitespace_cache[bufnr][1] else: buf = segment_info['buffer'] bws = b' \t' sws = str(bws) for i in range(len(buf)): try: line = buf[i] except UnicodeDecodeError: # May happen in Python 3 if hasattr(vim, 'bindeval'): line = vim.bindeval('getbufline({0}, {1})'.format( bufnr, i + 1)) has_trailing_ws = (line[-1] in bws) else: line = vim.eval('strtrans(getbufline({0}, {1}))'.format( bufnr, i + 1)) has_trailing_ws = (line[-1] in bws) else: has_trailing_ws = (line and line[-1] in sws) if has_trailing_ws: break if has_trailing_ws: ret = [{ 'contents': str(i + 1), 'highlight_groups': ['trailing_whitespace', 'warning'], }] else: ret = None trailing_whitespace_cache[bufnr] = (changedtick, ret) return ret