1212
1313import Foundation
1414import LSPLogging
15+ import LanguageServerProtocol
1516import SKSupport
1617
1718import struct TSCBasic. AbsolutePath
@@ -47,15 +48,15 @@ public struct CompilationDatabaseCompileCommand: Equatable {
4748
4849extension 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
6667public 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
112113public 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
170171public 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
0 commit comments