From 18fffd492fe7134ef1cc53e1725d1709ddbde20b Mon Sep 17 00:00:00 2001 From: tv Date: Fri, 6 Mar 2026 23:32:59 +0100 Subject: HyphenateSegments: Hyphenator -> Language This way we don't need to define bad orphan instances. --- src/TextViewport/Buffer/Item.hs | 8 +------- src/TextViewport/Render/Segmentation.hs | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/TextViewport/Buffer/Item.hs b/src/TextViewport/Buffer/Item.hs index b08aaf4..81d18ef 100644 --- a/src/TextViewport/Buffer/Item.hs +++ b/src/TextViewport/Buffer/Item.hs @@ -15,13 +15,7 @@ data SegmentStrategy = NoSegments | FixedWidthSegments | HyphenateSegments - { hsDict :: H.Hyphenator + { hsLang :: H.Language , hsCache :: HM.HashMap Text [(Text, Text)] } deriving (Eq, Show) - -instance Show H.Hyphenator where - show _ = "" - -instance Eq H.Hyphenator where - a == b = False diff --git a/src/TextViewport/Render/Segmentation.hs b/src/TextViewport/Render/Segmentation.hs index 584798e..55971ce 100644 --- a/src/TextViewport/Render/Segmentation.hs +++ b/src/TextViewport/Render/Segmentation.hs @@ -38,7 +38,7 @@ applyStrategy FixedWidthSegments width itemIx txt = | (lineIx, (off, chunk)) <- zip [0..] allChunks ] -applyStrategy (HyphenateSegments dict cache0) width itemIx txt = +applyStrategy (HyphenateSegments lang cache0) width itemIx txt = let rawLines = T.splitOn "\n" txt -- fold over each physical line, accumulating: @@ -59,13 +59,13 @@ applyStrategy (HyphenateSegments dict cache0) width itemIx txt = -- -> Text -- -> ([(Int, Text)], HM.HashMap Text [(Text, Text)], Int) segmentOneLine (acc, cache, off0) line = - let (chunks, cache1) = segmentWithHyphenationTeXLite dict width line cache + let (chunks, cache1) = segmentWithHyphenationTeXLite lang width line cache offsets = scanOffsetsFrom off0 chunks offNext = off0 + T.length line + 1 acc' = acc `DL.append` DL.fromList (zip offsets chunks) in (acc', cache1, offNext) --segmentOneLine (acc, cache, off0) line = - -- let chunks = segmentWithHyphenationTeXLite dict width line + -- let chunks = segmentWithHyphenationTeXLite lang width line -- offsets = scanOffsetsFrom off0 chunks -- offNext = off0 + T.length line + 1 -- +1 for newline -- acc' = acc ++ zip offsets chunks @@ -102,17 +102,17 @@ scanOffsetsFrom start = go start go !o (t:ts) = o : go (o + T.length t) ts segmentWithHyphenationTeXLite - :: H.Hyphenator + :: H.Language -> Int -> Text -> HM.HashMap Text [(Text, Text)] -> ([Text], HM.HashMap Text [(Text, Text)]) -segmentWithHyphenationTeXLite dict width txt cache0 = +segmentWithHyphenationTeXLite lang width txt cache0 = go cache0 (T.words txt) where go cache [] = ([], cache) go cache ws = - case lineCandidates dict width cache ws of + case lineCandidates lang width cache ws of ([], cache1) -> let chunks = breakWordSafe width ws in (chunks, cache1) @@ -138,12 +138,12 @@ breakWordSafe width ws = type Candidate = (Text, [Text], Bool) lineCandidates - :: H.Hyphenator + :: H.Language -> Int -> HM.HashMap Text [(Text, Text)] -> [Text] -> ([(Text, [Text], Bool)], HM.HashMap Text [(Text, Text)]) -lineCandidates dict width cache0 ws0 = +lineCandidates lang width cache0 ws0 = go [] [] cache0 ws0 where go _ acc cache [] = (acc, cache) @@ -162,7 +162,7 @@ lineCandidates dict width cache0 ws0 = case HM.lookup w cache of Just hs -> (hs, cache) Nothing -> - let hs = hyphenateWord dict w + let hs = hyphenateWord lang w in (hs, HM.insert w hs cache) hyphCands = @@ -178,9 +178,9 @@ lineCandidates dict width cache0 ws0 = then go (line ++ [w]) acc2 cache1 ws else (acc2, cache1) -hyphenateWord :: H.Hyphenator -> Text -> [(Text, Text)] -hyphenateWord dict word = - let parts = H.hyphenate dict (T.unpack word) +hyphenateWord :: H.Language -> Text -> [(Text, Text)] +hyphenateWord lang word = + let parts = H.hyphenate (H.languageHyphenator lang) (T.unpack word) in [ ( T.pack (concat (take i parts)) , T.pack (concat (drop i parts)) ) -- cgit v1.2.3