Commit Graph

335 Commits

Author SHA1 Message Date
somiljain2006 0fd40073f3 Use localReferences as fallback for non-indexed symbols 2026-06-05 07:43:02 +05:30
somiljain2006 b715835c3b Changed the index check back to .deletedFiles. 2026-06-02 22:00:40 +05:30
somiljain2006 39be8e4661 Improve reference lookup for local symbols and in-memory edits 2026-06-02 13:02:51 +05:30
somiljain2006 5b47421f8b Fix the filtering logic 2026-06-02 01:04:38 +05:30
somiljain2006 ee42692b08 Fix formatting 2026-05-30 22:03:04 +05:30
somiljain2006 477d33c89c Fix references for local variables and parameters 2026-05-30 21:55:01 +05:30
Rintaro Ishizaki a39c92022f Add forOpenDocument label to language service lookup methods
Rename languageServices(for:), primaryLanguageService(for:), and their
internal counterparts to use the `forOpenDocument` label, so the
precondition that the document must already be open is visible at call
sites.

Also make primaryLanguageService(forOpenDocument:) throw instead of
returning an optional, and switch several resolve-style handlers from
the find-or-create primaryLanguageService(for:_:) to
primaryLanguageService(forOpenDocument:), since those call sites already
have an open document in hand.
2026-05-20 10:31:44 -07:00
Rintaro Ishizaki f56a27b996 Fix build warnings 2026-05-18 18:04:44 -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
Rintaro Ishizaki 1d20e3f32e Merge pull request #2647 from rintaro/open-quickly-revise
Move Open Quickly and Jump to Definition docs to Contributor Documentation
2026-05-14 06:41:48 -07:00
Rintaro Ishizaki 292985e7dd Remove redundant fragment-clearing when constructing moduleFileURI 2026-05-13 10:25:09 -07:00
25harsh 90ceac197d Use ExpressibleByLiteral conformances for LSPAny 2026-05-13 16:19:42 +05:30
Rintaro Ishizaki 223a88cb8c Add workspace/symbolNames and workspace/symbolInfo LSP extensions (#2619)
- **`sourcekit/workspace/symbolNames`** — returns a flat, deduplicated
list of every symbol name in the workspace index (source and indexed
system modules). Clients use this to drive their search UI locally.

- **`sourcekit/workspace/symbolInfo`** — given a list of exact symbol
names, returns `WorkspaceSymbolItem` for each occurrence across all
workspaces, for display in the search result list. Source-file symbols
get `SymbolInformation` with a `file://` location. SDK/stdlib symbols
get a `WorkspaceSymbol` with `location: .uri(…)` The client must call
`workspaceSymbol/resolve` after the user selects an SDK/stdlib symbol to
obtain the concrete interface location.

- **`workspaceSymbol/resolve`** — resolves the deferred
`WorkspaceSymbol` location from `sourcekit/workspace/symbolInfo`. Parses
the `?module=` value into `moduleName`/`groupName`, finds a real source
file via `mainFiles(containing:)`, calls `openGeneratedInterface`, and
returns the symbol with `location` replaced by a full
`sourcekit-lsp://generated-swift-interface/` URI + range (or a temp
`file://` path for clients without `workspace/getReferenceDocument`
support).
2026-05-12 22:21:17 -07:00
Rintaro Ishizaki 83afc9d1e1 Move CopiedFileMap LSP adjustments to extensions on LSP types
`CopiedFileMap` previously held methods like
`workspaceEditAdjustedForCopiedFiles`,
`callHierarchyItemAdjustedForCopiedFiles`, etc. that encoded knowledge
of high-level LSP message types into `BuildServerIntegration`.

Replace them with `adjusted(for:)` methods on each LSP type
(`Location`, `[Location]`, `LocationsOrLocationLinksResponse`,
`WorkspaceEdit`, `CallHierarchyItem`, `TypeHierarchyItem`) in a new
`LSP+CopiedFileMap.swift` in the SourceKitLSP module. The primitive
URI remapping is extracted into `CopiedFileMap.adjustedURI(for:)`
which remains in `BuildServerIntegration`.

`CallHierarchyItem.adjusted(for:)` also drops manual `LSPAny`
dictionary construction in favour of `HierarchyItemData`.
2026-05-07 16:10:02 -07:00
Rintaro Ishizaki 070bc52b8c Use typed structs for LSP item data fields instead of manual LSPAny 2026-05-07 15:25:18 -07:00
Rintaro Ishizaki 3d7ebf7f20 Factor out CopiedFileMap as a Sendable value type
Extract the copied-file adjustment logic from BuildServerManager into a
Sendable value type CopiedFileMap. Call sites snapshot cachedCopiedFileMap
once per request (hoisted before loops) instead of awaiting the actor on
every iteration.
2026-04-28 12:12:40 -07:00
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
Alex Hoppen b4fafb64b3 Fix build warnings
Fix all build warnings except those caused by using old DocC request/response types, which are a little harder to resolve.
2026-04-12 16:11:11 +10:00
Alex Hoppen facfb28d39 Don’t implicitly cancel requests when an edit is detected
Instead, use `staleRequestSupport.retryOnContentModified` as an indicator which requests should return a `ContentModified` error when the document is modified. This fixes https://github.com/swiftlang/sourcekit-lsp/issues/2136 by no longer implicitly cancelling inlayHints in Helix while still implicitly cancelling semantic tokens request, which was the motivation for https://github.com/swiftlang/sourcekit-lsp/pull/1629.

Fixes #2136
2026-04-03 13:38:40 +02:00
Kavon Farvardin e45119ea18 Merge pull request #2416 from ahoppen/any-error
Allow `QueueBasedMessageHandler.handle` to return `any Error` instead of `ResponseError`
2026-04-02 08:55:26 -07:00
Rintaro Ishizaki f54f5c9ad0 Fix build errors and warnings in 6.2 toolchain 2026-03-30 14:57:07 -07:00
Rintaro Ishizaki 64aa354caf [TestDiscovery] "/refresh" style tests/playgrounds requests
When the client opts in to `workspace/tests/refresh` or
`workspace/playgrounds/refresh` via experimental client capabilities,
SourceKit-LSP now maintains a proactive cache of the current test and
playground lists and sends the corresponding `workspace/.../refresh`
notification whenever the cache changes. `workspaceTests()` /
`workspacePlaygrounds()` then serve subsequent fetch requests directly
from the cache.

Add `EntryPointManager`: runs background scans, stores the results,
fires callbacks on changes:
 - Start scanning when build targets are updated including initial
   updates, any watched files are changed, and index is updated.
 - Send '/refresh' server initiated requests when the cache has changed.
 - Coalesces rapid invalidations by cancelling any in-flight refresh task.

Also:
- Simplify `SourceKitIndexDelegate` from an `actor` with `AtomicInt32`
  to a plain `class`, since it is only called from IndexStoreDB's
  internal serial dispatch queue.
2026-03-27 09:32:59 -07:00
Alex Hoppen 9d7e7f3c30 Provide semantic functionality in copy destinations
If we have a source item `include/Test.h` that gets copied to `build/Test.h`, we currently don’t provide any semantic functionality for `build/Test.h`. Improve the build settings fallback logic to construct fallback build settings for `build/Test.h` based on `include/Test.h`.
2026-03-23 17:23:54 +01:00
Rintaro Ishizaki 894496e9be Merge pull request #2549 from Tabonx/feature/codeAction-resolve-support 2026-03-22 06:11:21 -07:00
Pavel Kroupa 8639c19347 Add codeAction/resolve support 2026-03-21 10:38:44 +01:00
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 ce86aae504 Merge pull request #2483 from ahoppen/explicitly-close-index 2026-03-01 08:18:19 +01:00
Steffeeen d748f80896 Implement support for textDocument/selectionRange
This enables hierarchical selection expansion in supported editors.

Selection ranges are computed from the AST by walking upward from the
smallest enclosing node to the root. This is implemented by AST nodes
conforming to the `SelectionRangeProvider` protocol. Most nodes use a
default implementation which returns the node's `trimmedRange`.
Other nodes are special-cased to adjust the returned selection ranges
based on other factors.
2026-02-26 09:15:01 +01: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
loveucifer 6028898c13 Optimize index usage for typeDefinition and inlay hint resolve requests. 2026-02-08 23:20:18 +05:30
loveucifer d73aba2c39 Remove duplicate definitionLocations and call shared function 2026-01-31 13:12:51 +05:30
loveucifer cccdde295c Use any LanguageService instead of closures 2026-01-30 02:47:52 +05:30
loveucifer 5ee63b7b0c Deduplicate helper functions between DefinitionLocations and SourceKitLSPServer 2026-01-19 17:29:23 +05:30
loveucifer 0e52fede00 unify definition locations 2026-01-18 16:28:55 +05:30
loveucifer b7e76ff90e unify typeDefinition 2026-01-17 16:17:59 +05:30
loveucifer d8b8d71fae Implement textDocument/typeDefinition request 2026-01-16 18:13:12 +05:30
Alex Hoppen 61ba0c92a4 Merge pull request #2436 from loveucifer/inlay-hint-go-to-definition 2026-01-16 13:16:45 +01:00
loveucifer 0695d3c67c Remove typeDefinition remnants 2026-01-12 06:02:46 +05:30
loveucifer 3630ab970c Address review feedback for inlay hint go-to-definition
- Use cursorInfo USR lookup instead of index (more accurate)
- Add document version tracking to reject stale resolve requests
- Make InlayHintResolveData conform to LSPAnyCodable
- Reference swiftlang/swift#86432 for mangled type workaround
- cursorInfoFromTypeUSR takes DocumentSnapshot for version safety
- Remove TypeDefinition.swift (defer to follow-up PR)
- Remove unnecessary comments
- Tests work without index
2026-01-10 07:00:43 +05:30
Alex Hoppen 2d8a44af76 Merge pull request #2422 from loveucifer/fix-language-service-shutdown-2211 2026-01-09 08:46:25 +01:00
loveucifer 27cf04e2ed Implement textDocument/typeDefinition request (#548)
Add support for the textDocument/typeDefinition LSP request, which
finds the type of the symbol at a given position and returns the
location of that type's definition.

This uses the same type definition lookup mechanism as the inlay hint
resolution feature, which queries cursorInfo for the new type
declaration location fields (typeDeclFilePath/Line/Column) with
fallback to index lookup using typeDeclUsr.

Fixes #548
2026-01-08 21:10:47 +05:30
loveucifer 27b536d612 format 2026-01-08 18:36:50 +05:30
loveucifer 2019ab711a mark SwiftLanguageService as immortal to skip shutdown 2026-01-08 05:01:42 +05:30
loveucifer 8e300ee443 add go-to-definition for inlay hints (#2318)
Implements resolveProvider for inlay hints to enable navigating to type
definitions. When an inlay hint showing a type is resolved, the server
looks up the type's definition location using cursorInfo and the index.

- store variable position in InlayHint.data for resolution
- add inlayHintResolve to LanguageService protocol
- implement resolve handler using cursorInfo and index lookup
- enable resolveProvider: true in capabilities
- add test for resolve functionality

Addresses #2318
2026-01-06 19:35:06 +05:30
loveucifer eeda468639 test case additon 2026-01-06 17:53:12 +05:30
lijunliang.9819 1b0fe79f93 remove not need label and format code 2026-01-06 15:57:02 +08:00
Kila2 7d92d17c44 Merge branch 'swiftlang:main' into feature/optimize_swift_definition_locations 2026-01-05 22:22:02 +08:00
loveucifer 173784e8b7 Closes #2211
Shut down language services when a workspace is closed

- Added allLanguageServices property to Workspace to get all services it references
- Added shutdownOrphanedLanguageServices to clean up services no longer in use
- When workspace folders are removed, we now shut down their associated language services
- This properly terminates clangd and other language server processes when workspaces close
2026-01-05 08:18:32 +05:30
Alex Hoppen e35c7a91c9 Merge pull request #2409 from loveucifer/fix-language-services-lifecycle
Fix language services lifecycle management in Workspace
2026-01-04 18:26:35 +01:00
Alex Hoppen a008e516e5 Allow QueueBasedMessageHandler.handle to return any Error instead of ResponseError
Update for the API changed in https://github.com/swiftlang/swift-tools-protocols/pull/33.
2026-01-04 08:44:11 +01:00