Skip to content

Commit 4ff14d2

Browse files
committed
[CodeCompletion] Extract CodeCompletionStringPrinter.h to its own file
1 parent 6e73caf commit 4ff14d2

File tree

5 files changed

+300
-217
lines changed

5 files changed

+300
-217
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//===--- CodeCompletionStringPrinter.h ------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_IDE_CODECOMPLETIONRESULTSTRINGPRINTER_H
14+
#define SWIFT_IDE_CODECOMPLETIONRESULTSTRINGPRINTER_H
15+
16+
#include "swift/AST/ASTPrinter.h"
17+
#include "swift/IDE/CodeCompletion.h"
18+
19+
namespace swift {
20+
namespace ide {
21+
22+
/// 'ASTPrinter' printing to 'CodeCompletionString' with appropriate ChunkKind.
23+
/// This is mainly used for printing types and override completions.
24+
class CodeCompletionStringPrinter : public ASTPrinter {
25+
protected:
26+
using ChunkKind = CodeCompletionString::Chunk::ChunkKind;
27+
28+
private:
29+
CodeCompletionResultBuilder &Builder;
30+
SmallString<16> Buffer;
31+
ChunkKind CurrChunkKind = ChunkKind::Text;
32+
ChunkKind NextChunkKind = ChunkKind::Text;
33+
SmallVector<PrintStructureKind, 2> StructureStack;
34+
unsigned int TypeDepth = 0;
35+
bool InPreamble = false;
36+
37+
bool isCurrentStructureKind(PrintStructureKind Kind) const {
38+
return !StructureStack.empty() && StructureStack.back() == Kind;
39+
}
40+
41+
bool isInType() const { return TypeDepth > 0; }
42+
43+
Optional<ChunkKind>
44+
getChunkKindForPrintNameContext(PrintNameContext context) const;
45+
46+
Optional<ChunkKind>
47+
getChunkKindForStructureKind(PrintStructureKind Kind) const;
48+
49+
void startNestedGroup(ChunkKind Kind);
50+
51+
void endNestedGroup();
52+
53+
protected:
54+
void setNextChunkKind(ChunkKind Kind) { NextChunkKind = Kind; }
55+
56+
public:
57+
CodeCompletionStringPrinter(CodeCompletionResultBuilder &Builder)
58+
: Builder(Builder) {}
59+
60+
~CodeCompletionStringPrinter() {
61+
// Flush the remainings.
62+
flush();
63+
}
64+
65+
void flush();
66+
67+
/// Start \c AttributeAndModifierListBegin group. This must be called before
68+
/// any attributes/modifiers printed to the output when printing an override
69+
/// compleion.
70+
void startPreamble();
71+
72+
/// End the current \c AttributeAndModifierListBegin group if it's still open.
73+
/// This is automatically called before the main part of the signature.
74+
void endPremable();
75+
76+
/// Implement \c ASTPrinter .
77+
public:
78+
void printText(StringRef Text) override;
79+
80+
void printTypeRef(
81+
Type T, const TypeDecl *TD, Identifier Name,
82+
PrintNameContext NameContext = PrintNameContext::Normal) override;
83+
84+
void printDeclLoc(const Decl *D) override;
85+
86+
void printDeclNameEndLoc(const Decl *D) override;
87+
88+
void printNamePre(PrintNameContext context) override;
89+
90+
void printNamePost(PrintNameContext context) override;
91+
92+
void printTypePre(const TypeLoc &TL) override;
93+
94+
void printTypePost(const TypeLoc &TL) override;
95+
96+
void printStructurePre(PrintStructureKind Kind, const Decl *D) override;
97+
98+
void printStructurePost(PrintStructureKind Kind, const Decl *D) override;
99+
};
100+
101+
} // end namespace ide
102+
} // end namespace swift
103+
104+
#endif // SWIFT_IDE_CODECOMPLETIONRESULTSTRINGPRINTER_H

