This repository has been archived by the owner on Dec 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert SourceKit snippet syntax to TextMate snippet syntax
- Loading branch information
Showing
3 changed files
with
60 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// Convert.swift | ||
// langserver-swift | ||
// | ||
// Created by Ryan Lovelett on 12/10/16. | ||
// | ||
// | ||
|
||
import Argo | ||
import Foundation | ||
import Regex | ||
|
||
fileprivate let pattern = try! Regex(pattern: "<#T##([^#]+)#(?:#[^#]+#)?>", groupNames: "type") | ||
|
||
/// Convert a SourceKit (read: Xcode) style snippet to a | ||
/// [TextMate snippet syntax](https://manual.macromates.com/en/snippets). | ||
/// | ||
/// Example: | ||
/// ======== | ||
/// | ||
/// ``` | ||
/// let str = "fatalError(<#T##message: String##String#>)" | ||
/// convert(str).value! // "fatalError({{1:message: String}})" | ||
/// ``` | ||
/// | ||
/// - Parameter sourceKit: A `String` possibly containing an SourceKit style snippet. | ||
/// - Returns: A `String` where any SourceKit style snippets have been converted to TextMate | ||
/// snippets. | ||
func convert(_ sourceKit: String) -> Decoded<String> { | ||
var cursorIndex = 0 | ||
return pure(pattern.replaceAll(in: sourceKit) { match in | ||
guard let group = match.group(at: 1) else { return nil } | ||
cursorIndex += 1 | ||
return "{{\(cursorIndex):\(group)}}" | ||
}) | ||
} |
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
22 changes: 22 additions & 0 deletions
22
Tests/LanguageServerProtocolTests/Functions/ConvertTests.swift
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,22 @@ | ||
// | ||
// ConvertTests.swift | ||
// langserver-swift | ||
// | ||
// Created by Ryan Lovelett on 12/10/16. | ||
// | ||
// | ||
|
||
@testable import LanguageServerProtocol | ||
import XCTest | ||
|
||
class ConvertTests: XCTestCase { | ||
|
||
func testExample() { | ||
XCTAssertEqual(convert("fatalError(<#T##message: String##String#>)").value, "fatalError({{1:message: String}})") | ||
XCTAssertEqual(convert("x: <#T##Int#>, y: <#T##String#>)").value, "x: {{1:Int}}, y: {{2:String}})") | ||
XCTAssertEqual(convert("debugPrint(<#T##items: Any...##Any#>, to: &<#T##Target#>)").value, "debugPrint({{1:items: Any...}}, to: &{{2:Target}})") | ||
XCTAssertEqual(convert("isKnownUniquelyReferenced(&<#T##object: T?##T?#>)").value, "isKnownUniquelyReferenced(&{{1:object: T?}})") | ||
XCTAssertEqual(convert("getVaList(<#T##args: [CVarArg]##[CVarArg]#>)").value, "getVaList({{1:args: [CVarArg]}})") | ||
} | ||
|
||
} |