git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #914 from jacereda/psc-docs-tags
ed4fe54Phil Freeman
psc-docs/Ctags.hsnew file+13
@@ -0,0 +1,13 @@
1+module Ctags (dumpCtags) where
2+
3+import qualified Language.PureScript as P
4+import Tags
5+import Data.List (sort)
6+
7+dumpCtags :: [(String, P.Module)] -> [String]
8+dumpCtags = sort . concat . (map renderModCtags)
9+
10+renderModCtags :: (String, P.Module) -> [String]
11+renderModCtags (path, mdl) = sort tagLines
12+ where tagLines = map tagLine $ tags mdl
13+ tagLine (name, line) = name ++ "\t" ++ path ++ "\t" ++ show line
psc-docs/Etags.hsnew file+15
@@ -0,0 +1,15 @@
1+module Etags (dumpEtags) where
2+
3+import qualified Language.PureScript as P
4+import Tags
5+
6+dumpEtags :: [(String, P.Module)] -> [String]
7+dumpEtags = concat . (map renderModEtags)
8+
9+renderModEtags :: (String, P.Module) -> [String]
10+renderModEtags (path, mdl) = ["\x0c", path ++ "," ++ show tagsLen] ++ tagLines
11+ where tagsLen = sum $ map length tagLines
12+ tagLines = map tagLine $ tags mdl
13+ tagLine (name, line) = "\x7f" ++ name ++ "\x01" ++ show line ++ ","
14+
15+
psc-docs/Main.hs+33−5
@@ -30,21 +30,37 @@ import qualified Paths_purescript as Paths
30import System.Exit (exitSuccess, exitFailure)
30import System.Exit (exitSuccess, exitFailure)
31import System.IO (hPutStrLn, stderr)
31import System.IO (hPutStrLn, stderr)
32
32
33+import Etags
34+import Ctags
35+
36+-- Available output formats
37+data Format = Markdown -- Output documentation in Markdown format
38+ | Ctags -- Output ctags symbol index suitable for use with vi
39+ | Etags -- Output etags symbol index suitable for use with emacs
40+
33data PSCDocsOptions = PSCDocsOptions
41data PSCDocsOptions = PSCDocsOptions
42+ { pscdFormat :: Format
34− { pscdInputFiles :: [FilePath]
43+ , pscdInputFiles :: [FilePath]
35 }
44 }
36
45
37docgen :: PSCDocsOptions -> IO ()
46docgen :: PSCDocsOptions -> IO ()
38−docgen (PSCDocsOptions input) = do
47+docgen (PSCDocsOptions fmt input) = do
39 e <- P.parseModulesFromFiles (fromMaybe "") <$> mapM (fmap (first Just) . parseFile) (nub input)
48 e <- P.parseModulesFromFiles (fromMaybe "") <$> mapM (fmap (first Just) . parseFile) (nub input)
40 case e of
49 case e of
41 Left err -> do
50 Left err -> do
42 hPutStrLn stderr $ show err
51 hPutStrLn stderr $ show err
43 exitFailure
52 exitFailure
44 Right ms -> do
53 Right ms -> do
54+ case fmt of
45− putStrLn . runDocs $ renderModules (map snd ms)
55+ Markdown -> putStrLn . runDocs $ renderModules (map snd ms)
56+ Etags -> ldump $ dumpEtags $ pairs ms
57+ Ctags -> ldump $ dumpCtags $ pairs ms
46 exitSuccess
58 exitSuccess
47−
59+ where pairs :: [(Maybe String, m)] -> [(String, m)]
60+ pairs = map (\(k,m) -> (fromMaybe "" k,m))
61+ ldump :: [String] -> IO ()
62+ ldump = mapM_ putStrLn
63+
48parseFile :: FilePath -> IO (FilePath, String)
64parseFile :: FilePath -> IO (FilePath, String)
49parseFile input = (,) input <$> readFile input
65parseFile input = (,) input <$> readFile input
50
66
@@ -222,8 +238,20 @@ inputFile = strArgument $
222 metavar "FILE"
238 metavar "FILE"
223 <> help "The input .purs file(s)"
239 <> help "The input .purs file(s)"
224
240
241+instance Read Format where
242+ readsPrec _ "etags" = [(Etags, "")]
243+ readsPrec _ "ctags" = [(Ctags, "")]
244+ readsPrec _ "markdown" = [(Markdown, "")]
245+ readsPrec _ _ = []
246+
247+format :: Parser Format
248+format = option auto $ value Markdown
249+ <> long "format"
250+ <> metavar "FORMAT"
251+ <> help "Set output FORMAT (markdown | etags | ctags)"
252+
225pscDocsOptions :: Parser PSCDocsOptions
253pscDocsOptions :: Parser PSCDocsOptions
226−pscDocsOptions = PSCDocsOptions <$> many inputFile
254+pscDocsOptions = PSCDocsOptions <$> format <*> many inputFile
227
255
228main :: IO ()
256main :: IO ()
229main = execParser opts >>= docgen
257main = execParser opts >>= docgen
psc-docs/Tags.hsnew file+18
@@ -0,0 +1,18 @@
1+module Tags where
2+
3+import qualified Language.PureScript as P
4+
5+tags :: P.Module -> [(String, Int)]
6+tags = concatMap dtags . P.exportedDeclarations
7+ where dtags (P.PositionedDeclaration sp _ d) = map tag $ names d
8+ where tag name = (name, line)
9+ line = P.sourcePosLine $ P.spanStart sp
10+ dtags _ = []
11+ names (P.DataDeclaration _ name _ dcons) = P.runProperName name : consNames
12+ where consNames = map (\(cname, _) -> P.runProperName cname) dcons
13+ names (P.TypeDeclaration ident _) = [show ident]
14+ names (P.ExternDeclaration _ ident _ _) = [show ident]
15+ names (P.TypeSynonymDeclaration name _ _) = [P.runProperName name]
16+ names (P.TypeClassDeclaration name _ _ _) = [P.runProperName name]
17+ names (P.TypeInstanceDeclaration name _ _ _ _) = [show name]
18+ names _ = []