@@ -6,7 +6,9 @@ module System.Console.Haskeline.Completion(
6
6
fallbackCompletion ,
7
7
-- * Word completion
8
8
completeWord ,
9
+ completeWord' ,
9
10
completeWordWithPrev ,
11
+ completeWordWithPrev' ,
10
12
completeQuotedWord ,
11
13
-- * Filename completion
12
14
completeFilename ,
@@ -59,6 +61,14 @@ completeWord :: Monad m => Maybe Char
59
61
-> CompletionFunc m
60
62
completeWord esc ws = completeWordWithPrev esc ws . const
61
63
64
+ -- | The same as 'completeWord' but takes a predicate for the whitespace characters
65
+ completeWord' :: Monad m => Maybe Char
66
+ -- ^ An optional escape character
67
+ -> (Char -> Bool ) -- ^ Characters which count as whitespace
68
+ -> (String -> m [Completion ]) -- ^ Function to produce a list of possible completions
69
+ -> CompletionFunc m
70
+ completeWord' esc ws = completeWordWithPrev' esc ws . const
71
+
62
72
-- | A custom 'CompletionFunc' which completes the word immediately to the left of the cursor,
63
73
-- and takes into account the line contents to the left of the word.
64
74
--
@@ -71,16 +81,27 @@ completeWordWithPrev :: Monad m => Maybe Char
71
81
-- line contents to the left of the word, reversed. The second argument is the word
72
82
-- to be completed.
73
83
-> CompletionFunc m
74
- completeWordWithPrev esc ws f (line, _) = do
84
+ completeWordWithPrev esc ws = completeWordWithPrev' esc (`elem` ws)
85
+
86
+ -- | The same as 'completeWordWithPrev' but takes a predicate for the whitespace characters
87
+ completeWordWithPrev' :: Monad m => Maybe Char
88
+ -- ^ An optional escape character
89
+ -> (Char -> Bool ) -- ^ Characters which count as whitespace
90
+ -> (String -> String -> m [Completion ])
91
+ -- ^ Function to produce a list of possible completions. The first argument is the
92
+ -- line contents to the left of the word, reversed. The second argument is the word
93
+ -- to be completed.
94
+ -> CompletionFunc m
95
+ completeWordWithPrev' esc wpred f (line, _) = do
75
96
let (word,rest) = case esc of
76
- Nothing -> break ( `elem` ws) line
97
+ Nothing -> break wpred line
77
98
Just e -> escapedBreak e line
78
99
completions <- f rest (reverse word)
79
- return (rest,map (escapeReplacement esc ws ) completions)
100
+ return (rest,map (escapeReplacement esc wpred ) completions)
80
101
where
81
- escapedBreak e (c: d: cs) | d == e && c `elem` (e : ws )
102
+ escapedBreak e (c: d: cs) | d == e && (c == e || wpred c )
82
103
= let (xs,ys) = escapedBreak e cs in (c: xs,ys)
83
- escapedBreak e (c: cs) | notElem c ws
104
+ escapedBreak e (c: cs) | not $ wpred c
84
105
= let (xs,ys) = escapedBreak e cs in (c: xs,ys)
85
106
escapedBreak _ cs = (" " ,cs)
86
107
@@ -105,12 +126,12 @@ completion str = Completion str str True
105
126
setReplacement :: (String -> String ) -> Completion -> Completion
106
127
setReplacement f c = c {replacement = f $ replacement c}
107
128
108
- escapeReplacement :: Maybe Char -> String -> Completion -> Completion
109
- escapeReplacement esc ws f = case esc of
129
+ escapeReplacement :: Maybe Char -> ( Char -> Bool ) -> Completion -> Completion
130
+ escapeReplacement esc wpred f = case esc of
110
131
Nothing -> f
111
132
Just e -> f {replacement = escape e (replacement f)}
112
133
where
113
- escape e (c: cs) | c `elem` (e : ws) = e : c : escape e cs
134
+ escape e (c: cs) | c == e || wpred c = e : c : escape e cs
114
135
| otherwise = c : escape e cs
115
136
escape _ " " = " "
116
137
@@ -127,7 +148,7 @@ completeQuotedWord esc qs completer alterative line@(left,_)
127
148
= case splitAtQuote esc qs left of
128
149
Just (w,rest) | isUnquoted esc qs rest -> do
129
150
cs <- completer (reverse w)
130
- return (rest, map (addQuotes . escapeReplacement esc qs ) cs)
151
+ return (rest, map (addQuotes . escapeReplacement esc ( `elem` qs) ) cs)
131
152
_ -> alterative line
132
153
133
154
addQuotes :: Completion -> Completion
0 commit comments