From 717ceeb85d651d4f76e1074e022cdf9490747ba1 Mon Sep 17 00:00:00 2001 From: ritheshSalyan Date: Fri, 19 Feb 2021 23:07:56 +0530 Subject: [PATCH] add Ignore Rule --- lib/models/rules/format.dart | 38 ++++++++++++++++++++++++++++++++++++ lib/models/rules/rule.dart | 3 ++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/lib/models/rules/format.dart b/lib/models/rules/format.dart index 13f7b518..db94e571 100644 --- a/lib/models/rules/format.dart +++ b/lib/models/rules/format.dart @@ -132,3 +132,41 @@ class ResolveInlineFormatRule extends FormatRule { return delta; } } + +class ResolveInlineIgnoreFormatRule extends FormatRule { + const ResolveInlineIgnoreFormatRule(); + + @override + Delta applyRule(Delta document, int index, + {int len, Object data, Attribute attribute}) { + if (attribute.scope != AttributeScope.IGNORE) { + return null; + } + + Delta delta = Delta()..retain(index, attribute.toJson()); + DeltaIterator itr = DeltaIterator(document); + itr.skip(index); + + Operation op; + for (int cur = 0; cur < len && itr.hasNext; cur += op.length) { + op = itr.next(len - cur); + String text = op.data is String ? op.data as String : ''; + int lineBreak = text.indexOf('\n'); + if (lineBreak < 0) { + delta.retain(op.length, attribute.toJson()); + continue; + } + int pos = 0; + while (lineBreak >= 0) { + delta..retain(lineBreak - pos, attribute.toJson())..retain(1); + pos = lineBreak + 1; + lineBreak = text.indexOf('\n', pos); + } + if (pos < op.length) { + delta.retain(op.length - pos, attribute.toJson()); + } + } + + return delta; + } +} diff --git a/lib/models/rules/rule.dart b/lib/models/rules/rule.dart index d0e3bc14..efd66521 100644 --- a/lib/models/rules/rule.dart +++ b/lib/models/rules/rule.dart @@ -34,6 +34,7 @@ class Rules { FormatLinkAtCaretPositionRule(), ResolveLineFormatRule(), ResolveInlineFormatRule(), + ResolveInlineIgnoreFormatRule(), InsertEmbedsRule(), ForceNewlineForInsertsAroundEmbedRule(), AutoExitBlockRule(), @@ -70,6 +71,6 @@ class Rules { throw e; } } - throw ('Apply rules failed'); + // throw ('Apply rules failed'); } }