mirror of
https://github.com/macvim-dev/macvim.git
synced 2026-06-11 15:37:29 +02:00
db26a3b1ac
Apple "Intelligence" Writing Tools was previously not working correctly with MacVim. When the user chooses to replace the original selection with the updated texts, MacVim mistreats the input and treat them as commands instead of raw texts. The reason was that even though this service uses the NSServicesMenuRequestor API to obtain the selected texts, it does not use it to send over the replacement. Instead, it uses NSTextInput's `insertText:replacementRange` to do so instead, which we never implemented properly. The reason behind this choice was probably because Writing Tools first shows a UI with user interaction and has a delay between obtaining the texts and replacing them, like a regular Services menu. This means the selection may already be invalid by the time it requests a replacement. To fix this, add a new IPC API `replaceSelectedText` to replace the selected texts and redirect `insertText:replacementRange` to use it if the replacement range is non-empty. This isn't the most correct implementation of the protocol but should work in most cases. We don't have a way to implement it "correctly" as MacVim does not have easy access to Vim's internal text storage. Also make sure the Service menu uses this API (for things like "convert to full width" and Traditional/Simplified Chinese conversions). The old method of simple injecting a normal mode command `s` before the text was horribly buggy. It also works with visual block selection properly now. The implementation uses Vim's register put functionality because Vim doesn't have an API to simply replace a block of text, and everything has to go through registers. At the same time, replace the implementation for the old `selectedText` IPC API to not do this, because Vim *did* end an API to do so for obtaining texts (via `getregion()`) and it's more stable to use this than to manually cache/restore registers. Related: vim/vim#16596 (fixes `setreg()` which this uses) Fix #1512
103 lines
3.2 KiB
Objective-C
103 lines
3.2 KiB
Objective-C
/* vi:set ts=8 sts=4 sw=4 ft=objc:
|
|
*
|
|
* VIM - Vi IMproved by Bram Moolenaar
|
|
* MacVim GUI port by Bjorn Winckler
|
|
*
|
|
* Do ":help uganda" in Vim to read copying and usage conditions.
|
|
* Do ":help credits" in Vim to see a list of people who contributed.
|
|
* See README.txt for an overview of the Vim source code.
|
|
*/
|
|
|
|
#import "MacVim.h"
|
|
|
|
|
|
@class MMWindowController;
|
|
@class MMTouchBarInfo;
|
|
|
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12
|
|
/// Button used for Touch Bar support, with an additional metadata to store the
|
|
/// Vim command it should send.
|
|
@interface MMTouchBarButton : NSButton {
|
|
NSArray *_desc;
|
|
}
|
|
- (NSArray *)desc;
|
|
- (void)setDesc:(NSArray *)desc;
|
|
@end
|
|
#endif
|
|
|
|
@interface MMVimController : NSObject<
|
|
NSToolbarDelegate
|
|
, NSOpenSavePanelDelegate
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12_2
|
|
, NSTouchBarDelegate
|
|
#endif
|
|
>
|
|
{
|
|
unsigned long identifier;
|
|
BOOL isInitialized;
|
|
MMWindowController *windowController;
|
|
id backendProxy;
|
|
NSMenu *mainMenu;
|
|
NSMutableArray *popupMenuItems;
|
|
|
|
// TODO: Move all toolbar code to window controller?
|
|
NSToolbar *toolbar;
|
|
NSMutableDictionary *toolbarItemDict;
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12_2
|
|
MMTouchBarInfo *touchbarInfo;
|
|
#endif
|
|
|
|
int pid;
|
|
NSString *serverName;
|
|
NSDictionary *vimState;
|
|
BOOL isPreloading;
|
|
NSDate *creationDate;
|
|
BOOL hasModifiedBuffer;
|
|
}
|
|
|
|
/// Mapping from internal names for system monospace font to user-visible one.
|
|
/// E.g. ".AppleSystemUIFontMonospaced-Medium" -> "-monospace-Medium"
|
|
@property (nonatomic, readonly) NSMutableDictionary<NSString*, NSString*>* systemFontNamesToAlias;
|
|
|
|
@property (nonatomic, readonly) BOOL isHandlingInputQueue;
|
|
|
|
- (id)initWithBackend:(id)backend pid:(int)processIdentifier;
|
|
- (void)uninitialize;
|
|
- (unsigned long)vimControllerId;
|
|
- (id)backendProxy;
|
|
- (int)pid;
|
|
- (void)setServerName:(NSString *)name;
|
|
- (NSString *)serverName;
|
|
- (MMWindowController *)windowController;
|
|
- (NSDictionary *)vimState;
|
|
- (id)objectForVimStateKey:(NSString *)key;
|
|
- (NSMenu *)mainMenu;
|
|
- (BOOL)isPreloading;
|
|
- (void)setIsPreloading:(BOOL)yn;
|
|
- (BOOL)hasModifiedBuffer;
|
|
- (NSDate *)creationDate;
|
|
- (void)cleanup;
|
|
- (void)dropFiles:(NSArray *)filenames forceOpen:(BOOL)force;
|
|
- (void)file:(NSString *)filename draggedToTabAtIndex:(NSUInteger)tabIndex;
|
|
- (void)filesDraggedToTabline:(NSArray *)filenames;
|
|
- (void)dropString:(NSString *)string;
|
|
- (void)appearanceChanged:(int)flag;
|
|
|
|
- (void)passArguments:(NSDictionary *)args;
|
|
- (void)sendMessage:(int)msgid data:(NSData *)data;
|
|
- (BOOL)sendMessageNow:(int)msgid data:(NSData *)data
|
|
timeout:(NSTimeInterval)timeout;
|
|
- (void)addVimInput:(NSString *)string;
|
|
- (NSString *)evaluateVimExpression:(NSString *)expr;
|
|
- (id)evaluateVimExpressionCocoa:(NSString *)expr
|
|
errorString:(NSString **)errstr;
|
|
- (BOOL)hasSelectedText;
|
|
- (NSString *)selectedText;
|
|
- (void)replaceSelectedText:(NSString *)text;
|
|
- (void)processInputQueue:(NSArray *)queue;
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12_2
|
|
- (NSTouchBar *)makeTouchBar;
|
|
#endif
|
|
@end
|