Skip to content

Commit 8d37c79

Browse files
authored
Merge pull request #17649 from AnthonyLatsis/code-compl-precedencegroups
[Parse][CodeCompletion] Completions for precedencegroup decls
2 parents e37dbf8 + f2ff87e commit 8d37c79

16 files changed

+404
-48
lines changed

include/swift/AST/AttrKind.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ enum class Associativity : uint8_t {
4040
Right
4141
};
4242

43+
/// Returns the in-source spelling of the given associativity.
44+
StringRef getAssociativitySpelling(Associativity value);
45+
4346
/// The kind of unary operator, if any.
4447
enum class UnaryOperatorKind : uint8_t {
4548
None,

include/swift/AST/Module.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,12 @@ class ModuleDecl : public DeclContext, public TypeDecl {
431431
/// The order of the results is not guaranteed to be meaningful.
432432
void getLocalTypeDecls(SmallVectorImpl<TypeDecl*> &Results) const;
433433

434+
/// Finds all precedence group decls of this module.
435+
///
436+
/// This does a simple local lookup, not recursively looking through imports.
437+
/// The order of the results is not guaranteed to be meaningful.
438+
void getPrecedenceGroups(SmallVectorImpl<PrecedenceGroupDecl*> &Results) const;
439+
434440
/// Finds all top-level decls that should be displayed to a client of this
435441
/// module.
436442
///
@@ -690,6 +696,13 @@ class FileUnit : public DeclContext {
690696
virtual void getTopLevelDecls(SmallVectorImpl<Decl*> &results) const {}
691697

692698

699+
/// Finds all precedence group decls in this file.
700+
///
701+
/// This does a simple local lookup, not recursively looking through imports.
702+
/// The order of the results is not guaranteed to be meaningful.
703+
virtual void
704+
getPrecedenceGroups(SmallVectorImpl<PrecedenceGroupDecl*> &Results) const {}
705+
693706
/// Finds all local type decls in this file.
694707
///
695708
/// This does a simple local lookup, not recursively looking through imports.
@@ -985,6 +998,9 @@ class SourceFile final : public FileUnit {
985998

986999
virtual void getTopLevelDecls(SmallVectorImpl<Decl*> &results) const override;
9871000

1001+
virtual void
1002+
getPrecedenceGroups(SmallVectorImpl<PrecedenceGroupDecl*> &results) const override;
1003+
9881004
virtual void
9891005
getLocalTypeDecls(SmallVectorImpl<TypeDecl*> &results) const override;
9901006

include/swift/IDE/CodeCompletion.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ enum class CompletionKind {
508508
PlatformConditon,
509509
AfterIfStmtElse,
510510
GenericParams,
511+
PrecedenceGroup,
511512
};
512513

513514
/// \brief A single code completion result.
@@ -891,7 +892,8 @@ void lookupCodeCompletionResultsFromModule(CodeCompletionResultSink &targetSink,
891892
/// restricting by \p onlyTypes.
892893
void copyCodeCompletionResults(CodeCompletionResultSink &targetSink,
893894
CodeCompletionResultSink &sourceSink,
894-
bool onlyTypes);
895+
bool onlyTypes,
896+
bool onlyPrecedenceGroups);
895897

896898
} // end namespace ide
897899
} // end namespace swift

include/swift/IDE/CodeCompletionCache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ struct RequestedCachedModule {
9595
CodeCompletionCache::Key Key;
9696
const ModuleDecl *TheModule;
9797
bool OnlyTypes;
98+
bool OnlyPrecedenceGroups;
9899
};
99100

100101
} // end namespace ide

include/swift/Parse/CodeCompletionCallbacks.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ class CodeCompletionCallbacks {
183183
/// @available.
184184
virtual void completeDeclAttrParam(DeclAttrKind DK, int Index) = 0;
185185

186+
/// Complete within a precedence group decl or after a colon in an
187+
/// operator decl.
188+
virtual void completeInPrecedenceGroup(SyntaxKind SK) = 0;
189+
186190
/// Complete the platform names inside #available statements.
187191
virtual void completePoundAvailablePlatform() = 0;
188192

include/swift/Parse/SyntaxParsingContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,8 @@ class alignas(1 << SyntaxAlignInBits) SyntaxParsingContext {
331331
/// are supported. See the implementation.
332332
void createNodeInPlace(SyntaxKind Kind);
333333

334-
/// Squshing nodes from the back of the pending syntax list to a given syntax
335-
/// collection kind. If there're no nodes can fit into the collection kind,
334+
/// Squashing nodes from the back of the pending syntax list to a given syntax
335+
/// collection kind. If there're no nodes that can fit into the collection kind,
336336
/// this function does nothing. Otherwise, it creates a collection node in place
337337
/// to contain all sequential suitable nodes from back.
338338
void collectNodesInPlace(SyntaxKind ColletionKind);

include/swift/Serialization/ModuleFile.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,9 @@ class ModuleFile
738738
/// Adds all top-level decls to the given vector.
739739
void getTopLevelDecls(SmallVectorImpl<Decl*> &Results);
740740

741+
/// Adds all precedence groups to the given vector.
742+
void getPrecedenceGroups(SmallVectorImpl<PrecedenceGroupDecl*> &Results);
743+
741744
/// Adds all local type decls to the given vector.
742745
void getLocalTypeDecls(SmallVectorImpl<TypeDecl*> &Results);
743746

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ class SerializedASTFile final : public LoadedFile {
175175

176176
virtual void getTopLevelDecls(SmallVectorImpl<Decl*> &results) const override;
177177

178+
virtual void
179+
getPrecedenceGroups(SmallVectorImpl<PrecedenceGroupDecl*> &Results) const override;
180+
178181
virtual void
179182
getLocalTypeDecls(SmallVectorImpl<TypeDecl*> &results) const override;
180183

lib/AST/Decl.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6062,6 +6062,15 @@ SourceRange DestructorDecl::getSourceRange() const {
60626062
return { getDestructorLoc(), getBody()->getEndLoc() };
60636063
}
60646064

6065+
StringRef swift::getAssociativitySpelling(Associativity value) {
6066+
switch (value) {
6067+
case Associativity::None: return "none";
6068+
case Associativity::Left: return "left";
6069+
case Associativity::Right: return "right";
6070+
}
6071+
llvm_unreachable("Unhandled Associativity in switch.");
6072+
}
6073+
60656074
PrecedenceGroupDecl *
60666075
PrecedenceGroupDecl::create(DeclContext *dc,
60676076
SourceLoc precedenceGroupLoc,

lib/AST/Module.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,20 @@ void SourceFile::getTopLevelDecls(SmallVectorImpl<Decl*> &Results) const {
547547
Results.append(Decls.begin(), Decls.end());
548548
}
549549

550+
void ModuleDecl::getPrecedenceGroups(
551+
SmallVectorImpl<PrecedenceGroupDecl*> &Results) const {
552+
FORWARD(getPrecedenceGroups, (Results));
553+
}
554+
555+
void SourceFile::getPrecedenceGroups(
556+
SmallVectorImpl<PrecedenceGroupDecl*> &Results) const {
557+
for (auto pair : PrecedenceGroups) {
558+
if (pair.second.getPointer() && pair.second.getInt()) {
559+
Results.push_back(pair.second.getPointer());
560+
}
561+
}
562+
}
563+
550564
void SourceFile::getLocalTypeDecls(SmallVectorImpl<TypeDecl*> &Results) const {
551565
Results.append(LocalTypeDecls.begin(), LocalTypeDecls.end());
552566
}

0 commit comments

Comments
 (0)