github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/linter/autodisable/placementStrategy.go (about)

     1  package autodisable
     2  
     3  import "github.com/yoheimuta/go-protoparser/v4/parser"
     4  
     5  // PlacementType is a selection of the placement strategies.
     6  type PlacementType int
     7  
     8  const (
     9  	// Noop does nothing
    10  	Noop PlacementType = iota
    11  	// ThisThenNext puts inline comments.
    12  	ThisThenNext
    13  	// Next puts newline comments.
    14  	Next
    15  )
    16  
    17  // NewPlacementStrategy creates a strategy object.
    18  func NewPlacementStrategy(ptype PlacementType, filename, ruleID string) (PlacementStrategy, error) {
    19  	if ptype == Noop {
    20  		return &noopPlacementStrategy{}, nil
    21  	}
    22  
    23  	c, err := newCommentator(filename, ruleID)
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  	switch ptype {
    28  	case ThisThenNext:
    29  		return newThisThenNextPlacementStrategy(c), err
    30  	case Next:
    31  		return newNextPlacementStrategy(c), err
    32  	default:
    33  		return nil, nil
    34  	}
    35  }
    36  
    37  // PlacementStrategy is an abstraction to put a comment.
    38  type PlacementStrategy interface {
    39  	Disable(offset int, comments []*parser.Comment, inline *parser.Comment)
    40  	Finalize() error
    41  }
    42  
    43  type noopPlacementStrategy struct{}
    44  
    45  func (p *noopPlacementStrategy) Disable(
    46  	offset int,
    47  	comments []*parser.Comment,
    48  	_ *parser.Comment) {
    49  }
    50  
    51  func (p *noopPlacementStrategy) Finalize() error {
    52  	return nil
    53  }