on master help sign in
Introduce Localizer module

Start figuring out how to localize types again. This version is opaque, allowing me to find all the current uses, and it can be constructed from a list of source inputs. This should be good for `make` and `repl`

commit296396abf24ac8b2632ee1b473c40a5485c05d8a authorEvan Czaplicki <evancz@users.noreply.github.com> dateWed 18 Apr 2018, 14:28 (9 years ago) parentd87788f
2 files changed+102
compiler/src/Reporting/Render/Type/Localizer.hs+101 elm.cabal+1
compiler/src/Reporting/Render/Type/Localizer.hsnew file+101
@@ -0,0 +1,101 @@
1+{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
3+module Reporting.Render.Type.Localizer
4+ ( Localizer
5+ , toDoc
6+ , empty
7+ , fromImports
8+
9+ )
10+ where
11+
12+
13+import qualified Data.Map as Map
14+import qualified Data.Set as Set
15+
16+import qualified AST.Source as Src
17+import qualified Elm.Name as N
18+import Reporting.Doc ((<>))
19+import qualified Reporting.Doc as D
20+import qualified Reporting.Annotation as A
21+
22+
23+
24+-- LOCALIZER
25+
26+
27+newtype Localizer =
28+ Localizer (Map.Map N.Name Import)
29+
30+
31+data Import =
32+ Import
33+ { _alias :: Maybe N.Name
34+ , _exposing :: Exposing
35+ }
36+
37+
38+data Exposing
39+ = All
40+ | Only (Set.Set N.Name)
41+
42+
43+empty :: Localizer
44+empty =
45+ Localizer Map.empty
46+
47+
48+
49+-- LOCALIZE
50+
51+
52+toDoc :: Localizer -> N.Name -> N.Name -> D.Doc
53+toDoc (Localizer localizer) home name =
54+ case Map.lookup home localizer of
55+ Nothing ->
56+ D.fromName home <> "." <> D.fromName name
57+
58+ Just (Import alias exposing) ->
59+ case exposing of
60+ All ->
61+ D.fromName name
62+
63+ Only set ->
64+ if Set.member name set then
65+ D.fromName name
66+ else
67+ D.fromName (maybe home id alias) <> "." <> D.fromName name
68+
69+
70+
71+-- MAKE
72+
73+
74+fromImports :: [Src.Import] -> Localizer
75+fromImports imports =
76+ Localizer $ Map.fromList (map toPair imports)
77+
78+
79+toPair :: Src.Import -> (N.Name, Import)
80+toPair (Src.Import (A.At _ name) alias exposing) =
81+ ( name
82+ , Import alias (toExposing exposing)
83+ )
84+
85+
86+toExposing :: Src.Exposing -> Exposing
87+toExposing exposing =
88+ case exposing of
89+ Src.Open ->
90+ All
91+
92+ Src.Explicit exposedList ->
93+ Only (foldr addType Set.empty exposedList)
94+
95+
96+addType :: A.Located Src.Exposed -> Set.Set N.Name -> Set.Set N.Name
97+addType (A.At _ exposed) types =
98+ case exposed of
99+ Src.Lower _ -> types
100+ Src.Upper name _ -> Set.insert name types
101+ Src.Operator _ -> types
elm.cabal+1
@@ -208,6 +208,7 @@ Executable elm
208208 Reporting.Region,
209209 Reporting.Render.Code,
210210 Reporting.Render.Type,
211+ Reporting.Render.Type.Localizer,
211212 Reporting.Report,
212213 Reporting.Result,
213214 Reporting.Suggest,