-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2cff6df
commit 3b1599b
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import Foundation | ||
|
||
/// A combined name and range | ||
/// | ||
/// Useful for generalizing data from query matches. | ||
public struct NamedRange { | ||
public let name: String | ||
public let range: NSRange | ||
|
||
public init(name: String, range: NSRange) { | ||
self.name = name | ||
self.range = range | ||
} | ||
} | ||
|
||
public extension QueryMatch { | ||
/// Intrepret the match using the "injections.scm" definition | ||
/// | ||
/// - `injection.content` defines the range of the injection | ||
/// - a node with `injection.language` specifies the value of the language in the text | ||
/// - if that is not prsent, uses `injection.language` metadata | ||
/// | ||
/// If `textProvider` is nil and a node contents is needed, the injection is dropped. | ||
func injection(with textProvider: ResolvingQueryCursor.TextProvider?) -> NamedRange? { | ||
guard let range = captures(named: "injection.content").first?.range else { | ||
return nil | ||
} | ||
|
||
let languageCapture = captures(named: "injection.language").first | ||
|
||
let nodeLanguage: String? | ||
|
||
if let node = languageCapture?.node { | ||
nodeLanguage = textProvider?(node.range, node.pointRange) | ||
} else { | ||
nodeLanguage = nil | ||
} | ||
|
||
let setLanguage = metadata["injection.language"] | ||
|
||
guard let language = nodeLanguage ?? setLanguage else { | ||
return nil | ||
} | ||
|
||
return NamedRange(name: language, range: range) | ||
} | ||
} |