Skip to content

Commit f84cfec

Browse files
committed
Make CompilationDatabase use DocumentURI instead of URL
1 parent 80c214b commit f84cfec

File tree

3 files changed

+40
-40
lines changed

3 files changed

+40
-40
lines changed

Sources/SKCore/CompilationDatabase.swift

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import Foundation
1414
import LSPLogging
15+
import LanguageServerProtocol
1516
import SKSupport
1617

1718
import struct TSCBasic.AbsolutePath
@@ -47,15 +48,15 @@ public struct CompilationDatabaseCompileCommand: Equatable {
4748

4849
extension CompilationDatabase.Command {
4950

50-
/// The `URL` for this file. If `filename` is relative and `directory` is
51+
/// The `DocumentURI` for this file. If `filename` is relative and `directory` is
5152
/// absolute, returns the concatenation. However, if both paths are relative,
5253
/// it falls back to `filename`, which is more likely to be the identifier
5354
/// that a caller will be looking for.
54-
public var url: URL {
55+
public var uri: DocumentURI {
5556
if filename.hasPrefix("/") || !directory.hasPrefix("/") {
56-
return URL(fileURLWithPath: filename)
57+
return DocumentURI(filePath: filename, isDirectory: false)
5758
} else {
58-
return URL(fileURLWithPath: directory).appendingPathComponent(filename, isDirectory: false)
59+
return DocumentURI(URL(fileURLWithPath: directory).appendingPathComponent(filename, isDirectory: false))
5960
}
6061
}
6162
}
@@ -65,7 +66,7 @@ extension CompilationDatabase.Command {
6566
/// See https://clang.llvm.org/docs/JSONCompilationDatabase.html
6667
public protocol CompilationDatabase {
6768
typealias Command = CompilationDatabaseCompileCommand
68-
subscript(_ path: URL) -> [Command] { get }
69+
subscript(_ uri: DocumentURI) -> [Command] { get }
6970
var allCommands: AnySequence<Command> { get }
7071
}
7172

@@ -110,13 +111,13 @@ public func tryLoadCompilationDatabase(
110111
///
111112
/// See https://clang.llvm.org/docs/JSONCompilationDatabase.html under Alternatives
112113
public struct FixedCompilationDatabase: CompilationDatabase, Equatable {
113-
public var allCommands: AnySequence<Command> { AnySequence([]) }
114+
public var allCommands: AnySequence<CompilationDatabaseCompileCommand> { AnySequence([]) }
114115

115116
private let fixedArgs: [String]
116117
private let directory: String
117118

118-
public subscript(path: URL) -> [Command] {
119-
[Command(directory: directory, filename: path.path, commandLine: fixedArgs + [path.path])]
119+
public subscript(path: DocumentURI) -> [CompilationDatabaseCompileCommand] {
120+
[Command(directory: directory, filename: path.pseudoPath, commandLine: fixedArgs + [path.pseudoPath])]
120121
}
121122
}
122123

@@ -168,32 +169,38 @@ extension FixedCompilationDatabase {
168169
///
169170
/// See https://clang.llvm.org/docs/JSONCompilationDatabase.html
170171
public struct JSONCompilationDatabase: CompilationDatabase, Equatable {
171-
var pathToCommands: [URL: [Int]] = [:]
172-
var commands: [Command] = []
172+
var pathToCommands: [DocumentURI: [Int]] = [:]
173+
var commands: [CompilationDatabaseCompileCommand] = []
173174

174-
public init(_ commands: [Command] = []) {
175-
commands.forEach { try! add($0) }
175+
public init(_ commands: [CompilationDatabaseCompileCommand] = []) {
176+
for command in commands {
177+
add(command)
178+
}
176179
}
177180

178-
public subscript(_ url: URL) -> [Command] {
179-
if let indices = pathToCommands[url] {
181+
public subscript(_ uri: DocumentURI) -> [CompilationDatabaseCompileCommand] {
182+
if let indices = pathToCommands[uri] {
180183
return indices.map { commands[$0] }
181184
}
182-
if let indices = pathToCommands[url.resolvingSymlinksInPath()] {
185+
if let fileURL = uri.fileURL, let indices = pathToCommands[DocumentURI(fileURL.resolvingSymlinksInPath())] {
183186
return indices.map { commands[$0] }
184187
}
185188
return []
186189
}
187190

188-
public var allCommands: AnySequence<Command> { AnySequence(commands) }
191+
public var allCommands: AnySequence<CompilationDatabaseCompileCommand> { AnySequence(commands) }
189192

190-
public mutating func add(_ command: Command) throws {
191-
let url = command.url
192-
pathToCommands[url, default: []].append(commands.count)
193+
public mutating func add(_ command: CompilationDatabaseCompileCommand) {
194+
let uri = command.uri
195+
pathToCommands[uri, default: []].append(commands.count)
193196

194-
let canonical = URL(fileURLWithPath: try resolveSymlinks(AbsolutePath(validating: url.path)).pathString)
195-
if canonical != url {
196-
pathToCommands[canonical, default: []].append(commands.count)
197+
if let fileURL = uri.fileURL,
198+
let symlinksResolved = try? resolveSymlinks(AbsolutePath(validating: fileURL.path))
199+
{
200+
let canonical = DocumentURI(filePath: symlinksResolved.pathString, isDirectory: false)
201+
if canonical != uri {
202+
pathToCommands[canonical, default: []].append(commands.count)
203+
}
197204
}
198205

199206
commands.append(command)
@@ -204,7 +211,7 @@ extension JSONCompilationDatabase: Codable {
204211
public init(from decoder: Decoder) throws {
205212
var container = try decoder.unkeyedContainer()
206213
while !container.isAtEnd {
207-
try self.add(try container.decode(Command.self))
214+
self.add(try container.decode(Command.self))
208215
}
209216
}
210217

Sources/SKCore/CompilationDatabaseBuildSystem.swift

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,8 @@ extension CompilationDatabaseBuildSystem: BuildSystem {
106106
in buildTarget: ConfiguredTarget,
107107
language: Language
108108
) async -> FileBuildSettings? {
109-
guard let url = document.fileURL else {
110-
// We can't determine build settings for non-file URIs.
111-
return nil
112-
}
113-
guard let db = database(for: url),
114-
let cmd = db[url].first
109+
guard let db = database(for: document),
110+
let cmd = db[document].first
115111
else { return nil }
116112
return FileBuildSettings(
117113
compilerArguments: Array(cmd.commandLine.dropFirst()),
@@ -153,8 +149,8 @@ extension CompilationDatabaseBuildSystem: BuildSystem {
153149
self.watchedFiles.remove(uri)
154150
}
155151

156-
private func database(for url: URL) -> CompilationDatabase? {
157-
if let path = try? AbsolutePath(validating: url.path) {
152+
private func database(for uri: DocumentURI) -> CompilationDatabase? {
153+
if let url = uri.fileURL, let path = try? AbsolutePath(validating: url.path) {
158154
return database(for: path)
159155
}
160156
return compdb
@@ -212,10 +208,7 @@ extension CompilationDatabaseBuildSystem: BuildSystem {
212208
}
213209

214210
public func fileHandlingCapability(for uri: DocumentURI) -> FileHandlingCapability {
215-
guard let fileUrl = uri.fileURL else {
216-
return .unhandled
217-
}
218-
if database(for: fileUrl) != nil {
211+
if database(for: uri) != nil {
219212
return .handled
220213
} else {
221214
return .unhandled
@@ -227,7 +220,7 @@ extension CompilationDatabaseBuildSystem: BuildSystem {
227220
return []
228221
}
229222
return compdb.allCommands.map {
230-
SourceFileInfo(uri: DocumentURI($0.url), isPartOfRootProject: true, mayContainTests: true)
223+
SourceFileInfo(uri: $0.uri, isPartOfRootProject: true, mayContainTests: true)
231224
}
232225
}
233226

Tests/SKCoreTests/CompilationDatabaseTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ final class CompilationDatabaseTests: XCTestCase {
157157

158158
let db = JSONCompilationDatabase([cmd1, cmd2, cmd3])
159159

160-
XCTAssertEqual(db[URL(fileURLWithPath: "b")], [cmd1])
161-
XCTAssertEqual(db[URL(fileURLWithPath: "/c/b")], [cmd2])
162-
XCTAssertEqual(db[URL(fileURLWithPath: "/b")], [cmd3])
160+
XCTAssertEqual(db[DocumentURI(filePath: "b", isDirectory: false)], [cmd1])
161+
XCTAssertEqual(db[DocumentURI(filePath: "/c/b", isDirectory: false)], [cmd2])
162+
XCTAssertEqual(db[DocumentURI(filePath: "/b", isDirectory: false)], [cmd3])
163163
}
164164

165165
func testJSONCompilationDatabaseFromDirectory() throws {
@@ -255,7 +255,7 @@ final class CompilationDatabaseTests: XCTestCase {
255255
XCTAssertNotNil(db)
256256

257257
XCTAssertEqual(
258-
db![URL(fileURLWithPath: "/a/b")],
258+
db![DocumentURI(filePath: "/a/b", isDirectory: false)],
259259
[
260260
CompilationDatabase.Command(
261261
directory: try AbsolutePath(validating: "/a").pathString,

0 commit comments

Comments
 (0)