Skip to content

Commit fea4607

Browse files
committed
[SourceKit] Update requests to handle locations within generated buffers
Update requests to handle being passed a separate `key.primary_file` which specifies the file to use for building the AST. `key.sourcefile` is then the file to find in `SourceManager`, which could be a generated buffer. Resolves rdar://106863186.
1 parent e8be44d commit fea4607

File tree

8 files changed

+349
-185
lines changed

8 files changed

+349
-185
lines changed

test/SourceKit/Macros/macro_basic.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,25 @@ macro anonymousTypes(_: () -> String) = #externalMacro(module: "MacroDefinition"
181181
// ATTACHED_EXPAND-NEXT: source.edit.kind.active:
182182
// ATTACHED_EXPAND-NEXT: 21:1-21:15 ""
183183

184+
//##-- Cursor info on the attribute expanded by @myTypeWrapper
185+
// RUN: %sourcekitd-test -req=cursor -cursor-action -req-opts=retrieve_symbol_graph=1 -offset=2 @__swiftmacro_9MacroUser1SV13myTypeWrapperfMA_.swift -primary-file %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=NESTED_ATTACHED_CURSOR %s
186+
// NESTED_ATTACHED_CURSOR: source.lang.swift.ref.macro
187+
// NESTED_ATTACHED_CURSOR-SAME: macro_basic.swift:10:27-10:43
188+
// NESTED_ATTACHED_CURSOR-LABEL: SYMBOL GRAPH BEGIN
189+
// NESTED_ATTACHED_CURSOR: "identifier": {
190+
// NESTED_ATTACHED_CURSOR-NEXT: "interfaceLanguage": "swift",
191+
// NESTED_ATTACHED_CURSOR-NEXT: "precise": "s:9MacroUser16accessViaStorageyycfm"
192+
// NESTED_ATTACHED_CURSOR-NEXT: },
193+
// NESTED_ATTACHED_CURSOR-NEXT: "kind": {
194+
// NESTED_ATTACHED_CURSOR-NEXT: "displayName": "Macro",
195+
// NESTED_ATTACHED_CURSOR-NEXT: "identifier": "swift.macro"
196+
// NESTED_ATTACHED_CURSOR-NEXT: },
197+
// NESTED_ATTACHED_CURSOR: SYMBOL GRAPH END
198+
// NESTED_ATTACHED_CURSOR-LABEL: ACTIONS BEGIN
199+
// NESTED_ATTACHED_CURSOR: source.refactoring.kind.expand.macro
200+
// NESTED_ATTACHED_CURSOR-NEXT: Expand Macro
201+
// NESTED_ATTACHED_CURSOR:ACTIONS END
202+
184203
//##-- Refactoring on the attribute expanded by @myTypeWrapper
185204
// RUN: %sourcekitd-test -req=refactoring.expand.macro -pos=1:2 @__swiftmacro_9MacroUser1SV13myTypeWrapperfMA_.swift -primary-file %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=NESTED_ATTACHED_EXPAND %s
186205
// NESTED_ATTACHED_EXPAND: source.edit.kind.active:

