MCPcopy Create free account
hub / github.com/microsoft/typescript-go / applyRuleEdits

Method applyRuleEdits

internal/format/span.go:690–742  ·  view source on GitHub ↗
(rule *ruleImpl, previousRange TextRangeWithKind, previousStartLine int, currentRange TextRangeWithKind, currentStartLine int)

Source from the content-addressed store, hash-verified

688}
689
690func (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
744type LineAction int
745

Callers 1

processPairMethod · 0.95

Calls 10

recordDeleteMethod · 0.95
recordReplaceMethod · 0.95
recordInsertMethod · 0.95
ActionMethod · 0.80
EndMethod · 0.65
PosMethod · 0.65
LenMethod · 0.65
TextMethod · 0.65
FlagsMethod · 0.45

Tested by

no test coverage detected