Skip to content

Commit ed64203

Browse files
Merge branch 'master' into format
# Conflicts: # Package.swift
2 parents a96a554 + b00cff1 commit ed64203

File tree

63 files changed

+2429
-326
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+2429
-326
lines changed

Documentation/Development.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ This document contains notes about development and testing of SourceKit-LSP.
1212

1313
## Getting Started Developing SourceKit-LSP
1414

15-
SourceKit-LSP is under heavy development, and requires the use of a Swift development toolchain.
15+
For maximum compatibility with toolchain components such as the Swift Package Manager, the only supported way to develop SourceKit-LSP is with the latest toolchain snapshot. We make an effort to keep the build and tests working with the latest release of Swift, but this is not always possible.
1616

17-
1. Install the latest master toolchain snapshot from https://swift.org/download/#snapshots. **If you're looking for swift-5.1 support**, use the `swift-5.1-branch` of SourceKit-LSP with the latest swift-5.1 toolchain snapshot. See [Toolchains](#toolchains) for more information.
17+
1. Install the latest master toolchain snapshot from https://swift.org/download/#snapshots. **If you're looking for swift-5.x**, use the `swift-5.x-branch` of SourceKit-LSP with the latest swift-5.x toolchain snapshot. See [Toolchains](#toolchains) for more information.
1818

