(rule *ruleImpl, previousRange TextRangeWithKind, previousStartLine int, currentRange TextRangeWithKind, currentStartLine int)
| 688 | } |
| 689 | |
| 690 | func (w *formatSpanWorker) applyRuleEdits(rule *ruleImpl, previousRange TextRangeWithKind, previousStartLine int, currentRange TextRangeWithKind, currentStartLine int) LineAction { |
| 691 | onLaterLine := currentStartLine != previousStartLine |
| 692 | switch rule.Action() { |
| 693 | case ruleActionStopProcessingSpaceActions: |
| 694 | // no action required |
| 695 | return LineActionNone |
| 696 | case ruleActionDeleteSpace: |
| 697 | if previousRange.Loc.End() != currentRange.Loc.Pos() { |
| 698 | // delete characters starting from t1.end up to t2.pos exclusive |
| 699 | w.recordDelete(previousRange.Loc.End(), currentRange.Loc.Pos()-previousRange.Loc.End()) |
| 700 | if onLaterLine { |
| 701 | return LineActionLineRemoved |
| 702 | } |
| 703 | return LineActionNone |
| 704 | } |
| 705 | case ruleActionDeleteToken: |
| 706 | w.recordDelete(previousRange.Loc.Pos(), previousRange.Loc.Len()) |
| 707 | case ruleActionInsertNewLine: |
| 708 | // exit early if we on different lines and rule cannot change number of newlines |
| 709 | // if line1 and line2 are on subsequent lines then no edits are required - ok to exit |
| 710 | // if line1 and line2 are separated with more than one newline - ok to exit since we cannot delete extra new lines |
| 711 | if rule.Flags() != ruleFlagsCanDeleteNewLines && previousStartLine != currentStartLine { |
| 712 | return LineActionNone |
| 713 | } |
| 714 | |
| 715 | // edit should not be applied if we have one line feed between elements |
| 716 | lineDelta := currentStartLine - previousStartLine |
| 717 | if lineDelta != 1 { |
| 718 | w.recordReplace(previousRange.Loc.End(), currentRange.Loc.Pos()-previousRange.Loc.End(), GetNewLineOrDefaultFromContext(w.ctx)) |
| 719 | if onLaterLine { |
| 720 | return LineActionNone |
| 721 | } |
| 722 | return LineActionLineAdded |
| 723 | } |
| 724 | case ruleActionInsertSpace: |
| 725 | // exit early if we on different lines and rule cannot change number of newlines |
| 726 | if rule.Flags() != ruleFlagsCanDeleteNewLines && previousStartLine != currentStartLine { |
| 727 | return LineActionNone |
| 728 | } |
| 729 | |
| 730 | posDelta := currentRange.Loc.Pos() - previousRange.Loc.End() |
| 731 | if posDelta != 1 || !strings.HasPrefix(w.sourceFile.Text()[previousRange.Loc.End():], " ") { |
| 732 | w.recordReplace(previousRange.Loc.End(), posDelta, " ") |
| 733 | if onLaterLine { |
| 734 | return LineActionLineRemoved |
| 735 | } |
| 736 | return LineActionNone |
| 737 | } |
| 738 | case ruleActionInsertTrailingSemicolon: |
| 739 | w.recordInsert(previousRange.Loc.End(), ";") |
| 740 | } |
| 741 | return LineActionNone |
| 742 | } |
| 743 | |
| 744 | type LineAction int |
| 745 |
no test coverage detected