forked from XVimProject/XVim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix XVimProject#629 if completionText doesn't contain placeholders
- Loading branch information
Showing
8 changed files
with
164 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// DVTTextCompletionController+XVim.h | ||
// XVim | ||
// | ||
// Created by Muronaka Hiroaki on 2015/09/27. | ||
// | ||
// | ||
|
||
#import "DVTKit.h" | ||
|
||
@interface DVTTextCompletionController (XVim) | ||
|
||
+(void)xvim_initialize; | ||
+(void)xvim_finalize; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// DVTTextCompletionController+XVim.m | ||
// XVim | ||
// | ||
// Created by Muronaka Hiroaki on 2015/09/27. | ||
// | ||
// | ||
|
||
#import "DVTTextCompletionController+XVim.h" | ||
#import "DVTTextCompletionListWindowController+XVim.h" | ||
#import "NSObject+XVimAdditions.h" | ||
|
||
@implementation DVTTextCompletionController (XVim) | ||
|
||
+ (void)xvim_initialize { | ||
[self xvim_swizzleInstanceMethod:@selector(acceptCurrentCompletion) with:@selector(xvim_acceptCurrentCompletion)]; | ||
} | ||
|
||
+ (void)xvim_finalize { | ||
[self xvim_swizzleInstanceMethod:@selector(acceptCurrentCompletion) with:@selector(xvim_acceptCurrentCompletion)]; | ||
} | ||
|
||
- (BOOL)xvim_acceptCurrentCompletion { | ||
|
||
if([self.currentSession.listWindowController tryExpandingCompletion]) { | ||
return YES; | ||
} | ||
|
||
return [self xvim_acceptCurrentCompletion]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// DVTTextCompletionListWindowController+XVim.h | ||
// XVim | ||
// | ||
// Created by Muronaka Hiroaki on 2015/09/27. | ||
// | ||
// | ||
|
||
#import "DVTKit.h" | ||
|
||
@interface DVTTextCompletionListWindowController (XVim) | ||
|
||
- (BOOL)tryExpandingCompletion; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// | ||
// DVTTextCompletionListWindowController+XVim.m | ||
// XVim | ||
// | ||
// Created by Muronaka Hiroaki on 2015/09/27. | ||
// | ||
// | ||
|
||
#import "IDEKit.h" | ||
#import "DVTTextCompletionListWindowController+XVim.h" | ||
#import "XVim.h" | ||
|
||
@implementation DVTTextCompletionListWindowController (XVim) | ||
|
||
- (BOOL)tryExpandingCompletion | ||
{ | ||
IDEIndexCompletionItem *item = [self _selectedCompletionItem]; | ||
|
||
[self expandCompletionItem:item]; | ||
|
||
return NO; | ||
} | ||
|
||
|
||
|
||
-(void)expandCompletionItem:(IDEIndexCompletionItem*)completionItem { | ||
NSMutableString* tempRepeatRegister = [XVim instance].tempRepeatRegister; | ||
unichar lastChar = [tempRepeatRegister characterAtIndex:tempRepeatRegister.length - 1]; | ||
if( [[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:lastChar] ) { | ||
[tempRepeatRegister deleteCharactersInRange:NSMakeRange(tempRepeatRegister.length - 1, 1)]; | ||
} | ||
|
||
// This is a little better if completionText contains "< # code # >". | ||
// NSRange range = [self rangeOfExpandFromItemName:completionItem.name tempRegister:tempRepeatRegister]; | ||
// if( range.location != NSNotFound ) { | ||
// [tempRepeatRegister replaceCharactersInRange:range withString:completionItem.completionText]; | ||
// return; | ||
// } | ||
// | ||
|
||
DVTSourceTextView *textView = (DVTSourceTextView *)self.session.textView; | ||
if(self.session.wordStartLocation == NSNotFound) { | ||
return; | ||
} | ||
|
||
|
||
NSRange wordRange = NSMakeRange(self.session.wordStartLocation, self.session.cursorLocation - self.session.wordStartLocation); | ||
NSString* word = [textView.string substringWithRange:wordRange]; | ||
NSRange range = [self rangeOfExpandWord:word tempRegister:tempRepeatRegister]; | ||
|
||
if( range.location != NSNotFound ) { | ||
[tempRepeatRegister replaceCharactersInRange:range withString:completionItem.completionText]; | ||
} | ||
} | ||
|
||
-(NSRange)rangeOfExpandFromItemName:(NSString*)itemName tempRegister:(NSString*)tempRepeatRegister { | ||
NSString* lowercaseTempRepeatRegister = [tempRepeatRegister lowercaseString]; | ||
NSString* lowercaseItemName = [itemName lowercaseString]; | ||
|
||
NSRange result = NSMakeRange(NSNotFound, 0); | ||
for(NSUInteger i = lowercaseItemName.length; i > 0; i--) { | ||
NSString* itemSubString = [[lowercaseItemName substringToIndex:i] lowercaseString]; | ||
if( [lowercaseTempRepeatRegister hasSuffix:itemSubString] ) { | ||
result = NSMakeRange(tempRepeatRegister.length - i, itemSubString.length); | ||
break; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
-(NSRange)rangeOfExpandWord:(NSString*)word tempRegister:(NSString*)tempRepeatRegister { | ||
NSString* lowercaseTempRepeatRegister = [tempRepeatRegister lowercaseString]; | ||
NSString* lowercaseWord = [word lowercaseString]; | ||
|
||
NSRange prevRange = NSMakeRange(NSNotFound, 0); | ||
for(NSInteger i = (NSInteger)lowercaseTempRepeatRegister.length - 1; i >= 0; i--) { | ||
NSRange currentRange = NSMakeRange((NSUInteger)i, lowercaseTempRepeatRegister.length - (NSUInteger)i); | ||
if( ![lowercaseWord hasSuffix:[lowercaseTempRepeatRegister substringWithRange:currentRange]] ) { | ||
break; | ||
} | ||
prevRange = currentRange; | ||
} | ||
return prevRange; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters