Skip to content

Commit

Permalink
Build an epic USD syntax highlighter.
Browse files Browse the repository at this point in the history
  • Loading branch information
furby-tm committed May 9, 2024
1 parent 53b7d46 commit 3b07166
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 78 deletions.
1 change: 0 additions & 1 deletion Bundler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ icon = "kraken.png"
[apps.Kraken.plist]
commit = '$(COMMIT_HASH)'
CFBundleURLTypes = [ { type = "dict", value = { CFBundleTypeRole = 'Viewer', CFBundleURLName = 'Kraken', CFBundleURLSchemes = [ 'kraken' ] } } ]

5 changes: 5 additions & 0 deletions Sources/Editors/Code/Cosmo/Enums/CaptureName.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ public enum CaptureName: String, CaseIterable, Sendable
case string
case type
case parameter
case namespace
case functionBuiltin = "function.builtin"
case typeBuiltin = "type.builtin"
case typeAlternate = "type_alternate"
case variableBuiltin = "variable.builtin"
case keywordReturn = "keyword.return"
case keywordFunction = "keyword.function"
case stringSpecial = "string.special"
case textUri = "text.uri"

/// Returns a specific capture name case from a given string.
/// - Parameter string: A string to get the capture name from
Expand Down
11 changes: 8 additions & 3 deletions Sources/Editors/Code/Cosmo/Theme/EditorTheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,18 @@ public extension Editor.Code
.keywordReturn, .keywordFunction, .repeat, .conditional, .tag:
keywords
case .comment: comments
case .variable, .property: variables
case .function, .method: variables
case .variable: variables
case .property: commands
case .function, .method: commands
case .number, .float: numbers
case .string: strings
case .type: types
case .parameter: variables
case .parameter: attributes
case .typeAlternate: attributes
case .namespace: numbers
case .typeBuiltin, .functionBuiltin: characters
case .textUri: strings
case .stringSpecial: attributes
default: text
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[ "." ";" ":" "," ] @punctuation.delimiter
[ "\\(" "(" ")" "[" "]" "{" "}"] @punctuation.bracket ; TODO: "\\(" ")" in interpolations should be @punctuation.special
[ "\\(" "(" ")" "[" "]" "{" "}" ] @punctuation.bracket ; TODO: "\\(" ")" in interpolations should be @punctuation.special

