module Unison.Codebase.Editor.HandleInput.FormatFile
( formatFile,
applyTextReplacements,
TextReplacement (..),
)
where
import Control.Lens hiding (List)
import Control.Monad.State
import Data.IntervalMap.Interval qualified as Interval
import Data.List qualified as List
import Data.List.NonEmpty.Extra qualified as NEL
import Data.Map qualified as Map
import Data.Set qualified as Set
import Data.Text qualified as Text
import Text.Builder qualified as TB
import U.Core.ABT qualified as ABT
import Unison.Codebase.Path qualified as Path
import Unison.DataDeclaration qualified as Decl
import Unison.HashQualified qualified as HQ
import Unison.Lexer.Pos qualified as Pos
import Unison.Name qualified as Name
import Unison.Parser.Ann qualified as Ann
import Unison.Prelude
import Unison.PrettyPrintEnv.Util qualified as PPE
import Unison.PrettyPrintEnvDecl qualified as PPED
import Unison.Reference qualified as Reference
import Unison.Symbol (Symbol)
import Unison.Syntax.DeclPrinter qualified as DeclPrinter
import Unison.Syntax.Name qualified as Name
import Unison.Syntax.TermPrinter qualified as TermPrinter
import Unison.Term qualified as Term
import Unison.UnisonFile (TypecheckedUnisonFile, UnisonFile)
import Unison.UnisonFile qualified as UF
import Unison.UnisonFile.Summary qualified as FileSummary
import Unison.Util.Pretty qualified as Pretty
import Unison.Util.Range (Range (..))
import Unison.Var qualified as Var
formatFile ::
(Monad m) =>
(Maybe (UnisonFile Symbol Ann.Ann) -> Maybe (TypecheckedUnisonFile Symbol Ann.Ann) -> m PPED.PrettyPrintEnvDecl) ->
Int ->
Path.Absolute ->
Maybe (UnisonFile Symbol Ann.Ann) ->
Maybe (TypecheckedUnisonFile Symbol Ann.Ann) ->
Maybe (Set Range) ->
m (Maybe [TextReplacement])
formatFile :: forall (m :: * -> *).
Monad m =>
(Maybe (UnisonFile Symbol Ann)
-> Maybe (TypecheckedUnisonFile Symbol Ann)
-> m PrettyPrintEnvDecl)
-> Int
-> Absolute
-> Maybe (UnisonFile Symbol Ann)
-> Maybe (TypecheckedUnisonFile Symbol Ann)
-> Maybe (Set Range)
-> m (Maybe [TextReplacement])
formatFile Maybe (UnisonFile Symbol Ann)
-> Maybe (TypecheckedUnisonFile Symbol Ann) -> m PrettyPrintEnvDecl
makePPEDForFile Int
formattingWidth Absolute
currentPath Maybe (UnisonFile Symbol Ann)
inputParsedFile Maybe (TypecheckedUnisonFile Symbol Ann)
inputTypecheckedFile Maybe (Set Range)
mayRangesToFormat = MaybeT m [TextReplacement] -> m (Maybe [TextReplacement])
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT m [TextReplacement] -> m (Maybe [TextReplacement]))
-> MaybeT m [TextReplacement] -> m (Maybe [TextReplacement])
forall a b. (a -> b) -> a -> b
$ do
let (Maybe (UnisonFile Symbol Ann)
mayParsedFile, Maybe (TypecheckedUnisonFile Symbol Ann)
mayTypecheckedFile) = Maybe (UnisonFile Symbol Ann)
-> Maybe (TypecheckedUnisonFile Symbol Ann)
-> (Maybe (UnisonFile Symbol Ann),
Maybe (TypecheckedUnisonFile Symbol Ann))
mkUnisonFilesDeterministic Maybe (UnisonFile Symbol Ann)
inputParsedFile Maybe (TypecheckedUnisonFile Symbol Ann)
inputTypecheckedFile
FileSummary
fileSummary <- Maybe FileSummary -> MaybeT m FileSummary
forall (m :: * -> *) a. Applicative m => Maybe a -> MaybeT m a
hoistMaybe (Maybe FileSummary -> MaybeT m FileSummary)
-> Maybe FileSummary -> MaybeT m FileSummary
forall a b. (a -> b) -> a -> b
$ Maybe (UnisonFile Symbol Ann)
-> Maybe (TypecheckedUnisonFile Symbol Ann) -> Maybe FileSummary
FileSummary.mkFileSummary Maybe (UnisonFile Symbol Ann)
mayParsedFile Maybe (TypecheckedUnisonFile Symbol Ann)
mayTypecheckedFile
PrettyPrintEnvDecl
filePPED <- m PrettyPrintEnvDecl -> MaybeT m PrettyPrintEnvDecl
forall (m :: * -> *) a. Monad m => m a -> MaybeT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m PrettyPrintEnvDecl -> MaybeT m PrettyPrintEnvDecl)
-> m PrettyPrintEnvDecl -> MaybeT m PrettyPrintEnvDecl
forall a b. (a -> b) -> a -> b
$ Maybe (UnisonFile Symbol Ann)
-> Maybe (TypecheckedUnisonFile Symbol Ann) -> m PrettyPrintEnvDecl
makePPEDForFile Maybe (UnisonFile Symbol Ann)
mayParsedFile Maybe (TypecheckedUnisonFile Symbol Ann)
mayTypecheckedFile
UnisonFile Symbol Ann
parsedFile <- Maybe (UnisonFile Symbol Ann) -> MaybeT m (UnisonFile Symbol Ann)
forall (m :: * -> *) a. Applicative m => Maybe a -> MaybeT m a
hoistMaybe Maybe (UnisonFile Symbol Ann)
mayParsedFile
TypecheckedUnisonFile Symbol Ann
_typecheckedFile <- Maybe (TypecheckedUnisonFile Symbol Ann)
-> MaybeT m (TypecheckedUnisonFile Symbol Ann)
forall (m :: * -> *) a. Applicative m => Maybe a -> MaybeT m a
hoistMaybe Maybe (TypecheckedUnisonFile Symbol Ann)
mayTypecheckedFile
Map Symbol (Ann, Pretty ColorText)
formattedDecls <-
(FileSummary
-> Map
Symbol
(TypeReferenceId,
Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann))
FileSummary.allTypeDecls FileSummary
fileSummary)
Map
Symbol
(TypeReferenceId,
Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann))
-> (Map
Symbol
(TypeReferenceId,
Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann))
-> Map
Symbol
(Ann, TypeReferenceId,
Either
(EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann)))
-> Map
Symbol
(Ann, TypeReferenceId,
Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann))
forall a b. a -> (a -> b) -> b
& ((TypeReferenceId,
Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann))
-> (Ann, TypeReferenceId,
Either
(EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann)))
-> Map
Symbol
(TypeReferenceId,
Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann))
-> Map
Symbol
(Ann, TypeReferenceId,
Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann))
forall a b. (a -> b) -> Map Symbol a -> Map Symbol b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
( \(TypeReferenceId
ref, Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann)
decl) ->
let tldAnn :: Ann
tldAnn = (EffectDeclaration Symbol Ann -> Ann)
-> (DataDeclaration Symbol Ann -> Ann)
-> Either
(EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann)
-> Ann
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (DataDeclaration Symbol Ann -> Ann
forall v a. DataDeclaration v a -> a
Decl.annotation (DataDeclaration Symbol Ann -> Ann)
-> (EffectDeclaration Symbol Ann -> DataDeclaration Symbol Ann)
-> EffectDeclaration Symbol Ann
-> Ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EffectDeclaration Symbol Ann -> DataDeclaration Symbol Ann
forall v a. EffectDeclaration v a -> DataDeclaration v a
Decl.toDataDecl) (DataDeclaration Symbol Ann -> Ann
forall v a. DataDeclaration v a -> a
Decl.annotation) Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann)
decl
in (Ann
tldAnn, TypeReferenceId
ref, Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann)
decl)
)
Map
Symbol
(Ann, TypeReferenceId,
Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann))
-> (Map
Symbol
(Ann, TypeReferenceId,
Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann))
-> Map
Symbol
(Ann, TypeReferenceId,
Either
(EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann)))
-> Map
Symbol
(Ann, TypeReferenceId,
Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann))
forall a b. a -> (a -> b) -> b
& ((Ann, TypeReferenceId,
Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann))
-> Bool)
-> Map
Symbol
(Ann, TypeReferenceId,
Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann))
-> Map
Symbol
(Ann, TypeReferenceId,
Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann))
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter (\(Ann
tldAnn, TypeReferenceId
_, Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann)
_) -> Ann -> Bool
isInFormatRange Ann
tldAnn)
Map
Symbol
(Ann, TypeReferenceId,
Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann))
-> (Map
Symbol
(Ann, TypeReferenceId,
Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann))
-> MaybeT m (Map Symbol (Ann, Pretty ColorText)))
-> MaybeT m (Map Symbol (Ann, Pretty ColorText))
forall a b. a -> (a -> b) -> b
& (Symbol
-> (Ann, TypeReferenceId,
Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann))
-> MaybeT m (Ann, Pretty ColorText))
-> Map
Symbol
(Ann, TypeReferenceId,
Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann))
-> MaybeT m (Map Symbol (Ann, Pretty ColorText))
forall i (t :: * -> *) (f :: * -> *) a b.
(TraversableWithIndex i t, Applicative f) =>
(i -> a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(Symbol -> a -> f b) -> Map Symbol a -> f (Map Symbol b)
itraverse \Symbol
sym (Ann
tldAnn, TypeReferenceId
ref, Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann)
decl) -> do
Name
symName <- Maybe Name -> MaybeT m Name
forall (m :: * -> *) a. Applicative m => Maybe a -> MaybeT m a
hoistMaybe (Symbol -> Maybe Name
forall v. Var v => v -> Maybe Name
Name.parseVar Symbol
sym)
let declNameSegments :: NonEmpty NameSegment
declNameSegments = [NameSegment] -> NonEmpty NameSegment -> NonEmpty NameSegment
forall a. [a] -> NonEmpty a -> NonEmpty a
NEL.appendr (Path -> [NameSegment]
Path.toList (Absolute -> Path
Path.unabsolute Absolute
currentPath)) (Name -> NonEmpty NameSegment
Name.segments Name
symName)
let declName :: Name
declName = NonEmpty NameSegment -> Name
Name.fromSegments NonEmpty NameSegment
declNameSegments
let hqName :: HashQualified Name
hqName = Name -> HashQualified Name
forall n. n -> HashQualified n
HQ.fromName Name
symName
let biasedPPED :: PrettyPrintEnvDecl
biasedPPED = [Name] -> PrettyPrintEnvDecl -> PrettyPrintEnvDecl
PPED.biasTo [Name
declName] PrettyPrintEnvDecl
filePPED
(Ann, Pretty ColorText) -> MaybeT m (Ann, Pretty ColorText)
forall a. a -> MaybeT m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Ann, Pretty ColorText) -> MaybeT m (Ann, Pretty ColorText))
-> (Ann, Pretty ColorText) -> MaybeT m (Ann, Pretty ColorText)
forall a b. (a -> b) -> a -> b
$
(Ann
tldAnn, PrettyPrintEnvDecl
-> TypeReference
-> HashQualified Name
-> Either
(EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann)
-> Pretty SyntaxText
forall v a.
Var v =>
PrettyPrintEnvDecl
-> TypeReference
-> HashQualified Name
-> Decl v a
-> Pretty SyntaxText
DeclPrinter.prettyDecl PrettyPrintEnvDecl
biasedPPED (TypeReferenceId -> TypeReference
forall h t. Id' h -> Reference' t h
Reference.DerivedId TypeReferenceId
ref) HashQualified Name
hqName Either (EffectDeclaration Symbol Ann) (DataDeclaration Symbol Ann)
decl)
(Ann, Pretty SyntaxText)
-> ((Ann, Pretty SyntaxText) -> (Ann, Pretty ColorText))
-> (Ann, Pretty ColorText)
forall a b. a -> (a -> b) -> b
& ASetter
(Ann, Pretty SyntaxText)
(Ann, Pretty ColorText)
(Pretty SyntaxText)
(Pretty ColorText)
-> (Pretty SyntaxText -> Pretty ColorText)
-> (Ann, Pretty SyntaxText)
-> (Ann, Pretty ColorText)
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter
(Ann, Pretty SyntaxText)
(Ann, Pretty ColorText)
(Pretty SyntaxText)
(Pretty ColorText)
forall s t a b. Field2 s t a b => Lens s t a b
Lens
(Ann, Pretty SyntaxText)
(Ann, Pretty ColorText)
(Pretty SyntaxText)
(Pretty ColorText)
_2 Pretty SyntaxText -> Pretty ColorText
forall r. Pretty (SyntaxText' r) -> Pretty ColorText
Pretty.syntaxToColor
Map Symbol (Ann, Pretty ColorText)
formattedTerms <-
(FileSummary
-> Map
Symbol
(Ann, Maybe TypeReferenceId, Term Symbol Ann,
Maybe (Type Symbol Ann))
FileSummary.termsBySymbol FileSummary
fileSummary)
Map
Symbol
(Ann, Maybe TypeReferenceId, Term Symbol Ann,
Maybe (Type Symbol Ann))
-> (Map
Symbol
(Ann, Maybe TypeReferenceId, Term Symbol Ann,
Maybe (Type Symbol Ann))
-> Map
Symbol
(Ann, Maybe TypeReferenceId, Term Symbol Ann,
Maybe (Type Symbol Ann)))
-> Map
Symbol
(Ann, Maybe TypeReferenceId, Term Symbol Ann,
Maybe (Type Symbol Ann))
forall a b. a -> (a -> b) -> b
& ((Ann, Maybe TypeReferenceId, Term Symbol Ann,
Maybe (Type Symbol Ann))
-> Bool)
-> Map
Symbol
(Ann, Maybe TypeReferenceId, Term Symbol Ann,
Maybe (Type Symbol Ann))
-> Map
Symbol
(Ann, Maybe TypeReferenceId, Term Symbol Ann,
Maybe (Type Symbol Ann))
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter (\(Ann
tldAnn, Maybe TypeReferenceId
_, Term Symbol Ann
trm, Maybe (Type Symbol Ann)
_) -> Ann -> Term Symbol Ann -> Bool
shouldFormatTerm Ann
tldAnn Term Symbol Ann
trm)
Map
Symbol
(Ann, Maybe TypeReferenceId, Term Symbol Ann,
Maybe (Type Symbol Ann))
-> (Map
Symbol
(Ann, Maybe TypeReferenceId, Term Symbol Ann,
Maybe (Type Symbol Ann))
-> MaybeT m (Map Symbol (Ann, Pretty ColorText)))
-> MaybeT m (Map Symbol (Ann, Pretty ColorText))
forall a b. a -> (a -> b) -> b
& (Symbol
-> (Ann, Maybe TypeReferenceId, Term Symbol Ann,
Maybe (Type Symbol Ann))
-> MaybeT m (Ann, Pretty ColorText))
-> Map
Symbol
(Ann, Maybe TypeReferenceId, Term Symbol Ann,
Maybe (Type Symbol Ann))
-> MaybeT m (Map Symbol (Ann, Pretty ColorText))
forall i (t :: * -> *) (f :: * -> *) a b.
(TraversableWithIndex i t, Applicative f) =>
(i -> a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(Symbol -> a -> f b) -> Map Symbol a -> f (Map Symbol b)
itraverse \Symbol
sym (Ann
tldAnn, Maybe TypeReferenceId
mayRefId, Term Symbol Ann
trm, Maybe (Type Symbol Ann)
_typ) -> do
Name
symName <- Maybe Name -> MaybeT m Name
forall (m :: * -> *) a. Applicative m => Maybe a -> MaybeT m a
hoistMaybe (Symbol -> Maybe Name
forall v. Var v => v -> Maybe Name
Name.parseVar Symbol
sym)
let defNameSegments :: NonEmpty NameSegment
defNameSegments = [NameSegment] -> NonEmpty NameSegment -> NonEmpty NameSegment
forall a. [a] -> NonEmpty a -> NonEmpty a
NEL.appendr (Path -> [NameSegment]
Path.toList (Absolute -> Path
Path.unabsolute Absolute
currentPath)) (Name -> NonEmpty NameSegment
Name.segments Name
symName)
let defName :: Name
defName = NonEmpty NameSegment -> Name
Name.fromSegments NonEmpty NameSegment
defNameSegments
let hqName :: HashQualified Name
hqName = Name -> HashQualified Name
forall n. n -> HashQualified n
HQ.NameOnly Name
symName
let biasedPPED :: PrettyPrintEnvDecl
biasedPPED = [Name] -> PrettyPrintEnvDecl -> PrettyPrintEnvDecl
PPED.biasTo [Name
defName] PrettyPrintEnvDecl
filePPED
let definitionPPE :: PrettyPrintEnv
definitionPPE = case Maybe TypeReferenceId
mayRefId of
Just TypeReferenceId
refId -> PrettyPrintEnvDecl -> TypeReference -> PrettyPrintEnv
PPE.declarationPPE PrettyPrintEnvDecl
biasedPPED (TypeReferenceId -> TypeReference
forall h t. Id' h -> Reference' t h
Reference.DerivedId TypeReferenceId
refId)
Maybe TypeReferenceId
Nothing -> PrettyPrintEnvDecl -> PrettyPrintEnv
PPED.suffixifiedPPE PrettyPrintEnvDecl
biasedPPED
let formattedTerm :: Pretty ColorText
formattedTerm = Pretty SyntaxText -> Pretty ColorText
forall r. Pretty (SyntaxText' r) -> Pretty ColorText
Pretty.syntaxToColor (Pretty SyntaxText -> Pretty ColorText)
-> Pretty SyntaxText -> Pretty ColorText
forall a b. (a -> b) -> a -> b
$ PrettyPrintEnv
-> HashQualified Name -> Term Symbol Ann -> Pretty SyntaxText
forall v at ap a.
Var v =>
PrettyPrintEnv
-> HashQualified Name -> Term2 v at ap v a -> Pretty SyntaxText
TermPrinter.prettyBinding PrettyPrintEnv
definitionPPE HashQualified Name
hqName (UnisonFile Symbol Ann
-> Symbol -> Term Symbol Ann -> Term Symbol Ann
forall a.
UnisonFile Symbol a -> Symbol -> Term Symbol a -> Term Symbol a
removeGeneratedTypeAnnotations UnisonFile Symbol Ann
parsedFile Symbol
sym Term Symbol Ann
trm)
(Ann, Pretty ColorText) -> MaybeT m (Ann, Pretty ColorText)
forall a. a -> MaybeT m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Ann
tldAnn, Pretty ColorText
formattedTerm)
let nonGeneratedDefs :: Map Symbol ((Pos, Pos), Pretty ColorText)
nonGeneratedDefs =
(Map Symbol (Ann, Pretty ColorText)
formattedTerms Map Symbol (Ann, Pretty ColorText)
-> Map Symbol (Ann, Pretty ColorText)
-> Map Symbol (Ann, Pretty ColorText)
forall a. Semigroup a => a -> a -> a
<> Map Symbol (Ann, Pretty ColorText)
formattedDecls)
Map Symbol (Ann, Pretty ColorText)
-> (Map Symbol (Ann, Pretty ColorText)
-> Map Symbol ((Pos, Pos), Pretty ColorText))
-> Map Symbol ((Pos, Pos), Pretty ColorText)
forall a b. a -> (a -> b) -> b
& ((Ann, Pretty ColorText) -> Maybe ((Pos, Pos), Pretty ColorText))
-> Map Symbol (Ann, Pretty ColorText)
-> Map Symbol ((Pos, Pos), Pretty ColorText)
forall a b. (a -> Maybe b) -> Map Symbol a -> Map Symbol b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe
( \case
(Ann.Ann {Pos
start :: Pos
$sel:start:Intrinsic :: Ann -> Pos
start, Pos
end :: Pos
$sel:end:Intrinsic :: Ann -> Pos
end}, Pretty ColorText
txt) -> ((Pos, Pos), Pretty ColorText)
-> Maybe ((Pos, Pos), Pretty ColorText)
forall a. a -> Maybe a
Just ((Pos
start, Pos
end), Pretty ColorText
txt)
(Ann, Pretty ColorText)
_ -> Maybe ((Pos, Pos), Pretty ColorText)
forall a. Maybe a
Nothing
)
let textEdits :: [TextReplacement]
textEdits =
Map Symbol ((Pos, Pos), Pretty ColorText)
nonGeneratedDefs Map Symbol ((Pos, Pos), Pretty ColorText)
-> (Map Symbol ((Pos, Pos), Pretty ColorText) -> [TextReplacement])
-> [TextReplacement]
forall a b. a -> (a -> b) -> b
& (((Pos, Pos), Pretty ColorText) -> [TextReplacement])
-> Map Symbol ((Pos, Pos), Pretty ColorText) -> [TextReplacement]
forall m a. Monoid m => (a -> m) -> Map Symbol a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap \((Pos
start, Pos
end), Pretty ColorText
txt) -> do
Range
range <- Maybe Range -> [Range]
forall a. Maybe a -> [a]
maybeToList (Maybe Range -> [Range]) -> Maybe Range -> [Range]
forall a b. (a -> b) -> a -> b
$ Ann -> Maybe Range
annToRange (Pos -> Pos -> Ann
Ann.Ann Pos
start Pos
end)
pure $ (Text -> Range -> TextReplacement
TextReplacement (String -> Text
Text.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ Width -> Pretty ColorText -> String
Pretty.toPlain (Int -> Width
Pretty.Width Int
formattingWidth) Pretty ColorText
txt) Range
range)
[TextReplacement] -> MaybeT m [TextReplacement]
forall a. a -> MaybeT m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [TextReplacement]
textEdits
where
isInFormatRange :: Ann.Ann -> Bool
isInFormatRange :: Ann -> Bool
isInFormatRange Ann
ann =
case Maybe (Set Range)
mayRangesToFormat of
Maybe (Set Range)
Nothing -> Bool
True
Just Set Range
rangesToFormat -> (Range -> Bool) -> Set Range -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Ann -> Range -> Bool
annRangeOverlap Ann
ann) Set Range
rangesToFormat
shouldFormatTerm :: Ann.Ann -> Term.Term Symbol Ann.Ann -> Bool
shouldFormatTerm :: Ann -> Term Symbol Ann -> Bool
shouldFormatTerm Ann
ann Term Symbol Ann
trm =
Ann -> Bool
isInFormatRange Ann
ann
Bool -> Bool -> Bool
&& Bool -> Bool
not (Term Symbol Ann -> Bool
isUntypecheckedDoc Term Symbol Ann
trm)
isUntypecheckedDoc :: Term.Term Symbol Ann.Ann -> Bool
isUntypecheckedDoc :: Term Symbol Ann -> Bool
isUntypecheckedDoc Term Symbol Ann
trm =
Term Symbol Ann -> Set Symbol
forall (f :: * -> *) v a. Term f v a -> Set v
ABT.freeVars Term Symbol Ann
trm
Set Symbol -> (Set Symbol -> Set String) -> Set String
forall a b. a -> (a -> b) -> b
& (Symbol -> String) -> Set Symbol -> Set String
forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map Symbol -> String
forall v. Var v => v -> String
Var.nameStr
Set String -> (Set String -> Bool) -> Bool
forall a b. a -> (a -> b) -> b
& String -> Set String -> Bool
forall a. Ord a => a -> Set a -> Bool
Set.member String
"syntax.docUntitledSection"
annRangeOverlap :: Ann.Ann -> Range -> Bool
annRangeOverlap :: Ann -> Range -> Bool
annRangeOverlap Ann
a Range
r =
Ann -> Maybe (Interval Pos)
annToInterval Ann
a Maybe (Interval Pos) -> (Maybe (Interval Pos) -> Bool) -> Bool
forall a b. a -> (a -> b) -> b
& \case
Maybe (Interval Pos)
Nothing -> Bool
False
Just Interval Pos
annI -> Range -> Interval Pos
rangeToInterval Range
r Interval Pos -> Interval Pos -> Bool
forall a. Ord a => Interval a -> Interval a -> Bool
`Interval.overlaps` Interval Pos
annI
removeGeneratedTypeAnnotations ::
UnisonFile Symbol a -> Symbol -> (Term.Term Symbol a) -> (Term.Term Symbol a)
removeGeneratedTypeAnnotations :: forall a.
UnisonFile Symbol a -> Symbol -> Term Symbol a -> Term Symbol a
removeGeneratedTypeAnnotations UnisonFile Symbol a
uf Symbol
v = \case
Term.Ann' Term Symbol a
tm Type Symbol a
_annotation | Bool -> Bool
not (UnisonFile Symbol a -> Symbol -> Bool
forall v a. Eq v => UnisonFile v a -> v -> Bool
hasUserTypeSignature UnisonFile Symbol a
uf Symbol
v) -> Term Symbol a
tm
Term Symbol a
x -> Term Symbol a
x
mkUnisonFilesDeterministic :: Maybe (UnisonFile Symbol Ann.Ann) -> Maybe (TypecheckedUnisonFile Symbol Ann.Ann) -> (Maybe (UnisonFile Symbol Ann.Ann), Maybe (TypecheckedUnisonFile Symbol Ann.Ann))
mkUnisonFilesDeterministic :: Maybe (UnisonFile Symbol Ann)
-> Maybe (TypecheckedUnisonFile Symbol Ann)
-> (Maybe (UnisonFile Symbol Ann),
Maybe (TypecheckedUnisonFile Symbol Ann))
mkUnisonFilesDeterministic Maybe (UnisonFile Symbol Ann)
mayUnisonFile Maybe (TypecheckedUnisonFile Symbol Ann)
mayTypecheckedFile =
let sortedUF :: Maybe (UnisonFile Symbol Ann)
sortedUF =
Maybe (UnisonFile Symbol Ann)
mayUnisonFile
Maybe (UnisonFile Symbol Ann)
-> (Maybe (UnisonFile Symbol Ann)
-> Maybe
(ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])))
-> Maybe
(ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))
forall a b. a -> (a -> b) -> b
& (UnisonFile Symbol Ann
-> Identity
(ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])))
-> Maybe (UnisonFile Symbol Ann)
-> Identity
(Maybe
(ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])))
forall a b (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f b) -> p (Maybe a) (f (Maybe b))
_Just ((UnisonFile Symbol Ann
-> Identity
(ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])))
-> Maybe (UnisonFile Symbol Ann)
-> Identity
(Maybe
(ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))))
-> ((DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> UnisonFile Symbol Ann
-> Identity
(ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])))
-> (DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> Maybe (UnisonFile Symbol Ann)
-> Identity
(Maybe
(ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann)))
-> UnisonFile Symbol Ann
-> Identity
(ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))
#dataDeclarationsId ((Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann)))
-> UnisonFile Symbol Ann
-> Identity
(ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])))
-> ((DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann)))
-> (DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> UnisonFile Symbol Ann
-> Identity
(ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((TypeReferenceId, DataDeclaration Symbol Ann)
-> Identity (TypeReferenceId, DataDeclaration Symbol Ann))
-> Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann))
forall (f :: * -> *) a b.
Traversable f =>
IndexedTraversal Int (f a) (f b) a b
IndexedTraversal
Int
(Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann))
(Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann))
(TypeReferenceId, DataDeclaration Symbol Ann)
(TypeReferenceId, DataDeclaration Symbol Ann)
traversed (((TypeReferenceId, DataDeclaration Symbol Ann)
-> Identity (TypeReferenceId, DataDeclaration Symbol Ann))
-> Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann)))
-> ((DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> (TypeReferenceId, DataDeclaration Symbol Ann)
-> Identity (TypeReferenceId, DataDeclaration Symbol Ann))
-> (DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> (TypeReferenceId, DataDeclaration Symbol Ann)
-> Identity (TypeReferenceId, DataDeclaration Symbol Ann)
forall s t a b. Field2 s t a b => Lens s t a b
Lens
(TypeReferenceId, DataDeclaration Symbol Ann)
(TypeReferenceId, DataDeclaration Symbol Ann)
(DataDeclaration Symbol Ann)
(DataDeclaration Symbol Ann)
_2 ((DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> Maybe (UnisonFile Symbol Ann)
-> Identity
(Maybe
(ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))))
-> (DataDeclaration Symbol Ann -> DataDeclaration Symbol Ann)
-> Maybe (UnisonFile Symbol Ann)
-> Maybe
(ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ DataDeclaration Symbol Ann -> DataDeclaration Symbol Ann
forall v. DataDeclaration v Ann -> DataDeclaration v Ann
sortConstructors
Maybe
(ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))
-> (Maybe
(ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))
-> Maybe (UnisonFile Symbol Ann))
-> Maybe (UnisonFile Symbol Ann)
forall a b. a -> (a -> b) -> b
& (ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])
-> Identity (UnisonFile Symbol Ann))
-> Maybe
(ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))
-> Identity (Maybe (UnisonFile Symbol Ann))
forall a b (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f b) -> p (Maybe a) (f (Maybe b))
_Just ((ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])
-> Identity (UnisonFile Symbol Ann))
-> Maybe
(ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))
-> Identity (Maybe (UnisonFile Symbol Ann)))
-> ((DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])
-> Identity (UnisonFile Symbol Ann))
-> (DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> Maybe
(ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))
-> Identity (Maybe (UnisonFile Symbol Ann))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann)))
-> ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])
-> Identity (UnisonFile Symbol Ann)
#effectDeclarationsId ((Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann)))
-> ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])
-> Identity (UnisonFile Symbol Ann))
-> ((DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann)))
-> (DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])
-> Identity (UnisonFile Symbol Ann)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity (TypeReferenceId, EffectDeclaration Symbol Ann))
-> Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann))
forall (f :: * -> *) a b.
Traversable f =>
IndexedTraversal Int (f a) (f b) a b
IndexedTraversal
Int
(Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann))
(Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann))
(TypeReferenceId, EffectDeclaration Symbol Ann)
(TypeReferenceId, EffectDeclaration Symbol Ann)
traversed (((TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity (TypeReferenceId, EffectDeclaration Symbol Ann))
-> Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann)))
-> ((DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> (TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity (TypeReferenceId, EffectDeclaration Symbol Ann))
-> (DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (EffectDeclaration Symbol Ann
-> Identity (EffectDeclaration Symbol Ann))
-> (TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity (TypeReferenceId, EffectDeclaration Symbol Ann)
forall s t a b. Field2 s t a b => Lens s t a b
Lens
(TypeReferenceId, EffectDeclaration Symbol Ann)
(TypeReferenceId, EffectDeclaration Symbol Ann)
(EffectDeclaration Symbol Ann)
(EffectDeclaration Symbol Ann)
_2 ((EffectDeclaration Symbol Ann
-> Identity (EffectDeclaration Symbol Ann))
-> (TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity (TypeReferenceId, EffectDeclaration Symbol Ann))
-> ((DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> EffectDeclaration Symbol Ann
-> Identity (EffectDeclaration Symbol Ann))
-> (DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> (TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity (TypeReferenceId, EffectDeclaration Symbol Ann)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> EffectDeclaration Symbol Ann
-> Identity (EffectDeclaration Symbol Ann)
forall v a (p :: * -> * -> *) (f :: * -> *).
(Profunctor p, Functor f) =>
p (DataDeclaration v a) (f (DataDeclaration v a))
-> p (EffectDeclaration v a) (f (EffectDeclaration v a))
Decl.asDataDecl_ ((DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> Maybe
(ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))
-> Identity (Maybe (UnisonFile Symbol Ann)))
-> (DataDeclaration Symbol Ann -> DataDeclaration Symbol Ann)
-> Maybe
(ReplaceArgs
(UnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))
-> Maybe (UnisonFile Symbol Ann)
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ DataDeclaration Symbol Ann -> DataDeclaration Symbol Ann
forall v. DataDeclaration v Ann -> DataDeclaration v Ann
sortConstructors
sortedTF :: Maybe (TypecheckedUnisonFile Symbol Ann)
sortedTF =
Maybe (TypecheckedUnisonFile Symbol Ann)
mayTypecheckedFile
Maybe (TypecheckedUnisonFile Symbol Ann)
-> (Maybe (TypecheckedUnisonFile Symbol Ann)
-> Maybe
(ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])))
-> Maybe
(ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))
forall a b. a -> (a -> b) -> b
& (TypecheckedUnisonFile Symbol Ann
-> Identity
(ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])))
-> Maybe (TypecheckedUnisonFile Symbol Ann)
-> Identity
(Maybe
(ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])))
forall a b (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f b) -> p (Maybe a) (f (Maybe b))
_Just ((TypecheckedUnisonFile Symbol Ann
-> Identity
(ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])))
-> Maybe (TypecheckedUnisonFile Symbol Ann)
-> Identity
(Maybe
(ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))))
-> ((DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> TypecheckedUnisonFile Symbol Ann
-> Identity
(ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])))
-> (DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> Maybe (TypecheckedUnisonFile Symbol Ann)
-> Identity
(Maybe
(ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann)))
-> TypecheckedUnisonFile Symbol Ann
-> Identity
(ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))
#dataDeclarationsId' ((Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann)))
-> TypecheckedUnisonFile Symbol Ann
-> Identity
(ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])))
-> ((DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann)))
-> (DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> TypecheckedUnisonFile Symbol Ann
-> Identity
(ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((TypeReferenceId, DataDeclaration Symbol Ann)
-> Identity (TypeReferenceId, DataDeclaration Symbol Ann))
-> Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann))
forall (f :: * -> *) a b.
Traversable f =>
IndexedTraversal Int (f a) (f b) a b
IndexedTraversal
Int
(Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann))
(Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann))
(TypeReferenceId, DataDeclaration Symbol Ann)
(TypeReferenceId, DataDeclaration Symbol Ann)
traversed (((TypeReferenceId, DataDeclaration Symbol Ann)
-> Identity (TypeReferenceId, DataDeclaration Symbol Ann))
-> Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann)))
-> ((DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> (TypeReferenceId, DataDeclaration Symbol Ann)
-> Identity (TypeReferenceId, DataDeclaration Symbol Ann))
-> (DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, DataDeclaration Symbol Ann))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> (TypeReferenceId, DataDeclaration Symbol Ann)
-> Identity (TypeReferenceId, DataDeclaration Symbol Ann)
forall s t a b. Field2 s t a b => Lens s t a b
Lens
(TypeReferenceId, DataDeclaration Symbol Ann)
(TypeReferenceId, DataDeclaration Symbol Ann)
(DataDeclaration Symbol Ann)
(DataDeclaration Symbol Ann)
_2 ((DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> Maybe (TypecheckedUnisonFile Symbol Ann)
-> Identity
(Maybe
(ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))))
-> (DataDeclaration Symbol Ann -> DataDeclaration Symbol Ann)
-> Maybe (TypecheckedUnisonFile Symbol Ann)
-> Maybe
(ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ DataDeclaration Symbol Ann -> DataDeclaration Symbol Ann
forall v. DataDeclaration v Ann -> DataDeclaration v Ann
sortConstructors
Maybe
(ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))
-> (Maybe
(ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))
-> Maybe (TypecheckedUnisonFile Symbol Ann))
-> Maybe (TypecheckedUnisonFile Symbol Ann)
forall a b. a -> (a -> b) -> b
& (ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])
-> Identity (TypecheckedUnisonFile Symbol Ann))
-> Maybe
(ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))
-> Identity (Maybe (TypecheckedUnisonFile Symbol Ann))
forall a b (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p a (f b) -> p (Maybe a) (f (Maybe b))
_Just ((ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])
-> Identity (TypecheckedUnisonFile Symbol Ann))
-> Maybe
(ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))
-> Identity (Maybe (TypecheckedUnisonFile Symbol Ann)))
-> ((DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])
-> Identity (TypecheckedUnisonFile Symbol Ann))
-> (DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> Maybe
(ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))
-> Identity (Maybe (TypecheckedUnisonFile Symbol Ann))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann)))
-> ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])
-> Identity (TypecheckedUnisonFile Symbol Ann)
#effectDeclarationsId' ((Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann)))
-> ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])
-> Identity (TypecheckedUnisonFile Symbol Ann))
-> ((DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann)))
-> (DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol])
-> Identity (TypecheckedUnisonFile Symbol Ann)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity (TypeReferenceId, EffectDeclaration Symbol Ann))
-> Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann))
forall (f :: * -> *) a b.
Traversable f =>
IndexedTraversal Int (f a) (f b) a b
IndexedTraversal
Int
(Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann))
(Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann))
(TypeReferenceId, EffectDeclaration Symbol Ann)
(TypeReferenceId, EffectDeclaration Symbol Ann)
traversed (((TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity (TypeReferenceId, EffectDeclaration Symbol Ann))
-> Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann)))
-> ((DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> (TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity (TypeReferenceId, EffectDeclaration Symbol Ann))
-> (DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity
(Map Symbol (TypeReferenceId, EffectDeclaration Symbol Ann))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (EffectDeclaration Symbol Ann
-> Identity (EffectDeclaration Symbol Ann))
-> (TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity (TypeReferenceId, EffectDeclaration Symbol Ann)
forall s t a b. Field2 s t a b => Lens s t a b
Lens
(TypeReferenceId, EffectDeclaration Symbol Ann)
(TypeReferenceId, EffectDeclaration Symbol Ann)
(EffectDeclaration Symbol Ann)
(EffectDeclaration Symbol Ann)
_2 ((EffectDeclaration Symbol Ann
-> Identity (EffectDeclaration Symbol Ann))
-> (TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity (TypeReferenceId, EffectDeclaration Symbol Ann))
-> ((DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> EffectDeclaration Symbol Ann
-> Identity (EffectDeclaration Symbol Ann))
-> (DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> (TypeReferenceId, EffectDeclaration Symbol Ann)
-> Identity (TypeReferenceId, EffectDeclaration Symbol Ann)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> EffectDeclaration Symbol Ann
-> Identity (EffectDeclaration Symbol Ann)
forall v a (p :: * -> * -> *) (f :: * -> *).
(Profunctor p, Functor f) =>
p (DataDeclaration v a) (f (DataDeclaration v a))
-> p (EffectDeclaration v a) (f (EffectDeclaration v a))
Decl.asDataDecl_ ((DataDeclaration Symbol Ann
-> Identity (DataDeclaration Symbol Ann))
-> Maybe
(ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))
-> Identity (Maybe (TypecheckedUnisonFile Symbol Ann)))
-> (DataDeclaration Symbol Ann -> DataDeclaration Symbol Ann)
-> Maybe
(ReplaceArgs
(TypecheckedUnisonFile Symbol Ann)
(Unify
(TypeReferenceId,
DataDeclaration (P 1 Symbol 'PTag) (P 0 Ann 'PTag))
(TypeReferenceId, DataDeclaration Symbol Ann)
++ '[ 'Sub 1 Symbol]))
-> Maybe (TypecheckedUnisonFile Symbol Ann)
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ DataDeclaration Symbol Ann -> DataDeclaration Symbol Ann
forall v. DataDeclaration v Ann -> DataDeclaration v Ann
sortConstructors
in (Maybe (UnisonFile Symbol Ann)
sortedUF, Maybe (TypecheckedUnisonFile Symbol Ann)
sortedTF)
sortConstructors :: Decl.DataDeclaration v Ann.Ann -> Decl.DataDeclaration v Ann.Ann
sortConstructors :: forall v. DataDeclaration v Ann -> DataDeclaration v Ann
sortConstructors DataDeclaration v Ann
dd =
DataDeclaration v Ann
dd DataDeclaration v Ann
-> (DataDeclaration v Ann -> DataDeclaration v Ann)
-> DataDeclaration v Ann
forall a b. a -> (a -> b) -> b
& ([(Ann, v, Type v Ann)] -> Identity [(Ann, v, Type v Ann)])
-> DataDeclaration v Ann -> Identity (DataDeclaration v Ann)
forall v a (f :: * -> *).
Functor f =>
([(a, v, Type v a)] -> f [(a, v, Type v a)])
-> DataDeclaration v a -> f (DataDeclaration v a)
Decl.constructors_ (([(Ann, v, Type v Ann)] -> Identity [(Ann, v, Type v Ann)])
-> DataDeclaration v Ann -> Identity (DataDeclaration v Ann))
-> ([(Ann, v, Type v Ann)] -> [(Ann, v, Type v Ann)])
-> DataDeclaration v Ann
-> DataDeclaration v Ann
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn @Ann.Ann (Getting Ann (Ann, v, Type v Ann) Ann -> (Ann, v, Type v Ann) -> Ann
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting Ann (Ann, v, Type v Ann) Ann
forall s t a b. Field1 s t a b => Lens s t a b
Lens (Ann, v, Type v Ann) (Ann, v, Type v Ann) Ann Ann
_1)
annToRange :: Ann.Ann -> Maybe Range
annToRange :: Ann -> Maybe Range
annToRange = \case
Ann
Ann.Intrinsic -> Maybe Range
forall a. Maybe a
Nothing
Ann
Ann.External -> Maybe Range
forall a. Maybe a
Nothing
Ann.GeneratedFrom Ann
a -> Ann -> Maybe Range
annToRange Ann
a
Ann.Ann Pos
start Pos
end -> Range -> Maybe Range
forall a. a -> Maybe a
Just (Range -> Maybe Range) -> Range -> Maybe Range
forall a b. (a -> b) -> a -> b
$ Pos -> Pos -> Range
Range Pos
start Pos
end
rangeToInterval :: Range -> Interval.Interval Pos.Pos
rangeToInterval :: Range -> Interval Pos
rangeToInterval (Range Pos
start Pos
end) =
Pos -> Pos -> Interval Pos
forall a. a -> a -> Interval a
Interval.ClosedInterval Pos
start Pos
end
annToInterval :: Ann.Ann -> Maybe (Interval.Interval Pos.Pos)
annToInterval :: Ann -> Maybe (Interval Pos)
annToInterval Ann
ann = Ann -> Maybe Range
annToRange Ann
ann Maybe Range -> (Range -> Interval Pos) -> Maybe (Interval Pos)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> Range -> Interval Pos
rangeToInterval
hasUserTypeSignature :: (Eq v) => UnisonFile v a -> v -> Bool
hasUserTypeSignature :: forall v a. Eq v => UnisonFile v a -> v -> Bool
hasUserTypeSignature UnisonFile v a
parsedFile v
sym =
Map v (a, Term v a) -> [(v, (a, Term v a))]
forall k a. Map k a -> [(k, a)]
Map.toList (UnisonFile v a -> Map v (a, Term v a)
forall v a. UnisonFile v a -> Map v (a, Term v a)
UF.terms UnisonFile v a
parsedFile)
[(v, (a, Term v a))] -> ([(v, (a, Term v a))] -> Bool) -> Bool
forall a b. a -> (a -> b) -> b
& ((v, (a, Term v a)) -> Bool) -> [(v, (a, Term v a))] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\(v
v, (a
_, Term v a
trm)) -> v
v v -> v -> Bool
forall a. Eq a => a -> a -> Bool
== v
sym Bool -> Bool -> Bool
&& Maybe (Type v a) -> Bool
forall a. Maybe a -> Bool
isJust (Term v a -> Maybe (Type v a)
forall v a. Term v a -> Maybe (Type v a)
Term.getTypeAnnotation Term v a
trm))
data TextReplacement = TextReplacement
{
TextReplacement -> Text
replacementText :: Text,
TextReplacement -> Range
replacementRange :: Range
}
deriving (TextReplacement -> TextReplacement -> Bool
(TextReplacement -> TextReplacement -> Bool)
-> (TextReplacement -> TextReplacement -> Bool)
-> Eq TextReplacement
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TextReplacement -> TextReplacement -> Bool
== :: TextReplacement -> TextReplacement -> Bool
$c/= :: TextReplacement -> TextReplacement -> Bool
/= :: TextReplacement -> TextReplacement -> Bool
Eq, Int -> TextReplacement -> ShowS
[TextReplacement] -> ShowS
TextReplacement -> String
(Int -> TextReplacement -> ShowS)
-> (TextReplacement -> String)
-> ([TextReplacement] -> ShowS)
-> Show TextReplacement
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TextReplacement -> ShowS
showsPrec :: Int -> TextReplacement -> ShowS
$cshow :: TextReplacement -> String
show :: TextReplacement -> String
$cshowList :: [TextReplacement] -> ShowS
showList :: [TextReplacement] -> ShowS
Show)
applyTextReplacements :: [TextReplacement] -> Text -> Text
applyTextReplacements :: [TextReplacement] -> Text -> Text
applyTextReplacements [TextReplacement]
replacements Text
inputText = [(Int, Int, Maybe Text)] -> [Text] -> Builder
applyTextReplacementsHelper [(Int, Int, Maybe Text)]
relativeOffsets (Text -> [Text]
Text.lines Text
inputText) Builder -> (Builder -> Text) -> Text
forall a b. a -> (a -> b) -> b
& Builder -> Text
TB.run
where
tupleReplacements :: [(Int, Int, Maybe Text)]
tupleReplacements :: [(Int, Int, Maybe Text)]
tupleReplacements =
[TextReplacement]
replacements
[TextReplacement]
-> ([TextReplacement] -> [(Int, Int, Maybe Text)])
-> [(Int, Int, Maybe Text)]
forall a b. a -> (a -> b) -> b
& (TextReplacement -> [(Int, Int, Maybe Text)])
-> [TextReplacement] -> [(Int, Int, Maybe Text)]
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap
( \(TextReplacement Text
txt (Range (Pos.Pos Int
startLine Int
startCol) (Pos.Pos Int
endLine Int
endCol))) ->
[(Int
startLine Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1, Int
startCol Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1, Maybe Text
forall a. Maybe a
Nothing), (Int
endLine Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1, Int
endCol Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1, Text -> Maybe Text
forall a. a -> Maybe a
Just Text
txt)]
)
relativeOffsets :: [(Int, Int, Maybe Text)]
relativeOffsets = [(Int, Int, Maybe Text)] -> [(Int, Int, Maybe Text)]
relativizeOffsets (((Int, Int, Maybe Text) -> (Int, Int))
-> [(Int, Int, Maybe Text)] -> [(Int, Int, Maybe Text)]
forall b a. Ord b => (a -> b) -> [a] -> [a]
List.sortOn (\(Int
line, Int
col, Maybe Text
_) -> (Int
line, Int
col)) [(Int, Int, Maybe Text)]
tupleReplacements)
relativizeOffsets :: [(Int, Int, Maybe Text)] -> [(Int, Int, Maybe Text)]
relativizeOffsets :: [(Int, Int, Maybe Text)] -> [(Int, Int, Maybe Text)]
relativizeOffsets [(Int, Int, Maybe Text)]
xs =
let grouped :: [[(Int, Int, Maybe Text)]]
grouped = ((Int, Int, Maybe Text) -> (Int, Int, Maybe Text) -> Bool)
-> [(Int, Int, Maybe Text)] -> [[(Int, Int, Maybe Text)]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
List.groupBy (\(Int
a, Int
_, Maybe Text
_) (Int
b, Int
_, Maybe Text
_) -> Int
a Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
b) [(Int, Int, Maybe Text)]
xs
in [[(Int, Int, Maybe Text)]]
grouped
[[(Int, Int, Maybe Text)]]
-> ([[(Int, Int, Maybe Text)]] -> [[(Int, Int, Maybe Text)]])
-> [[(Int, Int, Maybe Text)]]
forall a b. a -> (a -> b) -> b
& ([(Int, Int, Maybe Text)] -> [(Int, Int, Maybe Text)])
-> [[(Int, Int, Maybe Text)]] -> [[(Int, Int, Maybe Text)]]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Int, [(Int, Int, Maybe Text)]) -> [(Int, Int, Maybe Text)]
forall a b. (a, b) -> b
snd ((Int, [(Int, Int, Maybe Text)]) -> [(Int, Int, Maybe Text)])
-> ([(Int, Int, Maybe Text)] -> (Int, [(Int, Int, Maybe Text)]))
-> [(Int, Int, Maybe Text)]
-> [(Int, Int, Maybe Text)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> (Int, Int, Maybe Text) -> (Int, (Int, Int, Maybe Text)))
-> Int
-> [(Int, Int, Maybe Text)]
-> (Int, [(Int, Int, Maybe Text)])
forall (t :: * -> *) s a b.
Traversable t =>
(s -> a -> (s, b)) -> s -> t a -> (s, t b)
List.mapAccumL (\Int
acc (Int
line, Int
col, Maybe Text
r) -> (Int
col, (Int
line, Int
col Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
acc, Maybe Text
r))) Int
0)
[[(Int, Int, Maybe Text)]]
-> ([[(Int, Int, Maybe Text)]] -> [(Int, Int, Maybe Text)])
-> [(Int, Int, Maybe Text)]
forall a b. a -> (a -> b) -> b
& [[(Int, Int, Maybe Text)]] -> [(Int, Int, Maybe Text)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
List.concat
[(Int, Int, Maybe Text)]
-> ([(Int, Int, Maybe Text)] -> [(Int, Int, Maybe Text)])
-> [(Int, Int, Maybe Text)]
forall a b. a -> (a -> b) -> b
& (Int, [(Int, Int, Maybe Text)]) -> [(Int, Int, Maybe Text)]
forall a b. (a, b) -> b
snd ((Int, [(Int, Int, Maybe Text)]) -> [(Int, Int, Maybe Text)])
-> ([(Int, Int, Maybe Text)] -> (Int, [(Int, Int, Maybe Text)]))
-> [(Int, Int, Maybe Text)]
-> [(Int, Int, Maybe Text)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> (Int, Int, Maybe Text) -> (Int, (Int, Int, Maybe Text)))
-> Int
-> [(Int, Int, Maybe Text)]
-> (Int, [(Int, Int, Maybe Text)])
forall (t :: * -> *) s a b.
Traversable t =>
(s -> a -> (s, b)) -> s -> t a -> (s, t b)
List.mapAccumL (\Int
acc (Int
line, Int
col, Maybe Text
r) -> (Int
line, (Int
line Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
acc, Int
col, Maybe Text
r))) Int
0
applyTextReplacementsHelper :: [(Int, Int, Maybe Text)] -> [Text] -> TB.Builder
applyTextReplacementsHelper :: [(Int, Int, Maybe Text)] -> [Text] -> Builder
applyTextReplacementsHelper [] [Text]
ls = Builder -> [Builder] -> Builder
forall (foldable :: * -> *).
Foldable foldable =>
Builder -> foldable Builder -> Builder
TB.intercalate Builder
"\n" (Text -> Builder
TB.text (Text -> Builder) -> [Text] -> [Builder]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Text]
ls)
applyTextReplacementsHelper [(Int, Int, Maybe Text)]
_ [] = Builder
forall a. Monoid a => a
mempty
applyTextReplacementsHelper ((Int
0, Int
col, Maybe Text
r) : [(Int, Int, Maybe Text)]
rest) (Text
l : [Text]
ls) =
let (Text
prefix, Text
suffix) = Int -> Text -> (Text, Text)
Text.splitAt Int
col Text
l
in Text -> Builder
TB.text (Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
prefix Maybe Text
r) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> [(Int, Int, Maybe Text)] -> [Text] -> Builder
applyTextReplacementsHelper [(Int, Int, Maybe Text)]
rest (Text
suffix Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: [Text]
ls)
applyTextReplacementsHelper ((Int
line, Int
col, Maybe Text
r) : [(Int, Int, Maybe Text)]
rest) [Text]
ls =
case Int -> [Text] -> ([Text], [Text])
forall a. Int -> [a] -> ([a], [a])
List.splitAt Int
line [Text]
ls of
([Text]
prefixLines, []) -> Builder -> [Builder] -> Builder
forall (foldable :: * -> *).
Foldable foldable =>
Builder -> foldable Builder -> Builder
TB.intercalate Builder
"\n" (Text -> Builder
TB.text (Text -> Builder) -> [Text] -> [Builder]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Text]
prefixLines)
([Text]
prefixLines, (Text
lastLine : [Text]
restLines)) ->
let (Text
prefixChars, Text
suffixChars) = Int -> Text -> (Text, Text)
Text.splitAt Int
col Text
lastLine
segment :: Builder
segment =
(Builder -> [Builder] -> Builder
forall (foldable :: * -> *).
Foldable foldable =>
Builder -> foldable Builder -> Builder
TB.intercalate Builder
"\n" ([Builder] -> Builder) -> [Builder] -> Builder
forall a b. (a -> b) -> a -> b
$ (Text -> Builder) -> [Text] -> [Builder]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Builder
TB.text [Text]
prefixLines)
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Char -> Builder
TB.char Char
'\n'
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Text -> Builder
TB.text Text
prefixChars
in Builder -> (Text -> Builder) -> Maybe Text -> Builder
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Builder
segment Text -> Builder
TB.text Maybe Text
r Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> [(Int, Int, Maybe Text)] -> [Text] -> Builder
applyTextReplacementsHelper [(Int, Int, Maybe Text)]
rest (Text
suffixChars Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: [Text]
restLines)