Skip to content

Commit

Permalink
swifty NSTextStorage extension
Browse files Browse the repository at this point in the history
  • Loading branch information
pebble8888 committed Apr 17, 2021
1 parent 0fd20be commit e58dd45
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 70 deletions.
1 change: 1 addition & 0 deletions XVim2-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
#import "XVimMotionStyle.h"
#import "XVimDefs.h"
#import "XVim.h"
#import "NSString+VimHelper.h"

#endif /* XVim2_Bridging_Header_h */
10 changes: 7 additions & 3 deletions XVim2.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
A2B537B11F51C94400D83C0A /* NSObject+Swizzle.m in Sources */ = {isa = PBXBuildFile; fileRef = A2B537B01F51C94400D83C0A /* NSObject+Swizzle.m */; };
A2B5D78D2243319A00BD289D /* XcodeHeader in Resources */ = {isa = PBXBuildFile; fileRef = A2B5D78C2243319A00BD289D /* XcodeHeader */; };
A2C359962479552C00259730 /* XVimMotion.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2C359952479552C00259730 /* XVimMotion.swift */; };
A2C61BB8262AB7CF00E7E1DB /* NSTextStorage+VimOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2C61BB7262AB7CF00E7E1DB /* NSTextStorage+VimOperation.swift */; };
A2D464BC20D4042B00A43EC7 /* SwiftLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2D464BB20D4042B00A43EC7 /* SwiftLogger.swift */; };
A2FB1EE12077B65100D3E634 /* File.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2FB1EE02077B65100D3E634 /* File.swift */; };
ED0DED611F8C2CEC00886F6B /* XVimZEvaluator.m in Sources */ = {isa = PBXBuildFile; fileRef = ED0DED5D1F8C2CDD00886F6B /* XVimZEvaluator.m */; };
Expand Down Expand Up @@ -181,6 +182,7 @@
A2B537B01F51C94400D83C0A /* NSObject+Swizzle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+Swizzle.m"; sourceTree = "<group>"; };
A2B5D78C2243319A00BD289D /* XcodeHeader */ = {isa = PBXFileReference; lastKnownFileType = folder; path = XcodeHeader; sourceTree = "<group>"; };
A2C359952479552C00259730 /* XVimMotion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XVimMotion.swift; sourceTree = "<group>"; };
A2C61BB7262AB7CF00E7E1DB /* NSTextStorage+VimOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSTextStorage+VimOperation.swift"; sourceTree = "<group>"; };
A2D464BB20D4042B00A43EC7 /* SwiftLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = SwiftLogger.swift; path = XVim2/Helper/SwiftLogger.swift; sourceTree = SOURCE_ROOT; };
A2ED76F823743EC700C81258 /* SourceEditorViewProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SourceEditorViewProtocol.h; path = XVim2/Xcode/SourceEditorViewProtocol.h; sourceTree = SOURCE_ROOT; };
A2FB1EE02077B65100D3E634 /* File.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = File.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -558,13 +560,14 @@
EDF010501F6EEEF600461B2C /* XVim */ = {
isa = PBXGroup;
children = (
A2753926247D34E2001608F3 /* C */,
A20FB79A21BB7FF2001FDC62 /* about.xib */,
A2753926247D34E2001608F3 /* C */,
ED6331CD1F83797E00E9E6B0 /* CommandLine */,
A28D4128207E5695001E0E13 /* Evaluator */,
A2988E032629B1E700D83D22 /* NSString+Util.swift */,
ED6B8E4D1F80F896002F4A47 /* NSTextStorage+VimOperation.h */,
ED6B8E4A1F80F896002F4A47 /* NSTextStorage+VimOperation.m */,
A2C61BB7262AB7CF00E7E1DB /* NSTextStorage+VimOperation.swift */,
ED0DED7D1F8C41EF00886F6B /* NSURL+XVimXcodeModule.h */,
ED0DED7E1F8C41F000886F6B /* NSURL+XVimXcodeModule.m */,
EDF010751F6EF16600461B2C /* XVim.h */,
Expand All @@ -575,9 +578,9 @@
EDB8EF4E1F91F8DA004188F5 /* XVimConstants.m */,
EDF010521F6EEF0F00461B2C /* XVimDefs.h */,
ED0DED7B1F8C418200886F6B /* XVimEval.h */,
A275392A247D3826001608F3 /* XVimEvalFunc.swift */,
A2753928247D372D001608F3 /* XVimEvalArg.swift */,
ED0DED7A1F8C418100886F6B /* XVimEval.m */,
A2753928247D372D001608F3 /* XVimEvalArg.swift */,
A275392A247D3826001608F3 /* XVimEvalFunc.swift */,
A2053343247A969400412C8B /* XVimExArg.swift */,
A2053345247A973D00412C8B /* XVimExCmdname.swift */,
ED0DED6F1F8C3B0600886F6B /* XVimExCommand.h */,
Expand Down Expand Up @@ -817,6 +820,7 @@
A2A9F93E2070A85200E7BB53 /* XVimIDESourceEditorView.m in Sources */,
A275392B247D3826001608F3 /* XVimEvalFunc.swift in Sources */,
EDF010681F6EEFFC00461B2C /* XVimEvaluator.m in Sources */,
A2C61BB8262AB7CF00E7E1DB /* NSTextStorage+VimOperation.swift in Sources */,
EDB7F6031F6D8CE5007E0C41 /* rd_route.c in Sources */,
A2A9F9352070A84600E7BB53 /* SourceEditorViewProxy.m in Sources */,
ED0DED821F8C444800886F6B /* NSColor+Extra.m in Sources */,
Expand Down
20 changes: 4 additions & 16 deletions XVim2/XVim/NSTextStorage+VimOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

#import "XVimDefs.h"
#import "XVimTextStoring.h"
#import "XVim2-Swift.h"
#import "XVimMotionOption.h"

@class XVimMotionInfo;

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -75,21 +77,6 @@ NS_ASSUME_NONNULL_BEGIN

#pragma mark Definitions

// Determine if the position specified with "index" is EOF.
- (BOOL)isEOF:(NSUInteger)index;

// Determine if the position specified with "index" is LOL.
- (BOOL)isLOL:(NSUInteger)index;

// Determine if the position specified with "index" is EOL.
- (BOOL)isEOL:(NSUInteger)index;

// Determine if the position is a beginning of line
- (BOOL)isBOL:(NSUInteger)index;

// Determine if the position specified with "index" is newline.
- (BOOL)isNewline:(NSUInteger)index;

// Determine if the position specified with "index" is white space.
- (BOOL)isWhitespace:(NSUInteger)index;

Expand Down Expand Up @@ -235,6 +222,7 @@ NSInteger xv_findChar(NSString* string, NSInteger index, int repeatCount, char c

#pragma mark undo
- (void)xvim_undoCursorPos:(NSNumber*)num;

@end

NS_ASSUME_NONNULL_END
50 changes: 1 addition & 49 deletions XVim2/XVim/NSTextStorage+VimOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import "XVimStringBuffer.h"
#import "XVim.h"
#import "XVimOptions.h"
#import "XVim2-Swift.h"

@implementation NSTextStorage (VimOperation)

Expand Down Expand Up @@ -62,14 +63,6 @@ - (NSRange)xvim_indexRangeForLineNumber:(NSUInteger)num newLineLength:(NSUIntege
return NSMakeRange(NSNotFound, 0);
}

- (NSUInteger)validIndex:(NSUInteger)index
{
if (index > self.length){
return self.length;
}
return index;
}

- (NSRange)xvim_indexRangeForLineAtIndex:(NSUInteger)index newLineLength:(NSUInteger*)newLineLength
{
let string = self.xvim_string;
Expand Down Expand Up @@ -301,38 +294,6 @@ - (NSUInteger)xvim_nextDigitInLine:(NSUInteger)index

#pragma mark Definitions

- (BOOL)isEOF:(NSUInteger)index
{
index = [self validIndex:index];
return [[self xvim_string] length] == index;
}

- (BOOL)isLOL:(NSUInteger)index
{
index = [self validIndex:index];
return [self isEOF:index] == NO && [self isNewline:index] == NO && [self isNewline:index + 1];
}

- (BOOL)isEOL:(NSUInteger)index
{
index = [self validIndex:index];
return [self isNewline:index] || [self isEOF:index];
}

- (BOOL)isBOL:(NSUInteger)index
{
index = [self validIndex:index];
if (0 == index) {
return YES;
}

if ([self isNewline:index - 1]) {
return YES;
}

return NO;
}

- (BOOL)isLastCharacter:(NSUInteger)index
{
index = [self validIndex:index];
Expand All @@ -342,15 +303,6 @@ - (BOOL)isLastCharacter:(NSUInteger)index
return [[self xvim_string] length] - 1 == index;
}

- (BOOL)isNewline:(NSUInteger)index
{
index = [self validIndex:index];
if (index == [[self xvim_string] length]) {
return NO; // EOF is not a newline
}
return isNewline([[self xvim_string] characterAtIndex:index]);
}

- (BOOL)isWhitespace:(NSUInteger)index
{
index = [self validIndex:index];
Expand Down
55 changes: 55 additions & 0 deletions XVim2/XVim/NSTextStorage+VimOperation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// NSTextStorage+VimOperation.swift
// XVim2
//
// Created by pebble8888 on 2021/04/17.
// Copyright © 2021 Shuichiro Suzuki. All rights reserved.
//

import Foundation

extension NSTextStorage {
@objc func validIndex(_ index: UInt) -> UInt {
if index > self.string.count {
return UInt(self.string.count)

This comment has been minimized.

Copy link
@r-plus

r-plus Apr 17, 2021

Member

maybe this two lines...?

original implementation used self.length instead.

for: #372

This comment has been minimized.

Copy link
@r-plus

r-plus Apr 17, 2021

Member

yes, when I change to self.length, performance issue is resolved.

This comment has been minimized.

Copy link
@r-plus

r-plus Apr 17, 2021

Member

oh, when I rebuild not yet resolved.

}
return index
}

@objc func isEOF(_ index: UInt) -> Bool {
let index = validIndex(index)
return self.string.count == index

This comment has been minimized.

Copy link
@r-plus

r-plus Apr 17, 2021

Member

@pebble8888 FYI: Swift.String count is not always same to NSString's length because NSString length property return UTF-16 code unit count.

Is this change safe for XVim? Maybe this is breaking change if user editing source code contains emoji etc.

$ swift
Welcome to Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28).
Type :help for assistance.
  1> import Foundation
  2> let a = " \ \ \👦"00D"
a: String = "👨‍👩‍👦‍👦"
  3> a.count
$R0: Int = 1
  4> (a as NSString).length
$R1: Int = 11
}

@objc func isLOL(_ index: UInt) -> Bool {
let index = self.validIndex(index)
return self.isEOF(index) == false && self.isNewline(index) == false && self.isNewline(index + 1)
}

@objc func isEOL(_ index: UInt) -> Bool {
let index = self.validIndex(index)
return self.isNewline(index) || self.isEOF(index)
}

@objc func isBOL(_ index: UInt) -> Bool {
let index = self.validIndex(index)
if 0 == index {
return true
}

if self.isNewline(index - 1) {
return true
}

return false
}

@objc func isNewline(_ index: UInt) -> Bool {
let index = self.validIndex(index)
if index == self.string.count {
return false // EOF is not a newline
}

return self.string.isNewline(index)
}
}
1 change: 1 addition & 0 deletions XVim2/Xcode/SourceEditorViewProxy+Operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "SourceEditorViewProxy.h"
#import <Foundation/Foundation.h>
#import "XVimMotionType.h"

NS_ASSUME_NONNULL_BEGIN

Expand Down
4 changes: 2 additions & 2 deletions XVim2/Xcode/SourceEditorViewProxy+Operations.m
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ - (void)xvim_swapCase:(XVimMotion*)motion
}
if (m.motionInfo.reachedEndOfLine) {
[self xvim_swapCaseForRange:[self xvim_getOperationRangeFrom:r.begin
To:r.end
Type:CHARWISE_INCLUSIVE]];
To:r.end
Type:CHARWISE_INCLUSIVE]];
}
else {
[self xvim_swapCaseForRange:[self xvim_getOperationRangeFrom:r.begin
Expand Down

0 comments on commit e58dd45

Please sign in to comment.