@@ -37,6 +37,8 @@ const requestBlobAsync = (projectId, fileHash) =>
3737 } )
3838 } )
3939
40+ export const REQUIRE_FULL_TEXT = 'REQUIRES_FULL_TEXT'
41+
4042function buildHeaders ( payload ) {
4143 return `Content-Length: ${ Buffer . byteLength ( payload , 'utf8' ) } \r\n\r\n`
4244}
@@ -394,6 +396,11 @@ class TexlabProcess {
394396 let textForPosition = fullText ?? currentText
395397
396398 if ( ! existing ) {
399+ if ( typeof fullText !== 'string' ) {
400+ const err = new Error ( REQUIRE_FULL_TEXT )
401+ err . code = REQUIRE_FULL_TEXT
402+ throw err
403+ }
397404 const initialText = fullText ?? ''
398405 this . docs . set ( uri , { version : desiredVersion , text : initialText } )
399406 textForPosition = initialText
@@ -438,7 +445,19 @@ class TexlabProcess {
438445 }
439446 }
440447
441- const position = positionFromOffset ( textForPosition || '' , offset )
448+ const baseText = textForPosition || ''
449+ let safeOffset = offset
450+ if ( safeOffset > baseText . length ) {
451+ safeOffset = baseText . length
452+ texlabLogger . warn (
453+ { offset, safeOffset, length : baseText . length } ,
454+ '[Texlab] clamped completion offset'
455+ )
456+ } else if ( safeOffset < 0 ) {
457+ safeOffset = 0
458+ }
459+
460+ const position = positionFromOffset ( baseText , safeOffset )
442461 return await this . sendRequest (
443462 {
444463 jsonrpc : '2.0' ,
0 commit comments