@@ -27,21 +27,17 @@ pub struct TextDocument {
2727 language_id : LanguageId ,
2828 /// Line index for efficient position lookups
2929 line_index : LineIndex ,
30- /// Cached document length in UTF-8 bytes
31- length : u32 ,
3230}
3331
3432impl TextDocument {
3533 #[ must_use]
3634 pub fn new ( content : String , version : i32 , language_id : LanguageId ) -> Self {
3735 let line_index = LineIndex :: from_text ( & content) ;
38- let length = u32:: try_from ( content. len ( ) ) . unwrap_or ( 0 ) ;
3936 Self {
4037 content,
4138 version,
4239 language_id,
4340 line_index,
44- length,
4541 }
4642 }
4743
@@ -73,7 +69,7 @@ impl TextDocument {
7369 . lines ( )
7470 . get ( line as usize + 1 )
7571 . copied ( )
76- . unwrap_or ( self . length ) ;
72+ . unwrap_or_else ( || u32 :: try_from ( self . content . len ( ) ) . unwrap_or ( u32 :: MAX ) ) ;
7773
7874 Some ( self . content [ line_start as usize ..line_end as usize ] . to_string ( ) )
7975 }
@@ -103,7 +99,6 @@ impl TextDocument {
10399 if changes. len ( ) == 1 && changes[ 0 ] . range . is_none ( ) {
104100 self . content . clone_from ( & changes[ 0 ] . text ) ;
105101 self . line_index = LineIndex :: from_text ( & self . content ) ;
106- self . length = u32:: try_from ( self . content . len ( ) ) . unwrap_or ( 0 ) ;
107102 self . version = version;
108103 return ;
109104 }
@@ -128,12 +123,10 @@ impl TextDocument {
128123 // Rebuild line index after each change since positions shift
129124 // This is necessary for subsequent changes to have correct offsets
130125 self . line_index = LineIndex :: from_text ( & new_content) ;
131- self . length = u32:: try_from ( new_content. len ( ) ) . unwrap_or ( 0 ) ;
132126 } else {
133127 // No range means full replacement
134128 new_content = change. text ;
135129 self . line_index = LineIndex :: from_text ( & new_content) ;
136- self . length = u32:: try_from ( new_content. len ( ) ) . unwrap_or ( 0 ) ;
137130 }
138131 }
139132
@@ -164,7 +157,7 @@ impl TextDocument {
164157 // Handle line bounds - if line > line_count, return document length
165158 let line_start_utf8 = match self . line_index . lines ( ) . get ( position. line as usize ) {
166159 Some ( start) => * start,
167- None => return Some ( self . length ) , // Past end of document
160+ None => return Some ( u32 :: try_from ( text . len ( ) ) . unwrap_or ( u32 :: MAX ) ) , // Past end of document
168161 } ;
169162
170163 if position. character == 0 {
@@ -176,7 +169,7 @@ impl TextDocument {
176169 . lines ( )
177170 . get ( position. line as usize + 1 )
178171 . copied ( )
179- . unwrap_or ( self . length ) ;
172+ . unwrap_or_else ( || u32 :: try_from ( text . len ( ) ) . unwrap_or ( u32 :: MAX ) ) ;
180173
181174 let line_text = text. get ( line_start_utf8 as usize ..next_line_start as usize ) ?;
182175
0 commit comments