This is the most expensive part of window update; applying line span generation for wrapped text and unicode character sizes which may vary in byte count and physical pixel width It can take about a millisecond to do during editing on release buildj; but this is fast enough for now. There are several ways in which this function can be optimized: - Only regenerate spans which have changed, since on
| 485 | // - Have a no-wrap text mode and save a lot of the wrapping work. |
| 486 | // - Do some threading |
| 487 | void ZepWindow::UpdateLineSpans() { |
| 488 | TIME_SCOPE(UpdateLineSpans); |
| 489 | |
| 490 | m_maxDisplayLines = (long)std::max(0.0f, std::floor(m_textRegion->rect.Height() / m_defaultLineSize)); |
| 491 | |
| 492 | const auto& textBuffer = m_pBuffer->GetWorkingBuffer(); |
| 493 | |
| 494 | long bufferLine = 0; |
| 495 | long spanLine = 0; |
| 496 | float bufferPosYPx = 0.0f; |
| 497 | float xOffset = m_xPad; |
| 498 | |
| 499 | bool isMarkdown = m_pBuffer->GetFileExtension() == ".md"; |
| 500 | |
| 501 | // Nuke the existing spans |
| 502 | // In future we can in-place modify for speed |
| 503 | std::for_each(m_windowLines.begin(), m_windowLines.end(), [](SpanInfo* pInfo) { delete pInfo; }); |
| 504 | m_windowLines.clear(); |
| 505 | |
| 506 | auto widgetMarkers = m_pBuffer->GetRangeMarkers(RangeMarkerType::Widget); |
| 507 | auto itrWidgetMarkers = widgetMarkers.begin(); |
| 508 | |
| 509 | // Process every buffer line |
| 510 | for (;;) { |
| 511 | // We haven't processed this line yet, so we can't display anything |
| 512 | // else |
| 513 | if (m_pBuffer->GetLineCount() <= bufferLine) |
| 514 | break; |
| 515 | |
| 516 | if (m_pBuffer->IsLineHiddenByFold(bufferLine)) { |
| 517 | bufferLine++; |
| 518 | continue; |
| 519 | } |
| 520 | |
| 521 | ByteRange lineByteRange; |
| 522 | if (!m_pBuffer->GetLineOffsets(bufferLine, lineByteRange)) |
| 523 | break; |
| 524 | |
| 525 | const FoldRange* foldedRange = m_pBuffer->GetFoldStartingOnLine(bufferLine); |
| 526 | |
| 527 | // Padding at the top of the line |
| 528 | NVec2f topPadding = NVec2f(DPI_Y((float)GetEditor().GetConfig().lineMargins.x), DPI_Y((float)GetEditor().GetConfig().lineMargins.y)); |
| 529 | |
| 530 | auto markersOnLine = m_pBuffer->GetRangeMarkersOnLine(RangeMarkerType::All, bufferLine); |
| 531 | auto lineWidgetHeight = ArrangeLineMarkers(markersOnLine); |
| 532 | |
| 533 | // Move the line down by the height of the widget |
| 534 | bufferPosYPx += lineWidgetHeight.x; |
| 535 | |
| 536 | // TODO: Find a clean way to do this extra work during layout for extensions that need it |
| 537 | ZepTextType type = ZepTextType::Text; |
| 538 | if (isMarkdown) { |
| 539 | uint32_t headerCount = 0; |
| 540 | // Markdown experiment |
| 541 | for (auto ch = lineByteRange.first; ch < lineByteRange.second; ch += utf8_codepoint_length(textBuffer[ch])) { |
| 542 | if (textBuffer[ch] != '#') |
| 543 | break; |
| 544 | headerCount++; |
nothing calls this directly
no test coverage detected