|
| 1 | +//===--- SignatureHelpFormatter.cpp --- -------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 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 | +#include "swift/IDE/SignatureHelpFormatter.h" |
| 14 | +#include "CodeCompletionStringBuilder.h" |
| 15 | +#include "swift/AST/ParameterList.h" |
| 16 | +#include "swift/IDE/CommentConversion.h" |
| 17 | + |
| 18 | +using namespace swift; |
| 19 | +using namespace swift::ide; |
| 20 | + |
| 21 | +using ChunkKind = CodeCompletionString::Chunk::ChunkKind; |
| 22 | + |
| 23 | +/// \returns Array of parameters of \p VD accounting for implicitly curried |
| 24 | +/// instance methods. |
| 25 | +static ArrayRef<const ParamDecl *> |
| 26 | +getParameterArray(const ValueDecl *VD, bool IsImplicitlyCurried, |
| 27 | + const ParamDecl *&Scratch) { |
| 28 | + if (!VD) |
| 29 | + return {}; |
| 30 | + |
| 31 | + if (IsImplicitlyCurried) { |
| 32 | + auto *FD = dyn_cast<AbstractFunctionDecl>(VD); |
| 33 | + assert(FD && FD->hasImplicitSelfDecl()); |
| 34 | + |
| 35 | + Scratch = FD->getImplicitSelfDecl(); |
| 36 | + return ArrayRef(&Scratch, 1); |
| 37 | + } |
| 38 | + |
| 39 | + if (auto *ParamList = VD->getParameterList()) |
| 40 | + return ParamList->getArray(); |
| 41 | + |
| 42 | + return {}; |
| 43 | +} |
| 44 | + |
| 45 | +static StringRef copyAndClearString(llvm::BumpPtrAllocator &Allocator, |
| 46 | + SmallVectorImpl<char> &Str) { |
| 47 | + auto Ref = StringRef(Str.data(), Str.size()).copy(Allocator); |
| 48 | + Str.clear(); |
| 49 | + return Ref; |
| 50 | +} |
| 51 | + |
| 52 | +CodeCompletionString * |
| 53 | +SignatureHelpFormatter::createSignatureString(const ide::Signature &Signature, |
| 54 | + const DeclContext *DC) { |
| 55 | + ValueDecl *FD = Signature.FuncD; |
| 56 | + AnyFunctionType *AFT = Signature.FuncTy; |
| 57 | + |
| 58 | + GenericSignature GenericSig; |
| 59 | + if (FD) { |
| 60 | + if (auto *GC = FD->getAsGenericContext()) |
| 61 | + GenericSig = GC->getGenericSignature(); |
| 62 | + } |
| 63 | + |
| 64 | + CodeCompletionStringBuilder StringBuilder( |
| 65 | + Allocator, /*AnnotateResults=*/false, |
| 66 | + /*UnderscoreEmptyArgumentLabel=*/!Signature.IsSubscript, |
| 67 | + /*FullParameterFlags=*/true); |
| 68 | + |
| 69 | + DeclBaseName BaseName; |
| 70 | + |
| 71 | + if (!Signature.IsSecondApply && FD) { |
| 72 | + BaseName = FD->getBaseName(); |
| 73 | + } else if (Signature.IsSubscript) { |
| 74 | + BaseName = DeclBaseName::createSubscript(); |
| 75 | + } |
| 76 | + |
| 77 | + if (!BaseName.empty()) |
| 78 | + StringBuilder.addValueBaseName(BaseName, |
| 79 | + /*IsMember=*/bool(Signature.BaseType)); |
| 80 | + |
| 81 | + StringBuilder.addLeftParen(); |
| 82 | + |
| 83 | + const ParamDecl *ParamScratch; |
| 84 | + StringBuilder.addCallArgumentPatterns( |
| 85 | + AFT->getParams(), |
| 86 | + getParameterArray(FD, Signature.IsImplicitlyCurried, ParamScratch), DC, |
| 87 | + GenericSig, DefaultArgumentOutputMode::All, |
| 88 | + /*includeDefaultValues=*/true); |
| 89 | + |
| 90 | + StringBuilder.addRightParen(); |
| 91 | + |
| 92 | + if (!Signature.IsImplicitlyCurried) { |
| 93 | + if (Signature.IsSecondApply) { |
| 94 | + // For a second apply, we don't pass the declaration to avoid adding |
| 95 | + // incorrect rethrows and reasync which are only usable in a single apply. |
| 96 | + StringBuilder.addEffectsSpecifiers(AFT, /*AFD=*/nullptr); |
| 97 | + } else { |
| 98 | + StringBuilder.addEffectsSpecifiers( |
| 99 | + AFT, dyn_cast_or_null<AbstractFunctionDecl>(FD)); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if (FD && FD->isImplicitlyUnwrappedOptional()) { |
| 104 | + StringBuilder.addTypeAnnotationForImplicitlyUnwrappedOptional( |
| 105 | + AFT->getResult(), DC, GenericSig); |
| 106 | + } else { |
| 107 | + StringBuilder.addTypeAnnotation(AFT->getResult(), DC, GenericSig); |
| 108 | + } |
| 109 | + |
| 110 | + return StringBuilder.createCompletionString(); |
| 111 | +} |
| 112 | + |
| 113 | +FormattedSignatureHelp::Signature |
| 114 | +SignatureHelpFormatter::formatSignature(const DeclContext *DC, |
| 115 | + const ide::Signature &Signature) { |
| 116 | + auto *FD = Signature.FuncD; |
| 117 | + auto *AFT = Signature.FuncTy; |
| 118 | + |
| 119 | + bool IsConstructor = isa_and_nonnull<ConstructorDecl>(FD); |
| 120 | + |
| 121 | + auto *SignatureString = createSignatureString(Signature, DC); |
| 122 | + |
| 123 | + llvm::SmallString<512> SS; |
| 124 | + llvm::raw_svector_ostream OS(SS); |
| 125 | + |
| 126 | + bool SkipResult = AFT->getResult()->isVoid() || IsConstructor; |
| 127 | + |
| 128 | + SmallVector<FormattedSignatureHelp::Parameter, 8> FormattedParams; |
| 129 | + |
| 130 | + auto Chunks = SignatureString->getChunks(); |
| 131 | + auto C = Chunks.begin(); |
| 132 | + while (C != Chunks.end()) { |
| 133 | + if (C->is(ChunkKind::TypeAnnotation) && SkipResult) { |
| 134 | + ++C; |
| 135 | + continue; |
| 136 | + } |
| 137 | + |
| 138 | + if (C->is(ChunkKind::TypeAnnotation)) |
| 139 | + OS << " -> "; |
| 140 | + |
| 141 | + if (C->is(ChunkKind::CallArgumentBegin)) { |
| 142 | + unsigned NestingLevel = C->getNestingLevel(); |
| 143 | + ++C; |
| 144 | + |
| 145 | + auto &P = FormattedParams.emplace_back(); |
| 146 | + P.Offset = SS.size(); |
| 147 | + |
| 148 | + do { |
| 149 | + if (!C->is(ChunkKind::CallArgumentClosureType) && C->hasText()) |
| 150 | + OS << C->getText(); |
| 151 | + |
| 152 | + ++C; |
| 153 | + } while (C != Chunks.end() && !C->endsPreviousNestedGroup(NestingLevel)); |
| 154 | + |
| 155 | + P.Length = SS.size() - P.Offset; |
| 156 | + continue; |
| 157 | + } |
| 158 | + |
| 159 | + if (C->hasText()) |
| 160 | + OS << C->getText(); |
| 161 | + |
| 162 | + ++C; |
| 163 | + } |
| 164 | + |
| 165 | + StringRef SignatureText = copyAndClearString(Allocator, SS); |
| 166 | + |
| 167 | + // Parameter names. |
| 168 | + const ParamDecl *ParamScratch; |
| 169 | + auto ParamDecls = |
| 170 | + getParameterArray(FD, Signature.IsImplicitlyCurried, ParamScratch); |
| 171 | + |
| 172 | + if (!ParamDecls.empty()) { |
| 173 | + for (unsigned i = 0; i < FormattedParams.size(); ++i) { |
| 174 | + FormattedParams[i].Name = ParamDecls[i]->getParameterName().str(); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + // Documentation. |
| 179 | + StringRef DocComment; |
| 180 | + if (FD) { |
| 181 | + ide::getRawDocumentationComment(FD, OS); |
| 182 | + DocComment = copyAndClearString(Allocator, SS); |
| 183 | + } |
| 184 | + |
| 185 | + return FormattedSignatureHelp::Signature( |
| 186 | + SignatureText, DocComment, Signature.ParamIdx, |
| 187 | + ArrayRef(FormattedParams).copy(Allocator)); |
| 188 | +} |
| 189 | + |
| 190 | +FormattedSignatureHelp |
| 191 | +SignatureHelpFormatter::format(SignatureHelpResult Result) { |
| 192 | + SmallVector<FormattedSignatureHelp::Signature, 8> FormattedSignatures; |
| 193 | + FormattedSignatures.reserve(Result.Signatures.size()); |
| 194 | + |
| 195 | + for (auto &Signature : Result.Signatures) { |
| 196 | + FormattedSignatures.push_back(formatSignature(Result.DC, Signature)); |
| 197 | + } |
| 198 | + |
| 199 | + // FIXME: Ideally we should select an active signature based on the context. |
| 200 | + unsigned ActiveSignature = 0; |
| 201 | + |
| 202 | + return FormattedSignatureHelp(ArrayRef(FormattedSignatures).copy(Allocator), |
| 203 | + ActiveSignature); |
| 204 | +} |
0 commit comments