@@ -47,11 +47,20 @@ caseSplitAt fn updatefile l n
4747 iPrintResult new
4848
4949addClauseFrom :: FilePath -> Bool -> Int -> Name -> Idris ()
50−addClauseFrom fn updatefile l n
51− = do src <- runIO $ readSource fn
50+addClauseFrom fn updatefile l n = do
51+ -- if a definition already exists, add missing cases rather than
52+ -- adding a new definition.
53+ ist <- getIState
54+ cl <- getInternalApp fn l
55+ let fulln = getAppName cl
56+
57+ case lookup fulln (idris_metavars ist) of
58+ Nothing -> addMissing fn updatefile l n
59+ Just _ -> do
60+ src <- runIO $ readSource fn
5261 let (before, tyline : later) = splitAt (l-1) (lines src)
5362 let indent = getIndent 0 (show n) tyline
54− cl <- getClause l n fn
63+ cl <- getClause l fulln n fn
5564 -- add clause before first blank line in 'later'
5665 let (nonblank, rest) = span (not . all isSpace) (tyline:later)
5766 if updatefile
@@ -62,11 +71,16 @@ addClauseFrom fn updatefile l n
6271 unlines rest)
6372 runIO $ copyFile fb fn
6473 else iPrintResult cl
65− where
66− getIndent i n [] = 0
67− getIndent i n xs | take 9 xs == "instance " = i
68− getIndent i n xs | take (length n) xs == n = i
69− getIndent i n (x : xs) = getIndent (i + 1) n xs
74+ where
75+ getIndent i n [] = 0
76+ getIndent i n xs | take 9 xs == "instance " = i
77+ getIndent i n xs | take (length n) xs == n = i
78+ getIndent i n (x : xs) = getIndent (i + 1) n xs
79+
80+ getAppName (PApp _ r _) = getAppName r
81+ getAppName (PRef _ _ r) = r
82+ getAppName (PTyped n _) = getAppName n
83+ getAppName _ = n
7084
7185addProofClauseFrom :: FilePath -> Bool -> Int -> Name -> Idris ()
7286addProofClauseFrom fn updatefile l n
@@ -98,16 +112,17 @@ addMissing fn updatefile l n
98112 cl <- getInternalApp fn l
99113 let n' = getAppName cl
100114
101− extras <- case lookupCtxt n' (idris_patdefs i) of
102− [] -> return ""
103− [(_, tms)] -> do tms' <- nameMissing tms
104− showNew (show n ++ "_rhs") 1 indent tms'
105− other -> return "" -- happens if called on a metavar, or with no clauses
115+ extras <- case lookupCtxtExact n' (idris_patdefs i) of
116+ Nothing -> return ""
117+ Just (_, tms) -> do tms' <- nameMissing tms
118+ showNew (show n ++ "_rhs") 1 indent tms'
106119 let (nonblank, rest) = span (not . all isSpace) (tyline:later)
107120 if updatefile
108121 then do let fb = fn ++ "~"
109122 runIO $ writeSource fb (unlines (before ++ nonblank)
110− ++ extras ++ unlines rest)
123+ ++ extras ++
124+ (if null extras then ""
125+ else "\n" ++ unlines rest))
111126 runIO $ copyFile fb fn
112127 else iPrintResult extras
113128 where showPat = show . stripNS
@@ -121,6 +136,7 @@ addMissing fn updatefile l n
121136
122137 getAppName (PApp _ r _) = getAppName r
123138 getAppName (PRef _ _ r) = r
139+ getAppName (PTyped n _) = getAppName n
124140 getAppName _ = n
125141
126142 makeIndent ind | ".lidr" `isSuffixOf` fn = '>' : ' ' : replicate (ind-2) ' '
@@ -131,7 +147,8 @@ addMissing fn updatefile l n
131147 rest <- showNew nm i' ind tms
132148 return (makeIndent ind ++
133149 showPat tm ++ " = ?" ++ nm' ++
134− "\n" ++ rest)
150+ (if null rest then "" else
151+ "\n" ++ rest))
135152 showNew nm i _ [] = return ""
136153
137154 getIndent i n [] = 0