pub struct Codetag {
pub hide_mnemonic: bool,
/* private fields */
}Expand description
A finder that extracts codetags (TODO, FIXME, etc.) from text.
Codetags are special comments in source code that mark areas needing attention. This finder supports all mnemonics defined in PEP 350, plus common variants.
§Usage
- Create a default instance or configure with specific mnemonics
- Optionally call
Codetag::build_mnemonics_regexto build the matching tables eagerly (Finder::findbuilds them lazily otherwise) - Use the
Finder::findmethod to extract codetags
§Example
use squeeze::{codetag::Codetag, Finder};
let mut finder = Codetag::default();
finder.build_mnemonics_regex().unwrap();
let text = "// FIXME(john): this is broken";
if let Some(range) = finder.find(text) {
println!("Found: {}", &text[range]);
}Fields§
§hide_mnemonic: boolWhen true, the mnemonic (e.g., “TODO:”) is excluded from the result.
Implementations§
Source§impl Codetag
impl Codetag
Sourcepub fn add_mnemonic(&mut self, mnemonic: &str)
pub fn add_mnemonic(&mut self, mnemonic: &str)
Adds a custom mnemonic to search for.
When at least one mnemonic is added, only those mnemonics will be matched. If no mnemonics are added, all default PEP 350 mnemonics are used.
Mnemonic matching is case-insensitive. Surrounding whitespace is
trimmed; empty and whitespace-only mnemonics are ignored, as they
would otherwise match every word:.
Sourcepub fn build_mnemonics_regex(&mut self) -> Result<(), Infallible>
pub fn build_mnemonics_regex(&mut self) -> Result<(), Infallible>
Builds the matching tables for the configured mnemonics.
Calling this is optional: Finder::find builds them lazily on
first use. The name is kept from the regex-based implementation; it
cannot fail.
Trait Implementations§
Source§impl Finder for Codetag
impl Finder for Codetag
Source§fn dispatchable(&self) -> bool
fn dispatchable(&self) -> bool
try_at.Source§fn could_start_at(&self, byte: u8) -> bool
fn could_start_at(&self, byte: u8) -> bool
dispatchable returns true.Source§fn could_start_after(&self, prev: u8, cur: u8) -> bool
fn could_start_after(&self, prev: u8, cur: u8) -> bool
cur when the byte
immediately before it is prev: a dispatch-mode start, or a
trigger-mode trigger byte. Read moreSource§fn could_continue_with(&self, cur: u8, next: u8) -> bool
fn could_continue_with(&self, cur: u8, next: u8) -> bool
cur could succeed when the byte
immediately after it is next. Same contract as
could_start_after.Source§fn run_rules(&self) -> Vec<RunRule>
fn run_rules(&self) -> Vec<RunRule>
RunRule::allow before try_at
is called. Same contract as
could_start_after: when the rules
reject a position, try_at must return None there. The scanner
measures the runs once per position for all finders.Source§fn try_at(&self, input: &[u8], pos: usize) -> Option<Range<usize>>
fn try_at(&self, input: &[u8], pos: usize) -> Option<Range<usize>>
pos in the full input.
Only called when dispatchable returns true.Source§fn find(&self, s: &str) -> Option<Range<usize>>
fn find(&self, s: &str) -> Option<Range<usize>>
Source§fn try_trigger_at_memo(
&self,
input: &[u8],
pos: usize,
memo: &mut Memo,
) -> Option<Range<usize>>
fn try_trigger_at_memo( &self, input: &[u8], pos: usize, memo: &mut Memo, ) -> Option<Range<usize>>
Source§fn line_agnostic(&self) -> bool
fn line_agnostic(&self) -> bool
try_at/try_trigger_at give the same answer on a whole
buffer as on the line alone: \n and \r never belong to a match,
and every walk treats them exactly like the end of the input (as a
boundary), so a match never depends on what surrounds its line. A
finder that matches “up to the end of the line” (codetag), accepts
line terminators as whitespace (JSON, phone) or otherwise looks at
the line as a whole must keep the default. Line-agnostic finders are
run over whole buffers with absolute positions, which lets the
scanner skip resolving the line of every candidate; property tests
compare both modes. Defaults to false.Source§fn anchor(&self) -> Option<Anchor>
fn anchor(&self) -> Option<Anchor>
bytes), and the
bytes that can separate a match start from the first of them
(walk). When every finder of a scanner declares an anchor (or is a
trigger finder) and the anchor bytes number three or fewer, the
scanner searches for those bytes with memchr, walks back over
walk and tries the dispatch positions in order, so lines without
an anchor byte cost nothing. Read moreSource§fn triggerable(&self) -> bool
fn triggerable(&self) -> bool
Source§fn could_trigger_at(&self, _byte: u8) -> bool
fn could_trigger_at(&self, _byte: u8) -> bool
triggerable returns true.Source§fn has_trigger_context(&self) -> bool
fn has_trigger_context(&self) -> bool
trigger_context over the
65536 pairs of previous bytes and
trigger_context_exempt over the
next bytes, once, and consults them before calling the finder.
Defaults to false.Source§fn trigger_context(&self, _prev2: u8, _prev1: u8) -> bool
fn trigger_context(&self, _prev2: u8, _prev1: u8) -> bool
prev2 and
prev1, unless the byte after the trigger is exempt
(trigger_context_exempt): a
necessary condition for try_trigger_at
to match at that position. The scanner passes a space for a missing
previous byte (the trigger sits in the first two bytes of the
input). Defaults to true.Source§fn trigger_context_exempt(&self, _next: Option<u8>) -> bool
fn trigger_context_exempt(&self, _next: Option<u8>) -> bool
next (None at the end of the
input) escapes the previous-bytes condition: the URI finder needs a
registered scheme before a colon unless // follows. Defaults to
false.Source§fn try_trigger_at(&self, _input: &[u8], _pos: usize) -> Option<Range<usize>>
fn try_trigger_at(&self, _input: &[u8], _pos: usize) -> Option<Range<usize>>
pos in the full input.
Only called when triggerable returns true.