SublimeText / CTags

CTags support for Sublime Text
MIT License
987 stars 167 forks source link

Support for auto-updating #226

Open stephenfin opened 10 years ago

stephenfin commented 10 years ago

Files change, and the tagfile should change with them. The plugin should automatically update the tag file each time it detects a change (i.e. a save), without having to rebuild the entire tag file from scratch.

This is similar to the Vim plugin - AutoTag

Snake231088 commented 10 years ago

Any news? When this feature?

trungnt13 commented 10 years ago

Agree, I am waiting for this feature, too !

stephenfin commented 10 years ago

This is tough for me to do. There are example for Vim, like the aforementioned AutoTag or vim-easytags, but I'm not familiar with Vim script or the Vim API. Would appreciate some help here (a PR or an explanation of what those plugins are doing).

ctf0 commented 9 years ago

i was just going to ask for this feature today :) ,any updated on it yet ?

yongkangchen commented 9 years ago

Suggest implement refer to https://github.com/yongkangchen/atom-ctags :smile:

xenofanes9 commented 9 years ago

In the absence of code that just can adjust portions of the tag file on the fly, 've just been rebuilding the entire tag file from scratch on file changes with a short plugin. I can't use the ctags output until it's built, obviously, but it's the best I have for now.

import sublime
import sublime_plugin
import os

# on load determine ctag .tag file name

def get_tagfile_setting():
    """ piggy-backs on ctags settings file and alerts user if non-existent """
    settings = sublime.load_settings("CTags.sublime-settings")
    if settings:
        name = settings.get('tag_file', None)
        if name:
            return name
        else:
            sublime.error_message("CTags settings found, but could not find 'tag_file'!")
    else:
        sublime.error_message("CTags settings not found!")

def folders_to_ctag(window=None, initialOnly=True):
    """ if .tag files already exist, don't bother doing anything """
    if not window:
        window = sublime.active_window()

    folders = window.folders()
    global TAGFILE
    for folder in folders:
        if initialOnly and os.path.exists(os.path.join(folder, TAGFILE)):
            # don't need to keep scanning, we won't be issuing anything
            return []
    return folders

class AutoCtagListener(sublime_plugin.EventListener):

    def on_new_async(self, view):
        """ Check for initial tag creation """
        folders = folders_to_ctag(view.window())
        if folders:
            # build initial tag file
            view.window().run_command('rebuild_tags', args={'dirs': folders})

    def on_load_async(self, view):
        """ Check for initial tag creation """
        folders = folders_to_ctag(view.window())
        if folders:
            # build initial tag file
            view.window().run_command('rebuild_tags', args={'dirs': folders})

    def on_post_save_async(self, view):
        """ Update tag file for current file after changes saved """
        folders = folders_to_ctag(view.window(), initialOnly=False)
        if folders:
            view.window().run_command('rebuild_tags', args={'dirs': folders})
        else:
            f = view.file_name()
            view.window().run_command('rebuild_tags', args={'files': [f]})

def plugin_loaded():
    """ Must wait until notified that plugin is loaded before using sublime API """
    global TAGFILE
    TAGFILE = get_tagfile_setting()