Skip to content

Commit

Permalink
[SourceKit] Use offset to indicate the locations of parameters' paren…
Browse files Browse the repository at this point in the history
…ts to facilitate subsequent cursor-info requests.
  • Loading branch information
nkcsgexi committed Apr 5, 2017
1 parent f669aff commit 1fcbc90
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 32 deletions.
6 changes: 3 additions & 3 deletions test/SourceKit/CursorInfo/cursor_label.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion tools/SourceKit/include/SourceKit/Core/LangSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 6 additions & 13 deletions tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
15 changes: 4 additions & 11 deletions tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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";
}
}

Expand Down
6 changes: 2 additions & 4 deletions tools/SourceKit/tools/sourcekitd/lib/API/Requests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 1fcbc90

Please sign in to comment.