1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
|
//===--- CodeCompletionResultBuilder.h - Build completion results ---------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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_LIB_IDE_CODE_COMPLETION_RESULT_BUILDER_H
#define SWIFT_LIB_IDE_CODE_COMPLETION_RESULT_BUILDER_H
#include "swift/AST/Types.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/StringExtras.h"
#include "swift/IDE/CodeCompletionResult.h"
#include "swift/IDE/CodeCompletionResultSink.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
namespace clang {
class Module;
}
namespace swift {
class Decl;
class ModuleDecl;
namespace ide {
class CodeCompletionStringPrinter;
class CodeCompletionResultBuilder {
friend CodeCompletionStringPrinter;
CodeCompletionResultSink &Sink;
CodeCompletionResultKind Kind;
SemanticContextKind SemanticContext;
CodeCompletionFlair Flair;
unsigned NumBytesToErase = 0;
const Decl *AssociatedDecl = nullptr;
bool HasAsyncAlternative = false;
std::optional<CodeCompletionLiteralKind> LiteralKind;
CodeCompletionKeywordKind KeywordKind = CodeCompletionKeywordKind::None;
unsigned CurrentNestingLevel = 0;
SmallVector<CodeCompletionString::Chunk, 4> Chunks;
llvm::PointerUnion<const ModuleDecl *, const clang::Module *>
CurrentModule;
bool Cancelled = false;
ContextFreeNotRecommendedReason ContextFreeNotRecReason =
ContextFreeNotRecommendedReason::None;
ContextualNotRecommendedReason ContextualNotRecReason =
ContextualNotRecommendedReason::None;
StringRef BriefDocComment;
/// The result type that this completion item produces.
CodeCompletionResultType ResultType = CodeCompletionResultType::unknown();
/// The context in which this completion item is used. Used to compute the
/// type relation to \c ResultType.
const ExpectedTypeContext *TypeContext = nullptr;
const DeclContext *DC = nullptr;
bool CanCurrDeclContextHandleAsync = false;
void addChunkWithText(CodeCompletionString::Chunk::ChunkKind Kind,
StringRef Text);
void addChunkWithTextNoCopy(CodeCompletionString::Chunk::ChunkKind Kind,
StringRef Text) {
Chunks.push_back(CodeCompletionString::Chunk::createWithText(
Kind, CurrentNestingLevel, Text));
}
void addSimpleChunk(CodeCompletionString::Chunk::ChunkKind Kind) {
Chunks.push_back(
CodeCompletionString::Chunk::createSimple(Kind,
CurrentNestingLevel));
}
CodeCompletionString::Chunk &getLastChunk() {
return Chunks.back();
}
CodeCompletionResult *takeResult();
void finishResult();
public:
CodeCompletionResultBuilder(CodeCompletionResultSink &Sink,
CodeCompletionResultKind Kind,
SemanticContextKind SemanticContext)
: Sink(Sink), Kind(Kind), SemanticContext(SemanticContext) {}
~CodeCompletionResultBuilder() {
finishResult();
}
void cancel() {
Cancelled = true;
}
/// Annotated results are requested by the client.
///
/// This affects the structure of the CodeCompletionString.
bool shouldAnnotateResults() {
return Sink.annotateResult;
}
void setNumBytesToErase(unsigned N) {
NumBytesToErase = N;
}
void setAssociatedDecl(const Decl *D);
void setHasAsyncAlternative(bool HasAsyncAlternative) {
this->HasAsyncAlternative = HasAsyncAlternative;
}
void setLiteralKind(CodeCompletionLiteralKind kind) { LiteralKind = kind; }
void setKeywordKind(CodeCompletionKeywordKind kind) { KeywordKind = kind; }
void setContextFreeNotRecommended(ContextFreeNotRecommendedReason Reason) {
ContextFreeNotRecReason = Reason;
}
void setContextualNotRecommended(ContextualNotRecommendedReason Reason) {
ContextualNotRecReason = Reason;
}
void addFlair(CodeCompletionFlair Options) {
Flair |= Options;
}
/// Indicate that the code completion item does not produce something with a
/// sensible result type, like a keyword or a method override suggestion.
void setResultTypeNotApplicable() {
ResultType = CodeCompletionResultType::notApplicable();
}
/// Set the result type of this code completion item and the context that the
/// item may be used in.
/// This is not a single unique type because for code completion we consider
/// e.g. \c Int as producing both an \c Int metatype and an \c Int instance
/// type.
void setResultTypes(ArrayRef<Type> ResultTypes) {
this->ResultType = CodeCompletionResultType(ResultTypes);
}
/// Set context in which this code completion item occurs. Used to compute the
/// item's type relation.
void setTypeContext(const ExpectedTypeContext &TypeContext,
const DeclContext *DC) {
this->TypeContext = &TypeContext;
this->DC = DC;
}
void setCanCurrDeclContextHandleAsync(bool CanCurrDeclContextHandleAsync) {
this->CanCurrDeclContextHandleAsync = CanCurrDeclContextHandleAsync;
}
void withNestedGroup(CodeCompletionString::Chunk::ChunkKind Kind,
llvm::function_ref<void()> body);
void addAccessControlKeyword(AccessLevel Access) {
switch (Access) {
case AccessLevel::Private:
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::AccessControlKeyword,
"private ");
break;
case AccessLevel::FilePrivate:
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::AccessControlKeyword,
"fileprivate ");
break;
case AccessLevel::Internal:
// 'internal' is the default, don't add it.
break;
case AccessLevel::Package:
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::AccessControlKeyword,
"package ");
break;
case AccessLevel::Public:
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::AccessControlKeyword,
"public ");
break;
case AccessLevel::Open:
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::AccessControlKeyword,
"open ");
break;
}
}
void addRequiredKeyword() {
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::AccessControlKeyword,
"required ");
}
void addOverrideKeyword() {
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::OverrideKeyword, "override ");
}
void addDeclIntroducer(StringRef Text) {
addChunkWithText(CodeCompletionString::Chunk::ChunkKind::DeclIntroducer,
Text);
}
void addBaseName(StringRef Text) {
addChunkWithText(CodeCompletionString::Chunk::ChunkKind::BaseName, Text);
}
void addKeyword(StringRef Text) {
addChunkWithText(CodeCompletionString::Chunk::ChunkKind::Keyword, Text);
}
void addTextChunk(StringRef Text) {
addChunkWithText(CodeCompletionString::Chunk::ChunkKind::Text, Text);
}
void addAnnotatedTextChunk(StringRef Text) {
addTextChunk(Text);
getLastChunk().setIsAnnotation();
}
void addAnnotatedThrows() {
addThrows();
getLastChunk().setIsAnnotation();
}
void addThrows() {
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::EffectsSpecifierKeyword,
" throws");
}
void addAnnotatedAsync() {
addAsync();
getLastChunk().setIsAnnotation();
}
void addAsync() {
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::EffectsSpecifierKeyword,
" async");
}
void addAnnotatedRethrows() {
addRethrows();
getLastChunk().setIsAnnotation();
}
void addRethrows() {
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::EffectsSpecifierKeyword,
" rethrows");
}
void addAnnotatedLeftParen() {
addLeftParen();
getLastChunk().setIsAnnotation();
}
void addLeftParen() {
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::LeftParen, "(");
}
void addAnnotatedRightParen() {
addRightParen();
getLastChunk().setIsAnnotation();
}
void addRightParen() {
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::RightParen, ")");
}
void addAnnotatedLeftBracket() {
addLeftBracket();
getLastChunk().setIsAnnotation();
}
void addLeftBracket() {
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::LeftBracket, "[");
}
void addAnnotatedRightBracket() {
addRightBracket();
getLastChunk().setIsAnnotation();
}
void addRightBracket() {
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::RightBracket, "]");
}
void addLeftAngle() {
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::LeftAngle, "<");
}
void addRightAngle() {
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::RightAngle, ">");
}
void addLeadingDot() {
addDot();
}
void addDot() {
addChunkWithTextNoCopy(CodeCompletionString::Chunk::ChunkKind::Dot, ".");
}
void addEllipsis() {
addChunkWithTextNoCopy(CodeCompletionString::Chunk::ChunkKind::Ellipsis,
"...");
}
void addComma() {
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::Comma, ", ");
}
void addExclamationMark() {
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::ExclamationMark, "!");
}
void addQuestionMark() {
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::QuestionMark, "?");
}
void addEqual() {
addChunkWithTextNoCopy(CodeCompletionString::Chunk::ChunkKind::Equal, "=");
}
void addDeclAttrParamKeyword(StringRef Name, ArrayRef<StringRef> Parameters,
StringRef Annotation, bool NeedSpecify) {
addChunkWithText(CodeCompletionString::Chunk::ChunkKind::
DeclAttrParamKeyword, Name);
if (!Parameters.empty()) {
addLeftParen();
for (auto Parameter : Parameters) {
addSimpleNamedParameter(Parameter);
}
addRightParen();
}
if (NeedSpecify)
addChunkWithText(CodeCompletionString::Chunk::ChunkKind::
DeclAttrParamColon, ": ");
if (!Annotation.empty())
addTypeAnnotation(Annotation);
}
void addDeclAttrKeyword(StringRef Name, StringRef Annotation) {
addChunkWithText(CodeCompletionString::Chunk::ChunkKind::
DeclAttrKeyword, Name);
if (!Annotation.empty())
addTypeAnnotation(Annotation);
}
void addAttributeKeyword(StringRef Name, StringRef Annotation) {
addChunkWithText(CodeCompletionString::Chunk::ChunkKind::Attribute, Name);
if (!Annotation.empty())
addTypeAnnotation(Annotation);
}
StringRef escapeKeyword(StringRef Word, bool escapeAllKeywords,
llvm::SmallString<16> &EscapedKeyword) {
EscapedKeyword.clear();
bool shouldEscape = false;
if (escapeAllKeywords) {
#define KEYWORD(kw) .Case(#kw, true)
shouldEscape = llvm::StringSwitch<bool>(Word)
#include "swift/AST/TokenKinds.def"
.Default(false);
} else {
shouldEscape = !canBeArgumentLabel(Word);
}
if (!shouldEscape)
return Word;
EscapedKeyword.append("`");
EscapedKeyword.append(Word);
EscapedKeyword.append("`");
return EscapedKeyword;
}
void addCallParameterColon() {
addChunkWithText(CodeCompletionString::Chunk::ChunkKind::
CallArgumentColon, ": ");
}
void addSimpleNamedParameter(StringRef name) {
withNestedGroup(CodeCompletionString::Chunk::ChunkKind::CallArgumentBegin, [&] {
// Use internal, since we don't want the name to be outside the
// placeholder.
addChunkWithText(
CodeCompletionString::Chunk::ChunkKind::CallArgumentInternalName,
name);
});
}
void addSimpleTypedParameter(StringRef Annotation, bool IsVarArg = false) {
withNestedGroup(CodeCompletionString::Chunk::ChunkKind::CallArgumentBegin, [&] {
addChunkWithText(
CodeCompletionString::Chunk::ChunkKind::CallArgumentType,
Annotation);
if (IsVarArg)
addEllipsis();
});
}
void addCallArgument(Identifier Name, Identifier LocalName, Type Ty,
Type ContextTy, bool IsVarArg, bool IsInOut, bool IsIUO,
bool IsAutoClosure, bool IsLabeledTrailingClosure,
bool IsForOperator, bool HasDefault);
void addCallArgument(Identifier Name, Type Ty, Type ContextTy = Type(),
bool IsForOperator = false) {
addCallArgument(Name, Identifier(), Ty, ContextTy,
/*IsVarArg=*/false, /*IsInOut=*/false, /*IsIUO=*/false,
/*IsAutoClosure=*/false,
/*IsLabeledTrailingClosure=*/false, IsForOperator,
/*HasDefault=*/false);
}
void addGenericParameter(StringRef Name) {
withNestedGroup(CodeCompletionString::Chunk::ChunkKind::GenericParameterBegin,
[&] {
addChunkWithText(
CodeCompletionString::Chunk::ChunkKind::GenericParameterName, Name);
});
}
void addDynamicLookupMethodCallTail() {
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::DynamicLookupMethodCallTail,
"!");
getLastChunk().setIsAnnotation();
}
void addOptionalMethodCallTail() {
addChunkWithTextNoCopy(
CodeCompletionString::Chunk::ChunkKind::OptionalMethodCallTail, "?");
}
void addTypeAnnotation(StringRef Type) {
addChunkWithText(
CodeCompletionString::Chunk::ChunkKind::TypeAnnotation, Type);
getLastChunk().setIsAnnotation();
}
void addTypeAnnotation(Type T, PrintOptions PO, StringRef suffix = "");
void addBraceStmtWithCursor(StringRef Description = "") {
addChunkWithText(
CodeCompletionString::Chunk::ChunkKind::BraceStmtWithCursor,
Description);
}
void addWhitespace(StringRef space) {
addChunkWithText(
CodeCompletionString::Chunk::ChunkKind::Whitespace, space);
}
void addAnnotatedWhitespace(StringRef space) {
addWhitespace(space);
getLastChunk().setIsAnnotation();
}
void setBriefDocComment(StringRef comment) {
BriefDocComment = comment;
}
};
} // namespace ide
} // namespace swift
#endif // SWIFT_LIB_IDE_CODE_COMPLETION_RESULT_BUILDER_H
|