brk_rolldown_error 0.1.3

rolldown_error
Documentation
use arcstr::ArcStr;
use ariadne::{Label, Span};
use rustc_hash::FxHashMap;

use crate::{
  BuildDiagnostic, EventKindSwitcher,
  diagnostic::{DiagnosticFileId, RolldownLabelSpan},
};

pub fn is_context_too_long(
  label: &Label<RolldownLabelSpan>,
  files: &FxHashMap<DiagnosticFileId, ArcStr>,
) -> bool {
  let span = label.span();
  let source_id = span.source();
  let source = files.get(source_id).expect("should have file");
  if source.len() < 600 {
    return false;
  }
  let rope = ropey::Rope::from_str(source);
  // 1. If start to beginning of the file is less than 300 characters, treated as it has line feed before.
  // 2. If end to end of the file is less than 300 characters, treated as it has line feed after.
  let end = span.end();
  let start = span.start();
  let postfix = rope.slice(end..);
  let mut has_line_feed_after = false;
  let mut cnt = 0;
  for ch in postfix.chars() {
    if ch == '\n' {
      has_line_feed_after = true;
      break;
    }
    cnt += 1;
    if cnt > 300 {
      break;
    }
  }

  if cnt < 300 {
    has_line_feed_after = true;
  }

  let prefix = rope.slice(..start);

  let mut has_line_feed_before = false;
  let mut cnt = 0;
  for ch in prefix.chars().reversed() {
    if ch == '\n' {
      has_line_feed_before = true;
      break;
    }
    cnt += 1;
    if cnt > 300 {
      break;
    }
  }
  if cnt < 300 {
    has_line_feed_before = true;
  }

  !has_line_feed_after || !has_line_feed_before
}

pub fn filter_out_disabled_diagnostics(
  diagnostics: Vec<BuildDiagnostic>,
  switcher: &EventKindSwitcher,
) -> impl Iterator<Item = BuildDiagnostic> {
  diagnostics
    .into_iter()
    .filter(|d| switcher.contains(EventKindSwitcher::from_bits_truncate(1 << d.kind() as u32)))
}