
import sublime, sublime_plugin, time

# Ideas taken from C0D312, nizur & tito in http://www.sublimetext.com/forum/viewtopic.php?f=2&t=4589

s = sublime.load_settings('Function Name Display.sublime-settings')

class Pref:
  def load(self):
    Pref.display_class = s.get('display_class', True)
    Pref.display_function = s.get('display_function', True)
    Pref.wait_time = 0.02
    Pref.time = time.time()

Pref().load()
s.add_on_change('display_class', lambda:Pref().load())
s.add_on_change('display_function', lambda:Pref().load())

class FunctionNameStatusEventHandler(sublime_plugin.EventListener):

  # Event handlers
  def on_selection_modified(self, view):
    now = time.time()
    if now - Pref.time > Pref.wait_time:
      Pref.time = now
      self.display_current_class_and_function(view)
    else:
      Pref.time = now

  # display the current class and function name
  def display_current_class_and_function(self, view):
    if view.settings().get('is_widget'):
      return

    region = view.sel()[0]
    region_row, region_col = view.rowcol(region.begin())

    s = ""
    found = False

    # Look for any classes
    if Pref.display_class:
      class_regions = view.find_by_selector('entity.name.type.class')
      for r in reversed(class_regions):
        row, col = view.rowcol(r.begin())
        if row <= region_row:
          s = view.substr(r)
          found = True
          if Pref.display_function:
            s += "::"
          break;
    
    # Look for any functions including PHP magic functions
    if Pref.display_function:
      function_regions = view.find_by_selector('entity.name.function') + view.find_by_selector('support.function.magic.php')
      if function_regions:
        function_regions.sort()
        for r in reversed(function_regions):
          row, col = view.rowcol(r.begin())
          if row <= region_row:
            s = s + view.substr(r)
            found = True
            break

    if not found:
      view.erase_status('function')
    else:
      view.set_status('function', s)

@@ -989,20 +989,23 @@
 urn%0A
-%0A
+ 
    
+for
  region 
 = vi
@@ -1000,17 +1000,18 @@
  region 
-=
+in
  view.se
@@ -1017,12 +1017,12 @@
 el()
-%5B0%5D%0A
+:%0A  
     
@@ -1083,15 +1083,19 @@
     
+ 
+ 
 s = %22%22%0A
+  
     
@@ -1109,24 +1109,26 @@
  False%0A%0A    
+ 
+ 
 # Look for a
@@ -1138,16 +1138,18 @@
 classes%0A
+  
     if P
@@ -1173,16 +1173,18 @@
 :%0A      
+ 
+ 
 class_re
@@ -1241,24 +1241,26 @@
 ass')%0A      
+  
 for r in rev
@@ -1281,32 +1281,34 @@
 gions):%0A        
+ 
+ 
 row, col = view.
@@ -1317,32 +1317,34 @@
 wcol(r.begin())%0A
+  
         if row %3C
@@ -1363,24 +1363,26 @@
 :%0A          
+  
 s = view.sub
@@ -1380,32 +1380,34 @@
  view.substr(r)%0A
+  
           found 
@@ -1419,24 +1419,26 @@
 e%0A          
+  
 if Pref.disp
@@ -1447,24 +1447,26 @@
 y_function:%0A
+  
             
@@ -1489,20 +1489,20 @@
     
+  
 break;%0A
-  
+%0A
   
-%0A
     
@@ -1556,24 +1556,26 @@
 nctions%0A    
+ 
+ 
 if Pref.disp
@@ -1584,24 +1584,26 @@
 y_function:%0A
+  
       functi
@@ -1721,16 +1721,18 @@
 )%0A      
+  
 if funct
@@ -1744,16 +1744,18 @@
 egions:%0A
+  
         
@@ -1778,32 +1778,34 @@
 .sort()%0A        
+ 
+ 
 for r in reverse
@@ -1827,32 +1827,34 @@
 ons):%0A          
+  
 row, col = view.
@@ -1873,32 +1873,34 @@
 in())%0A          
+ 
+ 
 if row %3C= region
@@ -1897,32 +1897,34 @@
  %3C= region_row:%0A
+  
             s = 
@@ -1946,32 +1946,34 @@
 (r)%0A            
+  
 found = True%0A   
@@ -1961,32 +1961,34 @@
    found = True%0A
+  
             brea
@@ -1990,24 +1990,26 @@
  break%0A%0A    
+  
 if not found
@@ -2010,16 +2010,18 @@
  found:%0A
+  
       vi
@@ -2048,30 +2048,34 @@
 ction')%0A    
+  
 else:%0A
+  
       view.s
@@ -2099,8 +2099,55 @@
 on', s)%0A
+      return%0A    view.erase_status('function')%0A

Fix index out of range in selection when there is no selections