mirror of
https://github.com/NixOS/nix
synced 2025-07-06 21:41:48 +02:00
Implement a suggestions mechanism
Each `Error` class now includes a set of suggestions, and these are printed by the top-level handler.
This commit is contained in:
parent
b09baf690b
commit
c0792b1546
7 changed files with 191 additions and 6 deletions
41
src/libutil/suggestions.hh
Normal file
41
src/libutil/suggestions.hh
Normal file
|
@ -0,0 +1,41 @@
|
|||
#pragma once
|
||||
|
||||
#include "comparator.hh"
|
||||
#include "types.hh"
|
||||
#include <set>
|
||||
|
||||
namespace nix {
|
||||
|
||||
/**
|
||||
* A potential suggestion for the cli interface.
|
||||
*/
|
||||
class Suggestion {
|
||||
public:
|
||||
int distance; // The smaller the better
|
||||
std::string suggestion;
|
||||
|
||||
std::string pretty_print() const;
|
||||
|
||||
GENERATE_CMP(Suggestion, me->distance, me->suggestion)
|
||||
};
|
||||
|
||||
class Suggestions {
|
||||
public:
|
||||
std::set<Suggestion> suggestions;
|
||||
|
||||
std::string pretty_print() const;
|
||||
|
||||
Suggestions trim(
|
||||
int limit = 5,
|
||||
int maxDistance = 2
|
||||
) const;
|
||||
|
||||
static Suggestions bestMatches (
|
||||
std::set<std::string> allMatches,
|
||||
std::string query
|
||||
);
|
||||
|
||||
Suggestions& operator+=(const Suggestions & other);
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue