|
17 | 17 | #include "swift/AST/ASTPrinter.h"
|
18 | 18 | #include "swift/AST/ASTWalker.h"
|
19 | 19 | #include "swift/AST/Comment.h"
|
| 20 | +#include "swift/AST/GenericSignature.h" |
20 | 21 | #include "swift/AST/ImportCache.h"
|
21 | 22 | #include "swift/AST/Initializer.h"
|
22 |
| -#include "swift/AST/GenericSignature.h" |
23 | 23 | #include "swift/AST/LazyResolver.h"
|
24 | 24 | #include "swift/AST/NameLookup.h"
|
25 | 25 | #include "swift/AST/ParameterList.h"
|
|
32 | 32 | #include "swift/ClangImporter/ClangImporter.h"
|
33 | 33 | #include "swift/ClangImporter/ClangModule.h"
|
34 | 34 | #include "swift/Frontend/FrontendOptions.h"
|
| 35 | +#include "swift/IDE/CodeCompletionStringPrinter.h" |
35 | 36 | #include "swift/IDE/CodeCompletionCache.h"
|
36 | 37 | #include "swift/IDE/CodeCompletionResultPrinter.h"
|
37 | 38 | #include "swift/IDE/Utils.h"
|
38 | 39 | #include "swift/Parse/CodeCompletionCallbacks.h"
|
39 |
| -#include "swift/Sema/IDETypeChecking.h" |
40 | 40 | #include "swift/Sema/CodeCompletionTypeChecking.h"
|
41 |
| -#include "swift/Syntax/SyntaxKind.h" |
| 41 | +#include "swift/Sema/IDETypeChecking.h" |
42 | 42 | #include "swift/Strings.h"
|
43 | 43 | #include "swift/Subsystems.h"
|
| 44 | +#include "swift/Syntax/SyntaxKind.h" |
44 | 45 | #include "clang/AST/ASTContext.h"
|
45 | 46 | #include "clang/AST/Attr.h"
|
46 | 47 | #include "clang/AST/Comment.h"
|
|
51 | 52 | #include "llvm/ADT/SmallString.h"
|
52 | 53 | #include "llvm/ADT/StringRef.h"
|
53 | 54 | #include "llvm/Support/Compiler.h"
|
54 |
| -#include "llvm/Support/raw_ostream.h" |
55 | 55 | #include "llvm/Support/SaveAndRestore.h"
|
| 56 | +#include "llvm/Support/raw_ostream.h" |
56 | 57 | #include <algorithm>
|
57 | 58 | #include <string>
|
58 | 59 |
|
@@ -760,214 +761,6 @@ void CodeCompletionResultBuilder::setAssociatedDecl(const Decl *D) {
|
760 | 761 | }
|
761 | 762 | }
|
762 | 763 |
|
763 |
| -namespace { |
764 |
| - |
765 |
| -/// 'ASTPrinter' printing to 'CodeCompletionString' with appropriate ChunkKind. |
766 |
| -/// This is mainly used for printing types and override completions. |
767 |
| -class CodeCompletionStringPrinter : public ASTPrinter { |
768 |
| -protected: |
769 |
| - using ChunkKind = CodeCompletionString::Chunk::ChunkKind; |
770 |
| - |
771 |
| -private: |
772 |
| - CodeCompletionResultBuilder &Builder; |
773 |
| - SmallString<16> Buffer; |
774 |
| - ChunkKind CurrChunkKind = ChunkKind::Text; |
775 |
| - ChunkKind NextChunkKind = ChunkKind::Text; |
776 |
| - SmallVector<PrintStructureKind, 2> StructureStack; |
777 |
| - unsigned int TypeDepth = 0; |
778 |
| - bool InPreamble = false; |
779 |
| - |
780 |
| - bool isCurrentStructureKind(PrintStructureKind Kind) const { |
781 |
| - return !StructureStack.empty() && StructureStack.back() == Kind; |
782 |
| - } |
783 |
| - |
784 |
| - bool isInType() const { |
785 |
| - return TypeDepth > 0; |
786 |
| - } |
787 |
| - |
788 |
| - Optional<ChunkKind> |
789 |
| - getChunkKindForPrintNameContext(PrintNameContext context) const { |
790 |
| - switch (context) { |
791 |
| - case PrintNameContext::Keyword: |
792 |
| - if(isCurrentStructureKind(PrintStructureKind::EffectsSpecifiers)) { |
793 |
| - return ChunkKind::EffectsSpecifierKeyword; |
794 |
| - } |
795 |
| - return ChunkKind::Keyword; |
796 |
| - case PrintNameContext::IntroducerKeyword: |
797 |
| - return ChunkKind::DeclIntroducer; |
798 |
| - case PrintNameContext::Attribute: |
799 |
| - return ChunkKind::Attribute; |
800 |
| - case PrintNameContext::FunctionParameterExternal: |
801 |
| - if (isInType()) { |
802 |
| - return None; |
803 |
| - } |
804 |
| - return ChunkKind::ParameterDeclExternalName; |
805 |
| - case PrintNameContext::FunctionParameterLocal: |
806 |
| - if (isInType()) { |
807 |
| - return None; |
808 |
| - } |
809 |
| - return ChunkKind::ParameterDeclLocalName; |
810 |
| - default: |
811 |
| - return None; |
812 |
| - } |
813 |
| - } |
814 |
| - |
815 |
| - Optional<ChunkKind> |
816 |
| - getChunkKindForStructureKind(PrintStructureKind Kind) const { |
817 |
| - switch (Kind) { |
818 |
| - case PrintStructureKind::FunctionParameter: |
819 |
| - if (isInType()) { |
820 |
| - return None; |
821 |
| - } |
822 |
| - return ChunkKind::ParameterDeclBegin; |
823 |
| - case PrintStructureKind::DefaultArgumentClause: |
824 |
| - return ChunkKind::DefaultArgumentClauseBegin; |
825 |
| - case PrintStructureKind::DeclGenericParameterClause: |
826 |
| - return ChunkKind::GenericParameterClauseBegin; |
827 |
| - case PrintStructureKind::DeclGenericRequirementClause: |
828 |
| - return ChunkKind::GenericRequirementClauseBegin; |
829 |
| - case PrintStructureKind::EffectsSpecifiers: |
830 |
| - return ChunkKind::EffectsSpecifierClauseBegin; |
831 |
| - case PrintStructureKind::DeclResultTypeClause: |
832 |
| - return ChunkKind::DeclResultTypeClauseBegin; |
833 |
| - case PrintStructureKind::FunctionParameterType: |
834 |
| - return ChunkKind::ParameterDeclTypeBegin; |
835 |
| - default: |
836 |
| - return None; |
837 |
| - } |
838 |
| - } |
839 |
| - |
840 |
| - void startNestedGroup(ChunkKind Kind) { |
841 |
| - flush(); |
842 |
| - Builder.CurrentNestingLevel++; |
843 |
| - Builder.addSimpleChunk(Kind); |
844 |
| - } |
845 |
| - |
846 |
| - void endNestedGroup() { |
847 |
| - flush(); |
848 |
| - Builder.CurrentNestingLevel--; |
849 |
| - } |
850 |
| - |
851 |
| -protected: |
852 |
| - void setNextChunkKind(ChunkKind Kind) { |
853 |
| - NextChunkKind = Kind; |
854 |
| - } |
855 |
| - |
856 |
| -public: |
857 |
| - CodeCompletionStringPrinter(CodeCompletionResultBuilder &Builder) : Builder(Builder) {} |
858 |
| - |
859 |
| - ~CodeCompletionStringPrinter() { |
860 |
| - // Flush the remainings. |
861 |
| - flush(); |
862 |
| - } |
863 |
| - |
864 |
| - void flush() { |
865 |
| - if (Buffer.empty()) |
866 |
| - return; |
867 |
| - Builder.addChunkWithText(CurrChunkKind, Buffer); |
868 |
| - Buffer.clear(); |
869 |
| - } |
870 |
| - |
871 |
| - /// Start \c AttributeAndModifierListBegin group. This must be called before |
872 |
| - /// any attributes/modifiers printed to the output when printing an override |
873 |
| - /// compleion. |
874 |
| - void startPreamble() { |
875 |
| - assert(!InPreamble); |
876 |
| - startNestedGroup(ChunkKind::AttributeAndModifierListBegin); |
877 |
| - InPreamble = true; |
878 |
| - } |
879 |
| - |
880 |
| - /// End the current \c AttributeAndModifierListBegin group if it's still open. |
881 |
| - /// This is automatically called before the main part of the signature. |
882 |
| - void endPremable() { |
883 |
| - if (!InPreamble) |
884 |
| - return; |
885 |
| - InPreamble = false; |
886 |
| - endNestedGroup(); |
887 |
| - } |
888 |
| - |
889 |
| - /// Implement \c ASTPrinter . |
890 |
| -public: |
891 |
| - void printText(StringRef Text) override { |
892 |
| - // Detect ': ' and ', ' in parameter clauses. |
893 |
| - // FIXME: Is there a better way? |
894 |
| - if (isCurrentStructureKind(PrintStructureKind::FunctionParameter) && |
895 |
| - Text == ": ") { |
896 |
| - setNextChunkKind(ChunkKind::ParameterDeclColon); |
897 |
| - } else if ( |
898 |
| - isCurrentStructureKind(PrintStructureKind::FunctionParameterList) && |
899 |
| - Text == ", ") { |
900 |
| - setNextChunkKind(ChunkKind::Comma); |
901 |
| - } |
902 |
| - |
903 |
| - if (CurrChunkKind != NextChunkKind) { |
904 |
| - // If the next desired kind is different from the current buffer, flush |
905 |
| - // the current buffer. |
906 |
| - flush(); |
907 |
| - CurrChunkKind = NextChunkKind; |
908 |
| - } |
909 |
| - Buffer.append(Text); |
910 |
| - } |
911 |
| - |
912 |
| - void printTypeRef( |
913 |
| - Type T, const TypeDecl *TD, Identifier Name, |
914 |
| - PrintNameContext NameContext = PrintNameContext::Normal) override { |
915 |
| - |
916 |
| - NextChunkKind = TD->getModuleContext()->isSystemModule() |
917 |
| - ? ChunkKind::TypeIdSystem |
918 |
| - : ChunkKind::TypeIdUser; |
919 |
| - |
920 |
| - ASTPrinter::printTypeRef(T, TD, Name, NameContext); |
921 |
| - NextChunkKind = ChunkKind::Text; |
922 |
| - } |
923 |
| - |
924 |
| - void printDeclLoc(const Decl *D) override { |
925 |
| - endPremable(); |
926 |
| - setNextChunkKind(ChunkKind::BaseName); |
927 |
| - } |
928 |
| - |
929 |
| - void printDeclNameEndLoc(const Decl *D) override { |
930 |
| - setNextChunkKind(ChunkKind::Text); |
931 |
| - } |
932 |
| - |
933 |
| - void printNamePre(PrintNameContext context) override { |
934 |
| - if (context == PrintNameContext::IntroducerKeyword) |
935 |
| - endPremable(); |
936 |
| - if (auto Kind = getChunkKindForPrintNameContext(context)) |
937 |
| - setNextChunkKind(*Kind); |
938 |
| - } |
939 |
| - |
940 |
| - void printNamePost(PrintNameContext context) override { |
941 |
| - if (getChunkKindForPrintNameContext(context)) |
942 |
| - setNextChunkKind(ChunkKind::Text); |
943 |
| - } |
944 |
| - |
945 |
| - void printTypePre(const TypeLoc &TL) override { |
946 |
| - ++TypeDepth; |
947 |
| - } |
948 |
| - |
949 |
| - void printTypePost(const TypeLoc &TL) override { |
950 |
| - assert(TypeDepth > 0); |
951 |
| - --TypeDepth; |
952 |
| - } |
953 |
| - |
954 |
| - void printStructurePre(PrintStructureKind Kind, const Decl *D) override { |
955 |
| - StructureStack.push_back(Kind); |
956 |
| - |
957 |
| - if (auto chunkKind = getChunkKindForStructureKind(Kind)) |
958 |
| - startNestedGroup(*chunkKind); |
959 |
| - } |
960 |
| - |
961 |
| - void printStructurePost(PrintStructureKind Kind, const Decl *D) override { |
962 |
| - if (getChunkKindForStructureKind(Kind)) |
963 |
| - endNestedGroup(); |
964 |
| - |
965 |
| - assert(Kind == StructureStack.back()); |
966 |
| - StructureStack.pop_back(); |
967 |
| - } |
968 |
| -}; |
969 |
| -} // namespcae |
970 |
| - |
971 | 764 | void CodeCompletionResultBuilder::addCallArgument(
|
972 | 765 | Identifier Name, Identifier LocalName, Type Ty, Type ContextTy,
|
973 | 766 | bool IsVarArg, bool IsInOut, bool IsIUO, bool IsAutoClosure,
|
|
0 commit comments