Skip to content

[5.3-20201012] [completion] Filter completions on the server side #336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f8f98dd
[completion] Pass through new code-completion options
benlangmuir Jul 21, 2020
e0511b9
[gardening] Split code-completion into its own file
benlangmuir Jul 21, 2020
198266d
[completion] Add server-side filtering UIDs
benlangmuir Oct 16, 2020
ffd9690
[completion] Add initial server-side filtering implementation
benlangmuir Jul 22, 2020
1f28a7c
[completion] Set default maxResults = 200
benlangmuir Jul 27, 2020
9d5f547
[completion] Fix tests when using server-side filtering
benlangmuir Jul 28, 2020
34896d2
[log] Improve description of Position
benlangmuir Jul 28, 2020
b97dac9
[completion] Add an extension to pass completion options per-request
benlangmuir Jul 28, 2020
4434692
[completion] Add tests for server-side filtering
benlangmuir Jul 28, 2020
d8b5a98
[gardening] Fix copyright headers and formating in modified files
benlangmuir Jul 29, 2020
947e15d
[completion] Add usage note for server-side filtering command-line op…
benlangmuir Jul 29, 2020
8c1ef32
[gardening] Rename CodeCompletionOptions -> SKCompletionOptions
benlangmuir Jul 29, 2020
439428e
[completion] Add doc comment for CodeCompletionSession
benlangmuir Jul 29, 2020
4485191
[completion] Enable server-side filtering by default
benlangmuir Jul 29, 2020
2eb8998
[test] Update linuxmain
benlangmuir Jul 29, 2020
84848df
[buildsystem] Fix strong reference to delegate
benlangmuir Jun 9, 2020
91334e5
[jsonrpc] Workaround file descriptor lifetime issue
benlangmuir Jun 9, 2020
f333b3d
Shutdown toolchain connections on exit
benlangmuir Jun 9, 2020
08f5420
Fix merge conflict resolution errors
benlangmuir Oct 16, 2020
459e3b1
Fix issue where a code-completion session at a specific offset would …
DavidGoldman Oct 13, 2020
1667eea
Remove unneeded textDocumentChange from code-complete regression test
DavidGoldman Oct 14, 2020
cfe3e60
Better use of XCTest for code complete regression test
DavidGoldman Oct 14, 2020
64b2350
[sourcekit-lsp] Update spelling of completion filtering options to do…
benlangmuir Oct 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions Sources/LSPTestSupport/TestJSONRPCConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand All @@ -18,7 +18,7 @@ import XCTest
// Workaround ambiguity with Foundation.
public typealias Notification = LanguageServerProtocol.Notification

