Skip to content

Commit c5de051

Browse files
committed
Refactor TargetSourcesHandler logic
1 parent b44b8c5 commit c5de051

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright (c) 2025 Spotify AB.
2+
//
3+
// Licensed to the Apache Software Foundation (ASF) under one
4+
// or more contributor license agreements. See the NOTICE file
5+
// distributed with this work for additional information
6+
// regarding copyright ownership. The ASF licenses this file
7+
// to you under the Apache License, Version 2.0 (the
8+
// "License"); you may not use this file except in compliance
9+
// with the License. You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing,
14+
// software distributed under the License is distributed on an
15+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
// KIND, either express or implied. See the License for the
17+
// specific language governing permissions and limitations
18+
// under the License.
19+
20+
import BuildServerProtocol
21+
import Foundation
22+
import LanguageServerProtocol
23+
24+
/// Handles the `buildTarget/sources` request.
25+
///
26+
/// Returns the sources for the provided target based on previously gathered information.
27+
final class TargetSourcesHandler {
28+
29+
private let initializedConfig: InitializedServerConfig
30+
private let targetStore: BazelTargetStore
31+
32+
init(
33+
initializedConfig: InitializedServerConfig,
34+
targetStore: BazelTargetStore,
35+
) {
36+
self.initializedConfig = initializedConfig
37+
self.targetStore = targetStore
38+
}
39+
40+
func buildTargetSources(
41+
_ request: BuildTargetSourcesRequest,
42+
_ id: RequestID
43+
) throws -> BuildTargetSourcesResponse {
44+
let targets = request.targets
45+
logger.info("Fetching sources for \(targets.count, privacy: .public) targets")
46+
47+
var srcs: [SourcesItem] = []
48+
for target in targets {
49+
let targetSrcs = try targetStore.bazelTargetSrcs(forBSPURI: target.uri)
50+
let sources = convertToSourceItems(targetSrcs)
51+
srcs.append(
52+
SourcesItem(
53+
target: target,
54+
sources: sources
55+
)
56+
)
57+
}
58+
59+
let count = srcs.reduce(0) { $0 + $1.sources.count }
60+
61+
logger.info(
62+
"Returning \(srcs.count, privacy: .public) source specs (\(count, privacy: .public) total source entries)"
63+
)
64+
65+
return BuildTargetSourcesResponse(items: srcs)
66+
}
67+
68+
func convertToSourceItems(_ targetSrcs: [URI]) -> [SourceItem] {
69+
var result: [SourceItem] = []
70+
for src in targetSrcs {
71+
let kind: SourceKitSourceItemKind
72+
if src.stringValue.hasSuffix("h") {
73+
kind = .header
74+
} else {
75+
kind = .source
76+
}
77+
let language: Language
78+
if src.stringValue.hasSuffix("swift") {
79+
language = .swift
80+
} else {
81+
language = .objective_c
82+
}
83+
result.append(
84+
SourceItem(
85+
uri: src,
86+
kind: .file,
87+
generated: false, // FIXME: Need to handle this properly
88+
dataKind: .sourceKit,
89+
data: SourceKitSourceItemData(
90+
language: language,
91+
kind: kind,
92+
outputPath: nil // FIXME: Related to the same flag on initialize?
93+
).encodeToLSPAny()
94+
)
95+
)
96+
}
97+
return result
98+
}
99+
}

0 commit comments

Comments
 (0)