-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[SourceKit] ⚡️Fast code completion within function bodies #28727
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
rintaro
merged 18 commits into
swiftlang:master
from
rintaro:sourcekit-completion-reuseinstance
Dec 19, 2019
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f51f3b8
[SourceKit] Support VFS in 'typecontextinfo' and 'conformingmethods' req
rintaro cd284d1
[CodeCompletion] Align swiftCodeCompleteImpl() with other similar funcs
rintaro ff97c06
[CodeCompletion] Use offsets in the buffer for second pass state
rintaro 62c4412
[SourceKit] Reuse compiler instance between multiple completion
rintaro c1530ee
[SourceKit] Add option to enable ASTContext reusing for code completion
rintaro a83c7e8
[SourceKit] Calculate CompilerInvocation hash
rintaro fcc7e41
[SourceKit] Add a test case for fast code completion
rintaro c3d5828
[SourceKit] Limit compiler instance reuse count for completions
rintaro 2160134
[SourceKit] Add test file for fast completion
rintaro 204ae49
[CodeCompletion] Add doc-comment for CompletionInstance
rintaro f2466de
[CodeCompletion] Rename getReusingCompilerInstance()
rintaro c5b1ada
[SourceKit] Hold CompletionInstance with std::shared_ptr
rintaro 0f45267
[CodeCompletion] Use the arguments to check the equality of the invoc…
rintaro fcb50d6
[SourceKit] Add more test cases for fast completions
rintaro 044477e
[SourceKit/CodeCompletion] Use callback function to run the second pass
rintaro fe07d44
[CodeCompletion] Pack cached instance information into single struct
rintaro 2aec5d4
[CodeCompletionn] Block completions in other threads
rintaro eebcbf6
[SourceKit] Pass 'EnableASTCaching' flag as an argument
rintaro 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
//===--- CompletionInstance.h ---------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2019 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_COMPLETIONINSTANCE_H | ||
#define SWIFT_IDE_COMPLETIONINSTANCE_H | ||
|
||
#include "swift/Frontend/Frontend.h" | ||
#include "llvm/ADT/Hashing.h" | ||
#include "llvm/ADT/IntrusiveRefCntPtr.h" | ||
#include "llvm/ADT/SmallString.h" | ||
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/Support/MemoryBuffer.h" | ||
#include "llvm/Support/VirtualFileSystem.h" | ||
|
||
namespace swift { | ||
|
||
class CompilerInstance; | ||
class CompilerInvocation; | ||
class DiagnosticConsumer; | ||
|
||
namespace ide { | ||
|
||
/// Copy a memory buffer inserting '\0' at the position of \c origBuf. | ||
std::unique_ptr<llvm::MemoryBuffer> | ||
makeCodeCompletionMemoryBuffer(const llvm::MemoryBuffer *origBuf, | ||
unsigned &Offset, | ||
llvm::StringRef bufferIdentifier); | ||
|
||
/// Manages \c CompilerInstance for completion like operations. | ||
class CompletionInstance { | ||
unsigned MaxASTReuseCount = 100; | ||
|
||
std::mutex mtx; | ||
|
||
std::unique_ptr<CompilerInstance> CachedCI; | ||
llvm::hash_code CachedArgHash; | ||
unsigned CachedReuseCount = 0; | ||
|
||
/// Calls \p Callback with cached \c CompilerInstance if it's usable for the | ||
/// specified completion request. | ||
/// Returns \c if the callback was called. Returns \c false if the compiler | ||
/// argument has changed, primary file is not the same, the \c Offset is not | ||
/// in function bodies, or the interface hash of the file has changed. | ||
bool performCachedOperaitonIfPossible( | ||
const swift::CompilerInvocation &Invocation, llvm::hash_code ArgsHash, | ||
llvm::MemoryBuffer *completionBuffer, unsigned int Offset, | ||
DiagnosticConsumer *DiagC, | ||
llvm::function_ref<void(CompilerInstance &)> Callback); | ||
|
||
/// Calls \p Callback with new \c CompilerInstance for the completion | ||
/// request. The \c CompilerInstace passed to the callback already performed | ||
/// the first pass. | ||
/// Returns \c false if it fails to setup the \c CompilerInstance. | ||
bool performNewOperation( | ||
llvm::Optional<llvm::hash_code> ArgsHash, | ||
swift::CompilerInvocation &Invocation, | ||
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem, | ||
llvm::MemoryBuffer *completionBuffer, unsigned int Offset, | ||
std::string &Error, DiagnosticConsumer *DiagC, | ||
llvm::function_ref<void(CompilerInstance &)> Callback); | ||
|
||
public: | ||
/// Calls \p Callback with a \c CompilerInstance which is prepared for the | ||
/// second pass. \p Callback is resposible to perform the second pass on it. | ||
/// The \c CompilerInstance may be reused from the previous completions, | ||
/// and may be cached for the next completion. | ||
/// Return \c true if \p is successfully called, \c it fails. In failure | ||
/// cases \p Error is populated with an error message. | ||
/// | ||
/// NOTE: \p Args is only used for checking the equaity of the invocation. | ||
/// Since this function assumes that it is already normalized, exact the same | ||
/// arguments including their order is considered as the same invocation. | ||
bool performOperation( | ||
swift::CompilerInvocation &Invocation, llvm::ArrayRef<const char *> Args, | ||
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem, | ||
llvm::MemoryBuffer *completionBuffer, unsigned int Offset, | ||
bool EnableASTCaching, std::string &Error, DiagnosticConsumer *DiagC, | ||
llvm::function_ref<void(CompilerInstance &)> Callback); | ||
}; | ||
|
||
} // namespace ide | ||
} // namespace swift | ||
|
||
#endif // SWIFT_IDE_COMPLETIONINSTANCE_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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.