-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
108 additions
and
46 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
shell/src/main/java/org/jboss/forge/shell/command/DidYouMeanObserver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
shell/src/test/java/org/jboss/forge/shell/command/DidYouMeanObserverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
|
||
} |