@@ -47,11 +47,20 @@ caseSplitAt fn updatefile l n
47 iPrintResult new
47 iPrintResult new
49addClauseFrom :: FilePath -> Bool -> Int -> Name -> Idris ()
49addClauseFrom :: FilePath -> Bool -> Int -> Name -> Idris ()
50−addClauseFrom fn updatefile l n
50+addClauseFrom fn updatefile l n = do
51+ -- if a definition already exists, add missing cases rather than
52+ -- adding a new definition.
54+ cl <- getInternalApp fn l
55+ let fulln = getAppName cl
57+ case lookup fulln (idris_metavars ist) of
58+ Nothing -> addMissing fn updatefile l n
51− = do src <- runIO $ readSource fn
60+ src <- runIO $ readSource fn
52 let (before, tyline : later) = splitAt (l-1) (lines src)
61 let (before, tyline : later) = splitAt (l-1) (lines src)
53 let indent = getIndent 0 (show n) tyline
62 let indent = getIndent 0 (show n) tyline
54− cl <- getClause l n fn
63+ cl <- getClause l fulln n fn
55 -- add clause before first blank line in 'later'
64 -- add clause before first blank line in 'later'
56 let (nonblank, rest) = span (not . all isSpace) (tyline:later)
65 let (nonblank, rest) = span (not . all isSpace) (tyline:later)
57 if updatefile
66 if updatefile
@@ -62,11 +71,16 @@ addClauseFrom fn updatefile l n
62 unlines rest)
71 unlines rest)
63 runIO $ copyFile fb fn
72 runIO $ copyFile fb fn
64 else iPrintResult cl
73 else iPrintResult cl
66− getIndent i n [] = 0
75+ getIndent i n [] = 0
67− getIndent i n xs | take 9 xs == "instance " = i
76+ getIndent i n xs | take 9 xs == "instance " = i
68− getIndent i n xs | take (length n) xs == n = i
77+ getIndent i n xs | take (length n) xs == n = i
69− getIndent i n (x : xs) = getIndent (i + 1) n xs
78+ getIndent i n (x : xs) = getIndent (i + 1) n xs
80+ getAppName (PApp _ r _) = getAppName r
81+ getAppName (PRef _ _ r) = r
82+ getAppName (PTyped n _) = getAppName n
71addProofClauseFrom :: FilePath -> Bool -> Int -> Name -> Idris ()
85addProofClauseFrom :: FilePath -> Bool -> Int -> Name -> Idris ()
72addProofClauseFrom fn updatefile l n
86addProofClauseFrom fn updatefile l n
@@ -98,16 +112,17 @@ addMissing fn updatefile l n
98 cl <- getInternalApp fn l
112 cl <- getInternalApp fn l
99 let n' = getAppName cl
113 let n' = getAppName cl
101− extras <- case lookupCtxt n' (idris_patdefs i) of
115+ extras <- case lookupCtxtExact n' (idris_patdefs i) of
102− [] -> return ""
116+ Nothing -> return ""
103− [(_, tms)] -> do tms' <- nameMissing tms
117+ Just (_, tms) -> do tms' <- nameMissing tms
104− showNew (show n ++ "_rhs") 1 indent tms'
118+ showNew (show n ++ "_rhs") 1 indent tms'
105− other -> return "" -- happens if called on a metavar, or with no clauses
106 let (nonblank, rest) = span (not . all isSpace) (tyline:later)
119 let (nonblank, rest) = span (not . all isSpace) (tyline:later)
107 if updatefile
120 if updatefile
108 then do let fb = fn ++ "~"
121 then do let fb = fn ++ "~"
109 runIO $ writeSource fb (unlines (before ++ nonblank)
122 runIO $ writeSource fb (unlines (before ++ nonblank)
124+ (if null extras then ""
110− ++ extras ++ unlines rest)
125+ else "\n" ++ unlines rest))
111 runIO $ copyFile fb fn
126 runIO $ copyFile fb fn
112 else iPrintResult extras
127 else iPrintResult extras
113 where showPat = show . stripNS
128 where showPat = show . stripNS
@@ -121,6 +136,7 @@ addMissing fn updatefile l n
122 getAppName (PApp _ r _) = getAppName r
137 getAppName (PApp _ r _) = getAppName r
123 getAppName (PRef _ _ r) = r
138 getAppName (PRef _ _ r) = r
139+ getAppName (PTyped n _) = getAppName n
124 getAppName _ = n
140 getAppName _ = n
126 makeIndent ind | ".lidr" `isSuffixOf` fn = '>' : ' ' : replicate (ind-2) ' '
142 makeIndent ind | ".lidr" `isSuffixOf` fn = '>' : ' ' : replicate (ind-2) ' '
@@ -131,7 +147,8 @@ addMissing fn updatefile l n
131 rest <- showNew nm i' ind tms
147 rest <- showNew nm i' ind tms
132 return (makeIndent ind ++
148 return (makeIndent ind ++
133 showPat tm ++ " = ?" ++ nm' ++
149 showPat tm ++ " = ?" ++ nm' ++
150+ (if null rest then "" else
134− "\n" ++ rest)
151+ "\n" ++ rest))
135 showNew nm i _ [] = return ""
152 showNew nm i _ [] = return ""
137 getIndent i n [] = 0
154 getIndent i n [] = 0