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
23 changes: 20 additions & 3 deletions Sources/Core/SourceFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public struct SourceFile {
try self.init(contentsOf: URL(fileURLWithPath: filePath))
}

/// Creates an instance representing the in-memory edited file at `filePath`.
/// `withContent` is the content of the in-memory representation of the file,
/// which may be different from what is actually stored on disk.
public init(at filePath: URL, withContent content: String) {
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
/// Creates an instance representing the in-memory edited file at `filePath`.
/// `withContent` is the content of the in-memory representation of the file,
/// which may be different from what is actually stored on disk.
public init(at filePath: URL, withContent content: String) {
/// Creates an instance representing the in-memory `contents` of the file at `filePath`.
///
/// `contents` is the text of the file currently in memory, which may be different from what
/// is actually stored on disk when the file is being edited.
public init(at filePath: URL, withInMemoryContents contents: String) {

Note: contents with a "s" is consistent with how other parameters are named.

let storage = Storage(filePath, replaceExisting: 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 +352,26 @@ 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()`.
/// Storage instances are cached based on url, where `replaceExisting` defines if an
/// existing entry should be replaced with new content, otherwise the storage get the
/// content from the previously cached entry.
koliyo marked this conversation as resolved.
Show resolved Hide resolved
fileprivate convenience init(
_ url: URL, lineStarts: [Index]? = nil, makeText: () throws -> Substring
_ url: URL, lineStarts: [Index]? = nil, replaceExisting: 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 !replaceExisting && 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
9 changes: 9 additions & 0 deletions Tests/HyloTests/SourceFileTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Core
import XCTest
import Foundation

final class SourceFileTests: XCTestCase {

Expand Down Expand Up @@ -33,4 +34,12 @@ final class SourceFileTests: XCTestCase {
XCTAssertEqual(source.line(containing: source.text.endIndex).number, 3)
}

func testUpdateSourceFileContent() throws {
let url = URL(fileURLWithPath: "foo.hylo")
let s1 = SourceFile(at: url, withContent: "import A")
XCTAssertEqual(s1.text, "import A")
let s2 = SourceFile(at: url, withContent: "import B")
XCTAssertEqual(s2.text, "import B")
}

}
Loading