Skip to content

Commit dce993c

Browse files
committed
InlineBlame: when user's selection spans multiple lines, show info for & at the line with the caret
i.e. respect the direction of the selection and choose either the first or last line, rather than always the last line
1 parent 9d13116 commit dce993c

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

src/blame_inline.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,22 @@ def show_inline_blame(self):
107107
return
108108

109109
phantoms = []
110-
sels = self.view.sel()
111-
if len(sels) < 1:
112-
return
113110

111+
sels = self.view.sel()
114112
# @todo Support showing inline blame for multiple carets?
115113
# @body Maybe with a sanity check that there aren't too many (more than 10?)
116-
line = self.view.line(sels[0])
117-
if line.size() < 2:
118-
# avoid weird behaviour of regions on empty lines
119-
# < 2 is to check for newline character
114+
if len(sels) != 1:
120115
return
121-
pos = line.end()
122-
row, _ = self.view.rowcol(line.begin())
123-
anchor = sublime.Region(pos, pos)
116+
sel0 = sels[0]
117+
118+
phantom_pos, caret_line_num = self.calculate_positions(sel0)
119+
if not phantom_pos:
120+
return
121+
124122
try:
125-
blame_output = self.get_blame_text(self.view.file_name(), line_num=row + 1)
123+
blame_output = self.get_blame_text(
124+
self.view.file_name(), line_num=caret_line_num
125+
)
126126
except Exception:
127127
return
128128
blame = next(
@@ -152,13 +152,37 @@ def show_inline_blame(self):
152152
summary=summary,
153153
)
154154
phantom = sublime.Phantom(
155-
anchor, body, sublime.LAYOUT_INLINE, self.handle_phantom_button
155+
sublime.Region(phantom_pos),
156+
body,
157+
sublime.LAYOUT_INLINE,
158+
self.handle_phantom_button,
156159
)
157160
phantoms.append(phantom)
158161

159162
# Dispatch back onto the main thread to serialize a final is_dirty check.
160163
sublime.set_timeout(lambda: self.maybe_insert_phantoms(phantoms), 0)
161164

165+
def calculate_positions(self, user_selection):
166+
selection_goes_backwards = user_selection.a > user_selection.b
167+
168+
row, _ = self.view.rowcol(
169+
user_selection.begin() if selection_goes_backwards else user_selection.end()
170+
)
171+
caret_line_num = row + 1
172+
173+
# Quantise the arbitrary user selection to line(s)
174+
selection = self.view.line(user_selection)
175+
if selection.size() <= 1:
176+
# Not worth showing a blame phantom for an empty line.
177+
return (None, caret_line_num)
178+
179+
if selection_goes_backwards:
180+
phantom_pos = self.view.line(selection.begin()).end()
181+
else:
182+
phantom_pos = selection.end()
183+
184+
return (phantom_pos, caret_line_num)
185+
162186
def maybe_insert_phantoms(self, phantoms):
163187
if not self.view.is_dirty():
164188
self.phantom_set.update(phantoms)

0 commit comments

Comments
 (0)