Skip to content

Instantly share code, notes, and snippets.

Avatar

Marcin Krzyzanowski krzyzanowskim

View GitHub Profile
@krzyzanowskim
krzyzanowskim / UTF16_to_bytes.swift
Last active Mar 7, 2022
Creates a string from the given bytes in the UTF16 encoding.
View UTF16_to_bytes.swift
extension Array where Element == UInt16 {
/// Little-Endian UTF16 bytes
var bytes: [UInt8] {
flatMap {
[
UInt8($0.littleEndian & 0xFF),
UInt8(($0.littleEndian &>> 8) & 0xFF)
]
}
@krzyzanowskim
krzyzanowskim / CharacterSet+Character.swift
Last active Feb 28, 2022
Check whether CharacterSet contains Character
View CharacterSet+Character.swift
extension CharacterSet {
/// Check whether CharacterSet contains Character
func contains(_ character: Character) -> Bool {
character.unicodeScalars.allSatisfy(contains)
}
}
View NSRange+NSTextContentManager.swift
extension NSRange {
static let notFound = NSRange(location: NSNotFound, length: 0)
var isEmpty: Bool {
length == 0
}
init(_ textRange: NSTextRange, in textContentManager: NSTextContentManager) {
let offset = textContentManager.offset(from: textContentManager.documentRange.location, to: textRange.location)
View Makefile-tree-sitter
VERSION := 0.1.0
# Repository
SRC_DIR := src
PARSER_REPO_URL ?= $(shell git -C $(SRC_DIR) remote get-url origin)
$(eval PARSER_NAME=$(shell basename $(PARSER_REPO_URL) | cut -d '-' -f3 | sed 's#.git##' ))
UPPER_PARSER_NAME := $(shell echo $(PARSER_NAME) | tr a-z A-Z)
@krzyzanowskim
krzyzanowskim / NSTextLocation+Equatable.swift
Created Jan 30, 2022
NSTextLocation missing Equatable helper
View NSTextLocation+Equatable.swift
extension NSTextLocation {
static func == (lhs: Self, rhs: Self) -> Bool {
lhs.compare(rhs) == .orderedSame
}
static func != (lhs: Self, rhs: Self) -> Bool {
lhs.compare(rhs) != .orderedSame
}
View README.md

Given a NSTextContentStorage with a single character \n (newline), a layout manager results in 2 line fragments.

Two things to discuss here:

  1. The range of a second TextLineFragment is {1, 0}, that is 0 length range, yet the textLineFragment.attributedString.string value is "\n"
  2. Why two lines at all? I expected a single line. this seems to be a case only if \n is at the end of the storage string. The string "\n\n\n" results in 3 fragments, and 4 lines.
View SwiftUITextFieldEditingCase.swift
import SwiftUI
import PlaygroundSupport
struct TestView: View {
@State var text: String = ""
@State var dummy: String = "dummy"
@State var showTextField1: Bool = true
@State var showTextField2: Bool = true
var body: some View {
@krzyzanowskim
krzyzanowskim / Image+customSymbol.swift
Created Dec 23, 2021
Image system and custom SFSymbol initializer
View Image+customSymbol.swift
import SwiftUI
extension Image {
init(symbol name: String, accessibilityDescription: String? = nil) {
if let nsImage = NSImage(systemSymbolName: name, accessibilityDescription: accessibilityDescription) {
self.init(nsImage: nsImage)
} else if let nsImage = NSImage(named: name) {
nsImage.accessibilityDescription = accessibilityDescription
self.init(nsImage: nsImage)
@krzyzanowskim
krzyzanowskim / FB9762603.swift
Last active Dec 5, 2021
FB9762603: TextField with NumberFormatter() is broken on macOS 12 - no values
View FB9762603.swift
/*
16 November 2021
SwiftUI.TextField used with NumberFormatter() as a formatter does not set/update value on macOS 12 (Monterey). This is regression as it works ok on macOS 11 (BigSur)
Please find attached Playground code and code screenshot with marked bug
Related FB9180913. Reported June 15.
*/
View TextFormatter.swift
class TextFormatter: Formatter {
override func string(for obj: Any?) -> String? {
obj as? String
}
override func getObjectValue(_ obj: AutoreleasingUnsafeMutablePointer<AnyObject?>?, for string: String, errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool {
obj?.pointee = string as AnyObject
return true
}
}