Commit Graph

343 Commits

Author SHA1 Message Date
Alex Hoppen
d8a8e3c312 Merge remote-tracking branch 'swiftlang/main' into release/6.1 2024-12-17 16:32:09 +01:00
Alex Hoppen
03d58e8415 Normalize Windows drive letter to be uppercase
VS Code spells file paths with a lowercase drive letter, while the rest of Windows APIs use an uppercase drive letter. Normalize the drive letter spelling to be uppercase.

Fixes #1855
rdar://141001203
2024-12-10 16:16:27 -08:00
Alex Hoppen
c3f97b9080 Revert "Merge pull request #1854 from matthewbastien/documentation-language-service"
This reverts commit 9bbb8f3287, reversing
changes made to 21dfaf0f9c.
2024-12-10 10:33:28 -08:00
Matthew Bastien
9f3de1b21e mark the tutorial language as a LSP extension 2024-12-05 14:24:31 -05:00
Matthew Bastien
8fd30908e7 handle *.md and *.tutorial files for Swift DocC 2024-12-05 11:18:49 -05:00
Alex Hoppen
c85dd7c5e4 Merge pull request #1843 from ahoppen/empty-path-crash
Don’t crash when opening a file with an empty path
2024-12-03 14:39:55 -08:00
Alex Hoppen
14b8588221 Don’t crash when opening a file with an empty path
Fixes #1739
rdar://137886470
2024-11-22 15:59:07 +01:00
Alex Hoppen
e073a01bbc Reply with null to shutdown request
The LSP spec says the result of `shutdown` is `null`, not an empty object.

Fixes #1733
rdar://137886488
2024-11-22 15:52:27 +01:00
Alex Hoppen
5c7d495ebd Merge pull request #1808 from ahoppen/member-import-visibility
Adopt `MemberImportVisibility`
2024-11-07 15:38:05 -08:00
Alex Hoppen
06f58db5c8 Use build/taskStart, build/taskProgress and build/taskFinish to communicate progress from a BSP server to the client
Instead of defining BSP extensions for `window/workDoneProgress/create` and `$/progress`, we should be able to use the standard `build/taskStart`, `build/taskProgress` and `build/taskFinish` messages to the same effect, as suggested by https://forums.swift.org/t/extending-functionality-of-build-server-protocol-with-sourcekit-lsp/74400/9.

