12
12
13
13
import Foundation
14
14
import LSPLogging
15
+ import LanguageServerProtocol
15
16
import SKSupport
16
17
17
18
import struct TSCBasic. AbsolutePath
@@ -47,15 +48,15 @@ public struct CompilationDatabaseCompileCommand: Equatable {
47
48
48
49
extension CompilationDatabase . Command {
49
50
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
51
52
/// absolute, returns the concatenation. However, if both paths are relative,
52
53
/// it falls back to `filename`, which is more likely to be the identifier
53
54
/// that a caller will be looking for.
54
- public var url : URL {
55
+ public var uri : DocumentURI {
55
56
if filename. hasPrefix ( " / " ) || !directory. hasPrefix ( " / " ) {
56
- return URL ( fileURLWithPath : filename)
57
+ return DocumentURI ( filePath : filename, isDirectory : false )
57
58
} else {
58
- return URL ( fileURLWithPath: directory) . appendingPathComponent ( filename, isDirectory: false )
59
+ return DocumentURI ( URL ( fileURLWithPath: directory) . appendingPathComponent ( filename, isDirectory: false ) )
59
60
}
60
61
}
61
62
}
@@ -65,7 +66,7 @@ extension CompilationDatabase.Command {
65
66
/// See https://clang.llvm.org/docs/JSONCompilationDatabase.html
66
67
public protocol CompilationDatabase {
67
68
typealias Command = CompilationDatabaseCompileCommand
68
- subscript( _ path : URL ) -> [ Command ] { get }
69
+ subscript( _ uri : DocumentURI ) -> [ Command ] { get }
69
70
var allCommands : AnySequence < Command > { get }
70
71
}
71
72
@@ -110,13 +111,13 @@ public func tryLoadCompilationDatabase(
110
111
///
111
112
/// See https://clang.llvm.org/docs/JSONCompilationDatabase.html under Alternatives
112
113
public struct FixedCompilationDatabase : CompilationDatabase , Equatable {
113
- public var allCommands : AnySequence < Command > { AnySequence ( [ ] ) }
114
+ public var allCommands : AnySequence < CompilationDatabaseCompileCommand > { AnySequence ( [ ] ) }
114
115
115
116
private let fixedArgs : [ String ]
116
117
private let directory : String
117
118
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 ] ) ]
120
121
}
121
122
}
122
123
@@ -168,32 +169,38 @@ extension FixedCompilationDatabase {
168
169
///
169
170
/// See https://clang.llvm.org/docs/JSONCompilationDatabase.html
170
171
public struct JSONCompilationDatabase : CompilationDatabase , Equatable {
171
- var pathToCommands : [ URL : [ Int ] ] = [ : ]
172
- var commands : [ Command ] = [ ]
172
+ var pathToCommands : [ DocumentURI : [ Int ] ] = [ : ]
173
+ var commands : [ CompilationDatabaseCompileCommand ] = [ ]
173
174
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
+ }
176
179
}
177
180
178
- public subscript( _ url : URL ) -> [ Command ] {
179
- if let indices = pathToCommands [ url ] {
181
+ public subscript( _ uri : DocumentURI ) -> [ CompilationDatabaseCompileCommand ] {
182
+ if let indices = pathToCommands [ uri ] {
180
183
return indices. map { commands [ $0] }
181
184
}
182
- if let indices = pathToCommands [ url . resolvingSymlinksInPath ( ) ] {
185
+ if let fileURL = uri . fileURL , let indices = pathToCommands [ DocumentURI ( fileURL . resolvingSymlinksInPath ( ) ) ] {
183
186
return indices. map { commands [ $0] }
184
187
}
185
188
return [ ]
186
189
}
187
190
188
- public var allCommands : AnySequence < Command > { AnySequence ( commands) }
191
+ public var allCommands : AnySequence < CompilationDatabaseCompileCommand > { AnySequence ( commands) }
189
192
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)
193
196
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
+ }
197
204
}
198
205
199
206
commands. append ( command)
@@ -204,7 +211,7 @@ extension JSONCompilationDatabase: Codable {
204
211
public init ( from decoder: Decoder ) throws {
205
212
var container = try decoder. unkeyedContainer ( )
206
213
while !container. isAtEnd {
207
- try self . add ( try container. decode ( Command . self) )
214
+ self . add ( try container. decode ( Command . self) )
208
215
}
209
216
}
210
217
0 commit comments