View Single Post
06-18-17, 11:09 AM   #19
p3lim
A Pyroguard Emberseer
 
p3lim's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 1,710
Originally Posted by p3lim View Post
Resike asked me on IRC to make something for highlighting globals, could look like this:

(Yes, io and arg should be highlighted, this was just an example while I work out the kinks)

The only downside is that you'd have to have Lua installed (it don't run Lua scripts, but it requires the Lua compilator to find the globals properly).
Here's the source code for that plugin, I'm discontinuing it for two reasons.

Reason 1: It has a tendency to slow/crash sublime because the Lua compiler can be slow or return errors.
It could be handled, but it's not preferred.

Reason 2: The current method can't match the same global multiple times on one line, which could of course be improved.

Just dumping the code here in public domain in case anyone wants to use it.

Python Code:
  1. import sublime
  2. import sublime_plugin
  3.  
  4. import re
  5. from subprocess import Popen, PIPE
  6.  
  7. def get_luac(contents):
  8.     try:
  9.         process = Popen('luac -p -l -- -', stdin=PIPE, stderr=PIPE, stdout=PIPE, shell=True)
  10.         return process.communicate(input=contents.encode())[0].decode()
  11.     except BrokenPipeError as e:
  12.         return None
  13.  
  14. def get_regions(view):
  15.     regions = []
  16.  
  17.     # get view as a region
  18.     viewContents = sublime.Region(0, view.size())
  19.     # get every line region from the region
  20.     lineRegions = view.lines(viewContents)
  21.     # get every line content from the regions
  22.     lineContents = view.substr(viewContents).split('\n')
  23.  
  24.     # get globals from the current content
  25.     output = get_luac(view.substr(viewContents))
  26.     if not output:
  27.         return regions
  28.  
  29.     # parse each line from luac
  30.     for line in output.split('\n'):
  31.         # clean up the line
  32.         line = ' '.join(line.split())
  33.  
  34.         # match global references
  35.         match = re.match(r'\d+ \[(\d+)\] GETGLOBAL -?\d+ -?\d+ ; (.*)', line)
  36.         if match:
  37.             lineNumber, globalValue = match.groups()
  38.             lineNumber = int(lineNumber) - 1
  39.  
  40.             # sadly, luac doesn't tell us the character
  41.             # positions, so we'll have to grab that ourselves
  42.             region = lineRegions[lineNumber]
  43.             contents = view.substr(region)
  44.             print(contents, '---', globalValue)
  45.  
  46.             # grab the start character point
  47.             start = contents.find(globalValue)
  48.  
  49.             if start != -1:
  50.                 # grab the end character point
  51.                 end = start + len(globalValue)
  52.  
  53.                 # create a new region for the value
  54.                 valueRegion = sublime.Region(region.a + start, region.a + end)
  55.  
  56.                 # add the region to the output
  57.                 regions.append(valueRegion)
  58.  
  59.                 # TODO: now we have to remove the value so we don't
  60.                 # match it again (multiple of same globals on one line)
  61.  
  62.     return regions
  63.  
  64. def highlight_globals(view):
  65.     if view.settings().get('syntax').encode() == 'Packages/Lua/Lua.sublime-syntax'.encode():
  66.         # only execute in Lua files
  67.  
  68.         view.add_regions('HighlightLuaGlobals', get_regions(view),
  69.             'comment', '', sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE | sublime.DRAW_SOLID_UNDERLINE | sublime.HIDE_ON_MINIMAP)
  70.  
  71. class HighlightLuaGlobals(sublime_plugin.EventListener):
  72.     def on_modified_async(self, view):
  73.         highlight_globals(view)
  74.  
  75.     def on_activated_async(self, view):
  76.         highlight_globals(view)
  77.  
  78.     def on_load_async(self, view):
  79.         highlight_globals(view)
  80.  
  81. def plugin_loaded():
  82.     for window in sublime.windows():
  83.         for view in window.views():
  84.             view.settings().add_on_change('HighlightLuaGlobals', lambda: highlight_globals(view))
  Reply With Quote