diff --git a/UUID Generator Extension.xcodeproj/project.pbxproj b/UUID Generator Extension.xcodeproj/project.pbxproj index 56ad744..69798ca 100644 --- a/UUID Generator Extension.xcodeproj/project.pbxproj +++ b/UUID Generator Extension.xcodeproj/project.pbxproj @@ -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; @@ -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; @@ -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)", diff --git a/UUID Generator/SourceEditorCommand.swift b/UUID Generator/SourceEditorCommand.swift index e99a0c6..a36d860 100644 --- a/UUID Generator/SourceEditorCommand.swift +++ b/UUID Generator/SourceEditorCommand.swift @@ -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 @@ -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) @@ -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)) } @@ -67,7 +70,27 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand { finalString += replacementString let lastLineStartIndex = lastLine.index(lastLine.startIndex, offsetBy: range.end.column) finalString += String(lastLine[lastLineStartIndex.. 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 + } }