1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.tradefed.util; 17 18 import jline.Completor; 19 20 import java.util.List; 21 import java.util.regex.Matcher; 22 import java.util.regex.Pattern; 23 24 /** 25 * Implementation of the {@link Completor} for our TF configurations. 26 */ 27 public class ConfigCompletor implements Completor { 28 private List<String> mListConfig; 29 private static final String RUN_PATTERN = "(run +)(.*)?"; 30 private Pattern mRunPattern; 31 ConfigCompletor(List<String> listConfig)32 public ConfigCompletor(List<String> listConfig) { 33 mListConfig = listConfig; 34 mRunPattern = Pattern.compile(RUN_PATTERN); 35 } 36 37 /** 38 * {@inheritDoc} 39 */ 40 @SuppressWarnings({ "rawtypes", "unchecked" }) 41 @Override complete(String buffer, int cursor, List candidates)42 public int complete(String buffer, int cursor, List candidates) { 43 // if the only thing on the line is "run" we expose all possibilities. 44 if (buffer.trim().equals("run")) { 45 candidates.addAll(mListConfig); 46 } else if (buffer.startsWith("run ")) { 47 Matcher matcher = mRunPattern.matcher(buffer); 48 if (matcher.find()) { 49 for (Object configName : mListConfig) { 50 String match = matcher.group(1) + configName; 51 if (match.startsWith(buffer)) { 52 candidates.add(configName); 53 } 54 } 55 return buffer.length() - matcher.group(2).length(); 56 } 57 } 58 return cursor; 59 } 60 } 61