Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion Sources/BuildServerIntegration/BuildServerManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1557,7 +1557,7 @@ package actor BuildServerManager: QueueBasedMessageHandler {
}
}

package func testFiles() async throws -> [DocumentURI] {
package func projectTestFiles() async throws -> [DocumentURI] {
return try await sourceFiles(includeNonBuildableFiles: false).compactMap { (uri, info) -> DocumentURI? in
guard info.isPartOfRootProject, info.mayContainTests else {
return nil
Expand All @@ -1566,6 +1566,24 @@ package actor BuildServerManager: QueueBasedMessageHandler {
}
}

/// Differs from `sourceFiles(in targets: Set<BuildTargetIdentifier>)` making sure it only includes source files that
/// are part of the root project for cases where we don't care about dependency source files
///
/// - Parameter include: If `nil` will include all targets, otherwise only return files who are part of at least one matching target
/// - Returns: List of filtered source files in root project
package func projectSourceFiles(in include: Set<BuildTargetIdentifier>? = nil) async throws -> [DocumentURI] {
return try await sourceFiles(includeNonBuildableFiles: false).compactMap { (uri, info) -> DocumentURI? in
var includeTarget = true
if let include {
includeTarget = info.targets.contains(anyIn: include)
}
guard info.isPartOfRootProject, includeTarget else {
return nil
}
return uri
}
}

private func watchedFilesReferencing(mainFiles: Set<DocumentURI>) -> Set<DocumentURI> {
return Set(
watchedFiles.compactMap { (watchedFile, mainFileAndLanguage) in
Expand Down
9 changes: 8 additions & 1 deletion Sources/ClangLanguageService/ClangLanguageService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,14 @@ extension ClangLanguageService {
return nil
}

package static func syntacticTestItems(in uri: DocumentURI) async -> [AnnotatedTestItem] {
package func syntacticTestItems(for snapshot: DocumentSnapshot) async -> [AnnotatedTestItem] {
return []
}

package func syntacticPlaygrounds(
for snapshot: DocumentSnapshot,
in workspace: Workspace
) async -> [TextDocumentPlayground] {
return []
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,14 @@ package actor DocumentationLanguageService: LanguageService, Sendable {
// The DocumentationLanguageService does not do anything with document events
}

package static func syntacticTestItems(in uri: DocumentURI) async -> [AnnotatedTestItem] {
package func syntacticTestItems(for snapshot: DocumentSnapshot) async -> [AnnotatedTestItem] {
return []
}

package func syntacticPlaygrounds(
for snapshot: DocumentSnapshot,
in workspace: Workspace
) async -> [TextDocumentPlayground] {
return []
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/SourceKitLSP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ add_library(SourceKitLSP STATIC
MacroExpansionReferenceDocumentURLData.swift
MessageHandlingDependencyTracker.swift
OnDiskDocumentManager.swift
PlaygroundDiscovery.swift
ReferenceDocumentURL.swift
Rename.swift
SemanticTokensLegend+SourceKitLSPLegend.swift
Expand All @@ -22,7 +23,6 @@ add_library(SourceKitLSP STATIC
SourceKitLSPCommandMetadata.swift
SourceKitLSPServer.swift
SymbolLocation+DocumentURI.swift
SyntacticTestIndex.swift
TestDiscovery.swift
TextEdit+IsNoop.swift
Workspace.swift
Expand Down
10 changes: 9 additions & 1 deletion Sources/SourceKitLSP/LanguageService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,15 @@ package protocol LanguageService: AnyObject, Sendable {
/// Does not write the results to the index.
///
/// The order of the returned tests is not defined. The results should be sorted before being returned to the editor.
static func syntacticTestItems(in uri: DocumentURI) async -> [AnnotatedTestItem]
func syntacticTestItems(for snapshot: DocumentSnapshot) async -> [AnnotatedTestItem]

/// Returns the syntactically scanned playgrounds declared within the workspace.
///
/// The order of the returned playgrounds is not defined. The results should be sorted before being returned to the editor.
func syntacticPlaygrounds(
for snapshot: DocumentSnapshot,
in workspace: Workspace
) async -> [TextDocumentPlayground]

/// A position that is canonical for all positions within a declaration. For example, if we have the following
/// declaration, then all `|` markers should return the same canonical position.
Expand Down
2 changes: 2 additions & 0 deletions Sources/SourceKitLSP/MessageHandlingDependencyTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ package enum MessageHandlingDependencyTracker: QueueBasedMessageHandlerDependenc
self = .freestanding
case is WorkspaceTestsRequest:
self = .workspaceRequest
case is WorkspacePlaygroundsRequest:
self = .workspaceRequest
case let request as any TextDocumentRequest:
self = .documentRequest(request.textDocument.uri)
default:
Expand Down
43 changes: 43 additions & 0 deletions Sources/SourceKitLSP/PlaygroundDiscovery.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import BuildServerIntegration
@_spi(SourceKitLSP) import LanguageServerProtocol
@_spi(SourceKitLSP) import SKLogging
import SemanticIndex
import SwiftExtensions
@_spi(SourceKitLSP) import ToolsProtocolsSwiftExtensions

extension SourceKitLSPServer {

/// Return all the playgrounds in the given workspace.
///
/// The returned list of playgrounds is not sorted. It should be sorted before being returned to the editor.
private func playgrounds(in workspace: Workspace) async -> [Playground] {
// If files have recently been added to the workspace (which is communicated by a `workspace/didChangeWatchedFiles`
// notification, wait these changes to be reflected in the build server so we can include the updated files in the
// playgrounds.
await workspace.buildServerManager.waitForUpToDateBuildGraph()

let playgroundsFromSyntacticIndex = await workspace.syntacticIndex.playgrounds()

// We don't need to sort the playgrounds here because they will get sorted by `workspacePlaygrounds` request handler
return playgroundsFromSyntacticIndex
}

func workspacePlaygrounds(_ req: WorkspacePlaygroundsRequest) async throws -> [Playground] {
return await self.workspaces
.concurrentMap { await self.playgrounds(in: $0) }
.flatMap { $0 }
.sorted { $0.location < $1.location }
}
}
9 changes: 8 additions & 1 deletion Sources/SourceKitLSP/SourceKitLSPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,8 @@ extension SourceKitLSPServer: QueueBasedMessageHandler {
await request.reply { try await workspaceSymbols(request.params) }
case let request as RequestAndReply<WorkspaceTestsRequest>:
await request.reply { try await workspaceTests(request.params) }
case let request as RequestAndReply<WorkspacePlaygroundsRequest>:
await request.reply { try await workspacePlaygrounds(request.params) }
// IMPORTANT: When adding a new entry to this switch, also add it to the `MessageHandlingDependencyTracker` initializer.
default:
await request.reply { throw ResponseError.methodNotFound(Request.method) }
Expand Down Expand Up @@ -1118,6 +1120,7 @@ extension SourceKitLSPServer {
TriggerReindexRequest.method: .dictionary(["version": .int(1)]),
GetReferenceDocumentRequest.method: .dictionary(["version": .int(1)]),
DidChangeActiveDocumentNotification.method: .dictionary(["version": .int(1)]),
WorkspacePlaygroundsRequest.method: .dictionary(["version": .int(1)]),
]
for (key, value) in languageServiceRegistry.languageServices.flatMap({ $0.type.experimentalCapabilities }) {
if let existingValue = experimentalCapabilities[key] {
Expand Down Expand Up @@ -1531,8 +1534,12 @@ extension SourceKitLSPServer {
// settings). Inform the build server about all file changes.
await workspaces.concurrentForEach { await $0.filesDidChange(notification.changes) }

await filesDidChange(notification.changes)
}

func filesDidChange(_ events: [FileEvent]) async {
for languageService in languageServices.values.flatMap(\.self) {
await languageService.filesDidChange(notification.changes)
await languageService.filesDidChange(events)
}
}

Expand Down
Loading