Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace whitespace #960

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion XVim/NSTextView+VimOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,12 @@ - (BOOL)xvim_delete:(XVimMotion*)motion withMotionPoint:(NSUInteger)motionPoint
newPos = [self.textStorage xvim_indexOfLineNumber:sel.top column:sel.left];
}

[self.xvimDelegate textView:self didDelete:self.lastYankedText withType:self.lastYankedType];
// in case yank:NO is passed in and no yanking has been done yet
// usually we don't want to report unyanked deletes anyway, as they are usually
// internal plumbing
if (self.lastYankedText != NULL) {
[self.xvimDelegate textView:self didDelete:self.lastYankedText withType:self.lastYankedType];
}
[self xvim_changeSelectionMode:XVIM_VISUAL_NONE];
if (newPos != NSNotFound) {
[self xvim_moveCursor:newPos preserveColumn:NO];
Expand Down
13 changes: 12 additions & 1 deletion XVim/Test/XVimTester+Issues.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ - (NSArray*)issues_testcases{
static NSString* issue_606_result_spaces = @" aaa bbb ccc\n";
static NSString* issue_606_result_tabs = @" aaa bbb ccc\n";


static NSString* issue_770_text = @"aaaa(.)a\n";
static NSString* issue_770_newline_result = @"a\naa(.)a\n";
static NSString* issue_770_replace_multichar_result = @"bbbb(.)a\n";
static NSString* issue_770_eol_text = @"1234\n1234\n";
static NSString* issue_770_eol_result = @"123\n\n1234\n";

static NSString* issue_776_text = @"";
static NSString* issue_776_result = @"\n";
static NSString* issue_805_text = @"aaaa bbbb cccc dddd eeee ffff gggg\n"
Expand Down Expand Up @@ -83,6 +88,12 @@ - (NSArray*)issues_testcases{
? XVimMakeTestCase( text0, 0, 0, @"i<TAB><ESC>.", issue_606_result_tabs, 1, 0 ) // Issue #606. Repeating tab insertion crashes Xcode.
: XVimMakeTestCase( text0, 0, 0, @"i<TAB><ESC>.", issue_606_result_spaces, 7, 0 ), // Issue #606. Repeating tab insertion crashes Xcode.

XVimMakeTestCase(issue_770_text, 0, 0, @"r<cr>u", issue_770_text, 0, 0), // Issue #770
XVimMakeTestCase(issue_770_text, 0, 0, @"r u", issue_770_text, 0, 0), // Issue #770
XVimMakeTestCase(issue_770_text, 1, 0, @"r<cr>", issue_770_newline_result, 2, 0), // Issue #770
XVimMakeTestCase(issue_770_text, 0, 0, @"Rbbbb", issue_770_replace_multichar_result, 4, 0), // Issue #770
XVimMakeTestCase(issue_770_eol_text, 3, 0, @"r<cr>kj", issue_770_eol_result, 4, 0), // Issue #770

XVimMakeTestCase(issue_776_text, 0, 0, @"O<ESC>", issue_776_result, 0, 0), // Issue #776 crash
XVimMakeTestCase(issue_805_text, 33, 0, @"dd", issue_805_result, 0, 0), // Issue #805
XVimMakeTestCase(issue_809_a_text, 5, 0, @"dw", issue_809_a_result, 4, 0),
Expand Down
1 change: 1 addition & 0 deletions XVim/XVimKeyStroke.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ NSString* XVimKeyNotationFromXVimString(XVimString* string);
@property unsigned char modifier;
@property (nonatomic, readonly) BOOL isNumeric;
@property (nonatomic, readonly) BOOL isPrintable;
@property (nonatomic, readonly) BOOL isWhitespace;

- (id)initWithCharacter:(unichar)c modifier:(unsigned char)mod;

Expand Down
12 changes: 12 additions & 0 deletions XVim/XVimKeyStroke.m
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,13 @@ NS_INLINE BOOL isPrintable(unichar c)
return !isNSFunctionKey(c) && iswprint_l(c, s_locale);
}

NS_INLINE BOOL isWhitespace(unichar c)
{
init_maps();

return !isNSFunctionKey(c) && iswspace_l(c, s_locale);
}

NS_INLINE BOOL isValidKey(NSString *key)
{
init_maps();
Expand Down Expand Up @@ -620,6 +627,11 @@ - (BOOL)isPrintable
return !_modifier && isPrintable(_character);
}

- (BOOL)isWhitespace
{
return !_modifier && isWhitespace(_character);
}

- (NSString*)keyNotation{
NSMutableString *keyStr = [[NSMutableString alloc] init];
unichar charcode = _character;
Expand Down
32 changes: 24 additions & 8 deletions XVim/XVimReplaceEvaluator.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,31 @@ - (XVimEvaluator*)eval:(XVimKeyStroke*)keyStroke{
// Here we pass the key input to original text view.
// The input coming to this method is already handled by "Input Method"
// and the input maight be non ascii like 'あ'
if (self.oneCharMode || keyStroke.isPrintable) {
if (!keyStroke.isPrintable) {
nextEvaluator = [XVimEvaluator invalidEvaluator];
} else if (![self.sourceView xvim_replaceCharacters:keyStroke.character count:1]) {
nextEvaluator = [XVimEvaluator invalidEvaluator];
} else if (self.oneCharMode) {
nextEvaluator = nil;
BOOL relayEvent = NO;
BOOL newlinePressed = NO;
unichar replaceWith = keyStroke.character;

// injecting a CR right into the sourceView causes issues
if (replaceWith == '\r') {
replaceWith = '\n';
newlinePressed = YES;
}

if (!keyStroke.isPrintable && !keyStroke.isWhitespace) {
nextEvaluator = [XVimEvaluator invalidEvaluator];
} else if (!newlinePressed && ![self.sourceView xvim_replaceCharacters:replaceWith count:1]) {
nextEvaluator = [XVimEvaluator invalidEvaluator];
} else if (self.oneCharMode) {
if (newlinePressed) {
relayEvent = YES;
}
} else {
nextEvaluator = nil;
}
if (newlinePressed) {
XVimMotion* m = XVIM_MAKE_MOTION(MOTION_FORWARD, CHARACTERWISE_EXCLUSIVE, MOTION_OPTION_NONE, 1);
[self.sourceView xvim_delete:m andYank:NO];
}
if (relayEvent) {
NSEvent *event = [keyStroke toEventwithWindowNumber:0 context:nil];
[self.sourceView interpretKeyEvents:[NSArray arrayWithObject:event]];
}
Expand Down