Fixes #1783
rdar://138653131
2024-11-06 09:39:09 -08:00
Alex Hoppen
1d7c27bc4b Adopt MemberImportVisibility 2024-11-05 21:04:01 -08:00
Paul LeMarquand
d8b0577465 Fix inadvertent shadowing of ClientCapabilities subtypes
When removing the LanguageServerProtocol namespace from types in
ClientCapabilities (#1734), some types inadvertently started shadowing
themselves. This lead to client initialization errors when the
initialization request couldn't be decoded:

```
type mismatch at params.capabilities.textDocument.completion.completionItemKind.valueSet.Index 0 : Expected to decode Dictionary<String, Any> but found number instead.
```

Disambiguating these inner types in ClientCapabilities fixes decoding.
2024-10-08 12:24:24 -04:00
Alex Hoppen
aa7389c60f Merge pull request #1735 from ahoppen/frozen-messagekind
Mark `MessageDecodingError.MessageKind` as `@frozen`
2024-10-04 15:55:51 -07:00
Alex Hoppen
06f6f8f17b Remove references to the LanguageServerProtocol modules in that module itself 2024-10-02 14:16:33 -07:00
Alex Hoppen
2e61d33645 Mark MessageDecodingError.MessageKind as @frozen 2024-10-02 09:34:49 -07:00
Alex Hoppen
8cd831b55d Adopt InternalImportsByDefault 2024-09-27 09:17:13 -07:00
Alex Hoppen
5d1eb73a68 Revert "Add an extra percent encoding layer when encoding DocumentURIs to LSP requests"
This reverts commit b2c17c748d.

The workaround isn’t needed anymore because we have a proper fix in the VS Code extension: https://github.com/swiftlang/vscode-swift/pull/1026
2024-09-10 14:51:28 -07:00
Alex Hoppen
b4d04ce983 Introduce a BuiltInBuildSystemAdapter that can be used to transition BuildSystem to a type that implements BSP 2024-09-09 16:31:00 -07:00
Alex Hoppen
b2c17c748d Add an extra percent encoding layer when encoding DocumentURIs to LSP requests
The URI standard RFC 3986 is ambiguous about whether percent encoding and their represented characters are considered equivalent. VS Code considers them equivalent and treats them the same:

```js
vscode.Uri.parse("x://a?b=xxxx%3Dyyyy").toString() -> 'x://a?b%3Dxxxx%3Dyyyy'
vscode.Uri.parse("x://a?b=xxxx%3Dyyyy").toString(/*skipEncoding=*/true) -> 'x://a?b=xxxx=yyyy'
```

This causes issues because SourceKit-LSP's macro expansion URLs encoded by URLComponents use `=` do denote the separation of a key and a value in the outer query. The value of the `parent` key may itself contain query items, which use the escaped form '%3D'. Simplified, such a URL may look like `scheme://host?parent=scheme://host?line%3D2`.

But after running this through VS Code's URI type `=` and `%3D` get canonicalized and are indistinguishable.

To avoid this ambiguity, always percent escape the characters we use to distinguish URL query parameters, producing the following URL: `scheme://host?parent%3Dscheme://host%3Fline%253D2`.
2024-08-19 21:41:50 -07:00
Alex Hoppen
0bb605d641 Remove a few workarounds that should no longer be needed 2024-08-07 10:00:04 -07:00
Alex Hoppen
50a28bb86a Change FIXME and TODO comments to always have an associated issue
Do one of the following for every `FIXME` or `TODO` comment
- Add an issue that tracks the task
- Remove the comment if we are not planning to address it
2024-08-07 10:00:04 -07:00
Lokesh T R
0522e1aff6 Allow macro expansions to be viewed through GetReferenceDocumentRequest instead of storing in temporary files 2024-07-31 19:03:12 +05:30
Paul LeMarquand
960317b6cc Enumerate supported code lens commands 2024-07-21 07:40:17 -04:00
Paul LeMarquand
3e8f413b6b Let client supply code lenses it can run
As part of its initialization options the client can pass a
textDocument/codeLens object that lists the supported commands the
client can handle.

It is in the form of a dictionary where the key is the lens name
recognized by SourceKit-LSP and the value is the command as recognized
by the client.

```
initializationOptions: {
    "textDocument/codeLens": {
        supportedCommands: {
            "swift.run": "clientCommandName_Run",
            "swift.debug": "clientCommandName_Debug",
        }
    }
}
```
2024-07-21 07:37:29 -04:00
Paul LeMarquand
293f638186 Add Run/Debug CodeLens Support
Adds a response to the textDocument/codeLens request that returns two
code lenses on the `@main` attribute of an application.

The LSP documentation breaks out the code lens requests into a
[`Code Lens Request`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_codeLens)
and a
[`Code Lens Resolve Request`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#codeLens_resolve),
stating this is for performance reasons. However, there is no intensive
work we need to do in order to resolve the commands for a CodeLens;
we know them based on context at the time of discovery. For this reason
we return resolved lenses with Commands for code lens requests.

A missing piece is only returning code lenses if the file resides in an
executable product. To my knoledge Libraries and Plugins can't have an
`@main` entrypoint and so it doesn't make sense to provide these code
lenses in those contexts.

Some guidance is required on how to best determine if the textDocument
in the request is within an executable product.

`testCodeLensRequestWithInvalidProduct` asserts that no lenses are
returned with the `@main` attribute is on a file in a `.executable`, and
is currently failing until this is addressed.
2024-07-21 07:37:00 -04:00
Paul LeMarquand
b288767675 Add TriggerReindexRequest to buildinRequests
`TriggerReindexRequest` was missing from the list of builtinRequests,
which caused the LSP to respond with a methodNotFound error when it
recieved a `workspace/triggerReindex` request.
2024-07-10 11:57:06 -04:00
Lokesh T R
0221475b70 Implement PeekDocumentsRequest and update ShowDocumentRequest.
-------------------------------------------------------------------------------

This implements an LSP Extension `PeekDocumentsRequest` to let `ExpandMacroCommand` to open the macro expansions in a "peeked" editor window.
For this to work, the client has to pass "workspace/peekDocuments" enabled to `ClientCapabilities.experimental` and the client should handle the `PeekDocumentsRequest` and show the expansions in a "peeked" editor window.

PR to support the above capability in the "Swift for VS Code" Extension: https://github.com/swiftlang/vscode-swift/pull/945
The "Swift for VS Code" extension cannot send the client capability, so it instead passes the same through `initializationOptions` in the `InitializeRequest`.

For editors which doesn't support this capability, `sourcekit-lsp` sends a `ShowDocumentRequest`.
The `ShowDocumentRequest` is updated to show all the macro expansions in a single generated file. Moreover, its folder structure is updated to use hex string of MD5 hash of concatenation of buffer names of expansions.

Fixes https://github.com/swiftlang/vscode-swift/issues/564
Fixes https://github.com/swiftlang/sourcekit-lsp/issues/1498 ( rdar://130207754 )
2024-07-03 15:32:40 +05:30
Alex Hoppen
ef2dc17806 Merge pull request #1543 from ahoppen/time-out-sourcekitd
Add a maximum duration for sourcekitd requests
2024-07-03 01:48:37 +02:00
Alex Hoppen
191d366a2c Add a maximum duration for sourcekitd requests
VS Code does not cancel semantic tokens requests. If a source file gets into a state where an AST build takes very long, this can cause us to wait for the semantic tokens from sourcekitd for a few minutes, effectively blocking all other semantic functionality in that file.

To circumvent this problem (or any other problem where an editor might not be cancelling requests they are no longer interested in) add a maximum request duration for SourceKitD requests, defaulting to 2 minutes.

rdar://130948453
2024-07-02 22:47:27 +02:00
Kai Lau
eb6f919b5e removed OpenGeneratedInterfaceRequest
deleted OpenInterfaceRequest.swift and moved GeneratedInterfaceDetails to LanguageService.swift
replaced usages by passing its members as parameters directly, refactored usage of TextDocumentIdentifier to DocumentURI
removed from Messages.builtinRequests
removed from SwiftInterfaceTests
removed from Documentation/`LSP Extensions.md`
removed from Sources/LanguageServerProtocol/CMakeLists.txt
2024-07-01 15:14:44 -07:00
Alex Hoppen
d1cddb8c41 Allow configuring of SourceKit-LSP’s options using .sourcekit-lsp configuration files
The idea here is to unify the different ways in which we can currently set options on SourceKit-LSP in a scalable way: Environment variables, command line arguments to `sourcekit-lsp` and initialization options.

The idea is that a user can define a `~/.sourcekit-lsp/.sourcekit-lsp` file (we store logs in `~/.sourcekit-lsp/logs` on non-Darwin platforms), which will be used as the default configuration for all SourceKit-LSP instances. They can also place a `.sourcekit-lsp` file in the root of a workspace to configure SourceKit-LSP for that project specifically, eg. setting arguments that need to be passed to `swift build` for that project and which thus also need to be set on SourceKit-LSP.

For compatibility reasons, I’m mapping the existing command line options into the new options structure for now. I hope to delete the command line arguments in the future and solely rely on `.sourcekit-lsp` configuration files.

Environment variable will be migrated to `.sourcekit-lsp` in a follow-up commit.
2024-06-27 17:36:16 +02:00
Alex Hoppen
12142c024c Remove configuration options from WorkspaceFolder
This extension was added for VS Code but never used. Let’s remove it in favor of workspace-specific configuration files.
2024-06-27 07:01:24 +02:00
Alex Hoppen
80a35aabe8 Translate the semantic token legend used by clangd to the semantic token legend used by SourceKit-LSP
clangd uses a completely different semantic token legend than SourceKit-LSP (it doesn’t even adhere to the ordering of the pre-defined token types) but we were passing index offsets from clangd through assuming that clangd uses the same legend, which was incorrect.

When retrieving semantic tokens from clangd, translate the semantic tokens from clangd’s legend to SourceKit-LSP’s legend.

rdar://129895062
2024-06-26 09:30:16 +02:00
Alex Hoppen
e23e300f7c Add a request to re-index all files in SourceKit-LSP
Users should not need to rely on this request. The index should always be updated automatically in the background. Having to invoke this request manes there is a bug in SourceKit-LSP's automatic re-indexing. It does, however, offer a workaround to re-index files when such a bug occurs where otherwise there would be no workaround.

rdar://127476221
Resolves #1263
2024-06-26 06:33:32 +02:00
Lokesh T R
fd50560998 Add LSP support for @freestanding Macro Expansions.
------
Simplify `SemanticRefactoring` with new `Refactoring` protocol to handle sourcekitd requests

Create and implement `ExpandMacroCommand` while temporarily storing generated expansions.

Create test case `testFreestandingMacroExpansion`

Manually inject `ExpandMacroCommand` into `retrieveRefactorCodeActions` upon an "Inline Macro" from sourcekitd

Address Review Comments

Mark `@_spi(Testing) public` for `MacroExpansionEdit`

Address Review Comments

Create separate directory for each buffer with its name, containing a generated file named as the source file along with position range

Fixed generated macro expansion file extension not recognised, by switching to file names which don't contain fragments

Address Review Comments

Wrap the entire feature under `ExperimentalFeatures`

Address Review Comments

Make Swift Lint Pass

Fix Windows Build not passing
2024-06-20 18:14:45 +00:00
Lokesh T R
e5547e9bbf Create ShowDocumentRequest and ShowDocumentResponse 2024-06-20 10:52:02 +00:00
Alex Hoppen
2a0f8c79b9 A couple of improvements for generated interfaces
- Rename methods to highlight that we’re talking about generated interfaces here, not `.swiftinterface` files
- Don’t open the generated interface in `documentManager`. Opening documents in `documentManager` should only be done by the `textDocument/didOpen` notification from the LSP client. Otherwise we might indefinitely keep the document in the document manager
- After getting the generated interface from sourcekitd, close the document in sourcekitd again. We don’t provide semantic functionality in the generated interface yet, so we can’t interact with the generated interface path. Before, we left it open in sourcekitd indefinitely.
- A couple of code simplifications.

Fixes #878
rdar://116705653
2024-06-08 07:56:19 -07:00
Alex Hoppen
d04bf84893 Don’t cancel in-progress diagnostic generation when calling DiagnosticReportManager.removeItemsFromCache
This was causing a non-deterministic test failure: When target preparation finishes while a diagnostic request is in progress, it will re-open the document, which calls `DiagnosticReportManager.removeItemsFromCache` for that document’s URI. With the old implementation, we would thus cancel the diagnostics sourcekitd request and return a cancelled error to the diagnostics LSP request.

While doing this, I also realized that there was a race condition: Document re-opening would happen outside of the SourceKit-LSP message handling queue and could thus run concurrently to any other request. This means that a sourcekitd request could run after `reopenDocument` had closed the document but before it was opened again. Introduce an internal reopen request that can be handled on the main message handling queue and thus doesn’t have this problem
2024-06-03 17:51:23 -07:00
Alex Hoppen
f203b3a527 Merge pull request #1395 from ahoppen/uri-everywhere
Use `DocumentURI` instead of `URL` in more locations
2024-06-03 14:05:00 -07:00
Alex Hoppen
c44ee45c67 Add an extension to the LogMessageNotification to add a message to a specific log in the client
If the editor has support for this LSP extension, it can create a separate output view for the index log.

For example, in VS Code, we could have one output view for the standard LSP logging (which are the messages between VS Code and SourceKit-LSP if verbose logging is enabled in VS Code) and a separate log that shows information about background indexing.

rdar://128572032
2024-06-01 17:14:05 -07:00
Alex Hoppen
80c214b008 Change CheckedIndex to use DocumentURI instead of URL 2024-06-01 12:54:07 -07:00
Alex Hoppen
195d3af74e Move LocalConnection to LSPTestSupport
We weren’t logging requests sent to a `TestSourceKitLSPClient` because we were assuming that `JSONRPCConnection` logs those requests in `SourceKitLSPServer`. But no logging happens in `LocalConnection`, which `TestSourceKitLSPClient` uses.

Since `LangaugeServerProtcol` can’t depend on `LSPLogging`, move the type to `LSPTestsSupport`.
2024-05-17 11:59:40 -07:00
Alex Hoppen
7e7df04b48 Make the SourceKitLSP module build in Swift 6 mode
Swift 6 mode didn’t find any notable data races. But it’s good to know Swift 6 will prevent future ones.
2024-05-13 21:28:42 -07:00
Alex Hoppen
0685ee7773 Enhance syntactic test discovery with information from the semantic index
When the semantic index is out-of-date, we currently purely rely on the syntactic index to discover tests and completely ignore data from the semantic index. This may lead to confusing behavior. For example if you have

```
class MightInheritFromXCTestCaseOrNot {}

class MyClass: MightInheritFromXCTestCaseOrNot {
  func testStuff() {}
}
```

Then we don’t return any tests when the semantic index is up-to-date. But once the file is modified (either on disk or in-memory), we purely rely on the syntactic index, which reports `testStuff` as a test method. After a build / background indexing finishes, the test method disappears again.

We can mitigate this problem as follows: If we have stale semantic index data for the test file, for every test method found by the syntactic index, check if we have an entry for this method in the semantic index. If we do, but that entry is not marked as a test class/method, we know that the semantic index knows about this method but decided that it’s not a test method for some reason. So we should ignore it.

rdar://126492948
2024-05-02 21:10:46 -07:00
Doug Gregor
0830a95a72 Address code review and add "Add product to export this target" action 2024-04-24 21:25:55 -07:00
Doug Gregor
d11230cc24 Fix JSON encoding for TextDocumentEdit and VersionedTextDocumentIdentifier 2024-04-24 21:25:53 -07:00
Alex Hoppen
771d2a7c23 Merge pull request #1185 from ahoppen/ahoppen/jump-to-system-interface
Fix jump-to-definition to methods in swift interfaces that are in synthesized extensions
2024-04-22 22:59:51 -07:00
Alex Hoppen
440dc62e58 Fix jump-to-definition to methods in swift interfaces that are in synthesized extensions
For example when trying to go-to-definition to `filter` on `Array`, we get a USR `s:s14_ArrayProtocolPsE6filterySay7ElementQzGSbAEKXEKF::SYNTHESIZED::s:Sa`. We were trying to look it up in the index, which failed because synthesized extension methods are not indexed.

Instead, consult the `module` and `groupName` that `sourcekitd` returns in the cursor info request to decide which module to jump to.

rdar://126240558
2024-04-18 10:30:18 -07:00
Alex Hoppen
1770204e5d Implement test discovery for swift-testing tests for the textDocument/tests request
This allows us to return swift-testing tests within a single document. It does not look for swift-testing tests workspace-wide (the `workspace/tests` request), which will be a follow-up PR.
2024-04-15 21:50:16 -07:00
Alex Hoppen
67a73f47c1 Return document tests in a hierarchical format
Same as for workspace tests, instead of returning a flat list of symbols, return a hierarchical structure.
2024-04-05 20:18:46 -07:00