Skip to content
This repository has been archived by the owner on Dec 17, 2018. It is now read-only.

Commit

Permalink
Convert SourceKit snippet syntax to TextMate snippet syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
RLovelett committed Dec 11, 2016
1 parent dde8fad commit cc8f95e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
36 changes: 36 additions & 0 deletions Sources/LanguageServerProtocol/Functions/Convert.swift
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)}}"
})
}
4 changes: 2 additions & 2 deletions Sources/LanguageServerProtocol/Types/CompletionItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ extension CompletionItem : Decodable {
documentation = brief
sortText = nil
filterText = nil
insertText = nil
insertText = sourcetext
textEdit = nil
additionalTextEdits = nil
command = nil
Expand Down Expand Up @@ -177,7 +177,7 @@ extension CompletionItem : Decodable {
public static func decode(_ json: JSON) -> Decoded<CompletionItem> {
let description: Decoded<String> = json <| "key.description"
let kind: Decoded<CompletionItemKind?> = json <|? "key.kind"
let sourcetext: Decoded<String> = json <| "key.sourcetext"
let sourcetext: Decoded<String> = (json <| "key.sourcetext").flatMap(convert)
let type: Decoded<String?> = json <|? "key.typename"
let brief: Decoded<String?> = json <|? "key.doc.brief"
let context: Decoded<String> = json <| "key.context"
Expand Down
22 changes: 22 additions & 0 deletions Tests/LanguageServerProtocolTests/Functions/ConvertTests.swift
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]}})")
}

}

0 comments on commit cc8f95e

Please sign in to comment.