]> gitweb.factorcode.org Git - factor.git/commitdiff
documents.elements: adding support for paragraph navigation.
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 30 Mar 2021 22:25:37 +0000 (15:25 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 30 Mar 2021 22:25:37 +0000 (15:25 -0700)
basis/documents/elements/elements-tests.factor
basis/documents/elements/elements.factor

index c8266394bb143ff14760ef82efa5b5e4228dc67c..9a27a7f3b9c3466a70ab968819f55607596675ce 100644 (file)
@@ -54,6 +54,35 @@ SYMBOL: doc
 { { 1 0 } } [ { 1 3 } doc get one-line-elt prev-elt ] unit-test
 { { 1 14 } } [ { 1 3 } doc get one-line-elt next-elt ] unit-test
 
+! paragraph-elt
+<document> doc set
+"First line
+Second line
+
+Fourth line
+Fifth line
+Sixth line
+
+
+" doc get set-doc-string
+
+{ { 0 0 } } [ { 0 2 } doc get paragraph-elt prev-elt ] unit-test
+{ { 0 0 } } [ { 1 2 } doc get paragraph-elt prev-elt ] unit-test
+{ { 3 0 } } [ { 5 2 } doc get paragraph-elt prev-elt ] unit-test
+
+{ { 1 11 } } [ { 0 3 } doc get paragraph-elt next-elt ] unit-test
+{ { 1 11 } } [ { 1 3 } doc get paragraph-elt next-elt ] unit-test
+{ { 2 0 } } [ { 1 11 } doc get paragraph-elt next-elt ] unit-test
+{ { 5 10 } } [ { 2 0 } doc get paragraph-elt next-elt ] unit-test
+{ { 5 10 } } [ { 4 2 } doc get paragraph-elt next-elt ] unit-test
+
+{ { 3 0 } } [ { 6 0 } doc get paragraph-elt prev-elt ] unit-test
+{ { 6 0 } } [ { 7 0 } doc get paragraph-elt prev-elt ] unit-test
+
+{ { 7 0 } } [ { 6 0 } doc get paragraph-elt next-elt ] unit-test
+{ { 8 0 } } [ { 7 0 } doc get paragraph-elt next-elt ] unit-test
+
+
 ! page-elt
 <document> doc set
 "First line
index 284bbc3cce48eb6b67d3d681f021534b2e736c2f..470ae6da0eee86bb59f7fe5b32535e5c95bdf2c0 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2006, 2009 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors arrays combinators documents kernel math
-sequences unicode ;
+math.order sequences unicode ;
 IN: documents.elements
 
 GENERIC: prev-elt ( loc document elt -- newloc )
@@ -106,18 +106,48 @@ M: one-line-elt prev-elt
 M: one-line-elt next-elt
     drop [ first dup ] dip doc-line length 2array ;
 
-TUPLE: page-elt { lines read-only } ;
+<PRIVATE
+
+:: prev-paragraph ( loc document -- loc' )
+    loc first 1 [-] document value>>
+    [ empty? ] find-last-from drop [ 1 + ] [ 0 ] if* :> line#
+
+    loc first line# = loc second 0 = and [
+        line# 1 - 0 2array
+    ] [
+        line# 0 2array
+    ] if ;
+
+:: next-paragraph ( loc document -- loc' )
+    loc first 1 + document value>>
+    [ empty? ] find-from drop :> line#
+
+    line# document value>> length or 1 [-]
+    dup document doc-line length
+    [ zero? [ 1 + ] when ] keep 2array
+
+    dup loc = [ first 1 + 0 2array ] when ;
+
+PRIVATE>
+
+SINGLETON: paragraph-elt
+
+M: paragraph-elt prev-elt drop prev-paragraph ;
+
+M: paragraph-elt next-elt drop next-paragraph ;
+
+TUPLE: page-elt { #lines integer read-only } ;
 
 C: <page-elt> page-elt
 
 M: page-elt prev-elt
     nip
-    2dup [ first ] [ lines>> ] bi* <
-    [ 2drop { 0 0 } ] [ lines>> neg +line ] if ;
+    2dup [ first ] [ #lines>> ] bi* <
+    [ 2drop { 0 0 } ] [ #lines>> neg +line ] if ;
 
 M: page-elt next-elt
-    3dup [ first ] [ last-line# ] [ lines>> ] tri* - >
-    [ drop nip doc-end ] [ nip lines>> +line ] if ;
+    3dup [ first ] [ last-line# ] [ #lines>> ] tri* - >
+    [ drop nip doc-end ] [ nip #lines>> +line ] if ;
 
 CONSTANT: line-elt T{ page-elt f 1 }