macvim-dev / macvim

Vim - the text editor - for macOS
https://macvim.org
Vim License
7.47k stars 680 forks source link

Support looking up selected texts, and also add data detector for URLs etc #1313

Closed ychin closed 1 year ago

ychin commented 1 year ago

Part of Epic #1311

Add data detector support for lookup, so URLs etc will work properly

This automatically uses NSDataDetector to detect special data around the lookup cursor position, and if found, will manually call showDefinition instead of letting the OS do it for us (honestly, this feature should be built-in to the OS instead of such manual work). Right now only doing address/phone number/URL (URL has priority), because we don't have built-in definition support for the other types like flight info and so on. This also only works for what is drawn on-screen only, because as usual, MacVim doesn't have access to the native text storage, unless we rely on lots of callbacks back-and-forth (which is possible, but annoying to implement).


Make data lookup support selected text

Doing data lookup (e.g. Ctrl-Cmd-D) on top of a selected text now properly look up the entire selected range (e.g. "ice cream", instead of just "ice" or "cream"). This would have come by default if we could implement NSTextInputClient's selectedRange properly but since MacVim doesn't have access to the internal Vim buffers easily this is easier said than done. As such, we have a custom implementation where if we detect a lookup event, manually detect that we have the mouse cursor on top of selected text and show the definition for that manually.

Also fix a minor issue in text input client so that the baseline is now reported correctly for certan font size comboes, as our fontDescent is rounded up for some reason.