| 35 | // https://john.nachtimwald.com/2009/07/04/qcompleter-and-comma-separated-tags/ |
| 36 | |
| 37 | QString 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; |