Skip to content

Commit

Permalink
fix(ios): selection range sync for long contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
jahorton committed Mar 6, 2024
1 parent ce76f13 commit 4b11a5d
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions web/src/app/webview/src/contextManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,24 @@ export class ContextHost extends Mock {

updateContext(text: string, selStart: number, selEnd: number): boolean {
let shouldResetContext = false;
if(text != this.text) {
let tempMock = new Mock(text, selStart ?? text._kmwLength(), selEnd ?? text._kmwLength());

let newLeft = tempMock.getTextBeforeCaret();
let oldLeft = this.getTextBeforeCaret();
let tempMock = new Mock(text, selStart ?? text._kmwLength(), selEnd ?? text._kmwLength());
let newLeft = tempMock.getTextBeforeCaret();
let oldLeft = this.getTextBeforeCaret();

if(text != this.text) {
let unexpectedBeforeCharCount = findCommonSubstringEndIndex(newLeft, oldLeft, true) + 1;
shouldResetContext = !!unexpectedBeforeCharCount;
}

if(shouldResetContext) {
this.text = text;
this.selStart = selStart;
this.selEnd = selEnd;
} else {
// Transform selection coordinates to their location within the longform context window.
let delta = oldLeft._kmwLength() - newLeft._kmwLength();
this.selStart = selStart - delta;
this.selEnd = selEnd - delta;
}

if(selStart === undefined || selEnd === undefined) {
Expand Down

0 comments on commit 4b11a5d

Please sign in to comment.