Skip to content

Commit

Permalink
add Loc{,Offsets}::copyEndWithZeroLength helper (sorbet#5327)
Browse files Browse the repository at this point in the history
  • Loading branch information
froydnj authored Feb 19, 2022
1 parent 3273564 commit d2c97f0
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
8 changes: 8 additions & 0 deletions core/Loc.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ struct LocOffsets {
LocOffsets copyWithZeroLength() const {
return LocOffsets{beginPos(), beginPos()};
}
// As above, but returns a zero-length version that starts at the end of the location.
LocOffsets copyEndWithZeroLength() const {
return LocOffsets{endPos(), endPos()};
}

std::string showRaw(const Context ctx) const;
std::string showRaw(const MutableContext ctx) const;
Expand Down Expand Up @@ -80,6 +84,10 @@ class Loc final {
Loc copyWithZeroLength() const {
return {this->storage.fileRef, this->storage.offsets.copyWithZeroLength()};
}
// As above, but returns a zero-length version that starts at the end of the Loc.
Loc copyEndWithZeroLength() const {
return {this->storage.fileRef, this->storage.offsets.copyEndWithZeroLength()};
}

uint32_t beginPos() const {
return storage.offsets.beginLoc;
Expand Down
6 changes: 3 additions & 3 deletions packager/packager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ class PackageInfoImpl final : public core::packages::PackageInfo {
// first let's try adding it to the end of the imports.
if (!importedPackageNames.empty()) {
auto lastOffset = importedPackageNames.back().name.loc;
insertionLoc = {loc.file(), lastOffset.endPos(), lastOffset.endPos()};
insertionLoc = core::Loc{loc.file(), lastOffset.copyEndWithZeroLength()};
} else {
// if we don't have any imports, then we can try adding it
// either before the first export, or if we have no
Expand Down Expand Up @@ -368,8 +368,8 @@ class PackageInfoImpl final : public core::packages::PackageInfo {
auto insertionLoc = core::Loc::none(loc.file());
// first let's try adding it to the end of the imports.
if (!exports_.empty()) {
auto lastOffset = exports_.back().fqn.loc;
insertionLoc = {loc.file(), lastOffset.endPos(), lastOffset.endPos()};
auto lastOffset = exports_.back().fqn.loc.offsets();
insertionLoc = core::Loc{loc.file(), lastOffset.copyEndWithZeroLength()};
} else {
// if we don't have any imports, then we can try adding it
// either before the first export, or if we have no
Expand Down
2 changes: 1 addition & 1 deletion parser/Builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ class Builder::Impl {
loc = loc.join(tokLoc(dot));
}

auto methodLoc = locOffset(loc.endPos(), loc.endPos());
auto methodLoc = loc.copyEndWithZeroLength();

auto method = core::Names::methodNameMissing();
if ((dot != nullptr) && dot->view() == "&.") {
Expand Down
2 changes: 1 addition & 1 deletion resolver/CorrectTypeAlias.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void CorrectTypeAlias::eagerToLazy(core::Context ctx, core::ErrorBuilder &e, ast
argsLoc.source(ctx).value());
}
} else {
core::Loc endLoc(argsLoc.file(), argsLoc.endPos(), argsLoc.endPos());
core::Loc endLoc = argsLoc.copyEndWithZeroLength();
string argIndent = getIndent(ctx, endLoc);
string argSrc = fmt::format("{}{}", argIndent, argsLoc.source(ctx).value());
if (wrapHash) {
Expand Down
2 changes: 1 addition & 1 deletion rewriter/Prop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ optional<PropInfo> parseProp(core::MutableContext ctx, const ast::Send *send) {
}

if (addDefault) {
auto end = core::Loc{ctx.file, core::LocOffsets{send->loc.endPos(), send->loc.endPos()}};
auto end = core::Loc{ctx.file, send->loc.copyEndWithZeroLength()};
e.replaceWith("Add `default: nil`", end, ", default: nil");
}
}
Expand Down

0 comments on commit d2c97f0

Please sign in to comment.