-
Notifications
You must be signed in to change notification settings - Fork 553
MergeTree: Fix segment-offset typing #24842
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
anthony-murphy
merged 3 commits into
microsoft:main
from
anthony-murphy:mt/fix-segoff-typing
Jun 16, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -878,17 +878,20 @@ export class Client extends TypedEventEmitter<IClientEvents> { | |
const useNewSlidingBehavior = true; | ||
// Destructuring segment + offset is convenient and segment is reassigned | ||
// eslint-disable-next-line prefer-const | ||
let { segment: newSegment, offset: newOffset } = getSlideToSegoff( | ||
const segOff = getSlideToSegoff( | ||
{ segment: oldSegment, offset: oldOffset }, | ||
slidePreference, | ||
reconnectingPerspective, | ||
useNewSlidingBehavior, | ||
); | ||
|
||
newSegment ??= | ||
slidePreference === SlidingPreference.FORWARD | ||
? this._mergeTree.endOfTree | ||
: this._mergeTree.startOfTree; | ||
const { segment: newSegment, offset: newOffset } = segOff ?? { | ||
segment: | ||
slidePreference === SlidingPreference.FORWARD | ||
? this._mergeTree.endOfTree | ||
: this._mergeTree.startOfTree, | ||
offset: 0, | ||
}; | ||
|
||
assert( | ||
isSegmentLeaf(newSegment) && newOffset !== undefined, | ||
|
@@ -1612,10 +1615,12 @@ export class Client extends TypedEventEmitter<IClientEvents> { | |
pos: number, | ||
sequenceArgs?: Pick<ISequencedDocumentMessage, "referenceSequenceNumber" | "clientId">, | ||
localSeq?: number, | ||
): { | ||
segment: T | undefined; | ||
offset: number | undefined; | ||
} { | ||
): | ||
| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
segment: T; | ||
offset: number; | ||
} | ||
| undefined { | ||
let perspective: Perspective; | ||
const clientId = | ||
sequenceArgs === undefined | ||
|
@@ -1630,20 +1635,17 @@ export class Client extends TypedEventEmitter<IClientEvents> { | |
perspective = new PriorPerspective(refSeq, clientId); | ||
} | ||
|
||
return this._mergeTree.getContainingSegment(pos, perspective) as { | ||
segment: T | undefined; | ||
offset: number | undefined; | ||
}; | ||
return this._mergeTree.getContainingSegment(pos, perspective) as | ||
| { | ||
segment: T; | ||
offset: number; | ||
} | ||
| undefined; | ||
} | ||
|
||
getPropertiesAtPosition(pos: number): PropertySet | undefined { | ||
let propertiesAtPosition: PropertySet | undefined; | ||
const segoff = this.getContainingSegment(pos); | ||
const seg = segoff.segment; | ||
if (seg) { | ||
propertiesAtPosition = seg.properties; | ||
} | ||
return propertiesAtPosition; | ||
return segoff?.segment?.properties; | ||
} | ||
|
||
getRangeExtentsOfPosition(pos: number): { | ||
|
@@ -1654,7 +1656,7 @@ export class Client extends TypedEventEmitter<IClientEvents> { | |
let posAfterEnd: number | undefined; | ||
|
||
const segoff = this.getContainingSegment(pos); | ||
const seg = segoff.segment; | ||
const seg = segoff?.segment; | ||
if (seg) { | ||
posStart = this.getPosition(seg); | ||
posAfterEnd = posStart + seg.cachedLength; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -469,15 +469,17 @@ function getSlideToSegment( | |
* @internal | ||
*/ | ||
export function getSlideToSegoff( | ||
segoff: { segment: ISegmentInternal | undefined; offset: number | undefined }, | ||
segoff: { segment: ISegmentInternal; offset: number } | undefined, | ||
slidingPreference: SlidingPreference = SlidingPreference.FORWARD, | ||
perspective: Perspective = allAckedChangesPerspective, | ||
useNewSlidingBehavior: boolean = false, | ||
): { | ||
segment: ISegmentInternal | undefined; | ||
offset: number | undefined; | ||
} { | ||
if (!isSegmentLeaf(segoff.segment)) { | ||
): | ||
| { | ||
segment: ISegmentInternal; | ||
offset: number; | ||
} | ||
| undefined { | ||
if (!isSegmentLeaf(segoff?.segment)) { | ||
return segoff; | ||
} | ||
const [segment, _] = getSlideToSegment( | ||
|
@@ -490,8 +492,11 @@ export function getSlideToSegoff( | |
if (segment === segoff.segment) { | ||
return segoff; | ||
} | ||
const offset = | ||
segment && segment.ordinal < segoff.segment.ordinal ? segment.cachedLength - 1 : 0; | ||
if (segment === undefined) { | ||
return undefined; | ||
} | ||
|
||
const offset = segment.ordinal < segoff.segment.ordinal ? segment.cachedLength - 1 : 0; | ||
Comment on lines
+495
to
+499
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the code that would sometimes return just an offset with no segment |
||
return { | ||
segment, | ||
offset, | ||
|
@@ -849,10 +854,12 @@ export class MergeTree { | |
public getContainingSegment( | ||
pos: number, | ||
perspective: Perspective, | ||
): { | ||
segment: ISegmentLeaf | undefined; | ||
offset: number | undefined; | ||
} { | ||
): | ||
| { | ||
segment: ISegmentLeaf; | ||
offset: number; | ||
} | ||
| undefined { | ||
assert( | ||
perspective.localSeq === undefined || | ||
perspective.clientId === this.collabWindow.clientId, | ||
|
@@ -868,6 +875,9 @@ export class MergeTree { | |
return false; | ||
}; | ||
this.nodeMap(perspective, leaf, undefined, pos, pos + 1); | ||
if (segment === undefined || offset === undefined) { | ||
return undefined; | ||
} | ||
return { segment, offset }; | ||
} | ||
|
||
|
@@ -1260,10 +1270,11 @@ export class MergeTree { | |
): Marker | undefined { | ||
let foundMarker: Marker | undefined; | ||
|
||
const { segment } = this.getContainingSegment(startPos, this.localPerspective); | ||
if (!isSegmentLeaf(segment)) { | ||
const segoff = this.getContainingSegment(startPos, this.localPerspective); | ||
if (!isSegmentLeaf(segoff?.segment)) { | ||
return undefined; | ||
} | ||
const { segment } = segoff; | ||
|
||
depthFirstNodeWalk( | ||
segment.parent, | ||
|
@@ -1529,7 +1540,7 @@ export class MergeTree { | |
|
||
if (isSegmentLeaf(segmentInfo?.segment)) { | ||
const segmentPosition = this.getPosition(segmentInfo.segment, this.localPerspective); | ||
return segmentPosition + segmentInfo.offset!; | ||
return segmentPosition + segmentInfo.offset; | ||
} else { | ||
if (remoteClientPosition === this.getLength(remotePerspective)) { | ||
return this.getLength(this.localPerspective); | ||
|
@@ -2085,14 +2096,9 @@ export class MergeTree { | |
const createRefFromSequencePlace = ( | ||
place: InteriorSequencePlace, | ||
): LocalReferencePosition => { | ||
const { segment: placeSeg, offset: placeOffset } = this.getContainingSegment( | ||
place.pos, | ||
perspective, | ||
); | ||
assert( | ||
isSegmentLeaf(placeSeg) && placeOffset !== undefined, | ||
0xa3f /* segments cannot be undefined */, | ||
); | ||
const segOff = this.getContainingSegment(place.pos, perspective); | ||
assert(isSegmentLeaf(segOff?.segment), 0xa3f /* segments cannot be undefined */); | ||
anthony-murphy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const { segment: placeSeg, offset: placeOffset } = segOff; | ||
return this.createLocalReferencePosition( | ||
placeSeg, | ||
placeOffset, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another place where we would previously keep just the offset but reset the segment