Skip to content

Instantly share code, notes, and snippets.

@BlackMac
Created February 14, 2012 09:48
Show Gist options
  • Save BlackMac/1825401 to your computer and use it in GitHub Desktop.
Save BlackMac/1825401 to your computer and use it in GitHub Desktop.
autocomplete over project for Sublime Text 2
# CTags based autocompletion plugin for Sublime Text 2
# You can add the file to the User Package in ~/Library/Application Support/Sublime Text 2/Packages and restart Sublime Text 2.
# generate the .tags file in your project root with "ctags -R -f .tags"
import sublime, sublime_plugin, os
class AutocompleteAll(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
tags_path = view.window().folders()[0]+"/.tags"
results=[]
if (not view.window().folders() or not os.path.exists(tags_path)): #check if a project is open and the .tags file exists
return results
f=os.popen("grep -i '^"+prefix+"' '"+tags_path+"' | awk '{ print $1 }'") # grep tags from project directory .tags file
for i in f.readlines():
results.append([i.strip()])
results = [(item,item) for sublist in results for item in sublist] #flatten
results = list(set(results)) # make unique
results.sort() # sort
return results
@barneywilliams
Copy link

I am the maintainer of the CTags plugin. It would be by far best to integrate this into that plugin, rather than use a separate one. It keeps a dict loaded in RAM, and also sorts the tags file for easier lookup.

It already has a command to list all tags in the quick panel. I think this could tack onto that pretty easily. It just needs to load this list into the completions. I also had filed ticket to add support for ./-> completions (SublimeText/CTags#75). That will be a bit more involved, but along the same vein, and definitely desirable.

@Grawl
Copy link

Grawl commented Aug 8, 2013

I tried this and got ctags: illegal option -- R in Terminal.

@iainsmith
Copy link

@barneywilliams gettings this in CTags would be great.

@chucai
Copy link

chucai commented Nov 30, 2013

Cool! It's great !

@rscarvalho
Copy link

@Grawl this is because you are using the Mac OS X shipped version of ctags. Install ctags from homebrew and use the one located at /usr/local/bin/ctags.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment