Sublime Forum

Code Navigation

#1

I have a huge C++ project. I have CTags installed but that just lets me find function definitions pretty easily. How do I find all the places in a project where a particular function is being used?

I know there is a Find in Files feature but that doesn’t work for me because it does not allow me to search for multiple functions at the same time. Please let me know if there is a better way to do it or if there is some cool plugin out there which does that.

Thank you for your time.

0 Likes

#2

[quote=“ritwik”]
I know there is a Find in Files feature but that doesn’t work for me because it does not allow me to search for multiple functions at the same time[/quote]

Use a Regular Expression based search with it and you’ll find multiple functions.

0 Likes

#3

[quote=“BugFix”]

[quote=“ritwik”]
I know there is a Find in Files feature but that doesn’t work for me because it does not allow me to search for multiple functions at the same time[/quote]

Use a Regular Expression based search with it and you’ll find multiple functions.[/quote]

I was thinking more along the lines of clicking on the function or some such action and all the list of all usages of the function be populated in some sidebar kind of a thing or in a new sublime tab.

0 Likes

#4

OK, the following solution searches in all files of the open project for the called function name, that is given by input panel.
The result will shown as quick panel with entries “full_path#line_number”. If you select one - the related file will open and the function call gets the focus.
I’m not familiar with C++, but if I’ve read correctly, the functions will called with “func_name(optional_param);”. For this cases (also with spaces before and after the braces) works my pattern. If there are any other ways to call functions - you need to change the pattern.

Create a new PlugIn and insert the following code. Bind it in your keymap and/or add it to any menu. The command is: “search_funcs_in_project”

[code]import sublime
import sublime_plugin
import re

class SearchFuncsInProjectCommand(sublime_plugin.WindowCommand):
proj_files = None
found = None
panel_list = None
def run(self):
if self.get_project_files():
sublime.active_window().show_input_panel(" Function name to search: ",
‘’, self.on_param, None, None)

def on_param(self, user_input):
    self.found = {} # stores: {"file_path#line_number": [file_path, line_number]}
    self.panel_list = ] # stores: "file_path#line_number"]
    pattern = re.compile("%s%s" % (user_input, r'\s*\(^\)]*\)\s*;'))
    for i in range(0, len(self.proj_files)):
        content = None
        with open(self.proj_files*, 'r') as f:
            content = f.read().splitlines()
        if content:
            for j in range(0, len(content)):
                if re.search(pattern, content[j]):
                    key = "%s#%i" % (self.proj_files*, j+1)
                    self.panel_list.append(key)
                    self.found[key] = [self.proj_files[i], j]
    if len(self.panel_list) > 0:
        sublime.active_window().show_quick_panel(self.panel_list, self.on_selection)

def on_selection(self, index):
    if index != -1:
        key = self.panel_list[index]
        file = self.found[key][0]
        line = self.found[key][1]
        sublime.active_window().open_file(file)
        self.goto_line(line)

def get_project_files(self):
    proj_name = sublime.active_window().project_file_name()
    if not proj_name:
        sublime.message_dialog("None project open!")
        return
    proj_name = re.sub('project$', 'workspace', proj_name)
    with open(proj_name, 'r') as f:
        content = f.read()
    match = re.search(r'("buffers":\n\t\(^\]])+)', content, re.MULTILINE)
    if match:
        files = re.findall(r'"file":\s*"(^"]+)', match.group())
        if files:
            for i in range(0, len(files)):
                files* = re.sub('(^/)([A-Z])', r'\2:', files*)
                files* = re.sub('/', r'\\', files*)
            self.proj_files = files
            return True
    return False

def goto_line(self, line):
    view = sublime.active_window().active_view()
    pt = view.text_point(line, 0)
    vector = view.text_to_layout(pt)
    view.set_viewport_position(vector)
    view.sel().clear()
    view.sel().add(view.line(pt))

[/code]******

0 Likes