Skip to content

Commit 2b0b4e2

Browse files
committed
Use getregion() for showing definition of selected texts
Previously, MacVim added custom VimScript to query what the user selected text is before calling `showdefinition` on it. Since Vim has since implemented the `getregion()` API that provides that info natively, use that API instead of the custom implementation. Also, the previous implementation was broken when dealing with wide characters where it would select more characters than necessary. Using the native `getregion()` is much more robust and handle those situations.
1 parent 5ca4161 commit 2b0b4e2

1 file changed

Lines changed: 1 addition & 52 deletions

File tree

runtime/autoload/macvim.vim

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,11 @@ vim9script
33
# Maintainer: Yee Cheng Chin (macvim-dev@macvim.org)
44
# Last Change: 2023-03-15
55

6-
# Retrieves the text under the selection, without polluting the registers.
7-
# This is easier if we could yank, but we don't know what the user has been
8-
# doing. One way we could have accomplished this was to save the register info
9-
# and then restore it, but this runs into problems if the unnamed register was
10-
# pointing to the "* register as setting and restoring the system clipboard
11-
# could be iffy (if there are non-text items in the clipboard). It's cleaner
12-
# to just use a pure Vimscript solution without having to rely on yank.
13-
def SelectedText(): string
14-
var [line_start, column_start] = getpos("'<")[1 : 2]
15-
var [line_end, column_end] = getpos("'>")[1 : 2]
16-
final lines = getline(line_start, line_end)
17-
if len(lines) == 0
18-
return ''
19-
endif
20-
21-
const visualmode = visualmode()
22-
23-
if visualmode ==# 'v'
24-
if line_start == line_end && column_start == column_end
25-
# Exclusive has a special case where you always select at least one
26-
# char, so just handle the case here.
27-
return lines[0][column_start - 1]
28-
endif
29-
if &selection ==# "exclusive"
30-
column_end -= 1 # exclusive selection don't count the last column (usually)
31-
endif
32-
lines[-1] = lines[-1][ : column_end - 1]
33-
lines[0] = lines[0][column_start - 1 : ]
34-
elseif visualmode ==# "\<C-V>"
35-
if column_end <= column_start
36-
# This can happen with v_O, need to swap start/end
37-
const temp = column_start
38-
column_start = column_end
39-
column_end = temp
40-
# Also, exclusive mode is weird in this state in that we don't need to
41-
# do column_end -= 1, and it acts like inclusive instead.
42-
else
43-
if &selection ==# "exclusive"
44-
column_end -= 1 # normal exclusive behavior, need to cull the last column.
45-
endif
46-
endif
47-
for idx in range(len(lines))
48-
lines[idx] = lines[idx][column_start - 1 : column_end - 1]
49-
endfor
50-
else
51-
# Line mode doesn't have to do anything to trim the lines
52-
endif
53-
return join(lines, "\n")
54-
enddef
55-
56-
576
# Ask macOS to show the definition of the last selected text. Note that this
587
# uses '<, and therefore has to be used in normal mode where the mark has
598
# already been updated.
609
export def ShowDefinitionSelected()
61-
const sel_text = SelectedText()
10+
const sel_text = join(getregion(getpos("'<"), getpos("'>"), { type: visualmode(), exclusive: (&selection ==# "exclusive") }), "\n")
6211
if len(sel_text) > 0
6312
const sel_start = getpos("'<")
6413
const sel_screenpos = win_getid()->screenpos(sel_start[1], sel_start[2])

0 commit comments

Comments
 (0)