forked from integrum/SublimeRubyCoverage
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ruby_coverage_status.py
72 lines (54 loc) · 2.07 KB
/
ruby_coverage_status.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import sublime
import sublime_plugin
from .common.json_coverage_reader import JsonCoverageReader
STATUS_KEY = 'ruby-coverage-status'
class RubyCoverageStatusListener(sublime_plugin.EventListener):
"""Show coverage statistics in status bar."""
def on_load(self, view):
self.on_selection_modified(view)
def on_selection_modified(self, view):
if 'source.ruby' not in view.scope_name(0):
return
self.view = view
if sublime.load_settings('SimpleCov.sublime-settings').get('coverage_status_in_status_bar'):
sublime.set_timeout_async(self.update_status, 0)
else:
self.erase_status()
def update_status(self):
view = self.view
view.set_status(STATUS_KEY, self.get_view_coverage_status())
def erase_status(self):
view = self.view
view.erase_status(STATUS_KEY)
def get_view_coverage_status(self):
view = self.view
filename = view.file_name()
if not filename:
self.erase_status()
r = JsonCoverageReader(filename)
coverage = r.get_file_coverage(filename) if r else None
if coverage is None:
return 'File not covered'
line_number = self.get_line_number()
if line_number is None:
self.erase_status()
file_coverage = "File covered {:.1f}% ({}/{})".format(
coverage['covered_percent'],
coverage['covered_lines'],
coverage['lines_of_code']
)
line_coverage = coverage['coverage'][line_number] if len(coverage['coverage']) > line_number else None
if line_coverage is None:
line_coverage = 'Line not executable'
elif line_coverage > 0:
line_coverage = 'Line covered × {}'.format(line_coverage)
else:
line_coverage = 'Line not covered'
return file_coverage + ', ' + line_coverage
def get_line_number(self):
view = self.view
regions = view.sel()
if len(regions) > 1:
return
return view.rowcol(regions[0].a)[0]