Commit Graph

357 Commits

Author SHA1 Message Date
Alex Hoppen
00aebd6659 Don’t cancel build when closing a document
In SourceKit-LSP, we can get into the following situation:
1. We open A.swift
2. We issue a request for A.swift, the request takes a while to execute
3. The dependencies of A.swift are updated, which causes us to reopen the document in sourcekitd, so that the AST is rebuilt
4. This shouldn’t cause the request from (2) to be cancelled. We should continue executing it and only re-open the document after the request from (2) has finished

rdar://127475366
2024-05-23 13:59:39 -07:00
Alex Hoppen
c5e29b19fa Don’t load the syntax map etc when re-opening a file 2024-05-23 13:54:49 -07:00
Doug Gregor
c2af2f5337 Merge pull request #1240 from DougGregor/code-action-add-target
Add code actions for adding library/executable/macro targets to a package manifest
2024-05-17 15:24:11 -07:00
Kim de Vos
b30f59502e Add folding operator for IfConfigClauseSyntax 2024-05-17 13:32:05 +02:00
Alex Hoppen
7baffde1db Change compiler’s diagnostic provider name to SourceKit
The fact that they are coming from a service named `sourcekitd` should be an implentation detail of SourceKit-LSP and shouldn’t be exposed to users. Use the generic `SourceKit` term, which is vague about which SourceKit the diagnostics are coming from.
2024-05-14 10:02:35 -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
Kim de Vos
ed61630875 Replace IncrementalEdit with SourceEdit 2024-05-10 10:50:57 +02:00
Kim de Vos
c5699fb4dd Fix deprecated ByteSourceRange 2024-05-10 10:50:57 +02:00
Kim de Vos
a71b428568 Remove ?? position as it never was used 2024-05-10 10:25:49 +02:00
Paul LeMarquand
6a791fd7ca Merge pull request #1227 from plemarquand/merge-extension-tests
Merge tests defined in extensions
2024-05-09 20:51:16 -04:00
Paul LeMarquand
baa3f616c9 Handle XCTest extensions 2024-05-08 23:01:41 -04:00
Alex Hoppen
f1d6a081d2 Don’t show Add documentation refactoring for declarations that are not on a new line 2024-05-08 15:05:35 -07:00
Alex Hoppen
449d2a9b39 Fix a bug that caused documentation to be added at the start of the declaration’s trivia
This meant that if there were two newlines before the declaration, the documentation would be separated to the declaration by one newline and if the declaration was at the start of a line, the declaration would be on the same line as the doc comment, effectively making the documentation part of a comment.
2024-05-08 14:56:09 -07:00
Alex Hoppen
e3c498e3f1 Address my own review comments to #1179
Addresses a few minor comments and the following major ones:
- Add test cases for the syntax refactorings
- Don’t report code actions for refactorings that don’t actually modify the source
- Instead of just looking at the parent of the token of the selected range, walk up the syntax tree to find the syntax node to refactor. This makes the refactorings available in a lot more locations.
2024-05-08 14:56:09 -07:00
Paul LeMarquand
e740bb3394 Dont expose isExtension on TestItem 2024-05-08 14:30:16 -04:00
Doug Gregor
78f6132fb2 Add code actions for adding library/executable/macro targets to a package manifest 2024-05-07 23:29:44 -07:00
Doug Gregor
638fd5b7ce Add separate "add test" manifest actions for XCTest and Swift Testing 2024-05-07 22:52:51 -07:00
Paul LeMarquand
adfae1a77f Prioritize tests defined in type definition over those in extensions
Sort the list of test items prioritizing those defined in the
originating type definition over those in extensions.
2024-05-07 13:55:55 -04:00
Paul LeMarquand
7b315680c9 Merge tests defined in extensions
Merge the XCTests and swift-testing tests defined in extensions into
their parent TestItems.

This is done as another pass after the TestScanner visitors have walked
the tree.

