Skip to content

Expose finding ".swift-format" file#140

Merged
allevato merged 2 commits intoswiftlang:masterfrom
Trzyipolkostkicukru:finding-config
Feb 7, 2020
Merged

Expose finding ".swift-format" file#140
allevato merged 2 commits intoswiftlang:masterfrom
Trzyipolkostkicukru:finding-config

Conversation

@Trzyipolkostkicukru
Copy link
Copy Markdown
Contributor

Expose finding ".swift-format" file, and creating Configuration with that file, so that it could be used with sourcekit-lsp

I hope it's okay if there are no tests for this functionality in here, and instead it's tested indirectly by the sourcekit-lsp with an awesome Tibs that makes testing multi file projects easy

Copy link
Copy Markdown
Member

@allevato allevato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I was going to look into this in a couple days so it saves me some time. Some comments below.

self.version = highestSupportedConfigurationVersion
}

/// Constructs a Configuration using values from a JSON config file
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave the format of the file unspecified in the doc comment, so that we have the ability to change it in the future (while still retaining support for older formats):

/// Constructs a Configuration by loading it from a configuration file.

}

/// Constructs a Configuration using values from a JSON config file
public init(configFile url: URL) throws {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To match the naming convention of types like Data that load URLs, let's call this init(contentsOf url: URL) throws.

///
/// Looks for a ".swift-format" file in the same directory as the swift file, or its nearest parent.
/// If one is not found, returns "nil".
public static func configurationFile(forSwiftFile url: URL) -> URL? {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're generalizing this so that other people can call it, we don't have to require that the input URL point specifically to a Swift file. For example, we might want to allow someone to pass the URL of a directory to ask "what configuration file applies to files in this directory?"

With that in mind, we could name this func url(forConfigurationFileApplyingTo url: URL) -> URL?, and update the doc comment accordingly, such as

/// Returns the URL of the configuration file that applies to the given file or directory.

I think we can be vague about the exact search behavior in the doc comment because it will give us the flexibility to adjust it in the future as well.

But we'll also need to update the implementation below: before the loop, we'll need to check to see if the incoming URL is a file (rather than a directory), and if it is, delete the file component from the path. Then we'll also need to adjust the first line of the loop and the condition slightly to make sure that we still go up to the parent at the right time and exit correctly when we hit the root.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a placeholder component for files instead of adjusting the loop condition, because otherwise it would be hard to write it in a way that doesn't skip the root directory

Comment thread Sources/swift-format/main.swift Outdated
return testPath
}
} while path.path != "/"
if let swiftFileUrl = swiftFilePath.map(URL.init(fileURLWithPath:)),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use swiftFileURL and configFileURL throughout.

Update documentation to make it more future-proof
Make `url(forConfigurationFileApplyingTo:)` able to handle directories
Use better names
Copy link
Copy Markdown
Member

@allevato allevato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

I'm fine with no tests for this right now because the best way to test it would be to replace all of the existing FileManager usage with the FileSystem abstraction from swift-tools-support-core so that we can mock out the FS contents for tests, and that's a lot to try to do in this PR.

@allevato allevato merged commit 03c5dec into swiftlang:master Feb 7, 2020
aaditya-chandrasekhar pushed a commit to val-verde/swift-format that referenced this pull request May 20, 2021
* add import

* linux only fix

* update packages

* update to GraphViz 0.1.2

* update Package.resolved

* Add Changelog entry

Co-authored-by: Mattt <mattt@me.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants