Skip to content

Instantly share code, notes, and snippets.

@rchl
Last active August 11, 2020 05:20
Show Gist options
  • Save rchl/9a1efce6c55bb8aa31cfb97d7923ed67 to your computer and use it in GitHub Desktop.
Save rchl/9a1efce6c55bb8aa31cfb97d7923ed67 to your computer and use it in GitHub Desktop.
Sublime Text plugin for highlighting biggest offenders in Plugin Event Profile page
import sublime
import sublime_plugin
import re
class PluginEventProfileListener(sublime_plugin.ViewEventListener):
def on_activated_async(self):
view = self.view
if not view.is_scratch() or not view.name() or view.name() != 'Plugin Event Profile':
return
view.settings().set('draw_white_space', ['selection'])
view.settings().set('line_numbers', False)
lines = view.lines(sublime.Region(0, view.size()))
self._highlight_offenders(lines)
def _highlight_offenders(self, lines):
listeners = {}
# Ignore first two lines - a comment and a blank line.
from_index = 2
# Group lines into event-specific lists.
while from_index < len(lines):
listener_name = self.view.substr(lines[from_index])
listener_data = []
listeners[listener_name] = listener_data
from_index += 1
for line_region in lines[from_index:]:
from_index += 1
if line_region.a == line_region.b:
break
listener_data.append(line_region)
regions_to_highlight = {
'region.redish': [],
'region.orangish': [],
}
for (event_name, event_regions) in listeners.items():
# Highest-seen values for the event.
max_values = ([], [], [])
for region in event_regions:
match = re.match(r'\s+[^:]+: ([0-9\.]+)s total(?:, mean: ([0-9\.]+)s, max: ([0-9\.]+)s)?', self.view.substr(region))
values = [float(group) for group in match.groups() if group is not None]
for (i, value) in enumerate(values):
if value > 0:
match_start = region.begin() + match.start(i + 1)
# +1 to include 's' suffix
match_end = match_start + (match.end(i + 1) - match.start(i + 1) + 1)
match_region = sublime.Region(match_start, match_end)
max_values[i].append([value, match_region])
for values in max_values:
values.sort(key=lambda value: value[0])
if len(values):
regions_to_highlight['region.redish'].append(values.pop()[1])
if len(values):
regions_to_highlight['region.orangish'].append(values.pop()[1])
for region_key, regions in regions_to_highlight.items():
self.view.add_regions('max_' + region_key, regions, region_key,
flags=sublime.DRAW_SOLID_UNDERLINE | sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE)
@rchl
Copy link
Author

rchl commented Apr 10, 2020

plugin-profile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment