about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-06-23 11:09:49 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-06-23 11:09:49 -0700
commit009c7abb609931449457ea1250f7acccb66a217a (patch)
tree3655df3026f437ea3eb966015f9f7fe7d9bf8287
parent5a06c1286dabc2dc0fa82f41b9e79689e9a10105 (diff)
downloadview.love-009c7abb609931449457ea1250f7acccb66a217a.tar.gz
extract a function
-rw-r--r--select.lua3
-rw-r--r--text.lua29
2 files changed, 14 insertions, 18 deletions
diff --git a/select.lua b/select.lua
index 5659015..219fe3e 100644
--- a/select.lua
+++ b/select.lua
@@ -135,8 +135,7 @@ function Text.delete_selection_without_undo()
   Cursor1.pos = minp
   Selection1 = {}
   -- delete everything between min (inclusive) and max (exclusive)
-  Lines[minl].fragments = nil
-  Lines[minl].screen_line_starting_pos = nil
+  Text.clear_cache(Lines[minl])
   local min_offset = Text.offset(Lines[minl].data, minp)
   local max_offset = Text.offset(Lines[maxl].data, maxp)
   if minl == maxl then
diff --git a/text.lua b/text.lua
index 0ed46f8..23e6e05 100644
--- a/text.lua
+++ b/text.lua
@@ -155,8 +155,7 @@ end
 function Text.insert_at_cursor(t)
   local byte_offset = Text.offset(Lines[Cursor1.line].data, Cursor1.pos)
   Lines[Cursor1.line].data = string.sub(Lines[Cursor1.line].data, 1, byte_offset-1)..t..string.sub(Lines[Cursor1.line].data, byte_offset)
-  Lines[Cursor1.line].fragments = nil
-  Lines[Cursor1.line].screen_line_starting_pos = nil
+  Text.clear_cache(Lines[Cursor1.line])
   Cursor1.pos = Cursor1.pos+1
 end
 
@@ -202,8 +201,7 @@ function Text.keychord_pressed(chord)
         else
           Lines[Cursor1.line].data = string.sub(Lines[Cursor1.line].data, 1, byte_start-1)
         end
-        Lines[Cursor1.line].fragments = nil
-        Lines[Cursor1.line].screen_line_starting_pos = nil
+        Text.clear_cache(Lines[Cursor1.line])
         Cursor1.pos = Cursor1.pos-1
       end
     elseif Cursor1.line > 1 then
@@ -214,8 +212,7 @@ function Text.keychord_pressed(chord)
         -- join lines
         Cursor1.pos = utf8.len(Lines[Cursor1.line-1].data)+1
         Lines[Cursor1.line-1].data = Lines[Cursor1.line-1].data..Lines[Cursor1.line].data
-        Lines[Cursor1.line-1].fragments = nil
-        Lines[Cursor1.line-1].screen_line_starting_pos = nil
+        Text.clear_cache(Lines[Cursor1.line-1])
         table.remove(Lines, Cursor1.line)
       end
       Cursor1.line = Cursor1.line-1
@@ -250,8 +247,7 @@ function Text.keychord_pressed(chord)
         else
           Lines[Cursor1.line].data = string.sub(Lines[Cursor1.line].data, 1, byte_start-1)
         end
-        Lines[Cursor1.line].fragments = nil
-        Lines[Cursor1.line].screen_line_starting_pos = nil
+        Text.clear_cache(Lines[Cursor1.line])
         -- no change to Cursor1.pos
       end
     elseif Cursor1.line < #Lines then
@@ -260,8 +256,7 @@ function Text.keychord_pressed(chord)
       else
         -- join lines
         Lines[Cursor1.line].data = Lines[Cursor1.line].data..Lines[Cursor1.line+1].data
-        Lines[Cursor1.line].fragments = nil
-        Lines[Cursor1.line].screen_line_starting_pos = nil
+        Text.clear_cache(Lines[Cursor1.line])
         table.remove(Lines, Cursor1.line+1)
       end
     end
@@ -354,10 +349,8 @@ function Text.insert_return()
   local byte_offset = Text.offset(Lines[Cursor1.line].data, Cursor1.pos)
   table.insert(Lines, Cursor1.line+1, {mode='text', data=string.sub(Lines[Cursor1.line].data, byte_offset)})
   Lines[Cursor1.line].data = string.sub(Lines[Cursor1.line].data, 1, byte_offset-1)
-  Lines[Cursor1.line].fragments = nil
-  Lines[Cursor1.line].screen_line_starting_pos = nil
-  Lines[Cursor1.line+1].fragments = nil
-  Lines[Cursor1.line+1].screen_line_starting_pos = nil
+  Text.clear_cache(Lines[Cursor1.line])
+  Text.clear_cache(Lines[Cursor1.line+1])
   Cursor1.line = Cursor1.line+1
   Cursor1.pos = 1
 end
@@ -883,9 +876,13 @@ function Text.redraw_all()
 --?   print('clearing fragments')
   for _,line in ipairs(Lines) do
     line.y = nil
-    line.fragments = nil
-    line.screen_line_starting_pos = nil
+    Text.clear_cache(line)
   end
 end
 
+function Text.clear_cache(line)
+  line.fragments = nil
+  line.screen_line_starting_pos = nil
+end
+
 return Text
ref='#n291'>291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440