lib/IDE/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_swift_host_library(swiftIDE STATIC
55
CodeCompletionDiagnostics.cpp
66
CodeCompletionResultPrinter.cpp
77
CodeCompletionResultType.cpp
8+
CodeCompletionStringPrinter.cpp
89
CommentConversion.cpp
910
CompileInstance.cpp
1011
CompletionInstance.cpp
@@ -23,7 +24,8 @@ add_swift_host_library(swiftIDE STATIC
2324
APIDigesterData.cpp
2425
SourceEntityWalker.cpp
2526
TypeContextInfo.cpp
26-
IDERequests.cpp)
27+
IDERequests.cpp
28+
)
2729
target_link_libraries(swiftIDE PRIVATE
2830
swiftAST
2931
swiftClangImporter

lib/IDE/CodeCompletion.cpp

Lines changed: 5 additions & 212 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
#include "swift/AST/ASTPrinter.h"
1818
#include "swift/AST/ASTWalker.h"
1919
#include "swift/AST/Comment.h"
20+
#include "swift/AST/GenericSignature.h"
2021
#include "swift/AST/ImportCache.h"
2122
#include "swift/AST/Initializer.h"
22-
#include "swift/AST/GenericSignature.h"
2323
#include "swift/AST/LazyResolver.h"
2424
#include "swift/AST/NameLookup.h"
2525
#include "swift/AST/ParameterList.h"
@@ -32,15 +32,16 @@
3232
#include "swift/ClangImporter/ClangImporter.h"
3333
#include "swift/ClangImporter/ClangModule.h"
3434
#include "swift/Frontend/FrontendOptions.h"
35+
#include "swift/IDE/CodeCompletionStringPrinter.h"
3536
#include "swift/IDE/CodeCompletionCache.h"
3637
#include "swift/IDE/CodeCompletionResultPrinter.h"
3738
#include "swift/IDE/Utils.h"
3839
#include "swift/Parse/CodeCompletionCallbacks.h"
39-
#include "swift/Sema/IDETypeChecking.h"
4040
#include "swift/Sema/CodeCompletionTypeChecking.h"
41-
#include "swift/Syntax/SyntaxKind.h"
41+
#include "swift/Sema/IDETypeChecking.h"
4242
#include "swift/Strings.h"
4343
#include "swift/Subsystems.h"
44+
#include "swift/Syntax/SyntaxKind.h"
4445
#include "clang/AST/ASTContext.h"
4546
#include "clang/AST/Attr.h"
4647
#include "clang/AST/Comment.h"
@@ -51,8 +52,8 @@
5152
#include "llvm/ADT/SmallString.h"
5253
#include "llvm/ADT/StringRef.h"
5354
#include "llvm/Support/Compiler.h"
54-
#include "llvm/Support/raw_ostream.h"
5555
#include "llvm/Support/SaveAndRestore.h"
56+
#include "llvm/Support/raw_ostream.h"
5657
#include <algorithm>
5758
#include <string>
5859

@@ -760,214 +761,6 @@ void CodeCompletionResultBuilder::setAssociatedDecl(const Decl *D) {
760761
}
761762
}
762763

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-
971764
void CodeCompletionResultBuilder::addCallArgument(
972765
Identifier Name, Identifier LocalName, Type Ty, Type ContextTy,
973766
bool IsVarArg, bool IsInOut, bool IsIUO, bool IsAutoClosure,

lib/IDE/CodeCompletionResultBuilder.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,14 @@ namespace clang {
2525
class Module;
2626
}
2727

28-
namespace {
29-
class CodeCompletionStringPrinter;
30-
}
31-
3228
namespace swift {
3329
class Decl;
3430
class ModuleDecl;
3531

3632
namespace ide {
3733

34+
class CodeCompletionStringPrinter;
35+
3836
class CodeCompletionResultBuilder {
3937
friend CodeCompletionStringPrinter;
4038

0 commit comments

Comments
 (0)