tools/SourceKit/include/SourceKit/Core/LangSupport.h

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -637,9 +637,8 @@ enum class SemanticRefactoringKind {
637637

638638
struct SemanticRefactoringInfo {
639639
SemanticRefactoringKind Kind;
640-
// The name of the source file to start the refactoring in. Empty if it is
641-
// the primary file (in which case the primary file from the AST is used).
642-
// This must match the buffer identifier stored in the source manager.
640+
// The name of the source file to start the refactoring in. This must match
641+
// the buffer identifier stored in the source manager.
643642
StringRef SourceFile;
644643
unsigned Line;
645644
unsigned Column;
@@ -979,9 +978,10 @@ class LangSupport {
979978
EditorConsumer &Consumer) = 0;
980979

981980
virtual void getCursorInfo(
982-
StringRef Filename, unsigned Offset, unsigned Length, bool Actionables,
983-
bool SymbolGraph, bool CancelOnSubsequentRequest,
984-
ArrayRef<const char *> Args, Optional<VFSOptions> vfsOptions,
981+
StringRef PrimaryFile, StringRef InputFile, unsigned Offset,
982+
unsigned Length, bool Actionables, bool SymbolGraph,
983+
bool CancelOnSubsequentRequest, ArrayRef<const char *> Args,
984+
Optional<VFSOptions> vfsOptions,
985985
SourceKitCancellationToken CancellationToken,
986986
std::function<void(const RequestResult<CursorInfoData> &)> Receiver) = 0;
987987

@@ -1000,25 +1000,27 @@ class LangSupport {
10001000
Receiver) = 0;
10011001

10021002
virtual void getRangeInfo(
1003-
StringRef Filename, unsigned Offset, unsigned Length,
1004-
bool CancelOnSubsequentRequest, ArrayRef<const char *> Args,
1005-
SourceKitCancellationToken CancellationToken,
1003+
StringRef PrimaryFile, StringRef InputFile, unsigned Offset,
1004+
unsigned Length, bool CancelOnSubsequentRequest,
1005+
ArrayRef<const char *> Args, SourceKitCancellationToken CancellationToken,
10061006
std::function<void(const RequestResult<RangeInfo> &)> Receiver) = 0;
10071007

10081008
virtual void getCursorInfoFromUSR(
1009-
StringRef Filename, StringRef USR, bool CancelOnSubsequentRequest,
1010-
ArrayRef<const char *> Args, Optional<VFSOptions> vfsOptions,
1009+
StringRef PrimaryFile, StringRef InputFile, StringRef USR,
1010+
bool CancelOnSubsequentRequest, ArrayRef<const char *> Args,
1011+
Optional<VFSOptions> vfsOptions,
10111012
SourceKitCancellationToken CancellationToken,
10121013
std::function<void(const RequestResult<CursorInfoData> &)> Receiver) = 0;
10131014

10141015
virtual void findRelatedIdentifiersInFile(
1015-
StringRef Filename, unsigned Offset, bool CancelOnSubsequentRequest,
1016-
ArrayRef<const char *> Args, SourceKitCancellationToken CancellationToken,
1016+
StringRef PrimaryFile, StringRef InputFile, unsigned Offset,
1017+
bool CancelOnSubsequentRequest, ArrayRef<const char *> Args,
1018+
SourceKitCancellationToken CancellationToken,
10171019
std::function<void(const RequestResult<RelatedIdentsInfo> &)>
10181020
Receiver) = 0;
10191021

10201022
virtual void findActiveRegionsInFile(
1021-
StringRef Filename, ArrayRef<const char *> Args,
1023+
StringRef PrimaryFile, StringRef InputFile, ArrayRef<const char *> Args,
10221024
SourceKitCancellationToken CancellationToken,
10231025
std::function<void(const RequestResult<ActiveRegionsInfo> &)>
10241026
Receiver) = 0;
@@ -1056,7 +1058,7 @@ class LangSupport {
10561058
CategorizedEditsReceiver Receiver) = 0;
10571059

10581060
virtual void collectExpressionTypes(
1059-
StringRef FileName, ArrayRef<const char *> Args,
1061+
StringRef PrimaryFile, StringRef InputFile, ArrayRef<const char *> Args,
10601062
ArrayRef<const char *> ExpectedProtocols, bool FullyQualified,
10611063
bool CanonicalType, SourceKitCancellationToken CancellationToken,
10621064
std::function<void(const RequestResult<ExpressionTypesInFile> &)>
@@ -1066,7 +1068,7 @@ class LangSupport {
10661068
/// the source file. If `Offset` or `Length` are empty, variable types for
10671069
/// the entire document are collected.
10681070
virtual void collectVariableTypes(
1069-
StringRef FileName, ArrayRef<const char *> Args,
1071+
StringRef PrimaryFile, StringRef InputFile, ArrayRef<const char *> Args,
10701072
Optional<unsigned> Offset, Optional<unsigned> Length, bool FullyQualified,
10711073
SourceKitCancellationToken CancellationToken,
10721074
std::function<void(const RequestResult<VariableTypesInFile> &)>

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ void SwiftLangSupport::setFileSystemProvider(
991991

992992
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
993993
SwiftLangSupport::getFileSystem(const Optional<VFSOptions> &vfsOptions,
994-
Optional<StringRef> primaryFile,
994+
Optional<StringRef> inputFile,
995995
std::string &error) {
996996
// First, try the specified vfsOptions.
997997
if (vfsOptions) {
@@ -1005,8 +1005,8 @@ SwiftLangSupport::getFileSystem(const Optional<VFSOptions> &vfsOptions,
10051005
}
10061006

10071007
// Otherwise, try to find an open document with a filesystem.
1008-
if (primaryFile) {
1009-
if (auto doc = EditorDocuments->getByUnresolvedName(*primaryFile)) {
1008+
if (inputFile) {
1009+
if (auto doc = EditorDocuments->getByUnresolvedName(*inputFile)) {
10101010
return doc->getFileSystem();
10111011
}
10121012
}

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,12 @@ class SwiftLangSupport : public LangSupport {
396396
void setFileSystemProvider(StringRef Name, std::unique_ptr<FileSystemProvider> FileSystemProvider);
397397

398398
/// Returns the filesystem appropriate to use in a request with the given
399-
/// \p vfsOptions and \p primaryFile.
399+
/// \p vfsOptions and \p inputFile.
400400
///
401401
/// If \p vfsOptions is provided, returns a virtual filesystem created from
402402
/// a registered FileSystemProvider, or nullptr if there is an error.
403403
///
404-
/// If \p vfsOptions is None and \p primaryFile is provided, returns the
404+
/// If \p vfsOptions is None and \p inputFile is provided, returns the
405405
/// virtual filesystem associated with that editor document, if any.
406406
///
407407
/// Otherwise, returns the real filesystem.
@@ -411,7 +411,7 @@ class SwiftLangSupport : public LangSupport {
411411
/// \param error Set to a description of the error, if appropriate.
412412
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
413413
getFileSystem(const Optional<VFSOptions> &vfsOptions,
414-
Optional<StringRef> primaryFile, std::string &error);
414+
Optional<StringRef> inputFile, std::string &error);
415415

416416
static SourceKit::UIdent getUIDForDeclLanguage(const swift::Decl *D);
417417
static SourceKit::UIdent getUIDForDecl(const swift::Decl *D,
@@ -632,9 +632,9 @@ class SwiftLangSupport : public LangSupport {
632632
void editorExpandPlaceholder(StringRef Name, unsigned Offset, unsigned Length,
633633
EditorConsumer &Consumer) override;
634634

635-
void getCursorInfo(StringRef Filename, unsigned Offset, unsigned Length,
636-
bool Actionables, bool SymbolGraph,
637-
bool CancelOnSubsequentRequest,
635+
void getCursorInfo(StringRef PrimaryFile, StringRef InputFile,
636+
unsigned Offset, unsigned Length, bool Actionables,
637+
bool SymbolGraph, bool CancelOnSubsequentRequest,
638638
ArrayRef<const char *> Args,
639639
Optional<VFSOptions> vfsOptions,
640640
SourceKitCancellationToken CancellationToken,
@@ -655,26 +655,28 @@ class SwiftLangSupport : public LangSupport {
655655
override;
656656

657657
void getRangeInfo(
658-
StringRef Filename, unsigned Offset, unsigned Length,
659-
bool CancelOnSubsequentRequest, ArrayRef<const char *> Args,
660-
SourceKitCancellationToken CancellationToken,
658+
StringRef PrimaryFile, StringRef InputFile, unsigned Offset,
659+
unsigned Length, bool CancelOnSubsequentRequest,
660+
ArrayRef<const char *> Args, SourceKitCancellationToken CancellationToken,
661661
std::function<void(const RequestResult<RangeInfo> &)> Receiver) override;
662662

663663
void getCursorInfoFromUSR(
664-
StringRef Filename, StringRef USR, bool CancelOnSubsequentRequest,
665-
ArrayRef<const char *> Args, Optional<VFSOptions> vfsOptions,
664+
StringRef PrimaryFile, StringRef InputFile, StringRef USR,
665+
bool CancelOnSubsequentRequest, ArrayRef<const char *> Args,
666+
Optional<VFSOptions> vfsOptions,
666667
SourceKitCancellationToken CancellationToken,
667668
std::function<void(const RequestResult<CursorInfoData> &)> Receiver)
668669
override;
669670

670671
void findRelatedIdentifiersInFile(
671-
StringRef Filename, unsigned Offset, bool CancelOnSubsequentRequest,
672-
ArrayRef<const char *> Args, SourceKitCancellationToken CancellationToken,
672+
StringRef PrimaryFile, StringRef InputFile, unsigned Offset,
673+
bool CancelOnSubsequentRequest, ArrayRef<const char *> Args,
674+
SourceKitCancellationToken CancellationToken,
673675
std::function<void(const RequestResult<RelatedIdentsInfo> &)> Receiver)
674676
override;
675677

676678
void findActiveRegionsInFile(
677-
StringRef Filename, ArrayRef<const char *> Args,
679+
StringRef PrimaryFile, StringRef InputFile, ArrayRef<const char *> Args,
678680
SourceKitCancellationToken CancellationToken,
679681
std::function<void(const RequestResult<ActiveRegionsInfo> &)> Receiver)
680682
override;
@@ -695,14 +697,14 @@ class SwiftLangSupport : public LangSupport {
695697
CategorizedRenameRangesReceiver Receiver) override;
696698

697699
void collectExpressionTypes(
698-
StringRef FileName, ArrayRef<const char *> Args,
700+
StringRef PrimaryFile, StringRef InputFile, ArrayRef<const char *> Args,
699701
ArrayRef<const char *> ExpectedProtocols, bool FullyQualified,
700702
bool CanonicalType, SourceKitCancellationToken CancellationToken,
701703
std::function<void(const RequestResult<ExpressionTypesInFile> &)>
702704
Receiver) override;
703705

704706
void collectVariableTypes(
705-
StringRef FileName, ArrayRef<const char *> Args,
707+
StringRef PrimaryFile, StringRef InputFile, ArrayRef<const char *> Args,
706708
Optional<unsigned> Offset, Optional<unsigned> Length, bool FullyQualified,
707709
SourceKitCancellationToken CancellationToken,
708710
std::function<void(const RequestResult<VariableTypesInFile> &)> Receiver)

0 commit comments

Comments
 (0)