------------------------------------------------------------------------------- |-- Module : Text.Trifecta.Rope.Delta-- Copyright : (C) 2011 Edward Kmett-- License : BSD-style (see the file LICENSE)---- Maintainer : Edward Kmett <ekmett@gmail.com>-- Stability : experimental-- Portability : non-portable------------------------------------------------------------------------------moduleText.Trifecta.Rope.Delta(Delta(..),HasDelta(..),nextTab,rewind,near,column,columnByte)whereimportControl.ApplicativeimportData.SemigroupimportData.HashableimportData.IntimportData.WordimportData.FoldableimportData.Function(on)importData.FingerTreehiding(empty)importData.ByteStringhiding(empty)importqualifiedData.ByteString.UTF8asUTF8importText.Trifecta.Rope.BytesimportText.PrettyPrint.Freehiding(column)importSystem.Console.Terminfo.PrettyPrintdataDelta=Columns{-# UNPACK #-}!Int64-- the number of characters{-# UNPACK #-}!Int64-- the number of bytes|Tab{-# UNPACK #-}!Int64-- the number of characters before the tab{-# UNPACK #-}!Int64-- the number of characters after the tab{-# UNPACK #-}!Int64-- the number of bytes|Lines{-# UNPACK #-}!Int64-- the number of newlines contained{-# UNPACK #-}!Int64-- the number of characters since the last newline{-# UNPACK #-}!Int64-- number of bytes{-# UNPACK #-}!Int64-- the number of bytes since the last newline|Directed!ByteString-- current file name{-# UNPACK #-}!Int64-- the number of lines since the last line directive{-# UNPACK #-}!Int64-- the number of characters since the last newline{-# UNPACK #-}!Int64-- number of bytes{-# UNPACK #-}!Int64-- the number of bytes since the last newlinederivingShowinstanceEqDeltawhere(==)=(==)`on`bytesinstanceOrdDeltawherecompare=compare`on`bytesinstance(HasDeltal,HasDeltar)=>HasDelta(Eitherlr)wheredelta=eitherdeltadeltainstancePrettyDeltawhereprettyp=prettyTermp*>emptyinstancePrettyTermDeltawhereprettyTermd=casedofColumnsc_->kf0cTabxy_->kf0(nextTabx+y)Lineslc__->kflcDirectedfnlc__->k(UTF8.toStringfn)lcwherekfnlncn=bold(prettyfn)<>char':'<>bold(int64(ln+1))<>char':'<>bold(int64(cn+1))f="(interactive)"int64::Int64->Doceint64=pretty.showcolumn::HasDeltat=>t->Int64columnt=casedeltatofColumnsc_->cTabba_->nextTabb+aLines_c__->cDirected__c__->c{-# INLINE column #-}columnByte::Delta->Int64columnByte(Columns_b)=bcolumnByte(Tab__b)=bcolumnByte(Lines___b)=bcolumnByte(Directed____b)=b{-# INLINE columnByte #-}instanceHasBytesDeltawherebytes(Columns_b)=bbytes(Tab__b)=bbytes(Lines__b_)=bbytes(Directed___b_)=binstanceHashableDeltawherehash(Columnsca)=0`hashWithSalt`c`hashWithSalt`ahash(Tabxya)=1`hashWithSalt`x`hashWithSalt`y`hashWithSalt`ahash(Lineslcba)=2`hashWithSalt`l`hashWithSalt`c`hashWithSalt`b`hashWithSalt`ahash(Directedplcba)=3`hashWithSalt`p`hashWithSalt`l`hashWithSalt`c`hashWithSalt`b`hashWithSalt`ainstanceMonoidDeltawheremempty=Columns00mappend=(<>)instanceSemigroupDeltawhereColumnsca<>Columnsdb=Columns(c+d)(a+b)Columnsca<>Tabxyb=Tab(c+x)y(a+b)Columns_a<>Lineslcta'=Lineslc(t+a)a'Columns_a<>Directedplcta'=Directedplc(t+a)a'Lineslcta<>Columnsdb=Linesl(c+d)(t+b)(a+b)Lineslcta<>Tabxyb=Linesl(nextTab(c+x)+y)(t+b)(a+b)Linesl_t_<>Linesmdt'b=Lines(l+m)d(t+t')bLines__t_<>Directedplct'a=Directedplc(t+t')aTabxya<>Columnsdb=Tabx(y+d)(a+b)Tabxya<>Tabx'y'b=Tabx(nextTab(y+x')+y')(a+b)Tab__a<>Lineslcta'=Lineslc(t+a)a'Tab__a<>Directedplcta'=Directedplc(t+a)a'Directedplcta<>Columnsdb=Directedpl(c+d)(t+b)(a+b)Directedplcta<>Tabxyb=Directedpl(nextTab(c+x)+y)(t+b)(a+b)Directedpl_t_<>Linesmdt'b=Directedp(l+m)d(t+t')bDirected___t_<>Directedplct'b=Directedplc(t+t')bnextTab::Int64->Int64nextTabx=x+(8-modx8){-# INLINE nextTab #-}rewind::Delta->Deltarewind(Linesn_bd)=Linesn0(b-d)0rewind(Directedpn_bd)=Directedpn0(b-d)0rewind_=Columns00{-# INLINE rewind #-}near::(HasDeltas,HasDeltat)=>s->t->Boolnearst=rewind(deltas)==rewind(deltat){-# INLINE near #-}classHasDeltatwheredelta::t->DeltainstanceHasDeltaDeltawheredelta=idinstanceHasDeltaCharwheredelta'\t'=Tab001delta'\n'=Lines1010deltac|o<=0x7f=Columns11|o<=0x7ff=Columns12|o<=0xffff=Columns13|otherwise=Columns14whereo=fromEnumcinstanceHasDeltaWord8wheredelta9=Tab001delta10=Lines1010deltan|n<=0x7f=Columns11|n>=0xc0&&n<=0xf4=Columns11|otherwise=Columns01instanceHasDeltaByteStringwheredelta=foldMapdelta.unpackinstance(Measuredva,HasDeltav)=>HasDelta(FingerTreeva)wheredelta=delta.measure