You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2016/07/19 08:09:19 UTC

karaf git commit: [KARAF-4372] Fix url/file completion

Repository: karaf
Updated Branches:
  refs/heads/master f92737d63 -> 70915df91


[KARAF-4372] Fix url/file completion

Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/70915df9
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/70915df9
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/70915df9

Branch: refs/heads/master
Commit: 70915df911777aab2c789a94d2e026d4be3777f7
Parents: f92737d
Author: Guillaume Nodet <gn...@apache.org>
Authored: Tue Jul 19 09:27:48 2016 +0200
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Tue Jul 19 10:09:09 2016 +0200

----------------------------------------------------------------------
 .../karaf/shell/api/console/Candidate.java      |  4 ++
 .../karaf/shell/api/console/Completer.java      |  8 +++-
 .../shell/support/completers/FileCompleter.java | 15 ++++--
 .../shell/support/completers/UriCompleter.java  | 48 ++++++++------------
 4 files changed, 40 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/70915df9/shell/core/src/main/java/org/apache/karaf/shell/api/console/Candidate.java
----------------------------------------------------------------------
diff --git a/shell/core/src/main/java/org/apache/karaf/shell/api/console/Candidate.java b/shell/core/src/main/java/org/apache/karaf/shell/api/console/Candidate.java
index c882566..6930c44 100644
--- a/shell/core/src/main/java/org/apache/karaf/shell/api/console/Candidate.java
+++ b/shell/core/src/main/java/org/apache/karaf/shell/api/console/Candidate.java
@@ -32,6 +32,10 @@ public class Candidate implements Comparable<Candidate> {
         this(value, value, null, null, null, null, true);
     }
 
