mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[SourceKit] Use offset to indicate the locations of parameters' parents to facilitate subsequent cursor-info requests.
This commit is contained in:
@@ -10,6 +10,6 @@ c.foo(aa : 1)
|
||||
// RUN: %sourcekitd-test -req=cursor -pos=3:13 %s -- %s | %FileCheck %s -check-prefix=CHECK2
|
||||
// RUN: %sourcekitd-test -req=cursor -pos=4:24 %s -- %s | %FileCheck %s -check-prefix=CHECK3
|
||||
|
||||
// CHECK1: PARENT LOC: 2:3
|
||||
// CHECK2: PARENT LOC: 3:8
|
||||
// CHECK3: PARENT LOC: 4:3
|
||||
// CHECK1: PARENT OFFSET: 13
|
||||
// CHECK2: PARENT OFFSET: 37
|
||||
// CHECK3: PARENT OFFSET: 56
|
||||
|
||||
@@ -285,7 +285,7 @@ struct CursorInfo {
|
||||
/// All available actions on the code under cursor.
|
||||
ArrayRef<StringRef> AvailableActions;
|
||||
bool IsSystem = false;
|
||||
llvm::Optional<std::pair<unsigned, unsigned>> ParentNameLoc;
|
||||
llvm::Optional<unsigned> ParentNameOffset;
|
||||
};
|
||||
|
||||
struct RangeInfo {
|
||||
|
||||
@@ -610,8 +610,7 @@ static bool passCursorInfoForModule(ModuleEntity Mod,
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool getParamParentNameLoc(const ValueDecl *VD, unsigned &Line,
|
||||
unsigned &Column) {
|
||||
static Optional<unsigned> getParamParentNameOffset(const ValueDecl *VD) {
|
||||
SourceLoc Loc;
|
||||
if (auto PD = dyn_cast<ParamDecl>(VD)) {
|
||||
auto *DC = PD->getDeclContext();
|
||||
@@ -623,14 +622,14 @@ static bool getParamParentNameLoc(const ValueDecl *VD, unsigned &Line,
|
||||
Loc = cast<AbstractFunctionDecl>(DC)->getNameLoc();
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (Loc.isInvalid())
|
||||
return false;
|
||||
return None;
|
||||
auto &SM = VD->getASTContext().SourceMgr;
|
||||
std::tie(Line, Column) = SM.getLineAndColumn(Loc);
|
||||
return true;
|
||||
return SM.getLocOffsetInBuffer(Loc, SM.getIDForBufferIdentifier(SM.
|
||||
getBufferIdentifierForLoc(Loc)).getValue());
|
||||
}
|
||||
|
||||
/// Returns true for failure to resolve.
|
||||
@@ -853,12 +852,6 @@ static bool passCursorInfoForDecl(const ValueDecl *VD,
|
||||
bool IsSystem = VD->getModuleContext()->isSystemModule();
|
||||
std::string TypeInterface;
|
||||
|
||||
unsigned ParentLine, ParentCol;
|
||||
llvm::Optional<std::pair<unsigned, unsigned>> ParentLoc;
|
||||
if (getParamParentNameLoc(VD, ParentLine, ParentCol)) {
|
||||
ParentLoc.emplace(ParentLine, ParentCol);
|
||||
}
|
||||
|
||||
CursorInfo Info;
|
||||
Info.Kind = Kind;
|
||||
Info.Name = Name;
|
||||
@@ -879,7 +872,7 @@ static bool passCursorInfoForDecl(const ValueDecl *VD,
|
||||
Info.LocalizationKey = LocalizationKey;
|
||||
Info.IsSystem = IsSystem;
|
||||
Info.TypeInterface = StringRef();
|
||||
Info.ParentNameLoc = ParentLoc;
|
||||
Info.ParentNameOffset = getParamParentNameOffset(VD);
|
||||
Receiver(Info);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1269,15 +1269,8 @@ static void printCursorInfo(sourcekitd_variant_t Info, StringRef FilenameIn,
|
||||
KeyActionName));
|
||||
}
|
||||
|
||||
Optional<std::pair<unsigned, unsigned>> ParentLoc;
|
||||
sourcekitd_variant_t ParentLocObj =
|
||||
sourcekitd_variant_dictionary_get_value(Info, KeyParentLoc);
|
||||
if (sourcekitd_variant_get_type(ParentLocObj) ==
|
||||
SOURCEKITD_VARIANT_TYPE_DICTIONARY) {
|
||||
ParentLoc.emplace(
|
||||
sourcekitd_variant_dictionary_get_int64(ParentLocObj, KeyLine),
|
||||
sourcekitd_variant_dictionary_get_int64(ParentLocObj, KeyColumn));
|
||||
}
|
||||
uint64_t ParentOffset =
|
||||
sourcekitd_variant_dictionary_get_int64(Info, KeyParentLoc);
|
||||
|
||||
OS << Kind << " (";
|
||||
if (Offset.hasValue()) {
|
||||
@@ -1335,8 +1328,8 @@ static void printCursorInfo(sourcekitd_variant_t Info, StringRef FilenameIn,
|
||||
for (auto Action : AvailableActions)
|
||||
OS << Action << '\n';
|
||||
OS << "ACTIONS END\n";
|
||||
if (ParentLoc) {
|
||||
OS << "PARENT LOC: " << ParentLoc->first << ":" << ParentLoc->second << "\n";
|
||||
if (ParentOffset) {
|
||||
OS << "PARENT OFFSET: " << ParentOffset << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1430,10 +1430,8 @@ static void reportCursorInfo(const CursorInfo &Info, ResponseReceiver Rec) {
|
||||
Entry.set(KeyActionName, Name);
|
||||
}
|
||||
}
|
||||
if (Info.ParentNameLoc) {
|
||||
auto PL = Elem.setDictionary(KeyParentLoc);
|
||||
PL.set(KeyLine, Info.ParentNameLoc->first);
|
||||
PL.set(KeyColumn, Info.ParentNameLoc->second);
|
||||
if (Info.ParentNameOffset) {
|
||||
Elem.set(KeyParentLoc, Info.ParentNameOffset.getValue());
|
||||
}
|
||||
if (!Info.AnnotatedRelatedDeclarations.empty()) {
|
||||
auto RelDecls = Elem.setArray(KeyRelatedDecls);
|
||||
|
||||
Reference in New Issue
Block a user