-
Notifications
You must be signed in to change notification settings - Fork 1
/
HaxeFind.hs
77 lines (61 loc) · 2.31 KB
/
HaxeFind.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
module HaxeFind where
import System.Directory
import Data.List
import Control.Monad
{-main :: IO ()
main = do
hpath <- getEnv "HAXEPATH"
putStrLn hpath-}
findClasses :: String -> String -> IO [String]
findClasses haxePath iml = do
a <- findClassesInStd haxePath iml
b <- findClassesInLibs haxePath iml
return $ a ++ b
findClassesLocal :: String -> String -> IO [String]
findClassesLocal path iml = do
findNextFieldEx ".as" path $ split '.' iml
findClassesInStd :: String -> String -> IO [String]
findClassesInStd haxePath iml = do
stdResult <- findNextField (haxePath ++ "std") $ split '.' iml
flashResult <- findNextField (haxePath ++ "std\\flash") $ split '.' iml
return $ stdResult ++ flashResult
findClassesInLibs :: String -> String -> IO [String]
findClassesInLibs haxePath iml = do
libList <- getDirectoryContents (haxePath ++ "lib\\") >>= filterM (return . (/=".")) >>= filterM (return . (/=".."))
classes <- mapM (findClassesInLib haxePath iml) libList
return $ foldr (++) [] classes
findClassesInLib :: String -> String-> String -> IO [String]
findClassesInLib haxePath iml libName = do
content <- readFile $ libPath ++ "\\.current"
findNextField (libPath ++ '\\':replace '.' ',' content) imlist
where
imlist = split '.' iml
libPath = haxePath ++ "lib\\" ++ libName
findNextField :: String -> [String] -> IO [String]
findNextField = findNextFieldEx ".hx"
findNextFieldEx :: String -> String -> [String] -> IO [String]
findNextFieldEx ex path [] = do error "Invalid import clause."
findNextFieldEx ex path ["*;"] = do
b <- doesDirectoryExist path
if b
then getDirectoryContents path >>= filterM (return . isSuffixOf ex) >>= mapM (return . stripSuffix ex)
else return []
findNextFieldEx ex path iml = findNextFieldEx ex (path ++ '\\':head iml) $ tail iml
-- Helper
split :: Eq a => a -> [a] -> [[a]]
split c [] = []
split c s = first : split c rest
where (first, rest) = break (==c) $ dropWhile (==c) s
stripSuffix :: Eq a => [a] -> [a] -> [a]
stripSuffix suffix s =
if isSuffixOf suffix s then
take (length s - length suffix) s
else
s
replace :: Eq a => a -> a -> [a] -> [a]
replace c newC [] = []
replace c newC (x:xs) =
if x == c then
newC:replace c newC xs
else
x:replace c newC xs