+    public Candidate(String value, boolean complete) {
+        this(value, value, null, null, null, null, complete);
+    }
+
     public Candidate(String value, String displ, String group, String descr, String suffix, String key, boolean complete) {
         Objects.requireNonNull(value);
         this.value = value;

http://git-wip-us.apache.org/repos/asf/karaf/blob/70915df9/shell/core/src/main/java/org/apache/karaf/shell/api/console/Completer.java
----------------------------------------------------------------------
diff --git a/shell/core/src/main/java/org/apache/karaf/shell/api/console/Completer.java b/shell/core/src/main/java/org/apache/karaf/shell/api/console/Completer.java
index 527f0db..4392f04 100644
--- a/shell/core/src/main/java/org/apache/karaf/shell/api/console/Completer.java
+++ b/shell/core/src/main/java/org/apache/karaf/shell/api/console/Completer.java
@@ -38,9 +38,13 @@ public interface Completer {
 
     default void completeCandidates(Session session, CommandLine commandLine, List<Candidate> candidates) {
         List<String> strings = new ArrayList<>();
-        complete(session, commandLine, strings);
+        int idx = complete(session, commandLine, strings);
+        String word = "";
+        if (idx > commandLine.getBufferPosition() - commandLine.getArgumentPosition()) {
+            word = commandLine.getBuffer().substring(commandLine.getBufferPosition() - commandLine.getArgumentPosition(), idx);
+        }
         for (String string : strings) {
-            candidates.add(new Candidate(string));
+            candidates.add(new Candidate(word + string));
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/karaf/blob/70915df9/shell/core/src/main/java/org/apache/karaf/shell/support/completers/FileCompleter.java
----------------------------------------------------------------------
diff --git a/shell/core/src/main/java/org/apache/karaf/shell/support/completers/FileCompleter.java b/shell/core/src/main/java/org/apache/karaf/shell/support/completers/FileCompleter.java
index 6bf8f4d..d7e837f 100644
--- a/shell/core/src/main/java/org/apache/karaf/shell/support/completers/FileCompleter.java
+++ b/shell/core/src/main/java/org/apache/karaf/shell/support/completers/FileCompleter.java
@@ -20,6 +20,7 @@ package org.apache.karaf.shell.support.completers;
 import java.io.File;
 import java.util.List;
 
+import org.apache.karaf.shell.api.console.Candidate;
 import org.apache.karaf.shell.api.console.CommandLine;
 import org.apache.karaf.shell.api.console.Completer;
 import org.apache.karaf.shell.api.console.Session;
@@ -57,9 +58,13 @@ public class FileCompleter implements Completer
     }
 
     public int complete(final Session session, CommandLine commandLine, final List<String> candidates) {
+        throw new UnsupportedOperationException();
+    }
+
+    public void completeCandidates(final Session session, CommandLine commandLine, final List<Candidate> candidates) {
         // buffer can be null
         if (candidates == null) {
-            return 0;
+            return;
         }
 
         String buffer = commandLine.getCursorArgument();
@@ -97,7 +102,7 @@ public class FileCompleter implements Completer
 
         File[] entries = dir == null ? new File[0] : dir.listFiles();
 
-        return matchFiles(buffer, translated, entries, candidates) + commandLine.getBufferPosition() - commandLine.getArgumentPosition();
+        matchFiles(buffer, translated, entries, candidates);
     }
 
     protected String separator() {
@@ -112,7 +117,7 @@ public class FileCompleter implements Completer
         return new File(".");
     }
 
-    protected int matchFiles(final String buffer, final String translated, final File[] files, final List<String> candidates) {
+    protected int matchFiles(final String buffer, final String translated, final File[] files, final List<Candidate> candidates) {
         if (files == null) {
             return -1;
         }
@@ -127,8 +132,8 @@ public class FileCompleter implements Completer
         }
         for (File file : files) {
             if (file.getAbsolutePath().startsWith(translated)) {
-                CharSequence name = file.getName() + (matches == 1 && file.isDirectory() ? separator() : " ");
-                candidates.add(render(file, name).toString());
+                boolean dir = file.isDirectory();
+                candidates.add(new Candidate(render(file, dir ? file.toString() + "/" : file.toString()).toString(), !dir));
             }
         }
 

http://git-wip-us.apache.org/repos/asf/karaf/blob/70915df9/shell/core/src/main/java/org/apache/karaf/shell/support/completers/UriCompleter.java
----------------------------------------------------------------------
diff --git a/shell/core/src/main/java/org/apache/karaf/shell/support/completers/UriCompleter.java b/shell/core/src/main/java/org/apache/karaf/shell/support/completers/UriCompleter.java
index dacba12..70f68e6 100644
--- a/shell/core/src/main/java/org/apache/karaf/shell/support/completers/UriCompleter.java
+++ b/shell/core/src/main/java/org/apache/karaf/shell/support/completers/UriCompleter.java
@@ -24,6 +24,7 @@ import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.List;
 
+import org.apache.karaf.shell.api.console.Candidate;
 import org.apache.karaf.shell.api.console.CommandLine;
 import org.apache.karaf.shell.api.console.Completer;
 import org.apache.karaf.shell.api.console.Session;
@@ -32,18 +33,22 @@ public class UriCompleter implements Completer {
 
     @Override
     public int complete(Session session, CommandLine commandLine, List<String> candidates) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public void completeCandidates(Session session, CommandLine commandLine, List<Candidate> candidates) {
         String arg = commandLine.getCursorArgument();
         if (arg != null) {
             if (arg.startsWith("mvn:")) {
-                return maven(session, commandLine, candidates);
+                maven(session, commandLine, candidates);
             } else if (arg.startsWith("file:")) {
-                return file(session, commandLine, candidates);
+                file(session, commandLine, candidates);
             }
         }
-        return 0;
     }
 
-    private int file(Session session, CommandLine commandLine, List<String> candidates) {
+    private void file(Session session, CommandLine commandLine, List<Candidate> candidates) {
         String buffer = commandLine.getCursorArgument();
         String path = buffer.substring("file:".length(), commandLine.getArgumentPosition());
 
@@ -53,7 +58,7 @@ public class UriCompleter implements Completer {
             Path dir;
             if (path.length() == 0) {
                 for (Path root : FileSystems.getDefault().getRootDirectories()) {
-                    candidates.add(root.toString());
+                    candidates.add(new Candidate(root.toString(), false));
                 }
                 dir = Paths.get(".");
             } else {
@@ -70,24 +75,18 @@ public class UriCompleter implements Completer {
                 try (DirectoryStream<Path> paths = Files.newDirectoryStream(dir, rem + "*")) {
                     for (Path child : paths) {
                         String name = encode(child.getFileName().toString());
-                        if (Files.isDirectory(child)) {
+                        boolean isDir = Files.isDirectory(child);
+                        if (isDir) {
                             name += "/";
                         }
-                        candidates.add(name);
+                        String dirstr = dir.endsWith(File.separator) ? dir.toString() : dir.toString() + File.separator;
+                        candidates.add(new Candidate("file:" + dirstr + name, !isDir));
                     }
                 }
             }
         } catch (Exception e) {
             // Ignore
         }
-
-        if (candidates.size() == 1) {
-            String cand = candidates.get(0);
-            if (!cand.endsWith("/")) {
-                candidates.set(0, cand + " ");
-            }
-        }
-        return candidates.isEmpty() ? -1 : commandLine.getBufferPosition() - rem.length();
     }
 
     private String encode(String s) {
@@ -98,7 +97,7 @@ public class UriCompleter implements Completer {
         return s.replaceAll("%20", " ");
     }
 
-    private int maven(Session session, CommandLine commandLine, List<String> candidates) {
+    private void maven(Session session, CommandLine commandLine, List<Candidate> candidates) {
         String repo = System.getProperty("user.home") + "/.m2/repository";
         String buffer = commandLine.getCursorArgument();
         String mvn = buffer.substring("mvn:".length(), commandLine.getArgumentPosition());
@@ -123,12 +122,13 @@ public class UriCompleter implements Completer {
                     }
                     rem = dirs[dirs.length - 1];
                 }
-                Path dir = Paths.get(repo + File.separator + known);
+                Path rep = Paths.get(repo);
+                Path dir = rep.resolve(known);
                 try (DirectoryStream<Path> paths = Files.newDirectoryStream(dir, rem + "*")) {
                     for (Path path : paths) {
                         if (Files.isDirectory(path)) {
                             String name = path.getFileName().toString();
-                            candidates.add(group + name);
+                            candidates.add(new Candidate("mvn:" + group + name, false));
                         }
                     }
                 }
@@ -140,7 +140,7 @@ public class UriCompleter implements Completer {
                     for (Path path : paths) {
                         if (Files.isDirectory(path)) {
                             String name = path.getFileName().toString();
-                            candidates.add(name);
+                            candidates.add(new Candidate("mvn:" + parts[0] + "/" + name, false));
                         }
                     }
                 }
@@ -151,7 +151,7 @@ public class UriCompleter implements Completer {
                     for (Path path : paths) {
                         if (Files.isDirectory(path)) {
                             String name = path.getFileName().toString();
-                            candidates.add(name);
+                            candidates.add(new Candidate("mvn:" + parts[0] + "/" + parts[1] + "/" + name, true));
                         }
                     }
                 }
@@ -159,14 +159,6 @@ public class UriCompleter implements Completer {
         } catch (Exception e) {
             // Ignore
         }
-
-        if (candidates.size() == 1) {
-            String cand = candidates.get(0);
-            if (cand.split("/").length == 3) {
-                candidates.set(0, candidates.get(0) + " ");
-            }
-        }
-        return candidates.isEmpty() ? -1 : commandLine.getBufferPosition() - rem.length();
     }
 
 }