001 /*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019
020 package org.crsh.cmdline.spi;
021
022 import org.crsh.cmdline.ParameterDescriptor;
023
024 public interface Completer {
025
026 /**
027 * <p>Query the completer for a set of completion for the given prefix. The returned {@link ValueCompletion} object
028 * provides the possible suffixes matching the prefix argument. Each entry of the result maps to a possible
029 * completion: an entry key is the possible completion, its corresponding boolean value indicates wether the value can
030 * be further more completed or not.<p>
031 *
032 * <p>The prefix value of the completion result is optional and gives a prefix to use more than one result is provided.
033 * The interest of the prefix is to limit the size of the completion to display when they can be long, for instance
034 * a pat completion returning several values could be display in columns, however only the last name of the path would
035 * be displayed and not the entire path.</p>
036 *
037 * <p>The following guidelines should be respected:</p>
038 * <ul>
039 * <li>An empty completion means no completion can be determined, the framework will not do anything.</li>
040 * <li>A singleton map means the match was entire and the framework will complete it with the sole map entry.</li>
041 * <li>A map containing string sharing a common prefix instruct the framework to insert this common prefix.</li>
042 * <li>A list containing strings with no common prefix (other than the empty string) instruct the framework to display
043 * the list of possible completions. In that case the completion result prefix is used to preped the returned suffixes
044 * when displayed in rows.</li>
045 * <li>When a match is considered as full (the entry value is set to true), the completion should contain a trailing
046 * value that is usually a white space (but it could be a quote for quoted values).</li>
047 * </ul>
048 *
049 * <p>Example: a completer that would complete colors could</p>
050 * <ul>
051 * <li>return the result ["lack ":true,"lue ":true] with the prefix "b" for the prefix "b".</li>
052 * <li>return the result ["e ":true] with the prefix "blu" for the prefix "blu".</li>
053 * <li>return the result [] for the prefix "z".</li>
054 * </ul>
055 *
056 * <p>Example: a completer that would complete java packages could</p>
057 * <ul>
058 * <li>return the map ["ext":true,"ext.spi":false] for the prefix "java.t"</li>
059 * </ul>
060 *
061 * @param parameter the completed parameter
062 * @param prefix the prefix to complete
063 * @return the possible suffix map
064 * @throws Exception any exception that would prevent completion to perform correctly
065 */
066 ValueCompletion complete(ParameterDescriptor<?> parameter, String prefix) throws Exception;
067
068 }