Skip to content

Commit

Permalink
FORGE-656
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Aug 17, 2012
1 parent a8d6c1b commit 6d40a64
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2012 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.shell.command;

import java.util.Set;
import java.util.TreeSet;

import javax.enterprise.event.Observes;
import javax.inject.Inject;

import org.jboss.forge.parser.java.util.Strings;
import org.jboss.forge.shell.Shell;
import org.jboss.forge.shell.ShellColor;
import org.jboss.forge.shell.ShellMessages;
import org.jboss.forge.shell.events.CommandMissing;
import org.jboss.forge.shell.exceptions.NoSuchCommandException;
import org.jboss.forge.shell.plugins.builtin.AliasRegistry;

public class DidYouMeanObserver
{

private static final int LETTERS_NEEDED_TO_BE_REPLACED = 2;

@Inject
private PluginRegistry pluginRegistry;

@Inject
private AliasRegistry aliasRegistry;

/**
* Did you mean ?
*/
public void suggestMissingPlugin(@Observes CommandMissing commandMissing, Shell shell)
{
String pluginName = commandMissing.getOriginalStatement().split(" ")[0];
// Find similar plugins
Set<String> similarPlugins = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
for (String plugin : pluginRegistry.getPlugins().keySet())
{
if (Strings.getLevenshteinDistance(pluginName, plugin) < LETTERS_NEEDED_TO_BE_REPLACED)
{
similarPlugins.add(plugin);
}
}
for (String alias : aliasRegistry.getAliases().keySet())
{
if (Strings.getLevenshteinDistance(pluginName, alias) < LETTERS_NEEDED_TO_BE_REPLACED)
{
similarPlugins.add(alias);
}
}

if (similarPlugins.isEmpty())
{
throw new NoSuchCommandException(null, "No such command: "
+ commandMissing.getOriginalStatement());
}
else
{
ShellMessages.error(shell, "No such command: " + pluginName);
shell.println("Did you mean any of these ?");
for (String plugin : similarPlugins)
{
shell.println(ShellColor.BOLD, "\t" + plugin);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.UUID;

import javax.enterprise.event.Event;
import javax.enterprise.event.Observes;
import javax.inject.Inject;

import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode;
Expand Down Expand Up @@ -55,11 +52,8 @@
import org.jboss.forge.shell.ShellMessages;
import org.jboss.forge.shell.ShellPrintWriter;
import org.jboss.forge.shell.ShellPrompt;
import org.jboss.forge.shell.command.PluginRegistry;
import org.jboss.forge.shell.events.CommandMissing;
import org.jboss.forge.shell.events.ReinitializeEnvironment;
import org.jboss.forge.shell.exceptions.Abort;
import org.jboss.forge.shell.exceptions.NoSuchCommandException;
import org.jboss.forge.shell.plugins.Alias;
import org.jboss.forge.shell.plugins.Command;
import org.jboss.forge.shell.plugins.DefaultCommand;
Expand All @@ -82,8 +76,6 @@
public class ForgePlugin implements Plugin
{

private static final int LETTERS_NEEDED_TO_BE_REPLACED = 2;

private static final String MODULE_TEMPLATE_XML = "/org/jboss/forge/modules/module-template.xml";
private final Event<ReinitializeEnvironment> reinitializeEvent;
private final ShellPrintWriter writer;
Expand All @@ -92,12 +84,11 @@ public class ForgePlugin implements Plugin
private final ShellPrompt prompt;
private final Shell shell;
private final Configuration configuration;
private final PluginRegistry pluginRegistry;

@Inject
public ForgePlugin(final ForgeEnvironment environment, final Event<ReinitializeEnvironment> reinitializeEvent,
final ShellPrintWriter writer, final ShellPrompt prompt, final DependencyResolver resolver,
final Shell shell, final Configuration configuration, final PluginRegistry pluginRegistry)
final Shell shell, final Configuration configuration)
{
this.environment = environment;
this.reinitializeEvent = reinitializeEvent;
Expand All @@ -106,7 +97,6 @@ public ForgePlugin(final ForgeEnvironment environment, final Event<ReinitializeE
this.shell = shell;
this.resolver = resolver;
this.configuration = configuration;
this.pluginRegistry = pluginRegistry;
}

/*
Expand Down Expand Up @@ -877,39 +867,4 @@ public DirectoryResource getOrCreatePluginDependenciesModuleDirectory(final Depe
dir = dir.getOrCreateChildDirectory(dep.getVersion());
return dir;
}

/**
* Installs the plugin if missing
*
* @param commandMissing
*/
public void suggestMissingPlugin(@Observes CommandMissing commandMissing)
{
String pluginName = commandMissing.getOriginalStatement().split(" ")[0];

// Find similar plugins
Set<String> similarPlugins = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
for (String plugin : pluginRegistry.getPlugins().keySet())
{
if (Strings.getLevenshteinDistance(pluginName, plugin) < LETTERS_NEEDED_TO_BE_REPLACED)
{
similarPlugins.add(plugin);
}
}
if (similarPlugins.isEmpty())
{
throw new NoSuchCommandException(null, "No such command: "
+ commandMissing.getOriginalStatement());
}
else
{
ShellMessages.error(shell, "No such command: " + pluginName);
writer.println("Did you mean any of these ?");
for (String plugin : similarPlugins)
{
writer.println(ShellColor.BOLD, "\t" + plugin);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.jboss.forge.shell.command;

import static org.junit.Assert.assertTrue;

import org.jboss.forge.shell.exceptions.NoSuchCommandException;
import org.jboss.forge.test.AbstractShellTest;
import org.junit.Test;

public class DidYouMeanObserverTest extends AbstractShellTest
{

@Test(expected = NoSuchCommandException.class)
public void testNoSuggestionsSuggestMissingPlugin() throws Exception
{
getShell().execute("aninvalidcommand");
}

@Test
public void testSuggestInvalidCommand() throws Exception
{
getShell().execute("l");
assertTrue(getOutput().contains("Did you mean any of these ?\n\tls"));
}

@Test
public void testSuggestInvalidAliasedCommand() throws Exception
{
getShell().execute("alias \"ll=ls -l\"");
getShell().execute("l");
assertTrue(getOutput().contains("Did you mean any of these ?\n\tll\n\tls"));
}

}

0 comments on commit 6d40a64

Please sign in to comment.