]> git.f4mnq.fr Git - cells.git/commitdiff
Complétion instantanée du préfixe commun si plusieurs completions possibles
authorAmélia Coutard-Sander <git@f4mnq.fr>
Wed, 7 Jan 2026 13:38:53 +0000 (14:38 +0100)
committerAmélia Coutard-Sander <git@f4mnq.fr>
Wed, 7 Jan 2026 13:53:54 +0000 (14:53 +0100)
bin/modes.ml

index a4eb173b5ddd027cf279c55f18fa855ee2f264ef..5cded681a16c80d77b590abf992bf7311ebcc191 100644 (file)
@@ -73,6 +73,13 @@ module rec Command : (Mode with type initer = string) = struct
           | x :: t -> x :: ch_last v t
           | [] -> assert false
 
+  let common_prefix s s' =
+          let rec aux i m =
+                  if i < 0 then String.sub s 0 m else if s.[i] = s'.[i] then aux (i - 1) m else aux (i - 1) i
+          in
+          let len = min (String.length s) (String.length s') in
+          aux (len - 1) len
+
   let real_complete cmd completions =
           let last = last cmd in
           match List.filter (String.starts_with ~prefix:last) completions with
@@ -80,10 +87,10 @@ module rec Command : (Mode with type initer = string) = struct
                   Printf.fprintf stderr "No completions !\n%!";
                   String.concat " " (List.map Parse.shquote cmd)
           | [possibility] -> String.concat " " (List.map Parse.shquote (ch_last possibility cmd)) ^ " "
-          | possibilities ->
+          | p :: ps ->
                   Printf.fprintf stderr "Completions:\n%!";
-                  List.iter (Printf.fprintf stderr "\t%s\n%!") possibilities;
-                  String.concat " " (List.map Parse.shquote cmd)
+                  List.iter (Printf.fprintf stderr "\t%s\n%!") (p :: ps);
+                  String.concat " " (List.map Parse.shquote (ch_last (List.fold_left common_prefix p ps) cmd))
 
   let complete_file cmd f =
           let dir_files dir =