Skip to content

Commit c94b046

Browse files
committed
something
1 parent a8c913e commit c94b046

8 files changed

Lines changed: 190 additions & 15 deletions

File tree

purescript.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ library
274274
Language.PureScript.CST.Parser
275275
Language.PureScript.CST.Positions
276276
Language.PureScript.CST.Print
277+
Language.PureScript.Ide.ToIde
278+
Language.PureScript.Ide.ToI
277279
Language.PureScript.CST.Traversals
278280
Language.PureScript.CST.Traversals.Type
279281
Language.PureScript.CST.Types

src/Language/PureScript/Ide/Rebuild.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ shushProgress ma =
199199
-- | Stops any kind of codegen
200200
shushCodegen :: Monad m => P.MakeActions m -> P.MakeActions m
201201
shushCodegen ma =
202-
ma { P.codegen = \_ _ _ -> pure ()
202+
ma { P.codegen = \_ _ _ _ -> pure ()
203203
, P.ffiCodegen = \_ -> pure ()
204204
}
205205

src/Language/PureScript/Ide/SourceFile.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@ import Protolude
2424

2525
import Control.Parallel.Strategies (withStrategy, parList, rseq)
2626
import Data.Map qualified as Map
27-
import Language.PureScript qualified as P
2827
import Language.PureScript.CST qualified as CST
2928
import Language.PureScript.Ide.Error (IdeError)
3029
import Language.PureScript.Ide.Types (DefinitionSites, IdeNamespace(..), IdeNamespaced(..), TypeAnnotations)
3130
import Language.PureScript.Ide.Util (ideReadFile)
31+
import Language.PureScript.AST.Declarations qualified as P
32+
import Language.PureScript.AST.SourcePos qualified as P
33+
import Language.PureScript.Names qualified as P
34+
import Language.PureScript.Types qualified as P
3235

3336
parseModule :: FilePath -> Text -> Either FilePath (FilePath, P.Module)
3437
parseModule path file =

src/Language/PureScript/Ide/State.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Language.PureScript.Ide.State
1919
( getLoadedModulenames
2020
, getExternFiles
2121
, getFileState
22+
, toIdeDeclarationAnn
2223
, resetIdeState
2324
, cacheRebuild
2425
, cachedRebuild
@@ -251,6 +252,20 @@ populateVolatileStateSTM ref = do
251252
setVolatileStateSTM ref (IdeVolatileState (AstData asts) (map reResolved results) rebuildCache)
252253
pure (force results)
253254

255+
toIdeDeclarationAnn :: P.Module -> ExternsFile -> [IdeDeclarationAnn]
256+
toIdeDeclarationAnn m e = results
257+
where
258+
asts = extractAstInformation m
259+
(moduleDeclarations, reexportRefs) = convertExterns e
260+
results =
261+
moduleDeclarations
262+
& resolveDataConstructorsForModule
263+
& resolveLocationsForModule asts
264+
& resolveDocumentationForModule m
265+
-- & resolveInstances externs
266+
-- & resolveOperators
267+
-- & resolveReexports reexportRefs
268+
254269
resolveLocations
255270
:: ModuleMap (DefinitionSites P.SourceSpan, TypeAnnotations)
256271
-> ModuleMap [IdeDeclarationAnn]
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
module Language.PureScript.Ide.ToIde where
2+
3+
import Protolude hiding (moduleName, unzip)
4+
5+
import Control.Concurrent.STM (TVar, modifyTVar, readTVar, readTVarIO, writeTVar)
6+
import Control.Lens (Ixed(..), preview, view, (%~), (.~), (^.))
7+
import Data.IORef (readIORef, writeIORef)
8+
import Data.Map.Lazy qualified as Map
9+
import Data.Time.Clock (UTCTime)
10+
import Data.Zip (unzip)
11+
import Language.PureScript.Docs.Convert.Single (convertComments)
12+
import Language.PureScript.Externs (ExternsDeclaration(..), ExternsFile(..))
13+
import Language.PureScript.Ide.Externs (convertExterns)
14+
import Language.PureScript.Ide.SourceFile (extractAstInformation)
15+
import Language.PureScript.Ide.Types
16+
import Language.PureScript.Ide.Util (discardAnn, displayTimeSpec, logPerf, opNameT, properNameT, runLogger)
17+
import System.Directory (getModificationTime)
18+
import Database.SQLite.Simple qualified as SQLite
19+
import Debug.Trace qualified as Debug
20+
import Language.PureScript.AST.Declarations (Module (..))
21+
import Language.PureScript.AST.SourcePos qualified as P
22+
import Language.PureScript.Names qualified as P
23+
import Data.Text (Text)
24+
import Language.PureScript.AST.Declarations qualified as P
25+
import Language.PureScript.Comments qualified as P
26+
import Data.Maybe (Maybe)
27+
28+
toIdeDeclarationAnn :: Module -> ExternsFile -> [IdeDeclarationAnn]
29+
toIdeDeclarationAnn m e = results
30+
where
31+
asts = extractAstInformation m
32+
(moduleDeclarations, reexportRefs) = convertExterns e
33+
results =
34+
moduleDeclarations
35+
-- & resolveDataConstructorsForModule
36+
& resolveLocationsForModule asts
37+
& resolveDocumentationForModule m
38+
-- & resolveInstances externs
39+
-- & resolveOperators
40+
-- & resolveReexports reexportRefs
41+
42+
43+
resolveLocationsForModule
44+
:: (DefinitionSites P.SourceSpan, TypeAnnotations)
45+
-> [IdeDeclarationAnn]
46+
-> [IdeDeclarationAnn]
47+
resolveLocationsForModule (defs, types) =
48+
map convertDeclaration
49+
where
50+
convertDeclaration :: IdeDeclarationAnn -> IdeDeclarationAnn
51+
convertDeclaration (IdeDeclarationAnn ann d) = convertDeclaration'
52+
annotateFunction
53+
annotateValue
54+
annotateDataConstructor
55+
annotateType
56+
annotateType -- type classes live in the type namespace
57+
annotateModule
58+
d
59+
where
60+
annotateFunction x = IdeDeclarationAnn (ann { _annLocation = Map.lookup (IdeNamespaced IdeNSValue (P.runIdent x)) defs
61+
, _annTypeAnnotation = Map.lookup x types
62+
})
63+
annotateValue x = IdeDeclarationAnn (ann {_annLocation = Map.lookup (IdeNamespaced IdeNSValue x) defs})
64+
annotateDataConstructor x = IdeDeclarationAnn (ann {_annLocation = Map.lookup (IdeNamespaced IdeNSValue x) defs})
65+
annotateType x = IdeDeclarationAnn (ann {_annLocation = Map.lookup (IdeNamespaced IdeNSType x) defs})
66+
annotateModule x = IdeDeclarationAnn (ann {_annLocation = Map.lookup (IdeNamespaced IdeNSModule x) defs})
67+
68+
convertDeclaration'
69+
:: (P.Ident -> IdeDeclaration -> IdeDeclarationAnn)
70+
-> (Text -> IdeDeclaration -> IdeDeclarationAnn)
71+
-> (Text -> IdeDeclaration -> IdeDeclarationAnn)
72+
-> (Text -> IdeDeclaration -> IdeDeclarationAnn)
73+
-> (Text -> IdeDeclaration -> IdeDeclarationAnn)
74+
-> (Text -> IdeDeclaration -> IdeDeclarationAnn)
75+
-> IdeDeclaration
76+
-> IdeDeclarationAnn
77+
convertDeclaration' annotateFunction annotateValue annotateDataConstructor annotateType annotateClass annotateModule d =
78+
case d of
79+
IdeDeclValue v ->
80+
annotateFunction (v ^. ideValueIdent) d
81+
IdeDeclType t ->
82+
annotateType (t ^. ideTypeName . properNameT) d
83+
IdeDeclTypeSynonym s ->
84+
annotateType (s ^. ideSynonymName . properNameT) d
85+
IdeDeclDataConstructor dtor ->
86+
annotateDataConstructor (dtor ^. ideDtorName . properNameT) d
87+
IdeDeclTypeClass tc ->
88+
annotateClass (tc ^. ideTCName . properNameT) d
89+
IdeDeclValueOperator operator ->
90+
annotateValue (operator ^. ideValueOpName . opNameT) d
91+
IdeDeclTypeOperator operator ->
92+
annotateType (operator ^. ideTypeOpName . opNameT) d
93+
IdeDeclModule mn ->
94+
annotateModule (P.runModuleName mn) d
95+
96+
resolveDocumentationForModule
97+
:: Module
98+
-> [IdeDeclarationAnn]
99+
-> [IdeDeclarationAnn]
100+
resolveDocumentationForModule (Module _ moduleComments moduleName sdecls _) =
101+
map convertDecl
102+
where
103+
extractDeclComments :: P.Declaration -> [(P.Name, [P.Comment])]
104+
extractDeclComments = \case
105+
P.DataDeclaration (_, cs) _ ctorName _ ctors ->
106+
(P.TyName ctorName, cs) : map dtorComments ctors
107+
P.TypeClassDeclaration (_, cs) tyClassName _ _ _ members ->
108+
(P.TyClassName tyClassName, cs) : concatMap extractDeclComments members
109+
decl ->
110+
maybe [] (\name' -> [(name', snd (P.declSourceAnn decl))]) (name decl)
111+
112+
comments :: Map.Map P.Name [P.Comment]
113+
comments = Map.insert (P.ModName moduleName) moduleComments $
114+
Map.fromListWith (flip (<>)) $ concatMap extractDeclComments sdecls
115+
116+
dtorComments :: P.DataConstructorDeclaration -> (P.Name, [P.Comment])
117+
dtorComments dcd = (P.DctorName (P.dataCtorName dcd), snd (P.dataCtorAnn dcd))
118+
119+
name :: P.Declaration -> Maybe P.Name
120+
name (P.TypeDeclaration d) = Just $ P.IdentName $ P.tydeclIdent d
121+
name decl = P.declName decl
122+
123+
convertDecl :: IdeDeclarationAnn -> IdeDeclarationAnn
124+
convertDecl (IdeDeclarationAnn ann d) =
125+
convertDeclaration'
126+
(annotateValue . P.IdentName)
127+
(annotateValue . P.IdentName . P.Ident)
128+
(annotateValue . P.DctorName . P.ProperName)
129+
(annotateValue . P.TyName . P.ProperName)
130+
(annotateValue . P.TyClassName . P.ProperName)
131+
(annotateValue . P.ModName . P.moduleNameFromString)
132+
d
133+
where
134+
docs :: P.Name -> Text
135+
docs ident = fromMaybe "" $ convertComments =<< Map.lookup ident comments
136+
137+
annotateValue ident = IdeDeclarationAnn (ann { _annDocumentation = Just $ docs ident })
138+
139+
-- resolveDataConstructorsForModule
140+
-- :: [IdeDeclarationAnn]
141+
-- -> [IdeDeclarationAnn]
142+
-- resolveDataConstructorsForModule decls =
143+
-- map (idaDeclaration %~ resolveDataConstructors) decls
144+
-- where
145+
-- resolveDataConstructors :: IdeDeclaration -> IdeDeclaration
146+
-- resolveDataConstructors decl = case decl of
147+
-- IdeDeclType ty ->
148+
-- IdeDeclType (ty & ideTypeDtors .~ fromMaybe [] (Map.lookup (ty ^. ideTypeName) dtors))
149+
-- _ ->
150+
-- decl
151+
--
152+
-- dtors =
153+
-- decls
154+
-- & Map.mapMaybe (preview (idaDeclaration . _IdeDeclDataConstructor))
155+
-- & foldr (\(IdeDataConstructor name typeName type') ->
156+
-- Map.insertWith (<>) typeName [(name, type')]) Map.empty

src/Language/PureScript/Make.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ rebuildModuleWithIndex MakeActions{..} exEnv externs m@(Module _ _ moduleName _
132132
++ "; details:\n" ++ prettyPrintMultipleErrors defaultPPEOptions errs
133133
Right d -> d
134134

135-
evalSupplyT nextVar'' $ codegen renamed docs exts
135+
evalSupplyT nextVar'' $ codegen withPrim renamed docs exts
136136
return exts
137137

138138
-- | Compiles in "make" mode, compiling each module separately to a @.js@ file and an @externs.cbor@ file.

src/Language/PureScript/Make/Actions.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import Data.Text.Encoding qualified as TE
3434
import Data.Time.Clock (UTCTime)
3535
import Data.Version (showVersion)
3636
import Language.JavaScript.Parser qualified as JS
37-
import Language.PureScript.AST (SourcePos(..))
37+
import Language.PureScript.AST (SourcePos(..), Module)
3838
import Language.PureScript.Bundle qualified as Bundle
3939
import Language.PureScript.CodeGen.JS qualified as J
4040
import Language.PureScript.CodeGen.JS.Printer (prettyPrintJS, prettyPrintJSWithSourceMaps)
@@ -113,7 +113,7 @@ data MakeActions m = MakeActions
113113
, readExterns :: ModuleName -> m (FilePath, Maybe ExternsFile)
114114
-- ^ Read the externs file for a module as a string and also return the actual
115115
-- path for the file.
116-
, codegen :: CF.Module CF.Ann -> Docs.Module -> ExternsFile -> SupplyT m ()
116+
, codegen :: Module -> CF.Module CF.Ann -> Docs.Module -> ExternsFile -> SupplyT m ()
117117
-- ^ Run the code generator for the module and write any required output files.
118118
, ffiCodegen :: CF.Module CF.Ann -> m ()
119119
-- ^ Check ffi and print it in the output directory.
@@ -247,12 +247,12 @@ buildMakeActions outputDir filePathMap foreigns usePrefix =
247247
when (S.member Docs codegenTargets) $ for_ Docs.Prim.primModules $ \docsMod@Docs.Module{..} ->
248248
writeJSONFile (outputFilename modName "docs.json") docsMod
249249

250-
codegen :: CF.Module CF.Ann -> Docs.Module -> ExternsFile -> SupplyT Make ()
251-
codegen m docs exts = do
250+
codegen :: Module -> CF.Module CF.Ann -> Docs.Module -> ExternsFile -> SupplyT Make ()
251+
codegen ast m docs exts = do
252252
let mn = CF.moduleName m
253253
lift $ writeCborFile (outputFilename mn externsFileName) exts
254254
lift $ sqliteInit outputDir
255-
lift $ sqliteExtern outputDir docs exts
255+
lift $ sqliteExtern outputDir ast docs exts
256256
codegenTargets <- lift $ asks optionsCodegenTargets
257257
when (S.member CoreFn codegenTargets) $ do
258258
let coreFnFile = targetFilename mn CoreFn

src/Language/PureScript/Make/IdeCache.hs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module Language.PureScript.Make.IdeCache where
22

33
import Prelude
4+
5+
import Language.PureScript.Ide.ToIde (toIdeDeclarationAnn)
46
import Database.SQLite.Simple (NamedParam(..))
57
import Database.SQLite.Simple qualified as SQLite
68
import Codec.Serialise qualified as Serialise
@@ -29,9 +31,10 @@ import Language.PureScript.Docs.AsMarkdown (codeToString, declAsMarkdown, runDoc
2931
import Codec.Serialise (serialise)
3032
import Data.Aeson (encode)
3133
import Debug.Trace qualified as Debug
34+
import Language.PureScript.AST.Declarations (Module)
3235

33-
sqliteExtern :: (MonadIO m) => FilePath -> Docs.Module -> ExternsFile -> m ()
34-
sqliteExtern outputDir docs extern = liftIO $ do
36+
sqliteExtern :: (MonadIO m) => FilePath -> Module -> Docs.Module -> ExternsFile -> m ()
37+
sqliteExtern outputDir m docs extern = liftIO $ do
3538
conn <- SQLite.open db
3639
withRetry $ SQLite.executeNamed conn
3740
"INSERT INTO modules (module_name, comment, extern, dec) VALUES (:module_name, :docs, :extern, :dec)"
@@ -47,11 +50,7 @@ sqliteExtern outputDir docs extern = liftIO $ do
4750
, ":dependency" := runModuleName (eiModule i)
4851
])
4952

50-
51-
Debug.traceM $ show $ convertExterns extern
52-
Debug.traceM $ show $ Docs.modDeclarations docs
53-
54-
for_ (fst $ convertExterns extern) (\ideDeclaration -> do
53+
for_ (toIdeDeclarationAnn m extern) (\ideDeclaration -> do
5554
withRetry $ SQLite.executeNamed conn
5655
("INSERT INTO ide_declarations (module_name, name, namespace, declaration_type, span, declaration) " <>
5756
"VALUES (:module_name, :name, :namespace, :declaration_type, :span, :declaration)"

0 commit comments

Comments
 (0)