Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Source file from in-memory string for incremental LSP analysis #1163

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
17 changes: 14 additions & 3 deletions Sources/Core/SourceFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public struct SourceFile {
try self.init(contentsOf: URL(fileURLWithPath: filePath))
}

public init(filePath: URL, withContent content: String) {
let storage = Storage(filePath, replace: true) { content[...] }
self.storage = storage
}

/// Creates an instance for the `text` given by a multiline string literal in the given
/// `swiftFile`, the literal's textual content (the line after the opening quotes) being
/// startLine.
Expand Down Expand Up @@ -344,17 +349,23 @@ extension SourceFile {
}

/// The owner of all instances of `Storage`.
private static var allInstances = SharedMutable<[URL: Storage]>([:])
private static let allInstances = SharedMutable<[URL: Storage]>([:])
dabrahams marked this conversation as resolved.
Show resolved Hide resolved

/// Creates an alias to the instance with the given `url` if it exists, or creates a new
/// instance having the given `url` and the text resulting from `makeText()`.
fileprivate convenience init(
_ url: URL, lineStarts: [Index]? = nil, makeText: () throws -> Substring
_ url: URL, lineStarts: [Index]? = nil, replace: Bool = false, makeText: () throws -> Substring
) rethrows {
self.init(
aliasing: try Self.allInstances.modify { (c: inout [URL: Storage]) -> Storage in
try modify(&c[url]) { v in
let r = try v ?? Storage(url: url, lineStarts: lineStarts, text: makeText())
let r: Storage
if !replace && v != nil {
r = v!
}
else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
}
else {
} else {

I think swift-format won't like the newline anyway.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sf allows discretionary newlines. As you know this style often creates harmful vertical density. Why push for it?

Copy link
Contributor

Choose a reason for hiding this comment

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

swift-format will rewrite this. I just checked again.

Copy link
Collaborator

Choose a reason for hiding this comment

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

With "respectsExistingLineBreaks" : true? I am surprised; that seems like a bug.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Uh, yeah; this it's not the tool I thought it was.

r = try Storage(url: url, lineStarts: lineStarts, text: makeText())
}
v = r
return r
}
Expand Down
Loading