StyleFix.h header
#include <ew/app/StyleFix.h>
Namespace ew::app
StyleFix struct
struct ew::app::StyleFix
One correction a style rule offers for an issue it found: replace the code-unit span [offset, offset + length) of the analyzed text with replacement. Applying it is a plain substitution, so a caller needs no knowledge of what the fix means.
The span is the FIX'S OWN, not necessarily the diagnostic's flagged span, because a correction that reads as clean prose usually has to touch a little more than the words at fault: deleting a filler word must also close the gap it leaves, deleting a sentence's first word must re-capitalise the next one, and deleting a comma-set-off aside must take its commas. Building those wider spans is what deletionFix() is for.
It carries no menu label – kind and subject are the parts a caller needs to write one, so the wording (and its translation) stays in the UI where it belongs.
Members
StyleFixKind ew::app::style::StyleFix::kind =
Whether the fix cuts the words or swaps them.
QString ew::app::style::StyleFix::subject
The flagged words this fix acts on, exactly as they appear in the text – the phrase to name in a menu item. It is the rule's own span, never the widened one, so it reads as what the writer sees underlined.
qsizetype ew::app::style::StyleFix::offset = 0
Code-unit offset of the span this fix rewrites, within the analyzed text.
qsizetype ew::app::style::StyleFix::length = 0
Length in code units of the span this fix rewrites; may be zero (a pure insertion).
QString ew::app::style::StyleFix::replacement
The text that replaces the span. For a Replace fix it is the new wording; for a Delete fix it holds only what repairs the seam – a single space, a re-capitalised letter, or nothing.
bool operator==(const StyleFix &, const StyleFix &)=default
Fixes compare equal when every field matches.
Enumerations
enum class StyleFixKind { Delete, Replace }
What a fix does to the words it names, so a UI can phrase it in the reader's language.
Functions
QString ew::app::style::applyStyleFix(const QString &text, const StyleFix &fix)
Returns text with fix applied. The fix's span is clamped to text first, so a fix built against text that has since changed can only ever rewrite less than it meant to – never read out of bounds.
StyleFix ew::app::style::deletionFix(QStringView text, qsizetype offset, qsizetype length)
The fix that deletes [offset, length) of text, widened so what remains reads as if the words had never been there. Four repairs, in this order:
- Commas. A span set off by a comma on both sides (
She was, honestly, tired) takes both commas with it; a sentence-initial span followed by one (Actually, she left) takes that comma. A mid-sentence span with only a trailing comma keeps it – there the comma separates clauses the deletion does not join. - Whitespace. The runs on both sides go, replaced by a single space when text remains on both sides, or by nothing at a paragraph edge. A line break is never crossed or eaten, so a deletion cannot merge two paragraphs.
- Hugging punctuation. No space is inserted before punctuation that attaches to the word before it (
. , ; : ! ? ) ] } ” ’ …), soHe ran quickly.loses the adverb, not the spacing. The ASCII'and"are deliberately excluded: they are as likely to be opening quotes. - Capitalisation. Deleting the word that opened a sentence upper-cases the word now opening it, so
Just then, she leftbecomesThen, she leftand notthen, she left.
The span is clamped to text; an empty span yields a fix that changes nothing.
StyleFix ew::app::style::phraseReplacementFix(QStringView text, qsizetype offset, qsizetype length, const QString &replacement)
The fix that replaces [offset, length) of text with replacement, carrying the flagged span's leading capital onto it so a phrase replaced at the start of a sentence keeps its capital. The span is clamped to text. Use it when a rule has a concrete better wording (a wordy phrase's concise equivalent, a cliché's plain-language form); use deletionFix() when the correction is to cut words.