Fixes #1218
2024-05-07 13:39:25 -04:00
Doug Gregor
d4bbf9ccc1 Address review comments 2024-05-06 17:57:07 -07:00
Doug Gregor
037e55e9d6 Handle unexpected nodes following a closure 2024-05-06 14:32:29 -07:00
Doug Gregor
e54c99eebd Add a syntactic "Add Codable structs from JSON" code action
Add a syntactic action that takes JSON pasted into a Swift file or
placed in a string literal, then turns it into a set of Codable
structs that can represent the JSON. Our typical example starts like
this:

```
{
    "name": "Produce",
    "shelves": [
        {
            "name": "Discount Produce",
            "product": {
                "name": "Banana",
                "points": 200,
                "description": "A banana that's perfectly ripe."
            }
        }
    ]
}
```

and turns into this:

```swift
struct JSONValue: Codable {
    var name: String
    var shelves: [Shelves]

    struct Shelves: Codable {
        var name: String
        var product: Product

        struct Product: Codable {
            var description: String
            var name: String
            var points: Double
        }
    }
}
```

When converting to JSON, we attempt to reason about multiple JSON
objects on the same level to detect when there are optional fields,
due to either an explicit null or due to the absence of fields in some
of the JSON objects that are conceptually stored together.

The refactoring itself would live down in the swift-syntax package if
not for its dependency on Foundation. We'll move it when appropriate.
2024-05-06 13:53:14 -07:00
Alex Hoppen
329e3d3297 Use as(DeclSyntaxEnum.self) instead of force-unwrapping 2024-05-06 08:36:01 -07:00
Alex Hoppen
f8d0c6098b Use TokenSyntax.indentationOfLine from SwiftBasicFormat instead of duplicating indentation inferring logic 2024-05-06 08:25:21 -07:00
Alex Hoppen
e71aa5d35e Merge pull request #1214 from ahoppen/background-index-preparation
Miscellaneous commits in preparation for background indexing
2024-05-05 14:26:11 -07:00
Doug Gregor
b628738473 Add "Add documentation" code action to stub out documentation for a function
This code action takes an undocumented function declaration like

    func refactor(syntax: DeclSyntax, in context: Void) -> DeclSyntax?

and adds stub documentation for the parameters / result / etc., like this:

    /// A description
    /// - Parameters:
    ///   - syntax:
    ///   - context:
    ///
    /// - Returns:
2024-05-04 15:08:12 -07:00
Doug Gregor
ab32186382 Generalize SyntaxRefactoringCodeActionProvider to work with EditRefactoringProvider
Rather than only adapt refactoring actions that conform to
SyntaxRefactoringProvider, which takes a syntax node and produces a
syntax node, adapt to the less-constraining EditRefactoringProvider,
which takes a syntax node and produces edits. We can map edits over
just as effectively.
2024-05-04 14:16:51 -07:00
Alex Hoppen
d114399270 Add method on Process that waits until exit and sends a SIGINT to the process if the Task is cancelled 2024-05-03 14:48:15 -07:00
Alex Hoppen
a95496438b Move Collection.partition(intoNumberOfBatches:) to SKSupport
This way we’ll be able to use it from the semantic indexer.
2024-05-03 14:48:15 -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
Paul LeMarquand
ccabf7adc6 Use .text over .trimmed 2024-04-30 14:29:01 -04:00
Paul LeMarquand
f89cf51011 Trim parameter names in SwiftTestingScanner
Test specified with anonymous arguments would produce an id with extra
whitespace, i.e: `funcWithArgument(_ x: Int)` would produce a TestItem
id of `funcWithArgument(_ :)`

