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

支持3X,and直接在@""字符串中替换图片名 #55

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
<false/>
<key>IDESourceControlProjectIdentifier</key>
<string>38EA5A0B-F27C-46AF-84C4-880AF2D73FBE</string>
<key>IDESourceControlProjectName</key>
<string>KSImageNamed</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>C2F7F2B06ED7506D9B6499106F8DB5F0850044C7</key>
<string>https://github.com/musiczone/KSImageNamed-Xcode.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>KSImageNamed.xcodeproj</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>C2F7F2B06ED7506D9B6499106F8DB5F0850044C7</key>
<string>../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>https://github.com/musiczone/KSImageNamed-Xcode.git</string>
<key>IDESourceControlProjectVersion</key>
<integer>111</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
<string>C2F7F2B06ED7506D9B6499106F8DB5F0850044C7</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>C2F7F2B06ED7506D9B6499106F8DB5F0850044C7</string>
<key>IDESourceControlWCCName</key>
<string>KSImageNamed-Xcode</string>
</dict>
</array>
</dict>
</plist>
23 changes: 21 additions & 2 deletions KSImageNamed/DVTTextCompletionController+KSImageNamed.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,36 @@ + (void)load

- (BOOL)swizzle_acceptCurrentCompletion
{
BOOL success = [self swizzle_acceptCurrentCompletion];

NSRange selectedRange = [[self textView] selectedRange];

id item = nil;
if ([self.textView.textStorage respondsToSelector:@selector(sourceModelItemAtCharacterIndex:)]) {
item = [self.textView.textStorage sourceModelItemAtCharacterIndex:selectedRange.location]; //-2.插入时会增加@"
} else {
item = [[self.textView.textStorage sourceModelService] sourceModelItemAtCharacterIndex:selectedRange.location];
}
NSRange itemRange = [item range];
// NSLog(@"替换前,itemRange: %@ selectRange:%@",NSStringFromRange(itemRange),NSStringFromRange(selectedRange));
NSString *itemString = [[self.textView.textStorage string] substringWithRange:itemRange];
// NSLog(@"替换前,itemString:%@",itemString);

BOOL success = [self swizzle_acceptCurrentCompletion];

if (success) {
@try {
NSRange range = [[self textView] selectedRange];

if ([itemString hasPrefix:@"@\""]&&[itemString hasSuffix:@"\""]){
[self.textView insertText:@"" replacementRange:NSMakeRange(range.location, itemRange.location+itemRange.length-range.location+1+2)];
[self.textView insertText:@"" replacementRange:NSMakeRange(itemRange.location, 2)];
}

for (NSString *nextClassAndMethod in [[KSImageNamed sharedPlugin] completionStringsForType:KSImageNamedCompletionStringTypeClassAndMethod]) {
//If an autocomplete causes imageNamed: to get inserted, remove the token and immediately pop up autocomplete
if (range.location > [nextClassAndMethod length]) {
NSString *insertedString = [[[self textView] string] substringWithRange:NSMakeRange(range.location - [nextClassAndMethod length], [nextClassAndMethod length])];

// NSLog(@"准备插入替换.....%@,%@",insertedString,nextClassAndMethod);
if ([insertedString isEqualToString:nextClassAndMethod]) {
[[self textView] insertText:@"" replacementRange:range];
[self _showCompletionsAtCursorLocationExplicitly:YES];
Expand Down
10 changes: 8 additions & 2 deletions KSImageNamed/KSImageNamed.m
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,12 @@ - (NSArray *)_imageCompletionsForIndex:(id)index
NSString *normalFileName;
BOOL skip = NO;
BOOL is2x = NO;
BOOL is3x = NO;

if ([imageName hasSuffix:@"@2x"]) {
if ([imageName hasSuffix:@"@3x"]) {
normalFileName = [[imageName substringToIndex:[imageName length] - 3] stringByAppendingFormat:@".%@", [fileName pathExtension]];
is3x = YES;
}else if ([imageName hasSuffix:@"@2x"]) {
normalFileName = [[imageName substringToIndex:[imageName length] - 3] stringByAppendingFormat:@".%@", [fileName pathExtension]];
is2x = YES;
} else if ([imageName hasSuffix:@"@2x~ipad"]) {
Expand All @@ -246,7 +250,9 @@ - (NSArray *)_imageCompletionsForIndex:(id)index
KSImageNamedIndexCompletionItem *existingCompletionItem = [imageCompletionItems objectForKey:normalFileName];

if (existingCompletionItem) {
if (is2x) {
if (is3x) {
existingCompletionItem.has2x = YES;
}else if (is2x) {
existingCompletionItem.has2x = YES;
} else {
existingCompletionItem.has1x = YES;
Expand Down
1 change: 1 addition & 0 deletions KSImageNamed/KSImageNamedIndexCompletionItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
@property(nonatomic, strong, readonly) NSString *fileName;
@property(nonatomic, assign) BOOL has1x;
@property(nonatomic, assign) BOOL has2x;
@property(nonatomic, assign) BOOL has3x;
@property(nonatomic, assign) BOOL forSwift;
@property(nonatomic, assign, getter=isInAssetCatalog, readonly) BOOL inAssetCatalog;

Expand Down
12 changes: 10 additions & 2 deletions KSImageNamed/KSImageNamedIndexCompletionItem.m
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,20 @@ - (NSString *)displayText
{
NSString *displayFormat = @"%@ (%@)";

if (self.has1x && self.has2x) {
if (self.has1x && self.has2x && self.has3x) {
displayFormat = @"%@ (%@, 1x and 2x and 3x)";
}if (self.has2x && self.has3x) {
displayFormat = @"%@ (%@, 2x and 3x)";
}if (self.has1x && self.has3x) {
displayFormat = @"%@ (%@, 1x and 3x)";
}else if (self.has1x && self.has2x) {
displayFormat = @"%@ (%@, 1x and 2x)";
} else if (self.has1x) {
displayFormat = @"%@ (%@, 1x only)";
} else if (self.has2x) {
displayFormat = @"%@ (%@, 2x only)";
}else if (self.has3x){
displayFormat = @"%@ (%@, 3x only)";
}

return [NSString stringWithFormat:displayFormat, [self _imageNamedText], [[self fileURL] pathExtension]];
Expand All @@ -131,7 +139,7 @@ - (NSString *)_fileName
NSString *fileName = [[self fileURL] lastPathComponent];
NSString *imageName = [fileName stringByDeletingPathExtension];

if ([imageName hasSuffix:@"@2x"]) {
if ([imageName hasSuffix:@"@2x"] || [imageName hasSuffix:@"@3x"]) {
fileName = [[imageName substringToIndex:[imageName length] - 3] stringByAppendingFormat:@".%@", [fileName pathExtension]];
} else if ([imageName hasSuffix:@"@2x~ipad"]) {
//2x iPad images need to be handled separately since (image~ipad and image@2x~ipad are valid pairs)
Expand Down
1 change: 1 addition & 0 deletions KSImageNamed/XcodeMisc.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
- (id)stringConstantAtLocation:(unsigned long long)arg1; //DVTSourceModel

- (id)previousItem; //DVTSourceModelItem
- (id)nextItem;

- (id)_listWindowController; //DVTTextCompletionSession
@end
Expand Down