Skip to content

Commit

Permalink
Found that column does not equal character and adjusted post-insertio…
Browse files Browse the repository at this point in the history
…n selection for the presence of tabs
  • Loading branch information
Thomas Moore committed Jan 20, 2019
1 parent 3adcf14 commit f8a689e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
3 changes: 3 additions & 0 deletions UUID Generator Extension.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@
CODE_SIGN_IDENTITY = "Mac Developer";
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
Expand Down Expand Up @@ -435,6 +436,7 @@
CODE_SIGN_IDENTITY = "Mac Developer";
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
Expand Down Expand Up @@ -566,6 +568,7 @@
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = Y5R65N2GLX;
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = "UUID Generator/Info.plist";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand Down
31 changes: 27 additions & 4 deletions UUID Generator/SourceEditorCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ enum UUIDGenInvocation: String {
}

class SourceEditorCommand: NSObject, XCSourceEditorCommand {

static let tabRegex = try? NSRegularExpression.init(pattern: "[\t]", options: [])

func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: @escaping (Error?) -> Void ) -> Void {
let selections = invocation.buffer.selections
Expand All @@ -35,8 +37,9 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
let selectedLineRange = NSRange(location: startLine, length: endLine - startLine + 1)
let selectedLines = lines.subarray(with: selectedLineRange)
let (newLine, uuidrange) = replace(selectedLines: selectedLines,
range: textSelection,
replacementString: generatedUUID(invocation: invocation))
range: textSelection,
replacementString: generatedUUID(invocation: invocation),
tabWidth: invocation.buffer.tabWidth)
lines.removeObjects(in: selectedLineRange)
lines.insert(newLine, at: selectedLineRange.location)

Expand All @@ -56,7 +59,7 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
return newUuid
}

func replace(selectedLines: [Any], range: XCSourceTextRange, replacementString: String) -> (String, NSRange) {
func replace(selectedLines: [Any], range: XCSourceTextRange, replacementString: String, tabWidth: Int) -> (String, NSRange) {
guard let firstLine = selectedLines.first as? String, let lastLine = selectedLines.last as? String else {
return ("", NSRange(location: 0, length: 0))
}
Expand All @@ -67,7 +70,27 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
finalString += replacementString
let lastLineStartIndex = lastLine.index(lastLine.startIndex, offsetBy: range.end.column)
finalString += String(lastLine[lastLineStartIndex..<lastLine.endIndex])
return (finalString, NSRange(location: finalStringStartPosition, length: replacementString.count))
let offset = checkTabOffset(line: finalString, column: finalStringStartPosition, tabWidth: tabWidth)
return (finalString, NSRange(location: finalStringStartPosition + offset, length: replacementString.count))
}

//since columns != characters, we have to adjust position if tabs present
func checkTabOffset(line: String, column: Int, tabWidth: Int) -> Int {
guard let regex = SourceEditorCommand.tabRegex else { return 0 }
let matches = regex.matches(in: line, options: [], range: NSRange(location: 0, length: line.count))
var offset = matches.reduce(0, { (matches, result) -> Int in
var count = matches
let range = result.range
if range.location < column {
count += tabWidth
}
return count
})
if offset > 0 {
offset -= 1
}
return offset

}

}

0 comments on commit f8a689e

Please sign in to comment.