openMSX
Completer.cc
Go to the documentation of this file.
1 #include "Completer.hh"
2 #include "Interpreter.hh"
3 #include "InterpreterOutput.hh"
4 #include "FileContext.hh"
5 #include "FileOperations.hh"
6 #include "foreach_file.hh"
7 #include "ranges.hh"
8 #include "stl.hh"
9 #include "strCat.hh"
10 #include "stringsp.hh"
11 #include "TclObject.hh"
12 #include "utf8_unchecked.hh"
13 #include "view.hh"
14 #include "xrange.hh"
15 
16 using std::vector;
17 using std::string;
18 using std::string_view;
19 
20 namespace openmsx {
21 
22 static bool formatHelper(const vector<string_view>& input, size_t columnLimit,
23  vector<string>& result)
24 {
25  size_t column = 0;
26  auto it = begin(input);
27  do {
28  size_t maxcolumn = column;
29  for (size_t i = 0; (i < result.size()) && (it != end(input));
30  ++i, ++it) {
31  auto curSize = utf8::unchecked::size(result[i]);
32  strAppend(result[i], spaces(column - curSize), *it);
33  maxcolumn = std::max(maxcolumn,
34  utf8::unchecked::size(result[i]));
35  if (maxcolumn > columnLimit) return false;
36  }
37  column = maxcolumn + 2;
38  } while (it != end(input));
39  return true;
40 }
41 
42 static vector<string> format(const vector<string_view>& input, size_t columnLimit)
43 {
44  vector<string> result;
45  for (auto lines : xrange(1u, input.size())) {
46  result.assign(lines, string());
47  if (formatHelper(input, columnLimit, result)) {
48  return result;
49  }
50  }
51  append(result, input);
52  return result;
53 }
54 
55 vector<string> Completer::formatListInColumns(const vector<string_view>& input)
56 {
57  return format(input, output->getOutputColumns() - 1);
58 }
59 
60 bool Completer::equalHead(string_view s1, string_view s2, bool caseSensitive)
61 {
62  if (s2.size() < s1.size()) return false;
63  if (caseSensitive) {
64  return memcmp(s1.data(), s2.data(), s1.size()) == 0;
65  } else {
66  return strncasecmp(s1.data(), s2.data(), s1.size()) == 0;
67  }
68 }
69 
70 bool Completer::completeImpl(string& str, vector<string_view> matches,
71  bool caseSensitive)
72 {
73  assert(ranges::all_of(matches, [&](auto& m) {
74  return equalHead(str, m, caseSensitive);
75  }));
76 
77  if (matches.empty()) {
78  // no matching values
79  return false;
80  }
81  if (matches.size() == 1) {
82  // only one match
83  str = matches.front();
84  return true;
85  }
86 
87  // Sort and remove duplicates.
88  // For efficiency it's best if the list doesn't contain duplicates to
89  // start with. Though sometimes this is hard to avoid. E.g. when doing
90  // filename completion + some extra allowed strings and one of these
91  // extra strings is the same as one of the filenames.
92  ranges::sort(matches);
93  matches.erase(ranges::unique(matches), end(matches));
94 
95  bool expanded = false;
96  while (true) {
97  auto it = begin(matches);
98  if (str.size() == it->size()) {
99  // match is as long as first word
100  goto out; // TODO rewrite this
101  }
102  // expand with one char and check all strings
103  auto b = begin(*it);
104  auto e = b + str.size();
105  utf8::unchecked::next(e); // one more utf8 char
106  string_view string2(&*b, e - b);
107  for (; it != end(matches); ++it) {
108  if (!equalHead(string2, *it, caseSensitive)) {
109  goto out; // TODO rewrite this
110  }
111  }
112  // no conflict found
113  str = string2;
114  expanded = true;
115  }
116  out:
117  if (!expanded && output) {
118  // print all possibilities
119  for (auto& line : formatListInColumns(matches)) {
120  output->output(line);
121  }
122  }
123  return false;
124 }
125 
126 void Completer::completeFileName(vector<string>& tokens,
127  const FileContext& context)
128 {
129  completeFileNameImpl(tokens, context, vector<string_view>());
130 }
131 
132 void Completer::completeFileNameImpl(vector<string>& tokens,
133  const FileContext& context,
134  vector<string_view> matches)
135 {
136  string& filename = tokens.back();
139  string_view dirname1 = FileOperations::getDirName(filename);
140 
141  vector<string> paths;
143  paths.emplace_back();
144  } else {
145  paths = context.getPaths();
146  }
147 
148  vector<string> filenames;
149  for (auto& p : paths) {
150  auto pLen = p.size();
151  if (!p.empty() && (p.back() != '/')) ++pLen;
152  auto fileAction = [&](const string& path) {
153  const auto& nm = FileOperations::getConventionalPath(
154  path.substr(pLen));
155  if (equalHead(filename, nm, true)) {
156  filenames.push_back(nm);
157  }
158  };
159  auto dirAction = [&](string& path) {
160  path += '/';
161  fileAction(path);
162  path.pop_back();
163  };
165  FileOperations::join(p, dirname1),
166  fileAction, dirAction);
167  }
168  append(matches, filenames);
169  bool t = completeImpl(filename, matches, true);
170  if (t && !filename.empty() && (filename.back() != '/')) {
171  // completed filename, start new token
172  tokens.emplace_back();
173  }
174 }
175 
176 void Completer::checkNumArgs(span<const TclObject> tokens, unsigned exactly, const char* errMessage) const
177 {
178  checkNumArgs(tokens, exactly, Prefix{exactly - 1}, errMessage);
179 }
180 
181 void Completer::checkNumArgs(span<const TclObject> tokens, AtLeast atLeast, const char* errMessage) const
182 {
183  checkNumArgs(tokens, atLeast, Prefix{atLeast.min - 1}, errMessage);
184 }
185 
186 void Completer::checkNumArgs(span<const TclObject> tokens, Between between, const char* errMessage) const
187 {
188  checkNumArgs(tokens, between, Prefix{between.min - 1}, errMessage);
189 }
190 
191 void Completer::checkNumArgs(span<const TclObject> tokens, unsigned exactly, Prefix prefix, const char* errMessage) const
192 {
193  if (tokens.size() == exactly) return;
194  getInterpreter().wrongNumArgs(prefix.n, tokens, errMessage);
195 }
196 
197 void Completer::checkNumArgs(span<const TclObject> tokens, AtLeast atLeast, Prefix prefix, const char* errMessage) const
198 {
199  if (tokens.size() >= atLeast.min) return;
200  getInterpreter().wrongNumArgs(prefix.n, tokens, errMessage);
201 }
202 
203 void Completer::checkNumArgs(span<const TclObject> tokens, Between between, Prefix prefix, const char* errMessage) const
204 {
205  if (tokens.size() >= between.min && tokens.size() <= between.max) return;
206  getInterpreter().wrongNumArgs(prefix.n, tokens, errMessage);
207 }
208 
209 } // namespace openmsx
TclObject t
static void completeFileName(std::vector< std::string > &tokens, const FileContext &context, const RANGE &extra)
Definition: Completer.hh:144
static std::vector< std::string > formatListInColumns(const std::vector< std::string_view > &input)
Definition: Completer.cc:55
void checkNumArgs(span< const TclObject > tokens, unsigned exactly, const char *errMessage) const
Definition: Completer.cc:176
virtual Interpreter & getInterpreter() const =0
virtual unsigned getOutputColumns() const =0
virtual void output(std::string_view text)=0
void wrongNumArgs(unsigned argc, span< const TclObject > tokens, const char *message)
Definition: Interpreter.cc:458
Definition: span.hh:126
constexpr index_type size() const noexcept
Definition: span.hh:296
void append(Result &)
Definition: stl.hh:340
constexpr vecN< N, T > max(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:287
std::unique_ptr< Context > context
Definition: GLContext.cc:9
string expandTilde(string path)
Expand the '~' character to the users home directory.
string_view getDirName(string_view path)
Returns the directory portion of a path.
const std::string & getConventionalPath(const std::string &path)
Returns the path in conventional path-delimiter.
bool isAbsolutePath(string_view path)
Checks whether it's a absolute path or not.
const std::string & expandCurrentDirFromDrive(const std::string &path)
Get the current directory of the specified drive Linux: return the given string unchanged.
string join(string_view part1, string_view part2)
Join two paths.
This file implemented 3 utility functions:
Definition: Autofire.cc:9
constexpr const char *const filename
bool foreach_file_and_directory(std::string path, FileAction fileAction, DirAction dirAction)
auto unique(ForwardRange &&range)
Definition: ranges.hh:137
bool all_of(InputRange &&range, UnaryPredicate pred)
Definition: ranges.hh:119
void sort(RandomAccessRange &&range)
Definition: ranges.hh:35
size_t size(std::string_view utf8)
uint32_t next(octet_iterator &it)
strCatImpl::ConcatSpaces spaces(size_t n)
Definition: strCat.hh:700
void strAppend(std::string &result, Ts &&...ts)
Definition: strCat.hh:669
constexpr auto xrange(T e)
Definition: xrange.hh:155
constexpr auto begin(const zstring_view &x)
Definition: zstring_view.hh:82
constexpr auto end(const zstring_view &x)
Definition: zstring_view.hh:83