31 Commits

Author SHA1 Message Date
Rintaro Ishizaki 1b652de1f0 Use Mutex directly for class/actor stored properties
ThreadSafeBox is a `class` wrapping a `Mutex<Value>`. When stored as
a property of another class or actor, the outer reference type already
provides identity, so the extra ThreadSafeBox class indirection is just a
redundant heap allocation. Replace `ThreadSafeBox<T>` with `Mutex<T>` at
those sites.

Function-local ThreadSafeBox uses are unchanged since they need class
identity to be capturable in `@Sendable` closures.

CustomBuildServerTestProject.buildServerBox is also unchanged: it is
value-captured into a closure stored before `super.init`, which a
~Copyable Mutex can't support without a class wrapper.

Port SourceKitD's fileprivate `computeIfNil` extension from ThreadSafeBox
to Mutex (now `borrowing`).
2026-06-18 15:11:53 -07:00
Rintaro Ishizaki 99cac8f91d Update for the ThreadSafeBox.value setter removal 2026-05-27 14:36:56 -07:00
Rintaro Ishizaki 7c99f4f0e7 Use swift-tools-protocols's ThreadSafeBox
Delete sourcekit-lsp's `Sources/SwiftExtensions/ThreadSafeBox.swift`
and the now-unused `NSLock+WithLock.swift`.
Use the `Mutex`-backed `ThreadSafeBox` from swift-tools-protocols'

Adjust call sites for the new API: e.g.
`foo.value.mutate()` with `foo.withLock { $0.mutate() }` because it
doesn't provide `_modify` accessor.
2026-05-27 12:01:07 -07:00
Rintaro Ishizaki 626861d316 Merge pull request #2654 from rintaro/per-workspace-language-services
Scope language service lifetime to workspace
2026-05-18 14:59:47 -07:00
Rintaro Ishizaki f84abc1eb2 Use 'weak let' for immutable weak references (SE-0481)
Replace 'weak var' with 'weak let' for weak reference properties that are
set in init and never reassigned, using the Swift 6.3 feature introduced
by SE-0481.
2026-05-18 09:28:10 -07:00
Rintaro Ishizaki f2a121453d Scope language service instances per workspace
Previously, language services were held in a global registry on
SourceKitLSPServer and shared across workspaces, requiring complex
lifetime tracking (isImmortal, shutdownOrphanedLanguageServices) to
decide when to tear them down. In practice, every language service
already stored workspace-specific properties (buildServerManager,
semanticIndexManagerTask), so sharing them across workspaces was never
truly safe. Giving each Workspace its own service instances simplifies
lifetime management: services are created when needed and shut down
with their workspace.

Remove LanguageService.isImmortal, the workspace parameter from
canHandle(toolchain:), and the initialize/clientInitialized protocol
requirements.
2026-05-18 09:21:01 -07:00
25harsh 90ceac197d Use ExpressibleByLiteral conformances for LSPAny 2026-05-13 16:19:42 +05:30
Rintaro Ishizaki 4fa1b30d52 Add SymbolLocation.lspPosition, lspLocation, and make documentUri optional
Add computed properties to `SymbolLocation` that centralise the
index-to-LSP coordinate conversion:

- `uri: DocumentURI?` — returns nil when `path` is empty.
- `lspPosition: Position` — converts the 1-based line/utf8Column to a
  0-based LSP Position, using UTF-8 column as a UTF-16 approximation.
- `lspLocation: Location?` — wraps `documentUri` + `lspPosition` into
  an LSP Location, returning nil when `path` is empty.

Update all call sites.
2026-04-28 06:56:55 -07:00
Rintaro Ishizaki cf494e8506 Merge pull request #2572 from Bhushan-Aditya/fix-encoding-error-and-typo
Fix encoding error and typo
2026-03-23 17:13:26 -07:00
Bhushan-Aditya 6a3c97dec4 Fix encode throwing wrong error case and fix typo in error message
Made-with: Cursor
2026-03-23 12:32:37 +05:30
Bhushan-Aditya 61a94e96db Use SymbolLink.destination directly in MarkdownTitleFinder 2026-03-22 12:10:13 +05:30
Rintaro Ishizaki 1c99daabef [TestDiscovery] Prefer syntactic scan results over semantic index
Previously, test discovery used the semantic index as the primary
source and fell back to the syntactic index only for files where the
semantic index was out-of-date. This meant test locations came from the
semantic index, which only records a point position rather than the
full symbol range.

Flip the priority: use syntactic scan results as the primary source
(which have correct location ranges) and supplement with semantic index
results. The semantic results are range-fixed via
'textDocument/documentSymbol' before being returned.

This logic is unified into a single 'combineTests' helper shared by
both 'workspaceTests' and 'documentTests'.

