Repository: karaf Updated Branches: refs/heads/karaf-4.0.x fdf5cf8aa -> f15973c5c
[KARAF-4319] Completion does not work after semicolon Project: http://git-wip-us.apache.org/repos/asf/karaf/repo Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/f15973c5 Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/f15973c5 Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/f15973c5 Branch: refs/heads/karaf-4.0.x Commit: f15973c5c1d234c1a2026c51f2f48f5d1b3e292f Parents: fdf5cf8 Author: Guillaume Nodet <[email protected]> Authored: Fri Feb 19 12:02:56 2016 +0100 Committer: Guillaume Nodet <[email protected]> Committed: Fri Feb 19 12:10:47 2016 +0100 ---------------------------------------------------------------------- .../impl/console/CompleterAsCompletor.java | 11 +++- .../impl/console/parsing/CommandLineParser.java | 8 +++ .../impl/console/CompleterAsCompletorTest.java | 60 ++++++++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf/blob/f15973c5/shell/core/src/main/java/org/apache/karaf/shell/impl/console/CompleterAsCompletor.java ---------------------------------------------------------------------- diff --git a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/CompleterAsCompletor.java b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/CompleterAsCompletor.java index 49a4420..f1e87c8 100644 --- a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/CompleterAsCompletor.java +++ b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/CompleterAsCompletor.java @@ -19,6 +19,7 @@ package org.apache.karaf.shell.impl.console; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; import org.apache.karaf.shell.api.console.CommandLine; import org.apache.karaf.shell.api.console.Completer; @@ -37,8 +38,14 @@ public class CompleterAsCompletor implements jline.console.completer.Completer { @SuppressWarnings("unchecked") public int complete(String buffer, int cursor, @SuppressWarnings("rawtypes") List candidates) { - CommandLine cmdLine = CommandLineParser.buildCommandLine(session, buffer, cursor); - return completer.complete(session, cmdLine, candidates); + AtomicInteger begOfLine = new AtomicInteger(); + CommandLine cmdLine = CommandLineParser.buildCommandLine(session, buffer, cursor, begOfLine); + int res = completer.complete(session, cmdLine, candidates); + if (res >= 0) { + return res + begOfLine.get(); + } else { + return res; + } } } http://git-wip-us.apache.org/repos/asf/karaf/blob/f15973c5/shell/core/src/main/java/org/apache/karaf/shell/impl/console/parsing/CommandLineParser.java ---------------------------------------------------------------------- diff --git a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/parsing/CommandLineParser.java b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/parsing/CommandLineParser.java index a504c7c..bed4fce 100644 --- a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/parsing/CommandLineParser.java +++ b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/parsing/CommandLineParser.java @@ -18,6 +18,8 @@ */ package org.apache.karaf.shell.impl.console.parsing; +import java.util.concurrent.atomic.AtomicInteger; + import org.apache.karaf.shell.api.console.Command; import org.apache.karaf.shell.api.console.CommandLine; import org.apache.karaf.shell.api.console.Parser; @@ -28,6 +30,11 @@ import org.apache.karaf.shell.support.parsing.GogoParser; public class CommandLineParser { public static CommandLine buildCommandLine(Session session, String command, int cursor) { + AtomicInteger begOfLine = new AtomicInteger(); + return buildCommandLine(session, command, cursor, begOfLine); + } + + public static CommandLine buildCommandLine(Session session, final String command, int cursor, AtomicInteger begOfLine) { int pos = 0; while (true) { String rem = command.substring(pos); @@ -58,6 +65,7 @@ public class CommandLineParser { } pos += length; if (cursor <= pos) { + begOfLine.set(pos - length); return cmdLine; } } http://git-wip-us.apache.org/repos/asf/karaf/blob/f15973c5/shell/core/src/test/java/org/apache/karaf/shell/impl/console/CompleterAsCompletorTest.java ---------------------------------------------------------------------- diff --git a/shell/core/src/test/java/org/apache/karaf/shell/impl/console/CompleterAsCompletorTest.java b/shell/core/src/test/java/org/apache/karaf/shell/impl/console/CompleterAsCompletorTest.java new file mode 100644 index 0000000..abe740a --- /dev/null +++ b/shell/core/src/test/java/org/apache/karaf/shell/impl/console/CompleterAsCompletorTest.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.karaf.shell.impl.console; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; + +import org.apache.felix.gogo.runtime.threadio.ThreadIOImpl; +import org.apache.karaf.shell.api.console.CommandLine; +import org.apache.karaf.shell.api.console.Completer; +import org.apache.karaf.shell.api.console.Session; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class CompleterAsCompletorTest { + + @Test + public void testCompletionMultiCommand() { + SessionFactoryImpl sessionFactory = new SessionFactoryImpl(new ThreadIOImpl()); + Session session = new HeadlessSessionImpl(sessionFactory, sessionFactory.getCommandProcessor(), + new ByteArrayInputStream(new byte[0]), new PrintStream(new ByteArrayOutputStream()), new PrintStream(new ByteArrayOutputStream()) + ); + + Completer completer = new Completer() { + @Override + public int complete(Session session, CommandLine commandLine, List<String> candidates) { + assertEquals(" bundle:l", commandLine.getBuffer()); + candidates.add("bundle:list"); + return 1; + } + }; + jline.console.completer.Completer cmp = new CompleterAsCompletor(session, completer); + + String cmd = "bundle:list ; bundle:l"; + List<CharSequence> candidates = new ArrayList<>(); + int pos = cmp.complete(cmd, cmd.length(), candidates); + + assertEquals("bundle:list ; ".length(), pos); + } +}
