001/* 002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bstefanescu 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.shell.cmds.completors; 023 024import java.util.List; 025 026import jline.ArgumentCompletor.WhitespaceArgumentDelimiter; 027import jline.Completor; 028 029import org.nuxeo.shell.CommandType; 030import org.nuxeo.shell.Shell; 031import org.nuxeo.shell.cmds.Interactive; 032 033/** 034 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 035 */ 036public class ShellCompletor implements Completor { 037 038 private final Interactive interactive; 039 040 private final CommandCompletor completor; 041 042 public ShellCompletor(Interactive interactive) { 043 this.interactive = interactive; 044 completor = new CommandCompletor(interactive.getShell()); 045 } 046 047 @SuppressWarnings("rawtypes") 048 public int complete(String buffer, int cursor, List candidates) { 049 jline.ArgumentCompletor.ArgumentList list = new WhitespaceArgumentDelimiter().delimit(buffer, cursor); 050 String[] args = list.getArguments(); 051 // the current arg text (null if empty - i.e. after a space) 052 String argText = list.getCursorArgument(); 053 // the offset of the current arg 054 int argIndex = list.getCursorArgumentIndex(); 055 // the offset of the next character relative to the cursor in the 056 // current arg 057 int offset = list.getArgumentPosition(); 058 // the leading text in the current arg 059 String text = argText == null ? null : argText.substring(0, offset); 060 if (argIndex == 0) { 061 int ret = completor.complete(text, offset, candidates); 062 return ret + (list.getBufferPosition() - offset); 063 } else { 064 Shell shell = interactive.getShell(); 065 CommandType cmd = shell.getActiveRegistry().getCommandType(args[0]); 066 if (cmd == null) { // no such command 067 return -1; 068 } 069 if (argIndex >= args.length) { 070 // at the beginning of a new token 071 String[] newArgs = new String[args.length + 1]; 072 System.arraycopy(args, 0, newArgs, 0, args.length); 073 newArgs[args.length] = ""; 074 args = newArgs; 075 } else if (argIndex < args.length) { // TODO 076 String[] newArgs = new String[argIndex + 1]; 077 System.arraycopy(args, 0, newArgs, 0, newArgs.length); 078 args = newArgs; 079 } 080 Completor comp = cmd.getLastTokenCompletor(shell, args); 081 if (comp != null) { 082 int ret = comp.complete(text, offset, candidates); 083 return ret + (list.getBufferPosition() - offset); 084 } 085 return -1; 086 } 087 } 088 089}