Skip to content

Commit b446d6b

Browse files
authored
Merge pull request swiftlang#21377 from rintaro/ide-contextinfo
[SourceKit][IDE] New SourceKit request to retrieve context type information
2 parents e320fa7 + 3797b7a commit b446d6b

File tree

23 files changed

+1760
-663
lines changed

23 files changed

+1760
-663
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,6 +2412,10 @@ class ValueDecl : public Decl {
24122412

24132413
bool isUsableFromInline() const;
24142414

2415+
/// Returns \c true if this declaration is *not* intended to be used directly
2416+
/// by application developers despite of the visibility.
2417+
bool shouldHideFromEditor() const;
2418+
24152419
bool hasAccess() const {
24162420
return TypeAndAccess.getInt().hasValue();
24172421
}

include/swift/IDE/TypeContextInfo.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//===--- TypeContextInfo.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_TYPECONTEXTINFO_H
14+
#define SWIFT_IDE_TYPECONTEXTINFO_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 TypeContextInfoItem {
26+
public:
27+
/// Possible expected type.
28+
Type ExpectedTy;
29+
/// Members of \c ExpectedTy which can be referenced by "Implicit Member
30+
/// Expression".
31+
SmallVector<ValueDecl *, 0> ImplicitMembers;
32+
33+
TypeContextInfoItem(Type ExpectedTy) : ExpectedTy(ExpectedTy) {}
34+
};
35+
36+
/// An abstract base class for consumers of context info results.
37+
class TypeContextInfoConsumer {
38+
public:
39+
virtual ~TypeContextInfoConsumer() {}
40+
virtual void handleResults(ArrayRef<TypeContextInfoItem>) = 0;
41+
};
42+
43+
/// Printing consumer
44+
class PrintingTypeContextInfoConsumer : public TypeContextInfoConsumer {
45+
llvm::raw_ostream &OS;
46+
47+
public:
48+
PrintingTypeContextInfoConsumer(llvm::raw_ostream &OS) : OS(OS) {}
49+
50+
void handleResults(ArrayRef<TypeContextInfoItem>) override;
51+
};
52+
53+
/// Create a factory for code completion callbacks.
54+
CodeCompletionCallbacksFactory *
55+
makeTypeContextInfoCallbacksFactory(TypeContextInfoConsumer &Consumer);
56+
57+
} // namespace ide
58+
} // namespace swift
59+
60+
#endif // SWIFT_IDE_TYPECONTEXTINFO_H

lib/AST/Decl.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,6 +2550,34 @@ bool ValueDecl::isUsableFromInline() const {
25502550
return false;
25512551
}
25522552

2553+
/// Returns \c true if this declaration is *not* intended to be used directly
2554+
/// by application developers despite of the visibility.
2555+
bool ValueDecl::shouldHideFromEditor() const {
2556+
// Hide private stdlib declarations.
2557+
if (isPrivateStdlibDecl(/*treatNonBuiltinProtocolsAsPublic*/ false) ||
2558+
// ShowInInterfaceAttr is for decls to show in interface as exception but
2559+
// they are not intended to be used directly.
2560+
getAttrs().hasAttribute<ShowInInterfaceAttr>())
2561+
return true;
2562+
2563+
if (AvailableAttr::isUnavailable(this))
2564+
return true;
2565+
2566+
if (auto *ClangD = getClangDecl()) {
2567+
if (ClangD->hasAttr<clang::SwiftPrivateAttr>())
2568+
return true;
2569+
}
2570+
2571+
if (!isUserAccessible())
2572+
return true;
2573+
2574+
// Hide editor placeholders.
2575+
if (getBaseName().isEditorPlaceholder())
2576+
return true;
2577+
2578+
return false;
2579+
}
2580+
25532581
/// Return the access level of an internal or public declaration
25542582
/// that's been testably imported.
25552583
static AccessLevel getTestableOrPrivateImportsAccess(const ValueDecl *decl) {

lib/IDE/CMakeLists.txt

Lines changed: 3 additions & 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+
ExprContextAnalysis.cpp
56
Formatting.cpp
67
Refactoring.cpp
78
ModuleInterfacePrinting.cpp
@@ -13,7 +14,9 @@ add_swift_host_library(swiftIDE STATIC
1314
IDETypeChecking.cpp
1415
APIDigesterData.cpp
1516
SourceEntityWalker.cpp
17+
TypeContextInfo.cpp
1618
LINK_LIBRARIES
19+
swiftAST
1720
swiftFrontend
1821
swiftClangImporter
1922
swiftDWARFImporter

0 commit comments

Comments
 (0)