Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Default.sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
"command": "gtags_show_symbols",
"keys": ["ctrl+t", "ctrl+s"]
},
{
"command": "gtags_show_open_files_symbols",
"keys": ["ctrl+t", "ctrl+d"]
},
{ // override current default
"keys": ["ctrl+t"],
"command": "transpose",
"context": [
{ "key": "num_selections", "operator": "not_equal", "operand": 1 }
]
}
]
]
6 changes: 6 additions & 0 deletions Main.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
{
"command": "gtags_show_symbols"
},
{
"command": "gtags_show_open_files_symbols"
},
{
"command": "gtags_show_current_file_symbols"
},
{
"command": "gtags_rebuild_tags"
}
Expand Down
54 changes: 54 additions & 0 deletions gtags.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,60 @@ def rebuild(self):
print(stderr)
return success

def open_files_symbols(self):
files=" ".join(['"'+x.file_name()+'"' for x in sublime.active_window().views() if os.path.isfile(x.file_name())])

out=self.subprocess.stdout('global -q -f %s' % files)
if int(sublime.version()) >= 3000:
out = out.decode("utf-8")
return [line.split(" ",2)[0] for line in out.splitlines()]

def _find_includes(self, filename):
includes = set([])
if not os.path.isfile(filename):
return includes
with open(filename, 'r') as fp:
for line in fp:
line = line.lstrip();
if line.startswith("#include"):
quoted = re.search('^\s*#include\s+"(.*)"', line)
if quoted:
includes.add(quoted.group(1));
return includes

def _makefullpath(self, basepaths, filename):
for path in basepaths:
if os.path.isfile(path+"/"+filename):
return path+"/"+filename
return None

def _find_all_includes(self, basepaths, filename):
todo=set([filename])
done=set([])

while len(todo) > 0:
progress = todo.pop()
done.add(progress)
includes = self._find_includes(progress)
for i in includes:
j=self._makefullpath(basepaths,i)
if j and j not in todo and j not in done:
todo.add(j)
return done

def current_include_path(self, filename):
basepaths=[]
if not sublime.active_window().project_data():
basepaths.append(os.path.dirname(filename))
else:
projectpath = os.path.dirname(sublime.active_window().project_file_name())
for folder in sublime.active_window().project_data()['folders']:
basepaths.append(os.path.normpath(os.path.join(projectpath, folder['path'])))
files = self._find_all_includes(basepaths, filename)
out=self.subprocess.stdout("global -f '%s'" % "' '".join(files))
if int(sublime.version()) >= 3000:
out = out.decode("utf-8")
return [line.split(" ",2)[0] for line in out.splitlines()]

class GTagsTest(unittest.TestCase):
def test_start_with(self):
Expand Down
68 changes: 68 additions & 0 deletions gtagsplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,74 @@ def and_then(view, tags, root):
'No symbols found')


class ShowOpenFilesSymbolsThread(threading.Thread):
def __init__(self, view, tags, root):
threading.Thread.__init__(self)
self.view = view
self.tags = tags
self.root = root

def run(self):
symbols = self.tags.open_files_symbols()
self.success = len(symbols) > 0
if not self.success:
return

def on_select(index):
if index != -1:
definitions = self.tags.match(symbols[index])
gtags_jump_keyword(self.view, definitions, self.root)

sublime.set_timeout(
lambda: self.view.window().show_quick_panel(symbols, on_select), 0)


class GtagsShowOpenFilesSymbols(sublime_plugin.TextCommand):
def run(self, edit):
@run_on_cwd()
def and_then(view, tags, root):
thread = ShowOpenFilesSymbolsThread(view, tags, root)
thread.start()
ThreadProgress(thread,
'Getting symbols on %s' % root,
'Symbols have successfully obtained',
'No symbols found')

class ShowCurrentFileSymbolsThread(threading.Thread):
def __init__(self, view, tags, root, filename):
threading.Thread.__init__(self)
self.view = view
self.tags = tags
self.root = root
self.filename = filename

def run(self):
symbols = self.tags.current_include_path(self.filename)
self.success = len(symbols) > 0
if not self.success:
return

def on_select(index):
if index != -1:
definitions = self.tags.match(symbols[index])
gtags_jump_keyword(self.view, definitions, self.root)

sublime.set_timeout(
lambda: self.view.window().show_quick_panel(symbols, on_select), 0)


class GtagsShowCurrentFileSymbols(sublime_plugin.TextCommand):
def run(self, edit):
@run_on_cwd()
def and_then(view, tags, root):
thread = ShowCurrentFileSymbolsThread(view, tags, root, self.view.file_name())
thread.start()
ThreadProgress(thread,
'Getting symbols on %s' % root,
'Symbols have successfully obtained',
'No symbols found')


class GtagsNavigateToDefinition(sublime_plugin.TextCommand):
def run(self, edit):
@run_on_cwd()
Expand Down