Skip to content

Commit

Permalink
change swift syntax to 5.9.1
Browse files Browse the repository at this point in the history
add SourceConvert
  • Loading branch information
[email protected] committed Jan 17, 2024
1 parent 84cc8ba commit c5879fc
Show file tree
Hide file tree
Showing 10 changed files with 386 additions and 687 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "2c49d66d34dfd6f8130afdba889de77504b58ec0",
"version" : "508.0.1"
"revision" : "43c802fb7f96e090dde015344a94b5e85779eff1",
"version" : "509.1.0"
}
},
{
Expand Down
8 changes: 2 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ let package = Package(
// Dependencies declare other packages that this package depends on.
.package(
url: "https://github.com/apple/swift-syntax",
from: "508.0.0"
from: "509.1.0"
),

.package(url: "https://github.com/jpsim/SourceKitten", from: "0.34.1"),
Expand Down Expand Up @@ -82,12 +82,8 @@ let package = Package(
dependencies: [
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
// .product(name: "SwiftDiagnostics", package: "swift-syntax"),
// .product(name: "SwiftParserDiagnostics", package: "swift-syntax"),
// .product(name: "SwiftSyntaxParser", package: "swift-syntax"),
// .product(name: "IndexStoreDB", package: "IndexStoreDB"),
"Rainbow",
.product(name: "SourceKittenFramework", package: "SourceKitten"),
"Rainbow",
"Derived",
]
),
Expand Down
11 changes: 9 additions & 2 deletions Sources/SKClient/Reportable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import Foundation
import SwiftSyntax


public enum Reporter {
public typealias R = (CodeLocation, String?) -> Void

Expand All @@ -17,7 +16,7 @@ public enum Reporter {
case custom(R)

public func report(_ info: CodeLocation, reason: String? = nil) {
let defaultReason = info.syntax?.withoutTrivia().description ?? ""
let defaultReason = info.syntax?.withoutTrivia().description ?? ""
let newReason = reason ?? defaultReason
switch self {
case .vscode:
Expand All @@ -29,3 +28,11 @@ public enum Reporter {
}
}
}

extension SyntaxProtocol {
func withoutTrivia() -> Self {
return self
.with(\.leadingTrivia, .spaces(0))
.with(\.trailingTrivia, .spaces(0))
}
}
217 changes: 104 additions & 113 deletions Sources/SKClient/SKClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,121 +11,112 @@ import SwiftParser
import SourceKittenFramework

public struct SKClient {
/// file path or temp path.
public let path: String
/// snapshot code by ``path``.
public let code: String
public let arguments: [String]
public let sourceFile: SourceFileSyntax
public let converter: SourceLocationConverter

public init(path: String, sdk: SDK = .macosx) throws {
let arguments: [String] = sdk.pathArgs + [path]
try self.init(path: path, arguments: arguments)
}

public init(path: String, arguments: [String]) throws {
let code = try String(contentsOfFile: path, encoding: .utf8)
self.init(path: path, code: code, arguments: arguments)
}

private static let codePath = "code: /temp.swift"
public init(code: String, sdk: SDK = .macosx) {
let arguments: [String] = sdk.pathArgs
self.init(path: Self.codePath, code: code, arguments: arguments + [Self.codePath])
}

public init(code: String, arguments: [String]) {
self.init(path: Self.codePath, code: code, arguments: arguments + [Self.codePath])
}

public init(path: String, code: String, arguments: [String]) {
self.path = path
self.code = code
self.arguments = arguments
self.sourceFile = Parser.parse(source: code)
self.converter = SourceLocationConverter(file: path, tree: sourceFile)
}

// MARK: SK Response
@discardableResult
public func callAsFunction(_ offset: Int) throws -> SourceKitResponse {
return try self.cursorInfo(offset)
}

@discardableResult
public func callAsFunction<Syntax: SyntaxProtocol>(_ syntax: Syntax) throws -> SourceKitResponse {
return try self.cursorInfo(syntax.positionAfterSkippingLeadingTrivia.utf8Offset)
}

// MARK: code location
public func callAsFunction(offset syntax: SyntaxProtocol) -> SwiftSyntax.SourceLocation {
return self.converter.location(for: syntax.positionAfterSkippingLeadingTrivia)
}

public func callAsFunction(location syntax: SyntaxProtocol) -> CodeLocation {
return CodeLocation(path: path, location: self(offset: syntax), syntax: syntax)
}
/// file path or temp path.
public let path: String
/// snapshot code by ``path``.
public let code: String
public let arguments: [String]
public let sourceConvert: SourceConverter
public var sourceFile: SourceFileSyntax { sourceConvert.sourceFile }
public var converter: SourceLocationConverter { sourceConvert.converter }

public init(path: String, sdk: SDK = .macosx) throws {
let arguments: [String] = sdk.pathArgs + [path]
try self.init(path: path, arguments: arguments)
}

public init(path: String, arguments: [String]) throws {
let code = try String(contentsOfFile: path, encoding: .utf8)
self.init(path: path, code: code, arguments: arguments)
}

private static let codePath = "code: /temp.swift"
public init(code: String, sdk: SDK = .macosx) {
let arguments: [String] = sdk.pathArgs
self.init(path: Self.codePath, code: code, arguments: arguments + [Self.codePath])
}

public init(code: String, arguments: [String]) {
self.init(path: Self.codePath, code: code, arguments: arguments + [Self.codePath])
}

public init(path: String, code: String, arguments: [String]) {
self.path = path
self.code = code
self.arguments = arguments
self.sourceConvert = SourceConverter(path: path, code: code)
}

// MARK: SK Response
@discardableResult
public func callAsFunction(_ offset: Int) throws -> SourceKitResponse {
return try self.cursorInfo(offset)
}

@discardableResult
public func callAsFunction<Syntax: SyntaxProtocol>(_ syntax: Syntax) throws -> SourceKitResponse {
return try self.cursorInfo(syntax.positionAfterSkippingLeadingTrivia.utf8Offset)
}
}

// MARK: SourceKit Command
extension SKClient {
public func cursorInfo(_ offset: Int) throws -> SourceKitResponse {
let raw: [String : SourceKitRepresentable] = try Request.customRequest(request: [
"key.request": UID("source.request.cursorinfo"),
"key.name": path,
"key.sourcefile": path,
"key.sourcetext": code,
"key.offset": Int64(offset),
"key.compilerargs": arguments

]).send()
return SourceKitResponse(raw)
}
// @discardableResult
// public func cursorInfo(_ offset: Int) throws -> SourceKitResponse {
// let raw: [String : SourceKitRepresentable] = try Request.cursorInfo(file: path, offset: ByteCount(offset), arguments: arguments).send()
// return SourceKitResponse(raw)
// }
@discardableResult
public func index() throws -> SourceKitResponse {
let raw: [String : SourceKitRepresentable] = try Request.index(file: path, arguments: arguments).send()
return SourceKitResponse(raw)
}
@discardableResult
public func editorOpen() throws -> SourceKitResponse {
let raw: [String : SourceKitRepresentable] = try Request.customRequest(request: [
"key.request": UID("source.request.editor.open"),
"key.name": path,
"key.sourcetext": code,
"keys.compilerargs": arguments
]).send()
return SourceKitResponse(raw)
}
// public func editorOpen() throws -> SourceKitResponse{
// let raw: [String : SourceKitRepresentable] = try Request.editorOpen(file: File(path: path)!).send()
// return SourceKitResponse(raw)
// }

@discardableResult
public func editorClose() throws -> SourceKitResponse {
let raw: [String : SourceKitRepresentable] = try Request.customRequest(request: [
"key.request": UID("source.request.editor.close"),
"key.name": path,
// "key.sourcefile": path,
// "keys.compilerargs": arguments
]).send()
return SourceKitResponse(raw)
}
@discardableResult
public func docInfo() throws -> SourceKitResponse {
let text: String = try String(contentsOf: URL(fileURLWithPath: path))
let raw: [String : SourceKitRepresentable] = try Request.docInfo(text: text, arguments: arguments).send()
return SourceKitResponse(raw)
}
public func cursorInfo(_ offset: Int) throws -> SourceKitResponse {
let raw: [String : SourceKitRepresentable] = try Request.customRequest(request: [
"key.request": UID("source.request.cursorinfo"),
"key.name": path,
"key.sourcefile": path,
"key.sourcetext": code,
"key.offset": Int64(offset),
"key.compilerargs": arguments
]).send()
return SourceKitResponse(raw)
}

// @discardableResult
// public func cursorInfo(_ offset: Int) throws -> SourceKitResponse {
// let raw: [String : SourceKitRepresentable] = try Request.cursorInfo(file: path, offset: ByteCount(offset), arguments: arguments).send()
// return SourceKitResponse(raw)
// }

@discardableResult
public func index() throws -> SourceKitResponse {
let raw: [String : SourceKitRepresentable] = try Request.index(file: path, arguments: arguments).send()
return SourceKitResponse(raw)
}

@discardableResult
public func editorOpen() throws -> SourceKitResponse {
let raw: [String : SourceKitRepresentable] = try Request.customRequest(request: [
"key.request": UID("source.request.editor.open"),
"key.name": path,
"key.sourcetext": code,
"keys.compilerargs": arguments
]).send()
return SourceKitResponse(raw)
}

// public func editorOpen() throws -> SourceKitResponse{
// let raw: [String : SourceKitRepresentable] = try Request.editorOpen(file: File(path: path)!).send()
// return SourceKitResponse(raw)
// }
@discardableResult
public func editorClose() throws -> SourceKitResponse {
let raw: [String : SourceKitRepresentable] = try Request.customRequest(request: [
"key.request": UID("source.request.editor.close"),
"key.name": path,
// "key.sourcefile": path,
// "keys.compilerargs": arguments
]).send()
return SourceKitResponse(raw)
}

@discardableResult
public func docInfo() throws -> SourceKitResponse {
let text: String = try String(contentsOf: URL(fileURLWithPath: path))
let raw: [String : SourceKitRepresentable] = try Request.docInfo(text: text, arguments: arguments).send()
return SourceKitResponse(raw)
}
}
40 changes: 40 additions & 0 deletions Sources/SKClient/SourceConvert.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// File.swift
//
//
// Created by Tangram Yume on 2024/1/17.
//

import SwiftSyntax
import SwiftParser

public final class SourceConverter {
public let path: String
public let sourceFile: SourceFileSyntax
public let converter: SourceLocationConverter

public init(path: String, code: String) {
self.path = path
self.sourceFile = Parser.parse(source: code)
self.converter = SourceLocationConverter(fileName: path, tree: sourceFile)
}

public init(path: String, sourceFile: SourceFileSyntax) {
self.path = path
self.sourceFile = sourceFile
self.converter = SourceLocationConverter(fileName: path, tree: sourceFile)
}

public convenience init(path: String) throws {
let code = try String(contentsOfFile: path, encoding: .utf8)
self.init(path: path, code: code)
}

public func sourceLocation(_ syntax: SyntaxProtocol) -> SwiftSyntax.SourceLocation {
return self.converter.location(for: syntax.positionAfterSkippingLeadingTrivia)
}

public func codeLocation(_ syntax: SyntaxProtocol) -> CodeLocation {
return CodeLocation(path: path, location: sourceLocation(syntax), syntax: syntax)
}
}
2 changes: 1 addition & 1 deletion Sources/SKClient/SourceKitResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public struct SourceKitResponse {
guard let type = self.decl else { return nil }
guard type != "<<error type>>" else { return nil }

return SyntaxFactory.makeTypeIdentifier(type)
return TypeSyntax(IdentifierTypeSyntax(name: TokenSyntax.identifier(type)))
}

public var fully_annotated_decl: String? {
Expand Down
Loading

0 comments on commit c5879fc

Please sign in to comment.