on master help sign in
Do better job calculating COMP_CWORD
78cd7e7Evan Czaplicki
ui/terminal/src/Terminal/Args.hs+28−11
@@ -118,20 +118,18 @@ maybeAutoComplete args getSuggestions =
118getCompIndex :: String -> IO (Int, [String])
118getCompIndex :: String -> IO (Int, [String])
119getCompIndex line =
119getCompIndex line =
120 do maybePoint <- Env.lookupEnv "COMP_POINT"
120 do maybePoint <- Env.lookupEnv "COMP_POINT"
121− case Read.readMaybe =<< maybePoint :: Maybe Int of
121+ case Read.readMaybe =<< maybePoint of
122 Nothing ->
122 Nothing ->
123 do let chunks = words line
123 do let chunks = words line
124 return (length chunks, chunks)
124 return (length chunks, chunks)
125
125
126 Just point ->
126 Just point ->
127 let
127 let
128− isPoint (_,i) = i == point
129 groups = List.groupBy grouper (zip line [0..])
128 groups = List.groupBy grouper (zip line [0..])
130 rawChunks = drop 1 (filter (all (not . isSpace . fst)) groups)
129 rawChunks = drop 1 (filter (all (not . isSpace . fst)) groups)
131− maybeIndex = List.findIndex (any isPoint) rawChunks
132 in
130 in
133 return
131 return
134− ( maybe (length rawChunks) id maybeIndex
132+ ( findIndex 1 point rawChunks
135 , map (map fst) rawChunks
133 , map (map fst) rawChunks
136 )
134 )
137
135
@@ -146,6 +144,25 @@ isSpace char =
146 char == ' ' || char == '\t' || char == '\n'
144 char == ' ' || char == '\t' || char == '\n'
147
145
148
146
147+findIndex :: Int -> Int -> [[(Char,Int)]] -> Int
148+findIndex index point chunks =
149+ case chunks of
150+ [] ->
151+ index
152+
153+ chunk:cs ->
154+ let
155+ lo = snd (head chunk)
156+ hi = snd (last chunk)
157+ in
158+ if point < lo then
159+ 0
160+ else if point <= hi + 1 then
161+ index
162+ else
163+ findIndex (index + 1) point cs
164+
165+
149complexSuggest :: [Interface] -> Int -> [String] -> IO [String]
166complexSuggest :: [Interface] -> Int -> [String] -> IO [String]
150complexSuggest interfaces index strings =
167complexSuggest interfaces index strings =
151 case strings of
168 case strings of
@@ -153,15 +170,15 @@ complexSuggest interfaces index strings =
153 return (map toName interfaces)
170 return (map toName interfaces)
154
171
155 command : chunks ->
172 command : chunks ->
156− case List.find (\iface -> toName iface == command) interfaces of
157− Nothing ->
158− if index == 1 then
173+ if index == 1 then
159− return (filter (List.isPrefixOf command) (map toName interfaces))
174+ return (filter (List.isPrefixOf command) (map toName interfaces))
160− else
175+ else
176+ case List.find (\iface -> toName iface == command) interfaces of
177+ Nothing ->
161 return []
178 return []
162
179
163− Just (Interface _ _ _ _ args_ flags_ _) ->
180+ Just (Interface _ _ _ _ args_ flags_ _) ->
164− fst $ Chomp.chomp (Just (index-1)) chunks args_ flags_
181+ fst $ Chomp.chomp (Just (index-1)) chunks args_ flags_
165
182
166
183
167
184