/shell-api/src/main/java/org/jboss/forge/shell/completer/SimpleTokenCompleter.java

https://github.com/cunningt/core-1 · Java · 74 lines · 33 code · 5 blank · 36 comment · 9 complexity · e4399f9eec177797d21917498f57a523 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.forge.shell.completer;
  23. /**
  24. * The simplest possible command option completer. Matching of tokens is handled automatically based on comparing the
  25. * partial token against the beginning of each candidate, but also provides the least control over how completion
  26. * occurs.
  27. *
  28. * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
  29. *
  30. */
  31. public abstract class SimpleTokenCompleter implements CommandCompleter
  32. {
  33. /**
  34. * Return a list of tokens to be considered as possible completions for the current input buffer. Typically, this
  35. * should be a list of all possible candidates, but might vary depending on current state. If returning Objects,
  36. * ensure that their {{@link #toString()} method is appropriately overloaded; the value returned will be used in
  37. * completion.
  38. */
  39. public abstract Iterable<?> getCompletionTokens();
  40. @Override
  41. public void complete(final CommandCompleterState state)
  42. {
  43. Iterable<?> values;
  44. try
  45. {
  46. values = getCompletionTokens();
  47. String peek = state.getTokens().peek();
  48. if ((state.getTokens().size() <= 1) && values != null)
  49. {
  50. for (Object val : values)
  51. {
  52. if (val != null)
  53. {
  54. String prop = val.toString();
  55. if (prop.startsWith(peek == null ? "" : peek))
  56. {
  57. state.getCandidates().add(prop + " ");
  58. state.setIndex(state.getOriginalIndex() - (peek == null ? 0 : peek.length()));
  59. }
  60. }
  61. }
  62. }
  63. }
  64. catch (Exception e)
  65. {
  66. // TODO could not get options. this should eventually be logged
  67. }
  68. }
  69. }