jpsim / SourceKitten

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

[Question] Multi File Refactoring Tool #230

Open bolismauro opened 8 years ago

bolismauro commented 8 years ago

Hi everyone, First of all I want to apologize if I use this issue for a question and not for a real issue, probably SO would be a better place, but I haven't found any related or relevant question about SourceKit or SourceKitten so I decided to ask here, feel free to close this issue if this is not appropriate.

I'm trying to create a refactoring tool for our internal codebase and I started using SourceKitten and following this talk https://realm.io/news/appbuilders-jp-simard-sourcekit/ I also studied the source code of both SourceKitten and SwiftLint to understand what is the best approach.

Basically I'm using Request.Index to get information about the source code and in particular I'm using the USR to understand how refactor things. I managed to make it work for a single file, but now that I'm trying to make the refactor works across multiple files I'm blocked.

I basically don't find a way to find the same information (basically kind/usr/line/column) for multiple files.

So just to give an example. I have two files structs.swift

struct A { let prop = 0 }

struct B {
    let prop = 1

    func testFunction(multiplier: Int) -> Int {
        return prop * multiplier;
    }
}

main.swift

func test() {
    let b = B();
    b.testFunction(20)
}

I run this code

let mainFile = // path/to/main.swift
let requestResult = Request.Index(file: mainFile).send()
let entities = (requestResult["key.entities"] as! [SourceKitRepresentable]).map({ $0 as! [String: SourceKitRepresentable] })
print(toJSON(toAnyObject(requestResult)))

And this is what I get

{
  "key.dependencies" : [
    {
      "key.kind" : "source.lang.swift.import.module.swift",
      "key.name" : "Swift",
      "key.is_system" : true,
      "key.filepath" : "\/Applications\/Xcode.app\/Contents\/Developer\/Toolchains\/XcodeDefault.xctoolchain\/usr\/lib\/swift\/macosx\/x86_64\/Swift.swiftmodule",
      "key.hash" : "3J0WUGRJZL0YE"
    }
  ],
  "key.entities" : [
    {
      "key.kind" : "source.lang.swift.decl.function.free",
      "key.name" : "test()",
      "key.column" : 6,
      "key.usr" : "s:F4main4testFT_T_",
      "key.line" : 1
    }
  ],
  "key.hash" : "1A9N6V6C2X3KJ"
}

I basically lose the information about what is defined in struct.swift. If I merge everything in a single file and I run the same code I obtain what I need (I omit the result because it is very long). This of course happens also if I index struct.swift before and then main.swift.

I guess I need a sort of way to pass a module, a project or something to sourcekit. I tried to understand how the doc feature in SourceKitten works but I of course receive a different kind of information, and in particular I don't receive the information I need for the refactoring

Thanks for your help!

jpsim commented 8 years ago

High level, you'd need to send the compiler arguments to the Index request for SourceKit to index your files as a whole. In practice exactly what that means, what changes are necessary in SourceKittenFramework, how to invoke it, order of operations, you'd need to figure all that out.

bolismauro commented 8 years ago

Thanks for your answer, I found an (hacky?) way to make my example works. In need to further testing more complicated stuff.

let modulePath = "/path/to/project"
let moduleName = "projectName"
let module = Module(xcodeBuildArguments: [], name: moduleName, inPath: modulePath)

guard let module = module else { print("nope..!"); exit(0); }

let r = Request.Index(file: "/path/to/main.swift", arguments: module.compilerArguments).send()
print(toJSON(toAnyObject(r)))

What do you think?

jpsim commented 8 years ago

Yes @bolismauro, that's the way to do it. Though it should also be possible to perform the index without having to build the whole project with Xcode (which also indexes the files!). But you'd need to know the raw swiftc compiler arguments and pass those to Request.Index rather than rely on xcodebuild to parse them.

bolismauro commented 8 years ago

Alright, thanks! I will dig into the swift compiler, but I think this solution can be good enough for our use case since we don't have hard time constraints

Thanks again for your help!