jpsim / SourceKitten

An adorable little framework and command line tool for interacting with SourceKit.
MIT License
2.3k stars 226 forks source link

Cannot read .swift from SwiftDocs #465

Open KarthikS941 opened 6 years ago

KarthikS941 commented 6 years ago

I am trying to extract document JSON structure from a swift file using SwiftDocs class. I have imported Sourcekitten framework and below is the code snippet of the same.

  import Cocoa
  import SourceKittenFramework

  class ViewController: NSViewController {
    var sourcekitten: SwiftDocs?
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        sourcekitten = SwiftDocs(file: File(path: "/Users/.../sample.swift")!, arguments: [])!
        print(sourcekitten!.docsDictionary) // Should print document json structure
    }
 }

Expecting it to print a JSON string similar to below one

{
  "key.substructure" : [
    {
      "key.kind" : "source.lang.swift.decl.struct",
      "key.offset" : 0,
      "key.nameoffset" : 7,
      "key.namelength" : 1,
      "key.bodyoffset" : 10,
      "key.bodylength" : 13,
      "key.length" : 24,
      "key.substructure" : [
        {
          "key.kind" : "source.lang.swift.decl.function.method.instance",
          "key.offset" : 11,
          "key.nameoffset" : 16,
          "key.namelength" : 3,
          "key.bodyoffset" : 21,
          "key.bodylength" : 0,
          "key.length" : 11,
          "key.substructure" : [

          ],
          "key.name" : "b()"
        }
      ],
      "key.name" : "A"
    }
  ],
  "key.offset" : 0,
  "key.diagnostic_stage" : "source.diagnostic.stage.swift.parse",
  "key.length" : 24
}

Instead, I am getting cannot read file error.

When I did a 'po' this is what I got

Could not read contents of `/Users/.../SwiftyP/SwiftyP/SwiftyP/sample.swift`
Fatal error: Unexpectedly found nil while unwrapping an Optional value
2017-12-28 15:27:27.960442-0600 SwiftyP[70384:15566179] Fatal error: Unexpectedly found nil while unwrapping an Optional value
(lldb) po File(path: "/Users/.../SwiftyP/SwiftyP/SwiftyP/sample.swift")!
ainopara commented 6 years ago
SwiftDocs(file: File(path: "/Users/.../sample.swift")!, arguments: [])!

arguments here should at least contains full path to the file ["/Users/.../sample.swift"].

KarthikSCP commented 6 years ago

@ainopara I still get 'Could not read contents of /Users/karthiksa/sample.swift.

/*

 Abstract:
 Application preview view.
 */

import UIKit
import AVFoundation

class PreviewView: UIView {
    var videoPreviewLayer: AVCaptureVideoPreviewLayer {
        guard let layer = layer as? AVCaptureVideoPreviewLayer else {
            fatalError("Expected `AVCaptureVideoPreviewLayer` type for layer. Check PreviewView.layerClass implementation.")
        }

        return layer
    }

    var session: AVCaptureSession? {
        get {
            return videoPreviewLayer.session
        }
        set {
            videoPreviewLayer.session = newValue
        }
    }

    // MARK: UIView

    override class var layerClass: AnyClass {
        return AVCaptureVideoPreviewLayer.self
    }
}

Thats the code in 'sample.swift'

KarthikS941 commented 6 years ago

Attaching Error Log in this.


`/Users/karthiksa/SwiftyPaper/SwiftyPaper/SwiftyPaper/ViewController.swift`

produced error: error: Couldn't lookup symbols:
__T021SourceKittenFramework9SwiftDocsVN
ainopara commented 6 years ago

@KarthikS941 It seems you need to embed SourceKittenFramework.framework in your application.

ainopara commented 6 years ago

@KarthikSCP Is there a file at this path /Users/karthiksa/sample.swift? If there is, could you please share output of this code?

do {
    try String(contentsOfFile: "/Users/karthiksa/sample.swift", encoding: .utf8)
} catch {
    print("Error: \(error)")
}
KarthikSCP commented 6 years ago

Here is the output, Error: Error Domain=NSCocoaErrorDomain Code=257 "The file “ViewController.swift” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/Users/karthiksa/SwiftyPaper/SwiftyPaper/SwiftyPaper/ViewController.swift, NSUnderlyingError=0x6040002419b0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

ainopara commented 6 years ago

According to the error message, your application failed to get access to the content of this file. This seems not a SourceKitten issue. I am not expert in macOS application developing, so I can not give any further guidance.