public struct TestJSONRPCConnection {
public final class TestJSONRPCConnection {
public let clientToServer: Pipe = Pipe()
public let serverToClient: Pipe = Pipe()
public let client: TestClient
Expand All @@ -27,11 +27,6 @@ public struct TestJSONRPCConnection {
public let serverConnection: JSONRPCConnection

public init() {
// FIXME: DispatchIO doesn't like when the Pipes close behind its back even after the tests
// finish. Until we fix the lifetime, leak.
_ = Unmanaged.passRetained(clientToServer)
_ = Unmanaged.passRetained(serverToClient)

clientConnection = JSONRPCConnection(
protocol: testMessageRegistry,
inFD: serverToClient.fileHandleForReading.fileDescriptor,
Expand All @@ -47,8 +42,16 @@ public struct TestJSONRPCConnection {
client = TestClient(server: clientConnection)
server = TestServer(client: serverConnection)

clientConnection.start(receiveHandler: client)
serverConnection.start(receiveHandler: server)
clientConnection.start(receiveHandler: client) {
// FIXME: keep the pipes alive until we close the connection. This
// should be fixed systemically.
withExtendedLifetime(self) {}
}
serverConnection.start(receiveHandler: server) {
// FIXME: keep the pipes alive until we close the connection. This
// should be fixed systemically.
withExtendedLifetime(self) {}
}
}

public func close() {
Expand Down
14 changes: 9 additions & 5 deletions Sources/LanguageServerProtocol/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -104,14 +104,18 @@ public final class LocalConnection {

extension LocalConnection: Connection {
public func send<Notification>(_ notification: Notification) where Notification: NotificationType {
precondition(state == .started)
handler!.handle(notification, from: ObjectIdentifier(self))
handler?.handle(notification, from: ObjectIdentifier(self))
}

public func send<Request>(_ request: Request, queue: DispatchQueue, reply: @escaping (LSPResult<Request.Response>) -> Void) -> RequestID where Request: RequestType {
precondition(state == .started)
let id = nextRequestID()
handler!.handle(request, id: id, from: ObjectIdentifier(self)) { result in
guard let handler = handler else {
queue.async { reply(.failure(.cancelled)) }
return id
}

precondition(state == .started)
handler.handle(request, id: id, from: ObjectIdentifier(self)) { result in
queue.async {
reply(result)
}
Expand Down
15 changes: 13 additions & 2 deletions Sources/LanguageServerProtocol/Requests/CompletionRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand All @@ -22,6 +22,8 @@
/// - Parameters:
/// - textDocument: The document to perform completion in.
/// - position: The location to perform completion at.
/// - context: Optional code-completion context.
/// - sourcekitlspOptions: **(LSP Extension)** code-completion options for sourcekit-lsp.
///
/// - Returns: A list of completion items to complete the code at the given position.
public struct CompletionRequest: TextDocumentRequest, Hashable {
Expand All @@ -34,9 +36,18 @@ public struct CompletionRequest: TextDocumentRequest, Hashable {

public var context: CompletionContext?

public init(textDocument: TextDocumentIdentifier, position: Position) {
public var sourcekitlspOptions: SKCompletionOptions?

public init(
textDocument: TextDocumentIdentifier,
position: Position,
context: CompletionContext? = nil,
sourcekitlspOptions: SKCompletionOptions? = nil)
{
self.textDocument = textDocument
self.position = position
self.context = context
self.sourcekitlspOptions = sourcekitlspOptions
}
}

Expand Down
12 changes: 11 additions & 1 deletion Sources/LanguageServerProtocol/SupportTypes/Position.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -56,3 +56,13 @@ extension Position: LSPAnyCodable {
])
}
}

extension Position: CustomStringConvertible, CustomDebugStringConvertible {
public var description: String {
"\(line + 1):\(utf16index+1)"
}

public var debugDescription: String {
"Position(line: \(line), utf16index: \(utf16index))"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

/// Code-completion configuration.
///
/// **(LSP Extension)**: This is used as part of an extension to the
/// code-completion request.
public struct SKCompletionOptions: Codable, Hashable {

/// Whether to use server-side filtering or to return all results and let the
/// client handle all filtering.
public var serverSideFiltering: Bool

/// The maximum number of completion results to return, or `nil` for unlimited.
public var maxResults: Int?

public init(serverSideFiltering: Bool = true, maxResults: Int? = 200) {
self.serverSideFiltering = serverSideFiltering
self.maxResults = maxResults
}
}
6 changes: 5 additions & 1 deletion Sources/SKCore/BuildServerBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,11 @@ private func makeJSONRPCBuildServer(client: MessageHandler, serverPath: Absolute
outFD: clientToServer.fileHandleForWriting.fileDescriptor
)

connection.start(receiveHandler: client)
connection.start(receiveHandler: client) {
// FIXME: keep the pipes alive until we close the connection. This
// should be fixed systemically.
withExtendedLifetime((clientToServer, serverToClient)) {}
}
let process = Foundation.Process()

if #available(OSX 10.13, *) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SKCore/BuildSystemManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public final class BuildSystemManager {
weak var mainFilesProvider: MainFilesProvider?

/// Build system delegate that will receive notifications about setting changes, etc.
var _delegate: BuildSystemDelegate?
weak var _delegate: BuildSystemDelegate?

/// Create a BuildSystemManager that wraps the given build system. The new manager will modify the
/// delegate of the underlying build system.
Expand Down
39 changes: 22 additions & 17 deletions Sources/SKTestSupport/TestServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand All @@ -19,7 +19,7 @@ import SourceKit
import class Foundation.Pipe
import LSPTestSupport

public struct TestSourceKitServer {
public final class TestSourceKitServer {
public enum ConnectionKind {
case local, jsonrpc
}
Expand All @@ -40,6 +40,8 @@ public struct TestSourceKitServer {
public let client: TestClient
let connImpl: ConnectionImpl

public var hasShutdown: Bool = false

/// The server, if it is in the same process.
public let server: SourceKitServer?

Expand All @@ -63,11 +65,6 @@ public struct TestSourceKitServer {
let clientToServer: Pipe = Pipe()
let serverToClient: Pipe = Pipe()

// FIXME: DispatchIO doesn't like when the Pipes close behind its back even after the tests
// finish. Until we fix the lifetime, leak.
_ = Unmanaged.passRetained(clientToServer)
_ = Unmanaged.passRetained(serverToClient)

let clientConnection = JSONRPCConnection(
protocol: MessageRegistry.lspProtocol,
inFD: serverToClient.fileHandleForReading.fileDescriptor,
Expand All @@ -84,8 +81,16 @@ public struct TestSourceKitServer {
serverConnection.close()
})

clientConnection.start(receiveHandler: client)
serverConnection.start(receiveHandler: server!)
clientConnection.start(receiveHandler: client) {
// FIXME: keep the pipes alive until we close the connection. This
// should be fixed systemically.
withExtendedLifetime((clientToServer, serverToClient)) {}
}
serverConnection.start(receiveHandler: server!) {
// FIXME: keep the pipes alive until we close the connection. This
// should be fixed systemically.
withExtendedLifetime((clientToServer, serverToClient)) {}
}

connImpl = .jsonrpc(
clientToServer: clientToServer,
Expand All @@ -95,15 +100,15 @@ public struct TestSourceKitServer {
}
}

deinit {
close()
}

func close() {
switch connImpl {
case .local(clientConnection: let cc, serverConnection: let sc):
cc.close()
sc.close()

case .jsonrpc(clientToServer: _, serverToClient: _, clientConnection: let cc, serverConnection: let sc):
cc.close()
sc.close()
if !hasShutdown {
hasShutdown = true
_ = try! self.client.sendSync(ShutdownRequest())
self.client.send(ExitNotification())
}
}
}
14 changes: 12 additions & 2 deletions Sources/SourceKit/SourceKitServer+Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import LanguageServerProtocol
import SKCore

extension SourceKitServer {
Expand All @@ -27,10 +28,19 @@ extension SourceKitServer {
/// Additional options for the index.
public var indexOptions: IndexOptions

public init(buildSetup: BuildSetup = .default, clangdOptions: [String] = [], indexOptions: IndexOptions = .init()) {
/// Options for code-completion.
public var completionOptions: SKCompletionOptions

public init(
buildSetup: BuildSetup = .default,
clangdOptions: [String] = [],
indexOptions: IndexOptions = .init(),
completionOptions: SKCompletionOptions = .init())
{
self.buildSetup = buildSetup
self.clangdOptions = clangdOptions
self.indexOptions = indexOptions
self.completionOptions = completionOptions
}
}
}
30 changes: 27 additions & 3 deletions Sources/SourceKit/SourceKitServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public final class SourceKitServer: LanguageServer {
var language: Language
}

let options: Options
var options: Options

let toolchainRegistry: ToolchainRegistry

Expand Down Expand Up @@ -298,6 +298,21 @@ extension SourceKitServer {
if case .bool(let listenToUnitEvents) = options["listenToUnitEvents"] {
indexOptions.listenToUnitEvents = listenToUnitEvents
}
if case .dictionary(let completionOptions) = options["completion"] {
if case .bool(let serverSideFiltering) = completionOptions["serverSideFiltering"] {
self.options.completionOptions.serverSideFiltering = serverSideFiltering
}
switch completionOptions["maxResults"] {
case .none:
break
case .some(.null):
self.options.completionOptions.maxResults = nil
case .some(.int(let maxResults)):
self.options.completionOptions.maxResults = maxResults
case .some(let invalid):
log("expected null or int for 'maxResults'; got \(invalid)", level: .warning)
}
}
}

// Any messages sent before initialize returns are expected to fail, so this will run before
Expand Down Expand Up @@ -346,7 +361,7 @@ extension SourceKitServer {
save: TextDocumentSyncOptions.SaveOptions(includeText: false)
),
hoverProvider: true,
completionProvider: CompletionOptions(
completionProvider: LanguageServerProtocol.CompletionOptions(
resolveProvider: false,
triggerCharacters: ["."]
),
Expand Down Expand Up @@ -406,6 +421,10 @@ extension SourceKitServer {

func shutdown(_ request: Request<ShutdownRequest>) {
_prepareForExit()
for service in languageService.values {
service.shutdown()
}
languageService = [:]
request.reply(VoidResponse())
}

Expand Down Expand Up @@ -781,7 +800,12 @@ public func languageService(

case .swift:
guard let sourcekitd = toolchain.sourcekitd else { return nil }
return try makeLocalSwiftServer(client: client, sourcekitd: sourcekitd, buildSettings: (client as? SourceKitServer)?.workspace?.buildSettings, clientCapabilities: (client as? SourceKitServer)?.workspace?.clientCapabilities)
return try makeLocalSwiftServer(
client: client,
sourcekitd: sourcekitd,
buildSettings: (client as? SourceKitServer)?.workspace?.buildSettings,
clientCapabilities: (client as? SourceKitServer)?.workspace?.clientCapabilities,
options: options)

default:
return nil
Expand Down
3 changes: 2 additions & 1 deletion Sources/SourceKit/ToolchainLanguageServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand All @@ -20,6 +20,7 @@ public protocol ToolchainLanguageServer: AnyObject {

func initializeSync(_ initialize: InitializeRequest) throws -> InitializeResult
func clientInitialized(_ initialized: InitializedNotification)
func shutdown()

// MARK: - Text synchronization

Expand Down
Loading