insert a token of length 1 of the specified type
| 1549 | |
| 1550 | // insert a token of length 1 of the specified type |
| 1551 | void insertToken(tokenType insert_type, uint64_t insert_pos, std::vector<highlightToken>& tokens) { |
| 1552 | std::vector<highlightToken> new_tokens; |
| 1553 | new_tokens.reserve(tokens.size() + 1); |
| 1554 | uint64_t i; |
| 1555 | bool found = false; |
| 1556 | for (i = 0; i < tokens.size(); i++) { |
| 1557 | // find the exact position where we need to insert the token |
| 1558 | if (tokens[i].start == insert_pos) { |
| 1559 | // this token is exactly at this render position |
| 1560 | |
| 1561 | // insert highlighting for the bracket |
| 1562 | highlightToken token; |
| 1563 | token.start = insert_pos; |
| 1564 | token.type = insert_type; |
| 1565 | new_tokens.push_back(token); |
| 1566 | |
| 1567 | // now we need to insert the other token ONLY if the other token is not immediately |
| 1568 | // following this one |
| 1569 | if (i + 1 >= tokens.size() || tokens[i + 1].start > insert_pos + 1) { |
| 1570 | token.start = insert_pos + 1; |
| 1571 | token.type = tokens[i].type; |
| 1572 | new_tokens.push_back(token); |
| 1573 | } |
| 1574 | i++; |
| 1575 | found = true; |
| 1576 | break; |
| 1577 | } else if (tokens[i].start > insert_pos) { |
| 1578 | // the next token is AFTER the render position |
| 1579 | // insert highlighting for the bracket |
| 1580 | highlightToken token; |
| 1581 | token.start = insert_pos; |
| 1582 | token.type = insert_type; |
| 1583 | new_tokens.push_back(token); |
| 1584 | |
| 1585 | // now just insert the next token |
| 1586 | new_tokens.push_back(tokens[i]); |
| 1587 | i++; |
| 1588 | found = true; |
| 1589 | break; |
| 1590 | } else { |
| 1591 | // insert the token |
| 1592 | new_tokens.push_back(tokens[i]); |
| 1593 | } |
| 1594 | } |
| 1595 | // copy over the remaining tokens |
| 1596 | for (; i < tokens.size(); i++) { |
| 1597 | new_tokens.push_back(tokens[i]); |
| 1598 | } |
| 1599 | if (!found) { |
| 1600 | // token was not added - add it to the end |
| 1601 | highlightToken token; |
| 1602 | token.start = insert_pos; |
| 1603 | token.type = insert_type; |
| 1604 | new_tokens.push_back(token); |
| 1605 | } |
| 1606 | tokens = std::move(new_tokens); |
| 1607 | } |
| 1608 |
no test coverage detected