diff options
author | Ivan Donchevskii <ivan.donchevskii@qt.io> | 2018-05-17 11:50:03 +0200 |
---|---|---|
committer | Ivan Donchevskii <ivan.donchevskii@qt.io> | 2018-06-14 06:14:52 +0000 |
commit | e775457c4ede090c31de56fe20e5c59b6fe46b95 (patch) | |
tree | bb0158197bc7651e87b1beabbd6ea942d41bc992 /include | |
parent | 74dff2b4165421e67427db09f02d6a52ae1fa96d (diff) |
[backported/clang-7][libclang] Optionally add code completion results for arrow instead of dot
--------------------------------------------------------------------------
* https://reviews.llvm.org/D46862
--------------------------------------------------------------------------
Follow up for https://reviews.llvm.org/D41537 -
libclang part is extracted into this review
Change-Id: Ib166eb7b8675be605c81330df0c5f342942815ce
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
Diffstat (limited to 'include')
-rw-r--r-- | include/clang-c/Index.h | 87 | ||||
-rw-r--r-- | include/clang/Basic/SourceManager.h | 12 | ||||
-rw-r--r-- | include/clang/Sema/CodeCompleteConsumer.h | 50 |
3 files changed, 124 insertions, 25 deletions
diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h index d06c82a4db..41534f68e6 100644 --- a/include/clang-c/Index.h +++ b/include/clang-c/Index.h @@ -36,6 +36,7 @@ #define CINDEX_VERSION_HAS_ISINVALIDECL_BACKPORTED #define CINDEX_VERSION_HAS_PRETTYDECL_BACKPORTED #define CINDEX_VERSION_HAS_LIMITSKIPFUNCTIONBODIESTOPREAMBLE_BACKPORTED +#define CINDEX_VERSION_HAS_COMPLETION_FIXITS_BACKPORTED #define CINDEX_VERSION_ENCODE(major, minor) ( \ ((major) * 10000) \ @@ -4738,6 +4739,20 @@ typedef struct { } CXToken; /** + * \brief Get the raw lexical token starting with the given location. + * + * \param TU the translation unit whose text is being tokenized. + * + * \param Location the source location with which the token starts. + * + * \returns The token starting with the given location or NULL if no such tokens + * exist. The returned pointer must be freed with clang_disposeTokens before the + * translation unit is destroyed. + */ +CINDEX_LINKAGE CXToken *clang_getToken(CXTranslationUnit TU, + CXSourceLocation Location); + +/** * \brief Determine the kind of the given token. */ CINDEX_LINKAGE CXTokenKind clang_getTokenKind(CXToken); @@ -5233,6 +5248,70 @@ typedef struct { } CXCodeCompleteResults; /** + * \brief Retrieve the number of fix-its for the given completion string. + * + * Calling this makes sense only if CXCodeComplete_IncludeCompletionsWithFixIts + * option was set. + * + * \param results The structure keeping all completion results + * + * \param completion_index The index of the completion + * + * \return The number of fix-its which must be applied before the completion at + * completion_index can be applied + */ +CINDEX_LINKAGE unsigned +clang_getCompletionNumFixIts(CXCodeCompleteResults *results, + unsigned completion_index); + +/** + * \brief Fix-its that *must* be applied before inserting the text for the + * corresponding completion. + * + * By default, clang_codeCompleteAt() only returns completions with empty + * fix-its. Extra completions with non-empty fix-its should be explicitly + * requested by setting CXCodeComplete_IncludeCompletionsWithFixIts. + * + * For the clients to be able to compute position of the cursor after applying + * fix-its, the following conditions are guaranteed to hold for + * replacement_range of the stored fix-its: + * - Ranges in the fix-its are guaranteed to never contain the completion + * point (or identifier under completion point, if any) inside them, except + * at the start or at the end of the range. + * - If a fix-it range starts or ends with completion point (or starts or + * ends after the identifier under completion point), it will contain at + * least one character. It allows to unambiguously recompute completion + * point after applying the fix-it. + * + * The intuition is that provided fix-its change code around the identifier we + * complete, but are not allowed to touch the identifier itself or the + * completion point. One example of completions with corrections are the ones + * replacing '.' with '->' and vice versa: + * + * std::unique_ptr<std::vector<int>> vec_ptr; + * In 'vec_ptr.^', one of the completions is 'push_back', it requires + * replacing '.' with '->'. + * In 'vec_ptr->^', one of the completions is 'release', it requires + * replacing '->' with '.'. + * + * \param results The structure keeping all completion results + * + * \param completion_index The index of the completion + * + * \param fixit_index The index of the fix-it for the completion at + * completion_index + * + * \param replacement_range The fix-it range that must be replaced before the + * completion at completion_index can be applied + * + * \returns The fix-it string that must replace the code at replacement_range + * before the completion at completion_index can be applied + */ +CINDEX_LINKAGE CXString clang_getCompletionFixIt( + CXCodeCompleteResults *results, unsigned completion_index, + unsigned fixit_index, CXSourceRange *replacement_range); + +/** * \brief Flags that can be passed to \c clang_codeCompleteAt() to * modify its behavior. * @@ -5256,7 +5335,13 @@ enum CXCodeComplete_Flags { * \brief Whether to include brief documentation within the set of code * completions returned. */ - CXCodeComplete_IncludeBriefComments = 0x04 + CXCodeComplete_IncludeBriefComments = 0x04, + + /** + * \brief Whether to include completions with small + * fix-its, e.g. change '.' to '->' on member access, etc. + */ + CXCodeComplete_IncludeCompletionsWithFixIts = 0x10 }; /** diff --git a/include/clang/Basic/SourceManager.h b/include/clang/Basic/SourceManager.h index 397ad2e77f..80e277384c 100644 --- a/include/clang/Basic/SourceManager.h +++ b/include/clang/Basic/SourceManager.h @@ -1137,6 +1137,18 @@ public: /// be used by clients. SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const; + /// Form a SourceLocation from a FileID and Offset pair. + SourceLocation getComposedLoc(FileID FID, unsigned Offset) const { + bool Invalid = false; + const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); + if (Invalid) + return SourceLocation(); + + unsigned GlobalOffset = Entry.getOffset() + Offset; + return Entry.isFile() ? SourceLocation::getFileLoc(GlobalOffset) + : SourceLocation::getMacroLoc(GlobalOffset); + } + /// \brief Decompose the specified location into a raw FileID + Offset pair. /// /// The first element is the FileID, the second is the offset from the diff --git a/include/clang/Sema/CodeCompleteConsumer.h b/include/clang/Sema/CodeCompleteConsumer.h index 720a150f51..002f93ade2 100644 --- a/include/clang/Sema/CodeCompleteConsumer.h +++ b/include/clang/Sema/CodeCompleteConsumer.h @@ -680,30 +680,33 @@ public: /// \brief The availability of this result. CXAvailabilityKind Availability; - /// \brief FixIts that *must* be applied before inserting the text for the - /// corresponding completion item. + ///\brief Fix-its that *must* be applied before inserting the text for the + /// corresponding completion. /// - /// Completion items with non-empty fixits will not be returned by default, - /// they should be explicitly requested by setting - /// CompletionOptions::IncludeFixIts. For the editors to be able to - /// compute position of the cursor for the completion item itself, the - /// following conditions are guaranteed to hold for RemoveRange of the stored - /// fixits: - /// - Ranges in the fixits are guaranteed to never contain the completion + /// By default, CodeCompletionBuilder only returns completions with empty + /// fix-its. Extra completions with non-empty fix-its should be explicitly + /// requested by setting CompletionOptions::IncludeFixIts. + /// + /// For the clients to be able to compute position of the cursor after + /// applying fix-its, the following conditions are guaranteed to hold for + /// RemoveRange of the stored fix-its: + /// - Ranges in the fix-its are guaranteed to never contain the completion /// point (or identifier under completion point, if any) inside them, except /// at the start or at the end of the range. - /// - If a fixit range starts or ends with completion point (or starts or + /// - If a fix-it range starts or ends with completion point (or starts or /// ends after the identifier under completion point), it will contain at /// least one character. It allows to unambiguously recompute completion - /// point after applying the fixit. - /// The intuition is that provided fixits change code around the identifier we - /// complete, but are not allowed to touch the identifier itself or the - /// completion point. One example of completion items with corrections are the - /// ones replacing '.' with '->' and vice versa: + /// point after applying the fix-it. + /// + /// The intuition is that provided fix-its change code around the identifier + /// we complete, but are not allowed to touch the identifier itself or the + /// completion point. One example of completions with corrections are the ones + /// replacing '.' with '->' and vice versa: + /// /// std::unique_ptr<std::vector<int>> vec_ptr; - /// In 'vec_ptr.^', one of completion items is 'push_back', it requires + /// In 'vec_ptr.^', one of the completions is 'push_back', it requires /// replacing '.' with '->'. - /// In 'vec_ptr->^', one of completion items is 'release', it requires + /// In 'vec_ptr->^', one of the completions is 'release', it requires /// replacing '->' with '.'. std::vector<FixItHint> FixIts; @@ -736,13 +739,12 @@ public: bool QualifierIsInformative = false, bool Accessible = true, std::vector<FixItHint> FixIts = std::vector<FixItHint>()) - : Declaration(Declaration), Priority(Priority), - StartParameter(0), Kind(RK_Declaration), - Availability(CXAvailability_Available), Hidden(false), - QualifierIsInformative(QualifierIsInformative), - StartsNestedNameSpecifier(false), AllParametersAreInformative(false), - DeclaringEntity(false), Qualifier(Qualifier), - FixIts(std::move(FixIts)) { + : Declaration(Declaration), Priority(Priority), StartParameter(0), + Kind(RK_Declaration), Availability(CXAvailability_Available), + FixIts(std::move(FixIts)), Hidden(false), + QualifierIsInformative(QualifierIsInformative), + StartsNestedNameSpecifier(false), AllParametersAreInformative(false), + DeclaringEntity(false), Qualifier(Qualifier) { //FIXME: Add assert to check FixIts range requirements. computeCursorKindAndAvailability(Accessible); } |