Also fix 'SyntacticSwiftXCTestScanner' to emit extensions as proper
'AnnotatedTestItem' nodes (with 'isExtension: true') rather than a flat
list of methods, so extension test methods are correctly merged into
their class via 'mergingTestsInExtensions'. The class and extension
visitors are unified through a shared 'handleClassOrExtension' helper.
2026-03-04 16:58:21 -08:00
Alex Hoppen 425e1322a1 Explicitly close the index when shutting down SourceKit-LSP
`IndexStoreDB` moves its index to the `saved` directory when it is deallocated. Because `IndexStoreDB` is primarily owned by `UncheckedIndex`, we rely on deallocating this object to save the index store. This is fairly brittle because various parts of the codebase may hold transient references to that object as reported in https://github.com/swiftlang/sourcekit-lsp/issues/2455#issuecomment-3873561003.

Explicitly remove the reference from `UncheckedIndex` to `IndexStoreDB`. While this still isn’t perfect because other parts of the code base may hold references to `IndexStoreDB` but those should be a lot rarer, resulting in a more consistent closing of the index.
2026-02-15 18:02:53 +01:00
Adam Ward 308375a135 Move syntactic index back to Workspace 2025-12-08 15:16:36 -05:00
Adam Ward 9602433d2a Add new workspace/playgrounds request 2025-12-08 15:16:36 -05:00
Anthony Latsis 6b19657739 Enable ExistentialAny 2025-12-02 12:27:27 +00:00
Anthony Latsis b5bc65308c Fix build after https://github.com/swiftlang/swift-docc/pull/1331 2025-11-15 01:41:54 +00:00
Owen Voorhees f04b971726 Adopt swift-tools-protocols 2025-10-31 14:11:11 -07:00
Alex Hoppen a6c291b84e Do not block SourceKit-LSP functionality when a build server takes long to initialize
We previously waited for the initialization response from the build server during the creation of a `Workspace` so that we could create a `SemanticIndexManager` with the index store path etc. that was returned by the `build/initialize` response. This caused all functionality (including syntactic) of SourceKit-LSP to be blocked until the build server was initialized.

Change the computation of the `SemanticIndexManager` and related types to happen in the background so that we can provide functionality that doesn’t rely on the build server immediately.

Fixes #2304
2025-09-29 13:02:08 +01:00
Alex Hoppen 36ccc68784 Merge pull request #2275 from ahoppen/fix-warnings
Fix build warnings
2025-09-01 22:59:07 +02:00
Alex Hoppen 87a945d692 Fix build warnings 2025-09-01 17:26:44 +02:00
Alex Hoppen ef665080c9 Explicitly implement syntacticTestItems in DocumentationLanguageService
`SyntacticTestIndex` queries all language services for a document for its syntactic test items. This caused `syntacticDocumentTests not implemented in DocumentationLanguageService` to be logged for every Swift file in the project. Remove the default implementation of `syntacticDocumentTests` in `LanguageService` and instead provide an implementation that returns an empty array in `DocumentationLanguageService`.

While at it, also log the files for which unimplemented methods are called on `LanguageService`. This makes it easier to dermine why they are called.
2025-09-01 16:52:31 +02:00
Matthew Bastien 58096eb822 cache snapshots opened in sourcekitd by textDocument/doccDocumentation requests 2025-08-27 15:30:50 +02:00
Alex Hoppen 4040a5e4ac Add default implementations to LanguageService for methods that are not expected to be implemented by all language services
With the introduction of secondary language services, we don’t expect every language service to implement every request anymore. To simplify the addition of language services like `DocumentationLanguageService` add default implementations for methods that satisfy the following criteria:
 - `SourceKitLSPServer` does not expect side effects to happen when they are called
 - The method can throw or there is a reasonable default value
 - It is reasonable to expect that not all language services need to implement it
2025-08-26 16:10:36 +02:00
Alex Hoppen 49e1c3839c Merge DocumentationLanguageService and DocCDocumentation 2025-08-25 07:07:55 +02:00
Alex Hoppen 64fe439f24 Make DocumentationLanguageService the only module that imports DocCDocumentation 2025-08-21 10:10:54 +02:00
Alex Hoppen 1ca9780cd7 Merge pull request #2248 from ahoppen/lang-service-init-not-failable
Make `LanguageService` initializer non-failable
2025-08-20 06:51:27 +02:00
Alex Hoppen 08faa197af Make LanguageService initializer non-failable 2025-08-20 00:21:45 +02:00
Alex Hoppen 22fae21153 Handle documentation requests for Swift files in DocumentationLanguageService
This cleanly separates the responsibilities for handling documentation from those of handling Swift files. It also simplifies providing docc support for clang because we just need to implement the two `symbolGraph` methods in `ClangLanguageService` and can re-use the remaining infrastructure from `DoccLanguageService`.
2025-08-20 00:20:27 +02:00
Alex Hoppen 0e75e951d3 Allow multiple language services to handle a single document
This allows us to implement all of `doccDocumentation` in `DocumentationLanguageService`. `DocumentationLanguageService` will be a secondary language service for Swift files and can also provide the docc documentation support that’s currently in `SwiftLangaugeService`.
2025-08-18 07:52:19 +02:00
Alex Hoppen 83cc023b25 Move DocumentationLanguageService to a separate module 2025-08-15 11:48:27 +02:00