Skip to content

Commit 1d845d6

Browse files
committed
[IDE/SourceKit] New SourceKit request for filtered method list
`source.request.conformingmethods` is a new SourceKit request which receives a source position and a list of protocol namses, returns a list of methods whose return type conforms to the requested protocols. rdar://problem/44699573
1 parent 093d483 commit 1d845d6

File tree

22 files changed

+1052
-141
lines changed

22 files changed

+1052
-141
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===--- ConformingMethodList.h --- -----------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2019 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_CONFORMINGMETHODLIST_H
14+
#define SWIFT_IDE_CONFORMINGMETHODLIST_H
15+
16+
#include "swift/AST/Type.h"
17+
#include "swift/Basic/LLVM.h"
18+
19+
namespace swift {
20+
class CodeCompletionCallbacksFactory;
21+
22+
namespace ide {
23+
24+
/// A result item for context info query.
25+
class ConformingMethodListResult {
26+
public:
27+
/// The decl context of the parsed expression.
28+
DeclContext *DC;
29+
30+
/// The resolved type of the expression.
31+
Type ExprType;
32+
33+
/// Methods which satisfy the criteria.
34+
SmallVector<ValueDecl *, 0> Members;
35+
36+
ConformingMethodListResult(DeclContext *DC, Type ExprType)
37+
: DC(DC), ExprType(ExprType) {}
38+
};
39+
40+
/// An abstract base class for consumers of context info results.
41+
class ConformingMethodListConsumer {
42+
public:
43+
virtual ~ConformingMethodListConsumer() {}
44+
virtual void handleResult(const ConformingMethodListResult &result) = 0;
45+
};
46+
47+
/// Printing consumer
48+
class PrintingConformingMethodListConsumer
49+
: public ConformingMethodListConsumer {
50+
llvm::raw_ostream &OS;
51+
52+
public:
53+
PrintingConformingMethodListConsumer(llvm::raw_ostream &OS) : OS(OS) {}
54+
55+
void handleResult(const ConformingMethodListResult &result) override;
56+
};
57+
58+
/// Create a factory for code completion callbacks.
59+
CodeCompletionCallbacksFactory *makeConformingMethodListCallbacksFactory(
60+
ArrayRef<const char *> expectedTypeNames,
61+
ConformingMethodListConsumer &Consumer);
62+
63+
} // namespace ide
64+
} // namespace swift
65+
66+
#endif // SWIFT_IDE_CONFORMINGMETHODLIST_H

lib/IDE/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ add_swift_host_library(swiftIDE STATIC
22
CodeCompletion.cpp
33
CodeCompletionCache.cpp
44
CommentConversion.cpp
5+
ConformingMethodList.cpp
56
ExprContextAnalysis.cpp
67
Formatting.cpp
78
Refactoring.cpp

lib/IDE/CodeCompletion.cpp

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -374,48 +374,6 @@ static Stmt *findNearestStmt(const DeclContext *DC, SourceLoc Loc,
374374
const_cast<DeclContext *>(DC)->walkContext(Finder);
375375
return Finder.getFoundStmt();
376376
}
377-
/// Prepare the given expression for type-checking again, prinicipally by
378-
/// erasing any ErrorType types on the given expression, allowing later
379-
/// type-checking to make progress.
380-
///
381-
/// FIXME: this is fundamentally a workaround for the fact that we may end up
382-
/// typechecking parts of an expression more than once - first for checking
383-
/// the context, and later for checking more-specific things like unresolved
384-
/// members. We should restructure code-completion type-checking so that we
385-
/// never typecheck more than once (or find a more principled way to do it).
386-
static void prepareForRetypechecking(Expr *E) {
387-
assert(E);
388-
struct Eraser : public ASTWalker {
389-
std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {
390-
if (expr && expr->getType() && (expr->getType()->hasError() ||
391-
expr->getType()->hasUnresolvedType()))
392-
expr->setType(Type());
393-
if (auto *ACE = dyn_cast_or_null<AutoClosureExpr>(expr)) {
394-
return { true, ACE->getSingleExpressionBody() };
395-
}
396-
return { true, expr };
397-
}
398-
bool walkToTypeLocPre(TypeLoc &TL) override {
399-
if (TL.getType() && (TL.getType()->hasError() ||
400-
TL.getType()->hasUnresolvedType()))
401-
TL.setType(Type());
402-
return true;
403-
}
404-
405-
std::pair<bool, Pattern*> walkToPatternPre(Pattern *P) override {
406-
if (P && P->hasType() && (P->getType()->hasError() ||
407-
P->getType()->hasUnresolvedType())) {
408-
P->setType(Type());
409-
}
410-
return { true, P };
411-
}
412-
std::pair<bool, Stmt *> walkToStmtPre(Stmt *S) override {
413-
return { false, S };
414-
}
415-
};
416-
417-
E->walk(Eraser());
418-
}
419377

420378
CodeCompletionString::CodeCompletionString(ArrayRef<Chunk> Chunks) {
421379
std::uninitialized_copy(Chunks.begin(), Chunks.end(),

0 commit comments

Comments
 (0)