[Question] Multi File Refactoring Tool
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 2.4k
- Forks
- 237
- PR merge metrics
- No merged PRs in 30d
Description
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!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with Request.Index in SourceKitten and compare its results for structs.swift and main.swift, including the SourceKitten and SwiftLint approaches mentioned. Determine whether multi-file or module-level indexing can expose the needed kind, USR, line, and column information for cross-file refactoring, and document the supported path or limitation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100