-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[CodeCompletion] Refactor how code completion results are returned to support cancellation #39631
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
ahoppen
merged 12 commits into
swiftlang:main
from
ahoppen:pr/cancel-completion-infrastructure
Nov 9, 2021
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2fcb24e
[CodeCompletion] Refactor how code completion results are returned to…
ahoppen b6e03e3
[CodeCompletion] Make sure callback is always called from performOper…
ahoppen ab257bb
[SourceKit] Move invocation of code completion second pass for TypeCo…
ahoppen 367c981
[SourceKit] Move invocation of code completion second pass for Confor…
ahoppen 163ccf9
[SourceKit] Move invocation of code completion second pass for code c…
ahoppen 70e3c99
[SourceKit] Remove performCompletionLikeOperation
ahoppen 4ee9b0d
[swift-ide-test] Use dedicated method for typeContextInfo on Completi…
ahoppen 974829e
[swift-ide-test] Use dedicated method for conformingMethodList on Com…
ahoppen 76f2dbe
[swift-ide-test] Use dedicated method for code completion on Completi…
ahoppen 6b66d58
[SourceKit] Make CompletionInstance::performOperation private
ahoppen 95ae8a4
[SourceKit] Make completion-like helper functions static
ahoppen c9f5331
[SourceKit] Pass CompletionContext by reference to CompletionInstance
ahoppen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| //===--- CancellableResult.h ------------------------------------*- C++ -*-===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2014 - 2021 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef SWIFT_IDE_CANCELLABLE_RESULT_H | ||
| #define SWIFT_IDE_CANCELLABLE_RESULT_H | ||
|
|
||
| #include <string> | ||
|
|
||
| namespace swift { | ||
|
|
||
| namespace ide { | ||
|
|
||
| enum class CancellableResultKind { Success, Failure, Cancelled }; | ||
|
|
||
| /// A result type that can carry one be in one of the following states: | ||
| /// - Success and carry a value of \c ResultType | ||
| /// - Failure and carry an error description | ||
| /// - Cancelled in case the operation that produced the result was cancelled | ||
| /// | ||
| /// Essentially this emulates an enum with associated values as follows | ||
| /// \code | ||
| /// enum CancellableResult<ResultType> { | ||
| /// case success(ResultType) | ||
| /// case failure(String) | ||
| /// case cancelled | ||
| /// } | ||
| /// \endcode | ||
| /// | ||
| /// The implementation is inspired by llvm::optional_detail::OptionalStorage | ||
| template <typename ResultType> | ||
| class CancellableResult { | ||
| CancellableResultKind Kind; | ||
| union { | ||
| /// If \c Kind == Success, carries the result. | ||
| ResultType Result; | ||
| /// If \c Kind == Error, carries the error description. | ||
| std::string Error; | ||
| /// If \c Kind == Cancelled, this union is not initialized. | ||
| char Empty; | ||
| }; | ||
|
|
||
| CancellableResult(ResultType Result) | ||
| : Kind(CancellableResultKind::Success), Result(Result) {} | ||
|
|
||
| CancellableResult(std::string Error) | ||
| : Kind(CancellableResultKind::Failure), Error(Error) {} | ||
|
|
||
| explicit CancellableResult() | ||
| : Kind(CancellableResultKind::Cancelled), Empty() {} | ||
|
|
||
| public: | ||
| CancellableResult(const CancellableResult &Other) : Kind(Other.Kind), Empty() { | ||
| switch (Kind) { | ||
| case CancellableResultKind::Success: | ||
| ::new ((void *)std::addressof(Result)) ResultType(Other.Result); | ||
| break; | ||
| case CancellableResultKind::Failure: | ||
| ::new ((void *)std::addressof(Error)) std::string(Other.Error); | ||
| break; | ||
| case CancellableResultKind::Cancelled: | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| CancellableResult(CancellableResult &&Other) : Kind(Other.Kind), Empty() { | ||
| switch (Kind) { | ||
| case CancellableResultKind::Success: | ||
| ::new ((void *)std::addressof(Result)) | ||
| ResultType(std::move(Other.Result)); | ||
| break; | ||
| case CancellableResultKind::Failure: | ||
| ::new ((void *)std::addressof(Error)) std::string(std::move(Other.Error)); | ||
| break; | ||
| case CancellableResultKind::Cancelled: | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| ~CancellableResult() { | ||
| using std::string; | ||
| switch (Kind) { | ||
| case CancellableResultKind::Success: | ||
| Result.~ResultType(); | ||
| break; | ||
| case CancellableResultKind::Failure: | ||
| Error.~string(); | ||
| break; | ||
| case CancellableResultKind::Cancelled: | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| /// Construct a \c CancellableResult that carries a successful result. | ||
| static CancellableResult success(ResultType Result) { | ||
| return std::move(CancellableResult(ResultType(Result))); | ||
| } | ||
|
|
||
| /// Construct a \c CancellableResult that carries the error message of a | ||
| /// failure. | ||
| static CancellableResult failure(std::string Error) { | ||
| return std::move(CancellableResult(Error)); | ||
| } | ||
|
|
||
| /// Construct a \c CancellableResult representing that the producing operation | ||
| /// was cancelled. | ||
| static CancellableResult cancelled() { | ||
| return std::move(CancellableResult()); | ||
| } | ||
|
|
||
| /// Return the result kind this \c CancellableResult represents: success, | ||
| /// failure or cancelled. | ||
| CancellableResultKind getKind() { return Kind; } | ||
|
|
||
| /// Assuming that the result represents success, return the underlying result | ||
| /// value. | ||
| ResultType &getResult() { | ||
| assert(getKind() == CancellableResultKind::Success); | ||
| return Result; | ||
| } | ||
|
|
||
| /// Assuming that the result represents success, retrieve members of the | ||
| /// underlying result value. | ||
| ResultType *operator->() { return &getResult(); } | ||
|
|
||
| /// Assuming that the result represents success, return the underlying result | ||
| /// value. | ||
| ResultType &operator*() { return getResult(); } | ||
|
|
||
| /// Assuming that the result represents a failure, return the error message. | ||
| std::string getError() { | ||
| assert(getKind() == CancellableResultKind::Failure); | ||
| return Error; | ||
| } | ||
|
|
||
| /// If the result represents success, invoke \p Transform to asynchronously | ||
| /// transform the wrapped result type and produce a new result type that is | ||
| /// provided by the callback function passed to \p Transform. Afterwards call | ||
| /// \p Handle with either the transformed value or the failure or cancelled | ||
| /// result. | ||
| /// The \c async part of the map means that the transform might happen | ||
| /// asyncronously. This function does not introduce asynchronicity by itself. | ||
| /// \p Transform might also invoke the callback synchronously. | ||
| template <typename NewResultType> | ||
| void | ||
| mapAsync(llvm::function_ref< | ||
| void(ResultType &, | ||
| llvm::function_ref<void(CancellableResult<NewResultType>)>)> | ||
| Transform, | ||
| llvm::function_ref<void(CancellableResult<NewResultType>)> Handle) { | ||
| switch (getKind()) { | ||
| case CancellableResultKind::Success: | ||
| Transform(getResult(), [&](CancellableResult<NewResultType> NewResult) { | ||
| Handle(NewResult); | ||
| }); | ||
| break; | ||
| case CancellableResultKind::Failure: | ||
| Handle(CancellableResult<NewResultType>::failure(getError())); | ||
| break; | ||
| case CancellableResultKind::Cancelled: | ||
| Handle(CancellableResult<NewResultType>::cancelled()); | ||
| break; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| } // namespace ide | ||
| } // namespace swift | ||
|
|
||
| #endif // SWIFT_IDE_CANCELLABLE_RESULT_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain why not
Result = Other.Result?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It’s something to do with deleted copy constructor. IIUC we don’t want to destruct the
Resultin the union because it’s just uninitialized memory and it’s copy contractor might be deleted. I copied the implementation fromllvm::Optionalhttps://github.com/apple/llvm-project/blob/e67e2a8d4c65aaa2fa0e41a32630ab5961e0851a/llvm/include/llvm/ADT/Optional.h#L112