| 102 | } |
| 103 | |
| 104 | static FormattedExample processExample(const std::string &example) { |
| 105 | std::vector<std::tuple<int, int>> lineRanges; |
| 106 | std::stringstream exampleReindented; |
| 107 | int reindent = INT_MAX; |
| 108 | int dedent = INT_MAX; |
| 109 | |
| 110 | std::vector<std::string> lines; |
| 111 | std::string line; |
| 112 | std::istringstream stream(example); |
| 113 | |
| 114 | int lineNumber = 1; |
| 115 | while (std::getline(stream, line)) { |
| 116 | // Find first non-whitespace character |
| 117 | size_t firstNonWhitespace = line.find_first_not_of(" \t"); |
| 118 | if (firstNonWhitespace != std::string::npos) { |
| 119 | // If line starts with #, update reindent if needed |
| 120 | int leadingSpaces = firstNonWhitespace; |
| 121 | if (line[firstNonWhitespace] == '#') { |
| 122 | size_t firstNonWSAfterHash = |
| 123 | line.find_first_not_of(" \t", firstNonWhitespace + 1); |
| 124 | // We want to remove the leading # and the leading spaces but preserve |
| 125 | // the rest of the whitespace as we want normalize the whitespace. |
| 126 | std::string lineWithoutHash = line.substr(0, firstNonWhitespace) + |
| 127 | line.substr(firstNonWhitespace + 1); |
| 128 | lines.push_back(lineWithoutHash); |
| 129 | leadingSpaces = firstNonWSAfterHash - 1; |
| 130 | } else { |
| 131 | lines.push_back(line); |
| 132 | lineRanges.emplace_back(lineNumber, lineNumber); |
| 133 | dedent = std::min(dedent, leadingSpaces); |
| 134 | } |
| 135 | // Compute how must to reindent the line by. |
| 136 | reindent = std::min(reindent, leadingSpaces); |
| 137 | } else { |
| 138 | lines.push_back(""); |
| 139 | lineRanges.emplace_back(lineNumber, lineNumber); |
| 140 | } |
| 141 | lineNumber++; |
| 142 | } |
| 143 | |
| 144 | // If there was no leading indentation we don't want to reindent |
| 145 | // we used INT_MAX as a sentinel value. |
| 146 | reindent = std::max(0, reindent); |
| 147 | // We want to dedent the lines by the max of the visible lines's leading |
| 148 | // whitespace. |
| 149 | // |
| 150 | // For example if we display the body of a function we will reindent |
| 151 | // correctly but when we render the lines they will all have the same |
| 152 | // leading whitespace. |
| 153 | dedent = std::max(0, dedent); |
| 154 | dedent -= reindent; |
| 155 | |
| 156 | // Before we tracked only one line spans (i.e., 1-1, 2-2) |
| 157 | // this compresses continous spans (i.e., 1-2) to reduce the generated |
| 158 | // noise. |
| 159 | std::vector<std::tuple<int, int>> compressedLineRanges; |
| 160 | int startRange = 1; |
| 161 | int endRange = 0; |
no test coverage detected