; Identifiers
(attribute) @variable
(type_identifier) @type
(self_expression) @variable.builtin
(user_type (type_identifier) @variable.builtin (#eq? @variable.builtin "Self"))
(self_expression) @variable
(user_type (type_identifier) @keyword (#eq? @keyword "Self"))

; Declarations
"func" @keyword.function
Expand All @@ -16,20 +16,21 @@
(property_modifier)
(parameter_modifier)
(inheritance_modifier)
(mutation_modifier)
] @keyword

(function_declaration (simple_identifier) @method)
(function_declaration (simple_identifier) @function)
(init_declaration ["init" @constructor])
(deinit_declaration ["deinit" @constructor])
(throws) @keyword
"async" @keyword
"await" @keyword
(where_keyword) @keyword
(parameter external_name: (simple_identifier) @parameter)
(parameter name: (simple_identifier) @parameter)
(type_parameter (type_identifier) @parameter)
(inheritance_constraint (identifier (simple_identifier) @parameter))
(equality_constraint (identifier (simple_identifier) @parameter))
(parameter external_name: (simple_identifier) @property)
(parameter name: (simple_identifier) @property)
(type_parameter (type_identifier) @property)
(inheritance_constraint (identifier (simple_identifier) @property))
(equality_constraint (identifier (simple_identifier) @property))
(pattern bound_identifier: (simple_identifier)) @variable

[
Expand All @@ -46,6 +47,7 @@
"convenience"
"required"
"mutating"
"nonmutating"
"associatedtype"
] @keyword

Expand All @@ -66,60 +68,87 @@
(modify_specifier)
] @keyword

(class_body (property_declaration (pattern (simple_identifier) @property)))
(protocol_property_declaration (pattern (simple_identifier) @property))
(class_body (property_declaration (pattern (simple_identifier) @variable.parameter)))
(protocol_property_declaration (pattern (simple_identifier) @variable.parameter))

(import_declaration ["import" @include])
(value_argument
name: (value_argument_label) @property)

(enum_entry ["case" @keyword])
(import_declaration
"import" @keyword)

(enum_entry
"case" @keyword)

; Function calls
(call_expression (simple_identifier) @function.call) ; foo()
(call_expression (simple_identifier) @function) ; foo()
(call_expression ; foo.bar.baz(): highlight the baz()
(navigation_expression
(navigation_suffix (simple_identifier) @function.call)))
(navigation_suffix (simple_identifier) @function)))
((navigation_expression
(simple_identifier) @type) ; SomeType.method(): highlight SomeType as a type
(#match? @type "^[A-Z]"))
(call_expression (simple_identifier) @keyword (#eq? @keyword "defer")) ; defer { ... }
(simple_identifier) @variable) ; SomeType.method(): highlight SomeType as a type
(#match? @variable "^[A-Z]"))
(call_expression (simple_identifier) @type) ; SomeType { ... }

(try_operator) @operator
(try_operator ["try" @keyword])

(directive) @function.macro
(diagnostic) @function.macro
(directive) @number
(diagnostic) @number

; Statements
(for_statement ["for" @repeat])
(for_statement ["in" @repeat])
(for_statement ["for" @keyword])
(for_statement ["in" @keyword])
(for_statement (pattern) @variable)
(else) @keyword
(as_operator) @keyword

["while" "repeat" "continue" "break"] @repeat
["while" "repeat" "continue" "break"] @keyword

["let" "var"] @keyword

(guard_statement ["guard" @conditional])
(if_statement ["if" @conditional])
(switch_statement ["switch" @conditional])
(switch_entry ["case" @keyword])
(switch_entry ["fallthrough" @keyword])
(switch_entry (default_keyword) @keyword)
(guard_statement
"guard" @keyword)

(if_statement
"if" @keyword)

(switch_statement
"switch" @keyword)

(switch_entry
"case" @keyword)

(switch_entry
"fallthrough" @keyword)

(switch_entry
(default_keyword) @keyword)

"return" @keyword.return

(ternary_expression
["?" ":"] @conditional)
["?" ":"] @keyword)

["do" (throw_keyword) (catch_keyword)] @keyword

(statement_label) @label

; Comments
[
(comment)
(multiline_comment)
(comment)
(multiline_comment)
] @comment @spell

((comment) @comment.documentation
(#lua-match? @comment.documentation "^///[^/]"))

((comment) @comment.documentation
(#lua-match? @comment.documentation "^///$"))

((multiline_comment) @comment.documentation
(#lua-match? @comment.documentation "^/[*][*][^*].*[*]/$"))

; String literals
(line_str_text) @string
(str_escaped_char) @string
Expand All @@ -130,57 +159,57 @@
["\"" "\"\"\""] @string

; Lambda literals
(lambda_literal ["in" @keyword.operator])
(lambda_literal ["in" @keyword])

; Basic literals
[
(integer_literal)
(hex_literal)
(oct_literal)
(bin_literal)
(integer_literal)
(hex_literal)
(oct_literal)
(bin_literal)
] @number
(real_literal) @float
(real_literal) @number
(boolean_literal) @boolean
"nil" @variable.builtin
"nil" @keyword

; Regex literals
(regex_literal) @string.regex
(regex_literal) @string

; Operators
(custom_operator) @operator
(custom_operator) @keyword

[
"!"
"?"
"+"
"-"
"*"
"/"
"%"
"="
"+="
"-="
"*="
"/="
"<"
">"
"<="
">="
"++"
"--"
"&"
"~"
"%="
"!="
"!=="
"=="
"==="
"??"

"->"

"..<"
"..."
] @operator
"!"
"?"
"+"
"-"
"*"
"/"
"%"
"="
"+="
"-="
"*="
"/="
"<"
">"
"<="
">="
"++"
"--"
"&"
"~"
"%="
"!="
"!=="
"=="
"==="
"??"
"->"
"..<"
"..."
(bang)
] @keyword

(value_parameter_pack ["each" @keyword])
(value_pack_expansion ["repeat" @keyword])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
"="
] @operator

[
"prepend"
] @type

(attribute_type) @type
(
;; Reference: https://openusd.org/release/api/sdf_page_front.html
Expand Down
2 changes: 1 addition & 1 deletion Sources/Kraken/UI/Editors/KUI.CodeEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public extension Kraken.UI
@State private var language: Editor.Code.Language = .default

/** default code editor theme setting for syntax highlighting. */
@State private var theme: Editor.Code.Theme = .standard
@State private var theme: Editor.Code.Theme = .gruvbox

/** default code editor font setting for text. */
@State private var font: NSFont = .monospacedSystemFont(ofSize: 12, weight: .bold)
Expand Down
22 changes: 22 additions & 0 deletions Sources/Kraken/UI/Themes/KUI.CodeTheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,26 @@ extension Editor.Code.Theme
comments: .init(hex: "73A74E")
)
}

static var gruvbox: Editor.Code.Theme
{
Editor.Code.Theme(
text: .init(hex: "DDC7A1"),
insertionPoint: .init(hex: "E78A4E"),
invisibles: .init(hex: "32302f"),
background: .init(hex: "282828"),
lineHighlight: .init(hex: "32302F"),
selection: .init(hex: "374141"),
keywords: .init(hex: "EA6962"),
commands: .init(hex: "A9B665"),
types: .init(hex: "D8A657"),
attributes: .init(hex: "89B482"),
variables: .init(hex: "7DAEA3"),
values: .init(hex: "89B482"),
numbers: .init(hex: "D3869B"),
strings: .init(hex: "A9B665"),
characters: .init(hex: "E78A4E"),
comments: .init(hex: "7C6F64")
)
}
}

0 comments on commit 3b07166

Please sign in to comment.