Bug 1700051: part 7) Change `mozInlineSpellStatus::CreateForEditorChange` to return `Result<..., ...>`. r=smaug

Separates input from output more clearly.

Differential Revision: https://phabricator.services.mozilla.com/D109742
This commit is contained in:
Mirko Brodesser 2021-03-26 09:21:10 +00:00
Родитель daed84a8f4
Коммит d38cbe5571
2 изменённых файлов: 55 добавлений и 46 удалений

Просмотреть файл

@ -105,23 +105,24 @@ mozInlineSpellStatus::mozInlineSpellStatus(mozInlineSpellChecker* aSpellChecker)
// which are usually nullptr) to get a range with the union of these. // which are usually nullptr) to get a range with the union of these.
// static // static
nsresult mozInlineSpellStatus::CreateForEditorChange( Result<UniquePtr<mozInlineSpellStatus>, nsresult>
mozilla::UniquePtr<mozInlineSpellStatus>& aStatus, mozInlineSpellStatus::CreateForEditorChange(
mozInlineSpellChecker& aSpellChecker, EditSubAction aEditSubAction, mozInlineSpellChecker& aSpellChecker, EditSubAction aEditSubAction,
nsINode* aAnchorNode, uint32_t aAnchorOffset, nsINode* aPreviousNode, nsINode* aAnchorNode, uint32_t aAnchorOffset, nsINode* aPreviousNode,
uint32_t aPreviousOffset, nsINode* aStartNode, uint32_t aStartOffset, uint32_t aPreviousOffset, nsINode* aStartNode, uint32_t aStartOffset,
nsINode* aEndNode, uint32_t aEndOffset) { nsINode* aEndNode, uint32_t aEndOffset) {
if (NS_WARN_IF(!aAnchorNode) || NS_WARN_IF(!aPreviousNode)) { if (NS_WARN_IF(!aAnchorNode) || NS_WARN_IF(!aPreviousNode)) {
return NS_ERROR_FAILURE; return Err(NS_ERROR_FAILURE);
} }
aStatus = MakeUnique<mozInlineSpellStatus>(&aSpellChecker); UniquePtr<mozInlineSpellStatus> status =
MakeUnique<mozInlineSpellStatus>(&aSpellChecker);
// save the anchor point as a range so we can find the current word later // save the anchor point as a range so we can find the current word later
aStatus->mAnchorRange = status->mAnchorRange =
aStatus->PositionToCollapsedRange(aAnchorNode, aAnchorOffset); status->PositionToCollapsedRange(aAnchorNode, aAnchorOffset);
if (NS_WARN_IF(!aStatus->mAnchorRange)) { if (NS_WARN_IF(!status->mAnchorRange)) {
return NS_ERROR_FAILURE; return Err(NS_ERROR_FAILURE);
} }
bool deleted = aEditSubAction == EditSubAction::eDeleteSelectedContent; bool deleted = aEditSubAction == EditSubAction::eDeleteSelectedContent;
@ -135,70 +136,74 @@ nsresult mozInlineSpellStatus::CreateForEditorChange(
// Deletes are easy, the range is just the current anchor. We set the range // Deletes are easy, the range is just the current anchor. We set the range
// to check to be empty, FinishInitOnEvent will fill in the range to be // to check to be empty, FinishInitOnEvent will fill in the range to be
// the current word. // the current word.
aStatus->mOp = eOpChangeDelete; status->mOp = eOpChangeDelete;
aStatus->mRange = nullptr; status->mRange = nullptr;
return NS_OK; return status;
} }
aStatus->mOp = eOpChange; status->mOp = eOpChange;
// range to check // range to check
aStatus->mRange = nsRange::Create(aPreviousNode); status->mRange = nsRange::Create(aPreviousNode);
// ...we need to put the start and end in the correct order // ...we need to put the start and end in the correct order
ErrorResult errorResult; ErrorResult errorResult;
int16_t cmpResult = aStatus->mAnchorRange->ComparePoint( int16_t cmpResult = status->mAnchorRange->ComparePoint(
*aPreviousNode, aPreviousOffset, errorResult); *aPreviousNode, aPreviousOffset, errorResult);
if (NS_WARN_IF(errorResult.Failed())) { if (NS_WARN_IF(errorResult.Failed())) {
return errorResult.StealNSResult(); return Err(errorResult.StealNSResult());
} }
nsresult rv; nsresult rv;
if (cmpResult < 0) { if (cmpResult < 0) {
// previous anchor node is before the current anchor // previous anchor node is before the current anchor
rv = aStatus->mRange->SetStartAndEnd(aPreviousNode, aPreviousOffset, rv = status->mRange->SetStartAndEnd(aPreviousNode, aPreviousOffset,
aAnchorNode, aAnchorOffset); aAnchorNode, aAnchorOffset);
if (NS_WARN_IF(NS_FAILED(rv))) { if (NS_WARN_IF(NS_FAILED(rv))) {
return rv; return Err(rv);
} }
} else { } else {
// previous anchor node is after (or the same as) the current anchor // previous anchor node is after (or the same as) the current anchor
rv = aStatus->mRange->SetStartAndEnd(aAnchorNode, aAnchorOffset, rv = status->mRange->SetStartAndEnd(aAnchorNode, aAnchorOffset,
aPreviousNode, aPreviousOffset); aPreviousNode, aPreviousOffset);
if (NS_WARN_IF(NS_FAILED(rv))) { if (NS_WARN_IF(NS_FAILED(rv))) {
return rv; return Err(rv);
} }
} }
// On insert save this range: DoSpellCheck optimizes things in this range. // On insert save this range: DoSpellCheck optimizes things in this range.
// Otherwise, just leave this nullptr. // Otherwise, just leave this nullptr.
if (aEditSubAction == EditSubAction::eInsertText) { if (aEditSubAction == EditSubAction::eInsertText) {
aStatus->mCreatedRange = aStatus->mRange; status->mCreatedRange = status->mRange;
} }
// if we were given a range, we need to expand our range to encompass it // if we were given a range, we need to expand our range to encompass it
if (aStartNode && aEndNode) { if (aStartNode && aEndNode) {
cmpResult = cmpResult =
aStatus->mRange->ComparePoint(*aStartNode, aStartOffset, errorResult); status->mRange->ComparePoint(*aStartNode, aStartOffset, errorResult);
if (NS_WARN_IF(errorResult.Failed())) { if (NS_WARN_IF(errorResult.Failed())) {
return errorResult.StealNSResult(); return Err(errorResult.StealNSResult());
} }
if (cmpResult < 0) { // given range starts before if (cmpResult < 0) { // given range starts before
rv = aStatus->mRange->SetStart(aStartNode, aStartOffset); rv = status->mRange->SetStart(aStartNode, aStartOffset);
NS_ENSURE_SUCCESS(rv, rv); if (NS_WARN_IF(NS_FAILED(rv))) {
return Err(rv);
}
} }
cmpResult = cmpResult =
aStatus->mRange->ComparePoint(*aEndNode, aEndOffset, errorResult); status->mRange->ComparePoint(*aEndNode, aEndOffset, errorResult);
if (NS_WARN_IF(errorResult.Failed())) { if (NS_WARN_IF(errorResult.Failed())) {
return errorResult.StealNSResult(); return Err(errorResult.StealNSResult());
} }
if (cmpResult > 0) { // given range ends after if (cmpResult > 0) { // given range ends after
rv = aStatus->mRange->SetEnd(aEndNode, aEndOffset); rv = status->mRange->SetEnd(aEndNode, aEndOffset);
NS_ENSURE_SUCCESS(rv, rv); if (NS_WARN_IF(NS_FAILED(rv))) {
return Err(rv);
}
} }
} }
return NS_OK; return status;
} }
// mozInlineSpellStatus::InitForNavigation // mozInlineSpellStatus::InitForNavigation
@ -806,13 +811,17 @@ nsresult mozInlineSpellChecker::SpellCheckAfterEditorChange(
mNeedsCheckAfterNavigation = true; mNeedsCheckAfterNavigation = true;
// the anchor node is the position of the caret // the anchor node is the position of the caret
UniquePtr<mozInlineSpellStatus> status; Result<UniquePtr<mozInlineSpellStatus>, nsresult> res =
rv = mozInlineSpellStatus::CreateForEditorChange( mozInlineSpellStatus::CreateForEditorChange(
status, *this, aEditSubAction, aSelection.GetAnchorNode(), *this, aEditSubAction, aSelection.GetAnchorNode(),
aSelection.AnchorOffset(), aPreviousSelectedNode, aPreviousSelectedOffset, aSelection.AnchorOffset(), aPreviousSelectedNode,
aStartNode, aStartOffset, aEndNode, aEndOffset); aPreviousSelectedOffset, aStartNode, aStartOffset, aEndNode,
NS_ENSURE_SUCCESS(rv, rv); aEndOffset);
rv = ScheduleSpellCheck(std::move(status)); if (NS_WARN_IF(res.isErr())) {
return res.unwrapErr();
}
rv = ScheduleSpellCheck(res.unwrap());
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
// remember the current caret position after every change // remember the current caret position after every change

Просмотреть файл

@ -11,6 +11,7 @@
#include "nsIEditorSpellCheck.h" #include "nsIEditorSpellCheck.h"
#include "nsIInlineSpellChecker.h" #include "nsIInlineSpellChecker.h"
#include "mozInlineSpellWordUtil.h" #include "mozInlineSpellWordUtil.h"
#include "mozilla/Result.h"
#include "nsRange.h" #include "nsRange.h"
#include "nsWeakReference.h" #include "nsWeakReference.h"
@ -34,14 +35,13 @@ class mozInlineSpellStatus {
// @param aSpellChecker must be non-nullptr. // @param aSpellChecker must be non-nullptr.
explicit mozInlineSpellStatus(mozInlineSpellChecker* aSpellChecker); explicit mozInlineSpellStatus(mozInlineSpellChecker* aSpellChecker);
// @param aStatus only valid if this method returns no error. static mozilla::Result<mozilla::UniquePtr<mozInlineSpellStatus>, nsresult>
static nsresult CreateForEditorChange( CreateForEditorChange(mozInlineSpellChecker& aSpellChecker,
mozilla::UniquePtr<mozInlineSpellStatus>& aStatus, mozilla::EditSubAction aEditSubAction,
mozInlineSpellChecker& aSpellChecker, nsINode* aAnchorNode, uint32_t aAnchorOffset,
mozilla::EditSubAction aEditSubAction, nsINode* aAnchorNode, nsINode* aPreviousNode, uint32_t aPreviousOffset,
uint32_t aAnchorOffset, nsINode* aPreviousNode, uint32_t aPreviousOffset, nsINode* aStartNode, uint32_t aStartOffset,
nsINode* aStartNode, uint32_t aStartOffset, nsINode* aEndNode, nsINode* aEndNode, uint32_t aEndOffset);
uint32_t aEndOffset);
nsresult InitForNavigation(bool aForceCheck, int32_t aNewPositionOffset, nsresult InitForNavigation(bool aForceCheck, int32_t aNewPositionOffset,
nsINode* aOldAnchorNode, uint32_t aOldAnchorOffset, nsINode* aOldAnchorNode, uint32_t aOldAnchorOffset,
nsINode* aNewAnchorNode, uint32_t aNewAnchorOffset, nsINode* aNewAnchorNode, uint32_t aNewAnchorOffset,