1919
2. Build the language server executable `sourcekit-lsp` using `swift build`. See [Building](#building-sourcekit-lsp) for more information.
2020

@@ -59,7 +59,8 @@ Use the latest toolchain snapshot from https://swift.org/download/#snapshots. So
5959
| SourceKit-LSP branch | Toolchain |
6060
|:---------------------|:----------|
6161
| master | Trunk Development (master) |
62-
| swift-5.1-branch | Swift 5.1 Development |
62+
| swift-5.2-branch | Swift 5.2 Development |
63+
| swift-5.1-branch | Swift 5.1.1+ |
6364

6465
*Note*: there is no branch of SourceKit-LSP that supports Swift 5.0.
6566

Editors/README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
This document contains information about how to configure an editor to use SourceKit-LSP. If your editor is not listed below, but it supports the [Language Server Protocol](https://microsoft.github.io/language-server-protocol/) (LSP), see [Other Editors](#other-editors).
44

5+
In general, you will need to know where to find the `sourcekit-lsp` server exectuable. Some examples:
6+
7+
* With Xcode 11.4+
8+
* `xcrun sourcekit-lsp` - run the server
9+
* `xcrun --find sourcekit-lsp` - get the full path to the server
10+
* Toolchain from Swift.org
11+
* Linux
12+
* You will find `sourcekit-lsp` in the `bin` directory of the toolchain.
13+
* macOS
14+
* `xcrun --toolchain swift sourcekit-lsp` - run the server
15+
* `xcrun --toolchain swift --find sourcekit-lsp` - get the full path to the server
16+
* Built from source
17+
* `.build/<platform>/<configuration>/sourcekit-lsp`
18+
519
## Visual Studio Code
620

721
To use SourceKit-LSP with Visual Studio Code, you will need the [SourceKit-LSP
@@ -29,7 +43,7 @@ You will need the path to the `sourcekit-lsp` executable for the "command" secti
2943
{
3044
"enabled": true,
3145
"command": [
32-
"<path to sourcekit-lsp>"
46+
"<sourcekit-lsp command>"
3347
],
3448
"env": {
3549
// To override the toolchain, uncomment the following:

Editors/vscode/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ This extension adds support to Visual Studio Code for using SourceKit-LSP, a
44
language server for Swift and C/C++/Objective-C languages.
55

66
**Note**: SourceKit-LSP is under heavy development and this should be considered
7-
a preview. Users will need to separately provide the `sourcekit-lsp` executable
8-
as well as a Swift toolchain.
7+
a preview. Users will need to provide the `sourcekit-lsp` executable
8+
and Swift toolchain.
99

1010
## Building and Installing the Extension
1111

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ if getenv("SWIFTCI_USE_LOCAL_DEPS") == nil {
209209
package.dependencies += [
210210
.package(path: "../indexstore-db"),
211211
.package(path: "../swiftpm"),
212-
.package(path: "../swiftpm/TSC"),
213212
.package(path: "../swift-format"),
213+
.package(path: "../swiftpm/swift-tools-support-core"),
214214
]
215215
}

README.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ SourceKit-LSP is an implementation of the [Language Server Protocol](https://mic
44

55
## Getting Started
66

7-
SourceKit-LSP is under heavy development! The best way to try it out is to install a recent Swift development toolchain from https://swift.org/download/#snapshots. Make sure you build your package with the same toolchain as you got sourcekit-lsp from to ensure compatibility.
7+
The SourceKit-LSP server is included with the Swift toolchain. Depending on how you installed Swift, you may already have SourceKit-LSP. Make sure you build your package with the same toolchain as you use sourcekit-lsp from to ensure compatibility.
88

9-
### Using a Swift Toolchain from Swift.org
9+
1. Get SourceKit-LSP with a Swift toolchain
1010

11-
1. Install the latest master or 5.1 development toolchain snapshot from https://swift.org/download/#snapshots.
11+
1. If you have installed Xcode 11.4+ or the corresponding Command Line Tools package, the SourceKit-LSP server is included and can be run with `xcrun sourcekit-lsp`.
1212

13-
2. Configure your editor to use the `sourcekit-lsp` executable from the toolchain snapshot. See [Editors](Editors) for more information about editor integration.
13+
2. If you are using a [toolchain from Swift.org](https://swift.org/download/), the SourceKit-LSP server is included and can be run with `xcrun --toolchain swift sourcekit-lsp` on macOS, or using the full path to the `sourcekit-lsp` executable on Linux.
1414

15-
3. Build the project you are working on with `swift build` using the same toolchain snapshot. The language server depends on the build to provide module dependencies and to update the global index.
15+
3. If your toolchain did not come with SourceKit-LSP, see [Development](Documentation/Development.md) for how to build it from source.
16+
17+
2. Configure your editor to use SourceKit-LSP. See [Editors](Editors) for more information about editor integration.
18+
19+
3. Build the project you are working on with `swift build` using the same toolchain as the SourceKit-LSP server. The language server depends on the build to provide module dependencies and to update the global index.
1620

1721
## Development
1822

@@ -35,7 +39,7 @@ SourceKit-LSP is still in early development, so you may run into rough edges wit
3539
| Code completion || |
3640
| Quick Help (Hover) || |
3741
| Diagnostics || |
38-
| Fix-its | | |
42+
| Fix-its | | |
3943
| Jump to Definition || |
4044
| Find References || |
4145
| Background Indexing || Build project to update the index using [Indexing While Building](#indexing-while-building) |

Sources/LSPLogging/Logging.swift

+10
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ public func logAsync(level: LogLevel = .default, messageProducer: @escaping (_ c
3737
Logger.shared.logAsync(level: level, messageProducer: messageProducer)
3838
}
3939

40+
/// Log an error and trigger an assertion failure (if compiled with assertions).
41+
///
42+
/// If `level >= Logger.shared.currentLevel`, it will be emitted. However, the converse is not necessarily true: on platforms that provide `os_log`, the message may be emitted by `os_log` according to its own rules about log level.
43+
///
44+
/// - parameter message: The message to print.
45+
public func logAssertionFailure(_ message: String, file: StaticString = #file, line: UInt = #line) {
46+
Logger.shared.log(message, level: .error)
47+
assertionFailure(message, file: file, line: line)
48+
}
49+
4050
/// Like `try?`, but logs the error on failure.
4151
public func orLog<R>(
4252
_ prefix: String = "",

Sources/LanguageServerProtocol/CMakeLists.txt

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ add_library(LanguageServerProtocol
66
Message.swift
77
MessageRegistry.swift
88
Messages.swift
9-
PositionRange.swift
10-
Request.swift
11-
RequestID.swift
129
Notifications/CancelRequestNotification.swift
1310
Notifications/ConfigurationNotification.swift
1411
Notifications/DidChangeWorkspaceFoldersNotification.swift
@@ -18,6 +15,9 @@ add_library(LanguageServerProtocol
1815
Notifications/PublishDiagnosticsNotification.swift
1916
Notifications/ShowMessageNotification.swift
2017
Notifications/TextSynchronizationNotifications.swift
18+
PositionRange.swift
19+
Request.swift
20+
RequestID.swift
2121
Requests/ApplyEditRequest.swift
2222
Requests/CodeActionRequest.swift
2323
Requests/ColorPresentationRequest.swift
@@ -47,11 +47,11 @@ add_library(LanguageServerProtocol
4747
SupportTypes/Diagnostic.swift
4848
SupportTypes/DocumentURI.swift
4949
SupportTypes/FoldingRangeKind.swift
50+
SupportTypes/LSPAny.swift
5051
SupportTypes/Language.swift
5152
SupportTypes/Location.swift
5253
SupportTypes/LocationLink.swift
5354
SupportTypes/LocationsOrLocationLinksResponse.swift
54-
SupportTypes/LSPAny.swift
5555
SupportTypes/MarkupContent.swift
5656
SupportTypes/Position.swift
5757
SupportTypes/ServerCapabilities.swift
@@ -66,7 +66,8 @@ add_library(LanguageServerProtocol
6666
SupportTypes/WindowMessageType.swift
6767
SupportTypes/WorkspaceEdit.swift
6868
SupportTypes/WorkspaceFolder.swift
69-
SupportTypes/WorkspaceSettings.swift)
69+
SupportTypes/WorkspaceSettings.swift
70+
)
7071
set_target_properties(LanguageServerProtocol PROPERTIES
7172
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
7273
if(NOT CMAKE_SYSTEM_NAME STREQUAL Windows)

Sources/LanguageServerProtocol/Notifications/TextSynchronizationNotifications.swift

+8-1
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,19 @@ public struct DidChangeTextDocumentNotification: NotificationType, Hashable {
7474
/// Edits to the document.
7575
public var contentChanges: [TextDocumentContentChangeEvent]
7676

77+
/// Force the LSP to rebuild its AST for the given file. This is useful for clangd to workaround clangd's assumption that
78+
/// missing header files will stay missing.
79+
/// **LSP Extension from clangd**.
80+
public var forceRebuild: Bool? = nil
81+
7782
public init(
7883
textDocument: VersionedTextDocumentIdentifier,
79-
contentChanges: [TextDocumentContentChangeEvent])
84+
contentChanges: [TextDocumentContentChangeEvent],
85+
forceRebuild: Bool? = nil)
8086
{
8187
self.textDocument = textDocument
8288
self.contentChanges = contentChanges
89+
self.forceRebuild = forceRebuild
8390
}
8491
}
8592

Sources/LanguageServerProtocol/Requests/CodeActionRequest.swift

+6-12
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,11 @@ public enum CodeActionRequestResponse: ResponseType, Codable, Equatable {
5656
case commands([Command])
5757

5858
public init(codeActions: [CodeAction], clientCapabilities: TextDocumentClientCapabilities.CodeAction?) {
59-
if let literalSupport = clientCapabilities?.codeActionLiteralSupport {
60-
let supportedKinds = literalSupport.codeActionKind.valueSet
61-
self = .codeActions(codeActions.filter {
62-
if let kind = $0.kind {
63-
return supportedKinds.contains(kind)
64-
} else {
65-
// The client guarantees that unsupported kinds will be treated,
66-
// so it's probably safe to include unspecified kinds into the result.
67-
return true
68-
}
69-
})
59+
if clientCapabilities?.codeActionLiteralSupport != nil {
60+
// The client guarantees that unsupported kinds will be handled, and in
61+
// practice some clients use `"codeActionKind":{"valueSet":[]}`, since
62+
// they support all kinds anyway.
63+
self = .codeActions(codeActions)
7064
} else {
7165
self = .commands(codeActions.compactMap { $0.command })
7266
}
@@ -111,7 +105,7 @@ public struct CodeActionContext: Codable, Hashable {
111105
}
112106
}
113107

114-
public struct CodeAction: Codable, Equatable {
108+
public struct CodeAction: Codable, Hashable {
115109

116110
/// A short, human-readable, title for this code action.
117111
public var title: String

Sources/LanguageServerProtocol/SupportTypes/ClientCapabilities.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,14 @@ public struct TextDocumentClientCapabilities: Hashable, Codable {
356356
/// Whether the client accepts diagnostics with related information.
357357
public var relatedInformation: Bool? = nil
358358

359-
public init(relatedInformation: Bool? = nil) {
359+
/// Requests that SourceKit-LSP send `Diagnostic.codeActions`.
360+
/// **LSP Extension from clangd**.
361+
public var codeActionsInline: Bool? = nil
362+
363+
public init(relatedInformation: Bool? = nil,
364+
codeActionsInline: Bool? = nil) {
360365
self.relatedInformation = relatedInformation
366+
self.codeActionsInline = codeActionsInline
361367
}
362368
}
363369

Sources/LanguageServerProtocol/SupportTypes/Diagnostic.swift

+13-2
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,26 @@ public struct Diagnostic: Codable, Hashable {
4747
/// Related diagnostic notes.
4848
public var relatedInformation: [DiagnosticRelatedInformation]?
4949

50+
/// All the code actions that address this diagnostic.
51+
/// **LSP Extension from clangd**.
52+
public var codeActions: [CodeAction]?
53+
5054
public init(
5155
range: Range<Position>,
5256
severity: DiagnosticSeverity?,
5357
code: DiagnosticCode? = nil,
5458
source: String?,
5559
message: String,
56-
relatedInformation: [DiagnosticRelatedInformation]? = nil)
60+
relatedInformation: [DiagnosticRelatedInformation]? = nil,
61+
codeActions: [CodeAction]? = nil)
5762
{
5863
self._range = CustomCodable<PositionRange>(wrappedValue: range)
5964
self.severity = severity
6065
self.code = code
6166
self.source = source
6267
self.message = message
6368
self.relatedInformation = relatedInformation
69+
self.codeActions = codeActions
6470
}
6571
}
6672

@@ -71,9 +77,14 @@ public struct DiagnosticRelatedInformation: Codable, Hashable {
7177

7278
public var message: String
7379

74-
public init(location: Location, message: String) {
80+
/// All the code actions that address the parent diagnostic via this note.
81+
/// **LSP Extension from clangd**.
82+
public var codeActions: [CodeAction]?
83+
84+
public init(location: Location, message: String, codeActions: [CodeAction]? = nil) {
7585
self.location = location
7686
self.message = message
87+
self.codeActions = codeActions
7788
}
7889
}
7990

Sources/LanguageServerProtocol/SupportTypes/DocumentURI.swift

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public struct DocumentURI: Codable, Hashable {
2424
}
2525
}
2626

27+
/// The document's URL scheme, if present.
28+
public var scheme: String? {
29+
return storage.scheme
30+
}
31+
2732
/// Returns a filepath if the URI is a URL. If the URI is not a URL, returns
2833
/// the full URI as a fallback.
2934
/// This value is intended to be used when interacting with sourcekitd which

Sources/SKCore/BuildServerBuildSystem.swift

+6-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -149,7 +149,7 @@ extension BuildServerBuildSystem: BuildSystem {
149149

150150
/// Register the given file for build-system level change notifications, such as command
151151
/// line flag changes, dependency changes, etc.
152-
public func registerForChangeNotifications(for uri: DocumentURI) {
152+
public func registerForChangeNotifications(for uri: DocumentURI, language: Language) {
153153
let request = RegisterForChanges(uri: uri, action: .register)
154154
_ = self.buildServer?.send(request, queue: requestQueue, reply: { result in
155155
if let error = result.failure {
@@ -171,15 +171,14 @@ extension BuildServerBuildSystem: BuildSystem {
171171

172172
public func settings(for uri: DocumentURI, _ language: Language) -> FileBuildSettings? {
173173
if let response = try? self.buildServer?.sendSync(SourceKitOptions(uri: uri)) {
174-
return FileBuildSettings(compilerArguments: response.options, workingDirectory: response.workingDirectory)
174+
return FileBuildSettings(
175+
compilerArguments: response.options,
176+
workingDirectory: response.workingDirectory,
177+
language: language)
175178
}
176179
return nil
177180
}
178181

179-
public func toolchain(for: DocumentURI, _ language: Language) -> Toolchain? {
180-
return nil
181-
}
182-
183182
public func buildTargets(reply: @escaping (LSPResult<[BuildTarget]>) -> Void) {
184183
_ = self.buildServer?.send(BuildTargets(), queue: requestQueue) { response in
185184
switch response {

Sources/SKCore/BuildSystem.swift

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -34,16 +34,13 @@ public protocol BuildSystem: AnyObject {
3434
/// Returns the settings for the given url and language mode, if known.
3535
func settings(for: DocumentURI, _ language: Language) -> FileBuildSettings?
3636

37-
/// Returns the toolchain to use to compile this file
38-
func toolchain(for: DocumentURI, _ language: Language) -> Toolchain?
39-
4037
/// Delegate to handle any build system events such as file build settings
4138
/// changing.
4239
var delegate: BuildSystemDelegate? { get set }
4340

4441
/// Register the given file for build-system level change notifications, such
4542
/// as command line flag changes, dependency changes, etc.
46-
func registerForChangeNotifications(for: DocumentURI)
43+
func registerForChangeNotifications(for: DocumentURI, language: Language)
4744

4845
/// Unregister the given file for build-system level change notifications,
4946
/// such as command line flag changes, dependency changes, etc.

Sources/SKCore/BuildSystemList.swift

+3-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -47,9 +47,9 @@ extension BuildSystemList: BuildSystem {
4747

4848
/// Register the given file for build-system level change notifications, such as command
4949
/// line flag changes, dependency changes, etc.
50-
public func registerForChangeNotifications(for uri: DocumentURI) {
50+
public func registerForChangeNotifications(for uri: DocumentURI, language: Language) {
5151
// Only register with the primary build system, since we only use its delegate.
52-
providers.first?.registerForChangeNotifications(for: uri)
52+
providers.first?.registerForChangeNotifications(for: uri, language: language)
5353
}
5454

5555
/// Unregister the given file for build-system level change notifications, such as command
@@ -59,10 +59,6 @@ extension BuildSystemList: BuildSystem {
5959
providers.first?.unregisterForChangeNotifications(for: uri)
6060
}
6161

62-
public func toolchain(for uri: DocumentURI, _ language: Language) -> Toolchain? {
63-
return providers.first?.toolchain(for: uri, language)
64-
}
65-
6662
public func buildTargets(reply: @escaping (LSPResult<[BuildTarget]>) -> Void) {
6763
providers.first?.buildTargets(reply: reply)
6864
}

0 commit comments

Comments
 (0)