|
| 1 | +# Development |
| 2 | + |
| 3 | +This document contains notes about development and testing of SourceKit-LSP. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +* [Debugging](#debugging) |
| 8 | +* [Writing Tests](#writing-tests) |
| 9 | + |
| 10 | +## Debugging |
| 11 | + |
| 12 | +You can attach LLDB to SourceKit-LSP and set breakpoints to debug. You may want to instruct LLDB to wait for the sourcekit-lsp process to launch and then start your editor, which will typically launch |
| 13 | +SourceKit-LSP as soon as you open a Swift file: |
| 14 | + |
| 15 | +```sh |
| 16 | +$ lldb -w -n sourcekit-lsp |
| 17 | +``` |
| 18 | + |
| 19 | +If you are using the Xcode project, go to Debug, Attach to Process by PID or Name. |
| 20 | + |
| 21 | +### Print SourceKit Logs |
| 22 | + |
| 23 | +You can configure SourceKit-LSP to print log information from SourceKit to stderr by setting the following environment variable: |
| 24 | + |
| 25 | +```sh |
| 26 | +SOURCEKIT_LOGGING="N" |
| 27 | +``` |
| 28 | + |
| 29 | +Where "N" configures the log verbosity and is one of the following numbers: 0 (error), 1 (warning), 2 (info), or 3 (debug). |
| 30 | + |
| 31 | +## Writing Tests |
| 32 | + |
| 33 | +As much as is practical, all code should be covered by tests. New tests can be added under the `Tests` directory and should use `XCTest`. The rest of this section will describe the additional tools available in the `SKTestSupport` module to make it easier to write good and efficient tests. |
| 34 | + |
| 35 | +### Test Projects (Fixtures) |
| 36 | + |
| 37 | +SourceKit test projects should be put in the `Tests/INPUTS` directory. Generally, they should use the [Tibs](#tibs) build system to define their sources and targets. An exception is for tests that need to specifically test the interaction with the Swift Package Manager. An example Tibs test project might look like: |
| 38 | + |
| 39 | +``` |
| 40 | +Tests/ |
| 41 | + INPUTS/ |
| 42 | + MyTestProj/ |
| 43 | + a.swift |
| 44 | + b.swift |
| 45 | + c.cpp |
| 46 | +``` |
| 47 | + |
| 48 | +Where `project.json` describes the project's targets, for example |
| 49 | + |
| 50 | +``` |
| 51 | +{ "sources": ["a.swift", "b.swift", "c.cpp"] } |
| 52 | +``` |
| 53 | + |
| 54 | +Tibs supports more advanced project configurations, such as multiple swift modules with dependencies, etc. For much more information about Tibs, including what features it supports and how it works, see [Tibs](#tibs). |
| 55 | + |
| 56 | +### SKTibsTestWorkspace |
| 57 | + |
| 58 | +The `SKTibsTestWorkspace` pulls together the various pieces needed for working with tests, including opening a connection to the language server, building the project to produce index data, loading source code into open documents, etc. |
| 59 | + |
| 60 | +To create a `SKTibsTestWorkspace`, use the `staticSourceKitTibsWorkspace` method (the intent is to provide a `mutableSourceKitTibsWorkspace` method in the future for tests that mutate source code). |
| 61 | + |
| 62 | +```swift |
| 63 | +func testFoo() { |
| 64 | + // Create the workspace, including opening a connection to the TestServer. |
| 65 | + guard let ws = try staticSourceKitTibsWorkspace(name: "MyTestProj") else { return } |
| 66 | + let loc = ws.testLoc("myLocation") |
| 67 | + |
| 68 | + // Build the project and populate the index. |
| 69 | + try ws.buildAndIndex() |
| 70 | + |
| 71 | + // Open a document from the test project sources. |
| 72 | + try ws.openDocument(loc.url, language: .swift) |
| 73 | + |
| 74 | + // Send requests to the server. |
| 75 | + let response = try ws.sk.sendSync(...) |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +#### Source Locations |
| 80 | + |
| 81 | +It is common to want to refer to specific locations in the source code of a test project. This is supported using inline comment syntax. |
| 82 | + |
| 83 | +```swift |
| 84 | +Test.swift: |
| 85 | +func /*myFuncDef*/myFunc() { |
| 86 | + /*myFuncCall*/myFunc() |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +In a test, these locations can be referenced by name. The named location is immediately after the comment. |
| 91 | + |
| 92 | +```swift |
| 93 | + |
| 94 | +let loc = ws.testLoc("myFuncDef") |
| 95 | +// TestLocation(url: ..., line: 1, column: 19) |
| 96 | +``` |
| 97 | + |
| 98 | +`TestLocation`s can be easily converted to LSP `Location` and `Position`s. |
| 99 | + |
| 100 | +```swift |
| 101 | +Location(ws.testLoc("aaa:call")) |
| 102 | +Position(ws.testLoc("aaa:call")) |
| 103 | +``` |
| 104 | + |
| 105 | +## Tibs |
| 106 | + |
| 107 | +We use Tibs, the "Test Index Build System" from the IndexStoreDB project to provide build system support for test projects, including getting compiler arguments and building an index. |
| 108 | +For much more information about Tibs, see [IndexStoreDB/Documentation/Tibs.md](https://github.com/apple/indexstore-db/blob/master/Documentation/Tibs.md). |
0 commit comments