Skip to content

Commit 2666b8a

Browse files
committed
Allow querying the build system for the language of a document
The build system has potentially more information about a document's language than we do based on the file’s extension.
1 parent c2f6a50 commit 2666b8a

File tree

7 files changed

+45
-2
lines changed

7 files changed

+45
-2
lines changed

Sources/SKCore/BuildServerBuildSystem.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,10 @@ extension BuildServerBuildSystem: BuildSystem {
267267
return buildSettings[document]
268268
}
269269

270+
public func defaultLanguage(for document: DocumentURI) async -> Language? {
271+
return nil
272+
}
273+
270274
public func registerForChangeNotifications(for uri: DocumentURI) {
271275
let request = RegisterForChanges(uri: uri, action: .register)
272276
_ = self.buildServer?.send(request) { result in

Sources/SKCore/BuildSystem.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ public protocol BuildSystem: AnyObject, Sendable {
8787
/// file or if it hasn't computed build settings for the file yet.
8888
func buildSettings(for document: DocumentURI, language: Language) async throws -> FileBuildSettings?
8989

90+
/// If the build system has knowledge about the language that this document should be compiled in, return it.
91+
///
92+
/// This is used to determine the language in which a source file should be background indexed.
93+
///
94+
/// If `nil` is returned, the language based on the file's extension.
95+
func defaultLanguage(for document: DocumentURI) async -> Language?
96+
9097
/// Register the given file for build-system level change notifications, such
9198
/// as command line flag changes, dependency changes, etc.
9299
///
@@ -113,5 +120,4 @@ public protocol BuildSystem: AnyObject, Sendable {
113120
func addSourceFilesDidChangeCallback(_ callback: @Sendable @escaping () async -> Void) async
114121
}
115122

116-
public let buildTargetsNotSupported =
117-
ResponseError.methodNotFound(BuildTargets.method)
123+
public let buildTargetsNotSupported = ResponseError.methodNotFound(BuildTargets.method)

Sources/SKCore/BuildSystemManager.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,22 @@ extension BuildSystemManager {
106106
self.mainFilesProvider = mainFilesProvider
107107
}
108108

109+
/// Returns the language that a document should be interpreted in for background tasks where the editor doesn't
110+
/// specify the document's language.
111+
public func defaultLanguage(for document: DocumentURI) async -> Language? {
112+
if let defaultLanguage = await buildSystem?.defaultLanguage(for: document) {
113+
return defaultLanguage
114+
}
115+
switch document.fileURL?.pathExtension {
116+
case "c": return .c
117+
case "cpp", "cc", "cxx": return .cpp
118+
case "m": return .objective_c
119+
case "mm", "h": return .objective_cpp
120+
case "swift": return .swift
121+
default: return nil
122+
}
123+
}
124+
109125
private func buildSettings(
110126
for document: DocumentURI,
111127
language: Language

Sources/SKCore/CompilationDatabaseBuildSystem.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ extension CompilationDatabaseBuildSystem: BuildSystem {
114114
)
115115
}
116116

117+
public func defaultLanguage(for document: DocumentURI) async -> Language? {
118+
return nil
119+
}
120+
117121
public func registerForChangeNotifications(for uri: DocumentURI) async {
118122
self.watchedFiles.insert(uri)
119123
}

Sources/SKSwiftPMWorkspace/SwiftPMBuildSystem.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
341341
return nil
342342
}
343343

344+
public func defaultLanguage(for document: DocumentURI) async -> Language? {
345+
// TODO (indexing): Query The SwiftPM build system for the document's language
346+
return nil
347+
}
348+
344349
public func registerForChangeNotifications(for uri: DocumentURI) async {
345350
self.watchedFiles.insert(uri)
346351
}

Tests/SKCoreTests/BuildSystemManagerTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,10 @@ class ManualBuildSystem: BuildSystem {
449449
return map[uri]
450450
}
451451

452+
public func defaultLanguage(for document: DocumentURI) async -> Language? {
453+
return nil
454+
}
455+
452456
func registerForChangeNotifications(for uri: DocumentURI) async {
453457
}
454458

Tests/SourceKitLSPTests/BuildSystemTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ final class TestBuildSystem: BuildSystem {
4343
return buildSettingsByFile[document]
4444
}
4545

46+
public func defaultLanguage(for document: DocumentURI) async -> Language? {
47+
return nil
48+
}
49+
4650
func registerForChangeNotifications(for uri: DocumentURI) async {
4751
watchedFiles.insert(uri)
4852
}

0 commit comments

Comments
 (0)