Skip to content

Commit

Permalink
Merge pull request #313 from AndrejMitrovic/SpellCheck
Browse files Browse the repository at this point in the history
Add spellchecker to dub for --config mismatches.
  • Loading branch information
s-ludwig committed May 5, 2014
2 parents 7b7ca05 + 70addef commit c793c29
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
11 changes: 9 additions & 2 deletions source/dub/commandline.d
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import dub.packagemanager;
import dub.packagesupplier;
import dub.platform : determineCompiler;
import dub.project;
import dub.internal.utils : getDUBVersion;
import dub.internal.utils : getDUBVersion, getClosestMatch;

import std.algorithm;
import std.array;
Expand Down Expand Up @@ -372,7 +372,14 @@ abstract class PackageBuildCommand : Command {
m_defaultConfig = null;
enforce (loadSpecificPackage(dub, package_name), "Failed to load package.");

enforce(m_buildConfig.length == 0 || dub.configurations.canFind(m_buildConfig), "Unknown build configuration: "~m_buildConfig);
if (m_buildConfig.length != 0 && !dub.configurations.canFind(m_buildConfig))
{
string msg = "Unknown build configuration: "~m_buildConfig;
enum distance = 3;
if (auto match = dub.configurations.getClosestMatch(m_buildConfig, distance))
msg ~= ". Did you mean '" ~ match ~ "'?";
enforce(0, msg);
}

if (m_buildType.length == 0) {
if (environment.get("DFLAGS")) m_buildType = "$DFLAGS";
Expand Down
20 changes: 18 additions & 2 deletions source/dub/internal/utils.d
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
...
Copyright: © 2012 Matthias Dondorff
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
Authors: Matthias Dondorff
Expand Down Expand Up @@ -208,4 +208,20 @@ private bool isHexNumber(string str) {
default: return false;
}
return true;
}
}

/**
Get the closest match of $(D input) in the $(D array), where $(D distance)
is the maximum levenshtein distance allowed between the compared strings.
Returns $(D null) if no closest match is found.
*/
string getClosestMatch(string[] array, string input, size_t distance)
{
import std.algorithm : countUntil, map, levenshteinDistance;
import std.uni : toUpper;

auto distMap = array.map!(elem =>
levenshteinDistance!((a, b) => toUpper(a) == toUpper(b))(elem, input));
auto idx = distMap.countUntil!(a => a <= distance);
return (idx == -1) ? null : array[idx];
}

0 comments on commit c793c29

Please sign in to comment.