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
@griffinqiu
Copy link

How to use...

@BlackMac
Copy link
Author

You can add the file to the User Package in ~/Library/Application Support/Sublime Text 2/Packages and restart Sublime Text 2. Don't forget to generate the .tags file as explained in the comments.

@guillemcanal
Copy link

Hey there ;)
Nice piece of code.
Since i'm not very fluent in python and the command line for regex operations, may I ask you a question :
Is it possible to return function's argument like so : fonction_name($args1, $arg2, $so_on)

Thanks a lot!

@gs
Copy link

gs commented Jul 20, 2012

Awesome!!

:) You could make a plugin of it and add it to package controle to sublime! (http://wbond.net/sublime_packages/package_control)

Br,
Grzegorz

@heweshewes
Copy link

How to run this under windows?

@afanjul
Copy link

afanjul commented Nov 1, 2012

You can use this improved version (for windows) to look in all your project folders. Also you will need to download Grep and Gawk for windows (http://gnuwin32.sourceforge.net/packages.html) and add the installed folder to your system PATH and restart windows before try it.

# 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, subprocess

class AutocompleteAll(sublime_plugin.EventListener):

    def on_query_completions(self, view, prefix, locations):
        results=[]
        tags_paths = [folder + '/.tags' for folder in view.window().folders()]
        for tags_path in tags_paths:
            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
        tags_paths = ' '.join([str(x) for x in tags_paths])    
        f=os.popen('grep -ih "^'+prefix+'" ' + tags_paths + ' | gawk "{ print $1 }"') # egrep 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

@afanjul
Copy link

afanjul commented Nov 1, 2012

You can use this improved version (for windows) to look in all your project folders. Also you will need to download Grep and Gawk for windows (http://gnuwin32.sourceforge.net/packages.html) and add the installed folder to your system PATH and restart windows before try it.

# 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, subprocess

class AutocompleteAll(sublime_plugin.EventListener):

    def on_query_completions(self, view, prefix, locations):
        results=[]
        tags_paths = [folder + '/.tags' for folder in view.window().folders()]
        for tags_path in tags_paths:
            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
        tags_paths = ' '.join([str(x) for x in tags_paths])    
        f=os.popen('grep -ih "^'+prefix+'" ' + tags_paths + ' | gawk "{ print $1 }"') # egrep 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

@afanjul
Copy link

afanjul commented Nov 1, 2012

You can use this improved version (for windows) to look in all your project folders. Also you will need to download Grep and Gawk for windows (http://gnuwin32.sourceforge.net/packages.html) and add the installed folder to your system PATH and restart windows before try it.

# 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, subprocess

class AutocompleteAll(sublime_plugin.EventListener):

    def on_query_completions(self, view, prefix, locations):
        results=[]
        tags_paths = [folder + '/.tags' for folder in view.window().folders()]
        for tags_path in tags_paths:
            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
        tags_paths = ' '.join([str(x) for x in tags_paths])    
        f=os.popen('grep -ih "^'+prefix+'" ' + tags_paths + ' | gawk "{ print $1 }"') # egrep 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

@afanjul
Copy link

afanjul commented Nov 1, 2012

sdf

@hanoii
Copy link

hanoii commented Nov 27, 2012

It works, but it's a bit slow for big tags files. I wonder if there's something from the actual ctags plugin you might be able to use to speed it up?

@hanoii
Copy link

hanoii commented Nov 27, 2012

removing -i improves performance considerably, see my fork if needed.

@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