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

General improvements #18

Open
wants to merge 7 commits into
base: main
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
5 changes: 5 additions & 0 deletions Sources/Extensions/String+NSRange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ extension String {
func nsRange(fromRange range: Range<Index>) -> NSRange {
return NSRange(range, in: self)
}
init<T: StringProtocol>(fromRange range: NSRange, in fullString: T) {
let start: String.Index = fullString.index(fullString.startIndex, offsetBy: range.location)
let end: String.Index = fullString.index(start, offsetBy: range.length)
self = String(fullString[start..<end])
}
}
15 changes: 13 additions & 2 deletions Sources/Highlighter/Data/Languages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@

import Foundation
//TODO: if( is matched as a function not a keyword. Figure out a system to match if as a keyword not a function
let languages: [String: [String: Any]] = [
"default": [:],
var languages: [String: [String: Any]] = [
"default": defaultLanguage,
"jelly": jellyLanguage,
"swift": swiftLanguage
]
public var fireflyLanguages: [String: [String: Any]] {
get {
languages
}
set {
languages = newValue
}
}

let defaultLanguage: [String: Any] = [
"display_name": "Generic",
"string": [
"regex": #"(?<!\\)".*?(?<!\\)""#,
"group": 0,
Expand Down Expand Up @@ -45,6 +54,7 @@ let defaultLanguage: [String: Any] = [
]

let jellyLanguage: [String: Any] = [
"display_name": "Jelly",
"function": [
"regex": "([a-zA-Z_0-9\"\\[\\]\\+-]+)\\(.*?\\)",//\\. -Removed
"group": 1,
Expand Down Expand Up @@ -97,6 +107,7 @@ let jellyLanguage: [String: Any] = [
]

let swiftLanguage: [String: Any] = [
"display_name": "Swift",
"identifier": [
"regex": "(\\.[A-Za-z_]+\\w*)|((NS|UI)[A-Z][a-zA-Z]+)|((println|print)(?=\\())|(Any|Array|AutoreleasingUnsafePointer|BidirectionalReverseView|Bit|Bool|CFunctionPointer|COpaquePointer|CVaListPointer|Character|CollectionOfOne|ConstUnsafePointer|ContiguousArray|Data|Dictionary|DictionaryGenerator|DictionaryIndex|Double|EmptyCollection|EmptyGenerator|EnumerateGenerator|FilterCollectionView|FilterCollectionViewIndex|FilterGenerator|FilterSequenceView|Float|Float80|FloatingPointClassification|GeneratorOf|GeneratorOfOne|GeneratorSequence|HeapBuffer|HeapBuffer|HeapBufferStorage|HeapBufferStorageBase|ImplicitlyUnwrappedOptional|IndexingGenerator|Int|Int16|Int32|Int64|Int8|IntEncoder|LazyBidirectionalCollection|LazyForwardCollection|LazyRandomAccessCollection|LazySequence|Less|MapCollectionView|MapSequenceGenerator|MapSequenceView|MirrorDisposition|ObjectIdentifier|OnHeap|Optional|PermutationGenerator|QuickLookObject|RandomAccessReverseView|Range|RangeGenerator|RawByte|Repeat|ReverseBidirectionalIndex|Printable|ReverseRandomAccessIndex|SequenceOf|SinkOf|Slice|StaticString|StrideThrough|StrideThroughGenerator|StrideTo|StrideToGenerator|String|Index|UTF8View|Index|UnicodeScalarView|IndexType|GeneratorType|UTF16View|UInt|UInt16|UInt32|UInt64|UInt8|UTF16|UTF32|UTF8|UnicodeDecodingResult|UnicodeScalar|Unmanaged|UnsafeArray|UnsafeArrayGenerator|UnsafeMutableArray|UnsafePointer|VaListBuilder|Header|Zip2|ZipGenerator2)",
"group": 0,
Expand Down
11 changes: 10 additions & 1 deletion Sources/Highlighter/Data/Themes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

let themes: [String: [String: Any]] = [
var themes: [String: [String: Any]] = [
//https://github.com/hdoria/xcode-themes
"Basic": [
"default": "#000000",
Expand Down Expand Up @@ -1193,3 +1193,12 @@ let themes: [String: [String: Any]] = [
],

]

public var fireflyThemes: [String: [String: Any]] {
get {
themes
}
set {
themes = newValue
}
}
19 changes: 12 additions & 7 deletions Sources/Highlighter/Syntax/Syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,22 @@ public class Syntax {
/// Changes the current language & updates the definitions
/// - Parameter name: Name of the language
func setLanguage(to name: String) {
// clear previous definitions
definitions = []

// install language definitions
if let language = languages[name.lowercased()] {
for item in language {
let type = item.key
let dict: [String: Any] = item.value as! [String : Any]
let regex: String = dict["regex"] as? String ?? ""
let group: Int = dict["group"] as? Int ?? 0
let relevance: Int = dict["relevance"] as? Int ?? 0
let options: [NSRegularExpression.Options] = dict["options"] as? [NSRegularExpression.Options] ?? []
let multi: Bool = dict["multiline"] as? Bool ?? false
if let dict: [String: Any] = item.value as? [String : Any] {
let regex: String = dict["regex"] as? String ?? ""
let group: Int = dict["group"] as? Int ?? 0
let relevance: Int = dict["relevance"] as? Int ?? 0
let options: [NSRegularExpression.Options] = dict["options"] as? [NSRegularExpression.Options] ?? []
let multi: Bool = dict["multiline"] as? Bool ?? false

definitions.append(Definition(type: type, regex: regex, group: group, relevance: relevance, matches: options, multiLine: multi))
definitions.append(Definition(type: type, regex: regex, group: group, relevance: relevance, matches: options, multiLine: multi))
}
}
}
var editorPlaceholderPattern = "(<#)([^\"\\n]*?)"
Expand Down
112 changes: 111 additions & 1 deletion Sources/Views/FireflySyntaxView + TextDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ extension FireflySyntaxView: TextViewDelegate {
if let token = inside.1 {
let fullRange = NSRange(location: 0, length: self.text.utf16.count)
if token.range.upperBound < fullRange.upperBound {
#if os(iOS)
let textViewTextStorageString: String = textView.textStorage.string
#else
let textViewTextStorageString: String = textView.textStorage!.string
#endif
self.onTextChange(
oldText: String(fromRange: token.range, in: textViewTextStorageString),
location: token.range.location,
newText: text
)

textStorage.removeAttribute(.font, range: token.range)
textStorage.removeAttribute(.foregroundColor, range: token.range)
textStorage.removeAttribute(.editorPlaceholder, range: token.range)
Expand Down Expand Up @@ -71,6 +82,16 @@ extension FireflySyntaxView: TextViewDelegate {
// Update on backspace
updateGutterNow = true
self.textStorage.endEditing()
#if os(iOS)
let textViewTextStorageString: String = textView.textStorage.string
#else
let textViewTextStorageString: String = textView.textStorage!.string
#endif
self.onTextChange(
oldText: String(fromRange: range, in: textViewTextStorageString),
location: range.location,
newText: text
)
return true
}
}
Expand All @@ -97,6 +118,17 @@ extension FireflySyntaxView: TextViewDelegate {
} else if characterBuffer[1, default: ""] == "\"" && characterBuffer[0, default: ""] != "\"" && characterBuffer[2, default: ""] != "\"" {
insertingText += "\""

#if os(iOS)
let textViewTextStorageString: String = textView.textStorage.string
#else
let textViewTextStorageString: String = textView.textStorage!.string
#endif
self.onTextChange(
oldText: String(fromRange: selectedRange, in: textViewTextStorageString),
location: selectedRange.location,
newText: insertingText
)

#if canImport(UIKit)
textView.textStorage.replaceCharacters(in: selectedRange, with: insertingText)

Expand All @@ -122,6 +154,17 @@ extension FireflySyntaxView: TextViewDelegate {
} else if characterBuffer[1, default: ""] == "(" && characterBuffer[0, default: ""] != ")" {
insertingText += ")"

#if os(iOS)
let textViewTextStorageString: String = textView.textStorage.string
#else
let textViewTextStorageString: String = textView.textStorage!.string
#endif
self.onTextChange(
oldText: String(fromRange: selectedRange, in: textViewTextStorageString),
location: selectedRange.location,
newText: insertingText
)

#if canImport(UIKit)
textView.textStorage.replaceCharacters(in: selectedRange, with: insertingText)

Expand All @@ -148,6 +191,18 @@ extension FireflySyntaxView: TextViewDelegate {
// Update on new line
if text == "\n" {
insertingText += "\t\(newlineInsert)\n\(newlineInsert)}"

#if os(iOS)
let textViewTextStorageString: String = textView.textStorage.string
#else
let textViewTextStorageString: String = textView.textStorage!.string
#endif
self.onTextChange(
oldText: String(fromRange: selectedRange, in: textViewTextStorageString),
location: selectedRange.location,
newText: insertingText
)

#if canImport(UIKit)
textView.textStorage.replaceCharacters(in: selectedRange, with: insertingText)

Expand All @@ -164,6 +219,18 @@ extension FireflySyntaxView: TextViewDelegate {
#endif
} else {
insertingText += "}"

#if os(iOS)
let textViewTextStorageString: String = textView.textStorage.string
#else
let textViewTextStorageString: String = textView.textStorage!.string
#endif
self.onTextChange(
oldText: String(fromRange: selectedRange, in: textViewTextStorageString),
location: selectedRange.location,
newText: insertingText
)

#if canImport(UIKit)
textView.textStorage.replaceCharacters(in: selectedRange, with: insertingText)

Expand Down Expand Up @@ -200,6 +267,16 @@ extension FireflySyntaxView: TextViewDelegate {
lastCompleted = ""
insertingText += "\t\(newlineInsert)\n\(newlineInsert)*/"

#if os(iOS)
let textViewTextStorageString: String = textView.textStorage.string
#else
let textViewTextStorageString: String = textView.textStorage!.string
#endif
self.onTextChange(
oldText: String(fromRange: selectedRange, in: textViewTextStorageString),
location: selectedRange.location,
newText: insertingText
)
#if canImport(UIKit)
textView.textStorage.replaceCharacters(in: selectedRange, with: insertingText)

Expand Down Expand Up @@ -227,7 +304,17 @@ extension FireflySyntaxView: TextViewDelegate {
} else {
// insertingText.removeFirst()
insertingText += newlineInsert


#if os(iOS)
let textViewTextStorageString: String = textView.textStorage.string
#else
let textViewTextStorageString: String = textView.textStorage!.string
#endif
self.onTextChange(
oldText: String(fromRange: selectedRange, in: textViewTextStorageString),
location: selectedRange.location,
newText: insertingText
)
#if canImport(UIKit)
textView.textStorage.replaceCharacters(in: selectedRange, with: insertingText)

Expand Down Expand Up @@ -260,6 +347,16 @@ extension FireflySyntaxView: TextViewDelegate {
lastCompleted = ""
insertingText += "*/"

#if os(iOS)
let textViewTextStorageString: String = textView.textStorage.string
#else
let textViewTextStorageString: String = textView.textStorage!.string
#endif
self.onTextChange(
oldText: String(fromRange: selectedRange, in: textViewTextStorageString),
location: selectedRange.location,
newText: insertingText
)
#if canImport(UIKit)
textView.textStorage.replaceCharacters(in: selectedRange, with: insertingText)

Expand Down Expand Up @@ -288,6 +385,16 @@ extension FireflySyntaxView: TextViewDelegate {
}

self.textStorage.endEditing()
#if os(iOS)
let textViewTextStorageString: String = textView.textStorage.string
#else
let textViewTextStorageString: String = textView.textStorage!.string
#endif
self.onTextChange(
oldText: String(fromRange: range, in: textViewTextStorageString),
location: range.location,
newText: text
)
return true
}

Expand Down Expand Up @@ -341,6 +448,7 @@ extension FireflySyntaxView: TextViewDelegate {
public func textViewDidChangeSelection(_ textView: UITextView) {
updateCursorPosition()
delegate?.didChangeSelectedRange(self.textView, selectedRange: self.textView.selectedRange)
self.onSelectionChange(selectionRange: self.textView.selectedRange)
}

/// Override the key commands recognized by the view
Expand All @@ -365,6 +473,7 @@ extension FireflySyntaxView: TextViewDelegate {
public func textViewDidChangeSelection(_ notification: Notification) {
updateCursorPosition()
delegate?.didChangeSelectedRange(textView, selectedRange: textView.selectedRange())
self.onSelectionChange(selectionRange: textView.selectedRange())
}

/// Handles highlighting the correct amount of the view when text changes
Expand Down Expand Up @@ -428,6 +537,7 @@ extension FireflySyntaxView {
#elseif canImport(AppKit)
textView.setSelectedRange(range)
#endif
self.onSelectionChange(selectionRange: range)
}
}

Expand Down
Loading