This patch trims this extra whitespace to produce IDs that are
consistent with Swift's function ids, i.e: `funcWithArgument(_:)`
2024-04-30 14:05:10 -04:00
Paul LeMarquand
8f8e50ec9b Merge pull request #1191 from plemarquand/tag-member-access-syntax
Support member access in tags
2024-04-29 12:09:46 -04:00
Doug Gregor
3674e7f663 Address code review feedback 2024-04-24 21:56:55 -07:00
Doug Gregor
5428b9c250 Reformat 2024-04-24 21:25:55 -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
531d5777a7 Add refactoring action for adding a test target to a package manifest
Leverage the newly-introduced package manifest editing tools in SwiftPM
to create a package editing refactoring operation. This operation can
be triggered from the a target in the manifest itself, e.g.,

    .target(name: "MyLib")

and will add a test target to the package manifest that depends on
this target, i.e.,

    .testTarget(
      name: "MyLibTests",
      dependencies: [ "MyLib" ]
    )

It will also create a new source file `Tests/MyLibTests/MyLibTests.swift`
that that imports both MyLib and XCTest, and contains an XCTestCase subclass
with one test to get you started.
2024-04-24 21:25:54 -07:00
Paul LeMarquand
eb106c054a Lint fix 2024-04-24 16:49:40 -04:00
Paul LeMarquand
6f850ab084 Update tag tests 2024-04-24 14:25:55 -04:00
Paul LeMarquand
e26461eb3a More robust fully qualified name resolution
Add a `components` property to MemberAccessExprSyntax that provides all
the base names and the member's name as an array. When resolving
swift-testing Tags check if they start with Tag or Testing.Tag and drop
that from the name.
2024-04-24 14:20:44 -04:00
Paul LeMarquand
3bca8da4fe Remove leading dot on string tag representation 2024-04-24 14:20:38 -04:00
Paul LeMarquand
46286262a1 Simplify getting MemberAccessExprSyntax.components 2024-04-24 14:20:34 -04:00
Paul LeMarquand
f77f22bd5a All statics on Tag have a leading . 2024-04-24 14:20:31 -04:00
Paul LeMarquand
bff4a1efb2 Support statically defined tags
To remove namespacing ambiguity, support statically defined tags only.
2024-04-24 14:20:27 -04:00
Paul LeMarquand
35fe4aa378 More robust fully qualified name resolution
Add a `components` property to MemberAccessExprSyntax that provides all
the base names and the member's name as an array. When resolving
swift-testing Tags check if they start with Tag or Testing.Tag and drop
that from the name.
2024-04-24 14:20:16 -04:00
Paul LeMarquand
def5285102 Account for nested structs on Tag declarations
When a tag is declared within a nested type on Tag include the types
name in the tags string representation.
2024-04-24 14:20:12 -04:00
Paul LeMarquand
76c0860d15 Support member access in tags
Currently tags are only recognized when the tag is specified by string
literal, i.e: @Tag("foo").

Support Tags added via the staticMember Tag.Kind.
2024-04-24 14:20:06 -04:00
Alex Hoppen
ec5c614318 Merge pull request #1175 from ahoppen/syntactic-test-index 2024-04-24 07:01:34 -07:00
Alex Hoppen
ae215f924c Fix a bug that would cause a file to never be part of the syntactic index again after it got deleted once 2024-04-23 20:51:15 -07:00
Alex Hoppen
fd7b268431 Reload a file when other files within the same module or a .swiftmodule file has been changed
When the client sends us `workspace/didChangeWatchedFiles` notification of an updated `.swift` file, we should refresh the other open files in that module since they might be referencing functions from that updated file.

If a `.swiftmodule` file has been updated, we refresh all the files within the package since they might import that module. Technically, we would only need to refresh files that are in module that are downstream of the updated module but we don’t currently have that information easily available from SwiftPM. Also, usually, if the client has a file from a low-level module open, he’ll be working on that module which means that such an optimization won’t help. The real solution here is to wait for us to finish preparation (which we would exactly know when it finishes since sourcekit-lsp would schedule it) but for that we need to implement background preparation.

Fixes #620
Fixes #1116
rdar://99329579
rdar://123971779
2024-04-23 09:34:20 -07:00