Advertisement
a3f

CTags

a3f
Jan 6th, 2015
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.04 KB | None | 0 0
  1. def get_settings():
  2.     """
  3.    Load settings.
  4.  
  5.    :returns: dictionary containing settings
  6.    """
  7.     return sublime.load_settings("CTags.sublime-settings")
  8.  
  9. def get_setting(key, default=None):
  10.     """
  11.    Load individual setting.
  12.  
  13.    :param key: setting key to get value for
  14.    :param default: default value to return if no value found
  15.  
  16.    :returns: value for ``key`` if ``key`` exists, else ``default``
  17.    """
  18.     return get_settings().get(key, default)
  19.  
  20. setting = get_setting
  21.  
  22. class NavigateToDefinition(sublime_plugin.TextCommand):
  23.     """
  24.    Provider for the ``navigate_to_definition`` command.
  25.  
  26.    Command navigates to the definition for a symbol in the open file(s) or
  27.    folder(s).
  28.    """
  29.     is_enabled = check_if_building
  30.  
  31.     def __init__(self, args):
  32.         sublime_plugin.TextCommand.__init__(self, args)
  33.         self.endings = re.compile(RUBY_SPECIAL_ENDINGS)
  34.  
  35.     def is_visible(self):
  36.         return setting('show_context_menus')
  37.  
  38.     @ctags_goto_command(jump_directly=setting("jump_single_match")) # this is it
  39.     def run(self, view, args, tags_file):
  40.         region = view.sel()[0]
  41.         if region.begin() == region.end():  # point
  42.             region = view.word(region)
  43.  
  44.             # handle special line endings for Ruby
  45.             language = view.settings().get('syntax')
  46.             endings = view.substr(sublime.Region(region.end(), region.end()+1))
  47.  
  48.             if 'Ruby' in language and self.endings.match(endings):
  49.                 region = sublime.Region(region.begin(), region.end()+1)
  50.         symbol = view.substr(region)
  51.  
  52.         return JumpToDefinition.run(sym
  53.  
  54.  
  55.  
  56.  
  57.  
  58. # this isn't part of the problem, but I included it to show other code uses setting() normally in conditionals
  59. class TestCtags(sublime_plugin.TextCommand):
  60.     routine = None
  61.  
  62.     def run(self, edit, **args):
  63.         if self.routine is None:
  64.             self.routine = self.co_routine(self.view)
  65.             next(self.routine)
  66.  
  67.     def __next__(self):
  68.         try:
  69.             next(self.routine)
  70.         except Exception as e:
  71.             print(e)
  72.             self.routine = None
  73.  
  74.     def co_routine(self, view):
  75.         tag_file = find_tags_relative_to(
  76.             view.file_name(), setting('tag_file'))
  77.  
  78.         with codecs.open(tag_file, encoding='utf-8') as tf:
  79.             tags = parse_tag_lines(tf, tag_class=TagElements)
  80.  
  81.         print('Starting Test')
  82.  
  83.         ex_failures = []
  84.         line_failures = []
  85.  
  86.         for symbol, tag_list in list(tags.items()):
  87.             for tag in tag_list:
  88.                 tag.root_dir = os.path.dirname(tag_file)
  89.  
  90.                 def hook(av):
  91.                     test_context = av.sel()[0]
  92.  
  93.                     if tag.ex_command.isdigit():
  94.                         test_string = tag.symbol
  95.                     else:
  96.                         test_string = tag.ex_command
  97.                         test_context = av.line(test_context)
  98.  
  99.                     if not av.substr(test_context).startswith(test_string):
  100.                         failure = 'FAILURE %s' % pprint.pformat(tag)
  101.                         failure += av.file_name()
  102.  
  103.                         if setting('debug'):
  104.                             if not sublime.question_box('%s\n\n\n' % failure):
  105.                                 self.routine = None
  106.  
  107.                             return sublime.set_clipboard(failure)
  108.                         ex_failures.append(tag)
  109.                     sublime.set_timeout(self.__next__, 5)
  110.                 scroll_to_tag(view, tag, hook)
  111.                 yield
  112.  
  113.         failures = line_failures + ex_failures
  114.         tags_tested = sum(len(v) for v in list(tags.values())) - len(failures)
  115.  
  116.         view = sublime.active_window().new_file()
  117.  
  118.         with Edit(view) as edit:
  119.             edit.insert(view.size(), '%s Tags Tested OK\n' % tags_tested)
  120.             edit.insert(view.size(), '%s Tags Failed' % len(failures))
  121.  
  122.         view.set_scratch(True)
  123.         view.set_name('CTags Test Results')
  124.  
  125.         if failures:
  126.             sublime.set_clipboard(pprint.pformat(failures))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement