def __search_symbol_and_rename(tu, symbol_usr, new_name, prompt): if tu is None: return symbols = [] clighter_helper.search_cursors_by_usr(tu.cursor, symbol_usr, symbols) if not symbols: return # all symbols with the same name old_name = clighter_helper.get_spelling_or_displayname(symbols[0]) locs = set() for sym in symbols: clighter_helper.search_referenced_tokens(tu, sym, locs) if len(locs): if prompt >= 1: if vim.eval( "confirm(\"found symbols in {0}, rename them?\", \"&Yes\n&No\", 1)" .format(vim.current.buffer.name)) == "2": return __vim_multi_replace(locs, old_name, new_name, prompt)
def __search_usr_and_rename_refs(tu, usr, new_name): if tu is None: return symbols = [] clighter_helper.find_cursors_by_usr(tu.cursor, usr, symbols) if not symbols: return # all symbols with the same name old_name = clighter_helper.get_spelling_or_displayname(symbols[0]) locs = set() for sym in symbols: clighter_helper.search_ref_tokens(tu, sym, locs) if len(locs): if vim.vars['clighter_rename_prompt_level'] >= 1: if vim.bindeval( "confirm(\"found symbols in {0}, rename them?\", \"&Yes\n&No\", 1)".format( vim.current.buffer.name)) == 2: return __vim_multi_replace( locs, old_name, new_name, vim.vars['clighter_rename_prompt_level'])
def __search_symbol_and_rename(tu, symbol_usr, new_name, prompt): if not tu: return symbols = [] clighter_helper.search_cursors_by_usr(tu.cursor, symbol_usr, symbols) if not symbols: return # all symbols with the same name old_name = clighter_helper.get_spelling_or_displayname(symbols[0]) locs = set() for sym in symbols: clighter_helper.search_referenced_tokens(tu, sym, locs) if len(locs): if prompt >= 1: if vim.eval( 'confirm("found symbols in {0}, rename them?", "&Yes\n&No", 1)'.format( vim.current.buffer.name)) == "2": return __vim_multi_replace( locs, old_name, new_name, prompt)
def rename(clang_service): clang_service.update_buffers(__get_bufctx_list()) clang_service.parse_all(vim.eval('g:clighter_compile_args')) cc = clang_service.get_cc(vim.current.buffer.name) if not cc: return tu = cc.current_tu if not tu: return symbol = clighter_helper.get_vim_symbol( clighter_helper.get_vim_cursor(tu, tu.get_file(cc.name))) if not symbol: return old_name = clighter_helper.get_spelling_or_displayname(symbol) vim.command("echohl WildMenu") new_name = vim.eval( "input(' Rename {0} : ', '{1}')".format(old_name, old_name)) vim.command("echohl None") if not new_name or old_name == new_name: return print ' ' pos = vim.current.window.cursor locs = set() locs.add((symbol.location.line, symbol.location.column, symbol.location.file.name)) clighter_helper.search_referenced_tokens( tu, symbol, locs) prompt = string.atoi(vim.eval('g:clighter_rename_prompt_level')) __vim_multi_replace( locs, old_name, new_name, prompt) if clighter_helper.is_global_symbol( symbol) and vim.eval('g:clighter_enable_cross_rename') == '1': __cross_buffer_rename( clang_service, symbol.get_usr(), new_name, prompt) vim.current.window.cursor = pos clang_service.update_buffers(__get_bufctx_list()) clang_service.switch(vim.current.buffer.name)
def rename(clang_service): clang_service.update_buffers(__get_bufctx_list(), False) clang_service.parse_all( string.atoi(vim.eval('g:clighter_heuristic_compile_args')), vim.bindeval('g:clighter_compile_args')) cc = clang_service.get_cc(vim.current.buffer.name) if not cc: return tu = cc.current_tu if not tu: return symbol = clighter_helper.get_vim_symbol( clighter_helper.get_vim_cursor(tu, tu.get_file(cc.name))) if not symbol: return old_name = clighter_helper.get_spelling_or_displayname(symbol) vim.command("echohl WildMenu") new_name = vim.eval("input(' Rename {0} : ', '{1}')".format( old_name, old_name)) vim.command("echohl None") if not new_name or old_name == new_name: return print ' ' pos = vim.current.window.cursor locs = set() locs.add((symbol.location.line, symbol.location.column, symbol.location.file.name)) clighter_helper.search_referenced_tokens(tu, symbol, locs) prompt = string.atoi(vim.eval('g:clighter_rename_prompt_level')) __vim_multi_replace(locs, old_name, new_name, prompt) if clighter_helper.is_global_symbol(symbol) and vim.eval( 'g:clighter_enable_cross_rename') == '1': __cross_buffer_rename(clang_service, symbol.get_usr(), new_name, prompt) vim.current.window.cursor = pos clang_service.update_buffers(__get_bufctx_list(), True)
def rename(clang_service): tu_ctx = clang_service.get_tu_ctx(vim.current.buffer.name) if tu_ctx is None: return clang_service.update_unsaved_dict(__get_buffer_dict(), False) clang_service.parse_all() vim_cursor, def_cursor = clighter_helper.get_vim_cursor_and_def(tu_ctx) if vim_cursor is None or def_cursor is None: return old_name = clighter_helper.get_spelling_or_displayname(def_cursor) vim.command("echohl WildMenu") new_name = vim.bindeval( "input(' Rename {0} : ', '{1}')".format(old_name, old_name)) vim.command("echohl None") if not new_name or old_name == new_name: return print ' ' pos = vim.current.window.cursor locs = set() locs.add((def_cursor.location.line, def_cursor.location.column, def_cursor.location.file.name)) clighter_helper.search_ref_tokens( tu_ctx.translation_unit, def_cursor, locs) __vim_multi_replace( locs, old_name, new_name, vim.vars['clighter_rename_prompt_level']) if clighter_helper.is_symbol_cursor( def_cursor) and vim.vars['clighter_enable_cross_rename'] == 1: __cross_buffer_rename(clang_service, def_cursor.get_usr(), new_name) vim.current.window.cursor = pos clang_service.update_unsaved_dict(__get_buffer_dict(), True)
def rename(clang_service): tu_ctx = clang_service.get_tu_ctx(vim.current.buffer.name) if tu_ctx is None: return clang_service.update_unsaved_dict(__get_buffer_dict(), False) clang_service.parse_all() vim_cursor, def_cursor = clighter_helper.get_vim_cursor_and_def(tu_ctx) if vim_cursor is None or def_cursor is None: return old_name = clighter_helper.get_spelling_or_displayname(def_cursor) vim.command("echohl WildMenu") new_name = vim.bindeval("input(' Rename {0} : ', '{1}')".format( old_name, old_name)) vim.command("echohl None") if not new_name or old_name == new_name: return print ' ' pos = vim.current.window.cursor locs = set() locs.add((def_cursor.location.line, def_cursor.location.column, def_cursor.location.file.name)) clighter_helper.search_ref_tokens(tu_ctx.translation_unit, def_cursor, locs) __vim_multi_replace(locs, old_name, new_name, vim.vars['clighter_rename_prompt_level']) if clighter_helper.is_symbol_cursor( def_cursor) and vim.vars['clighter_enable_cross_rename'] == 1: __cross_buffer_rename(clang_service, def_cursor.get_usr(), new_name) vim.current.window.cursor = pos clang_service.update_unsaved_dict(__get_buffer_dict(), True)
def highlight_window(clang_service, extend=50): tu_ctx = clang_service.get_tu_ctx(vim.current.buffer.name) if tu_ctx is None: clear_highlight() return tu = tu_ctx.translation_unit if tu is None: clear_highlight() return (top, bottom) = (vim.bindeval("line('w0')"), vim.bindeval("line('w$')")) draw_syntax = False draw_def_ref = False if highlight_window.hl_tick < clang_service.parse_tick or highlight_window.syntactic_range is None or top < highlight_window.syntactic_range[ 0] or bottom > highlight_window.syntactic_range[1]: draw_syntax = True vim.command("call s:clear_match_pri([{0}])".format(SYNTAX_PRI)) highlight_window.hl_tick = clang_service.parse_tick if vim.vars["ClighterCursorHL"] == 1: vim_cursor, def_cursor = clighter_helper.get_vim_cursor_and_def(tu_ctx) if highlight_window.highlighted_define_cur is not None and( def_cursor is None or highlight_window.highlighted_define_cur != def_cursor): vim.command("call s:clear_match_pri([{0}])".format(DEF_REF_PRI)) if def_cursor is not None and( highlight_window. highlighted_define_cur is None or highlight_window.highlighted_define_cur != def_cursor): draw_def_ref = True # special case for preprocessor if def_cursor.kind.is_preprocessing( ) and def_cursor.location.file.name == vim.current.buffer.name: __vim_matchaddpos( 'clighterCursorDefRef', def_cursor.location.line, def_cursor.location.column, len( clighter_helper.get_spelling_or_displayname(def_cursor)), DEF_REF_PRI) highlight_window.highlighted_define_cur = def_cursor if not draw_syntax and not draw_def_ref: return target_range = [top, bottom] if draw_syntax: buflinenr = len(vim.current.buffer) target_range = [max(top - extend, 1), min(bottom + extend, buflinenr)] highlight_window.syntactic_range = target_range file = tu.get_file(tu_ctx.bufname) tokens = tu.get_tokens( extent=cindex.SourceRange.from_locations( cindex.SourceLocation.from_position( tu, file, target_range[0], 1), cindex.SourceLocation.from_position( tu, file, target_range[1] + 1, 1))) for t in tokens: """ Do semantic highlighting' """ if t.kind.value != 2: continue t_cursor = cindex.Cursor.from_location( tu, cindex.SourceLocation.from_position( tu, file, t.location.line, t.location.column)) # cursor under vim if draw_syntax: __draw_token(t.location.line, t.location.column, len( t.spelling), t_cursor.kind, t_cursor.type.kind) """ Do definition/reference highlighting' """ if draw_def_ref: t_def_cursor = clighter_helper.get_semantic_definition(t_cursor) if t_def_cursor is not None and t_def_cursor == highlight_window.highlighted_define_cur: __vim_matchaddpos( 'clighterCursorDefRef', t.location.line, t.location.column, len( t.spelling), DEF_REF_PRI)
def highlight_window(clang_service, extend=50): tu_ctx = clang_service.get_tu_ctx(vim.current.buffer.name) if tu_ctx is None: clear_highlight() return tu = tu_ctx.translation_unit if tu is None: clear_highlight() return (top, bottom) = (vim.bindeval("line('w0')"), vim.bindeval("line('w$')")) draw_syntax = False draw_def_ref = False if highlight_window.hl_tick < clang_service.parse_tick or highlight_window.syntactic_range is None or top < highlight_window.syntactic_range[ 0] or bottom > highlight_window.syntactic_range[1]: draw_syntax = True vim.command("call s:clear_match_pri([{0}])".format(SYNTAX_PRI)) highlight_window.hl_tick = clang_service.parse_tick if vim.vars["ClighterCursorHL"] == 1: vim_cursor, def_cursor = clighter_helper.get_vim_cursor_and_def(tu_ctx) if highlight_window.highlighted_define_cur is not None and ( def_cursor is None or highlight_window.highlighted_define_cur != def_cursor): vim.command("call s:clear_match_pri([{0}])".format(DEF_REF_PRI)) if def_cursor is not None and ( highlight_window.highlighted_define_cur is None or highlight_window.highlighted_define_cur != def_cursor): draw_def_ref = True # special case for preprocessor if def_cursor.kind.is_preprocessing( ) and def_cursor.location.file.name == vim.current.buffer.name: __vim_matchaddpos( 'clighterCursorDefRef', def_cursor.location.line, def_cursor.location.column, len(clighter_helper.get_spelling_or_displayname( def_cursor)), DEF_REF_PRI) highlight_window.highlighted_define_cur = def_cursor if not draw_syntax and not draw_def_ref: return target_range = [top, bottom] if draw_syntax: buflinenr = len(vim.current.buffer) target_range = [max(top - extend, 1), min(bottom + extend, buflinenr)] highlight_window.syntactic_range = target_range file = tu.get_file(tu_ctx.bufname) tokens = tu.get_tokens(extent=cindex.SourceRange.from_locations( cindex.SourceLocation.from_position(tu, file, target_range[0], 1), cindex.SourceLocation.from_position(tu, file, target_range[1] + 1, 1))) for t in tokens: """ Do semantic highlighting' """ if t.kind.value != 2: continue t_cursor = cindex.Cursor.from_location( tu, cindex.SourceLocation.from_position( tu, file, t.location.line, t.location.column)) # cursor under vim if draw_syntax: __draw_token(t.location.line, t.location.column, len(t.spelling), t_cursor.kind, t_cursor.type.kind) """ Do definition/reference highlighting' """ if draw_def_ref: t_def_cursor = clighter_helper.get_semantic_definition(t_cursor) if t_def_cursor is not None and t_def_cursor == highlight_window.highlighted_define_cur: __vim_matchaddpos('clighterCursorDefRef', t.location.line, t.location.column, len(t.spelling), DEF_REF_PRI)