MCPcopy Create free account
hub / github.com/FreeCAD/FreeCAD / perform

Method perform

src/App/ExpressionTokenizer.cpp:37–157  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

35// https://john.nachtimwald.com/2009/07/04/qcompleter-and-comma-separated-tags/
36
37QString ExpressionTokenizer::perform(const QString& prefix, int pos)
38{
39 // ExpressionParser::tokenize() only supports std::string but we need a tuple QString
40 // because due to UTF-8 encoding a std::string may be longer than a QString
41 // See https://forum.freecad.org/viewtopic.php?f=3&t=69931
42 auto tokenizeExpression = [](const QString& expr) {
43 std::vector<std::tuple<int, int, std::string>> result =
44 ExpressionParser::tokenize(expr.toStdString());
45 std::vector<std::tuple<int, int, QString>> tokens;
46 std::transform(
47 result.cbegin(),
48 result.cend(),
49 std::back_inserter(tokens),
50 [&](const std::tuple<int, int, std::string>& item) {
51 return std::make_tuple(
52 std::get<0>(item),
53 QString::fromStdString(expr.toStdString().substr(0, std::get<1>(item))).size(),
54 QString::fromStdString(std::get<2>(item)));
55 });
56 return tokens;
57 };
58
59 QString completionPrefix;
60
61 // Compute start; if prefix starts with =, start parsing from offset 1.
62 int start = (prefix.size() > 0 && prefix.at(0) == QChar::fromLatin1('=')) ? 1 : 0;
63
64 // Tokenize prefix
65 std::vector<std::tuple<int, int, QString>> tokens = tokenizeExpression(prefix.mid(start));
66
67 // No tokens
68 if (tokens.empty()) {
69 return {};
70 }
71
72 prefixEnd = prefix.size();
73
74 // Pop those trailing tokens depending on the given position, which may be
75 // in the middle of a token, and we shall include that token.
76 for (auto it = tokens.begin(); it != tokens.end(); ++it) {
77 int tokenType = std::get<0>(*it);
78 int location = std::get<1>(*it);
79 int tokenLength = static_cast<int> (std::get<2>(*it).size());
80 if (location >= pos) {
81 // Include the immediately followed '.' or '#', because we'll be
82 // inserting these separators too, in ExpressionCompleteModel::pathFromIndex()
83 if (it != tokens.begin() && tokenType != '.' && tokenType != '#') {
84 --it;
85 location = std::get<1>(*it);
86 tokenLength = static_cast<int>(std::get<2>(*it).size());
87 }
88 tokens.resize(it - tokens.begin() + 1);
89 prefixEnd = start + location + tokenLength;
90 break;
91 }
92 }
93
94 int trim = 0;

Callers 2

slotUpdateMethod · 0.45
TEST_FFunction · 0.45

Calls 13

tokenizeFunction · 0.85
toStdStringMethod · 0.80
cbeginMethod · 0.80
cendMethod · 0.80
substrMethod · 0.80
atMethod · 0.80
transformFunction · 0.50
sizeMethod · 0.45
emptyMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
resizeMethod · 0.45

Tested by

no test coverage detected