aboutsummaryrefslogtreecommitdiff
path: root/rewrite.go
blob: 4f7465019bd65e4807a156024cc9c45592da5f85 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main

import "strings"

func (f *Formatter) rewrite(formattedSource []byte, lineInformationMap map[int]*lineInformation) []byte {
	sourceLines := strings.Split(string(formattedSource), "\n")
	resultLines := make([]string, 0, len(sourceLines))
	previousWasOpenBrace := false
	previousStatementType := ""
	previousWasComment := false
	previousWasTopLevel := false
	previousWasScoped := false
	insideRawString := false

	for lineIndex, currentLine := range sourceLines {
		backtickCount := countRawStringDelimiters(currentLine)
		wasInsideRawString := insideRawString

		if backtickCount%2 == 1 {
			insideRawString = !insideRawString
		}

		if wasInsideRawString {
			resultLines = append(resultLines, currentLine)

			continue
		}

		lineNumber := lineIndex + 1
		trimmedLine := strings.TrimSpace(currentLine)

		if trimmedLine == "" {
			continue
		}

		isClosingBraceLine := isClosingBrace(currentLine)
		isOpeningBraceLine := isOpeningBrace(currentLine)
		isCaseLabelLine := isCaseLabel(currentLine)
		isCommentOnlyLine := isCommentOnly(currentLine)
		isPackageDeclaration := isPackageLine(trimmedLine)
		currentInformation := lineInformationMap[lineNumber]
		currentStatementType := ""

		if currentInformation != nil {
			currentStatementType = currentInformation.statementType
		}

		if isPackageDeclaration {
			currentStatementType = "package"
		}

		needsBlankLine := false
		currentIsTopLevel := currentInformation != nil && currentInformation.isTopLevel
		currentIsScoped := currentInformation != nil && currentInformation.isScoped

		if len(resultLines) > 0 && !previousWasOpenBrace && !isClosingBraceLine && !isCaseLabelLine {
			if currentIsTopLevel && previousWasTopLevel && currentStatementType != previousStatementType {
				if f.CommentMode == CommentsFollow && previousWasComment {
				} else {
					needsBlankLine = true
				}
			} else if currentInformation != nil && (currentIsScoped || previousWasScoped) {
				if f.CommentMode == CommentsFollow && previousWasComment {
				} else {
					needsBlankLine = true
				}
			} else if currentStatementType != "" && previousStatementType != "" && currentStatementType != previousStatementType {
				if f.CommentMode == CommentsFollow && previousWasComment {
				} else {
					needsBlankLine = true
				}
			}

			if f.CommentMode == CommentsFollow && isCommentOnlyLine && !previousWasComment {
				nextLineNumber := f.findNextNonCommentLine(sourceLines, lineIndex+1)

				if nextLineNumber > 0 {
					nextInformation := lineInformationMap[nextLineNumber]

					if nextInformation != nil {
						nextIsTopLevel := nextInformation.isTopLevel
						nextIsScoped := nextInformation.isScoped

						if nextIsTopLevel && previousWasTopLevel && nextInformation.statementType != previousStatementType {
							needsBlankLine = true
						} else if nextIsScoped || previousWasScoped {
							needsBlankLine = true
						} else if nextInformation.statementType != "" && previousStatementType != "" && nextInformation.statementType != previousStatementType {
							needsBlankLine = true
						}
					}
				}
			}
		}

		if needsBlankLine {
			resultLines = append(resultLines, "")
		}

		resultLines = append(resultLines, currentLine)
		previousWasOpenBrace = isOpeningBraceLine || isCaseLabelLine
		previousWasComment = isCommentOnlyLine

		if currentInformation != nil {
			previousStatementType = currentInformation.statementType
			previousWasTopLevel = currentInformation.isTopLevel
			previousWasScoped = currentInformation.isScoped
		} else if currentStatementType != "" {
			previousStatementType = currentStatementType
			previousWasTopLevel = false
			previousWasScoped = false
		}
	}

	outputString := strings.Join(resultLines, "\n")

	if !strings.HasSuffix(outputString, "\n") {
		outputString += "\n"
	}

	return []byte(outputString)
}

func (f *Formatter) findNextNonCommentLine(sourceLines []string, startLineIndex int) int {
	for lineIndex := startLineIndex; lineIndex < len(sourceLines); lineIndex++ {
		trimmedLine := strings.TrimSpace(sourceLines[lineIndex])

		if trimmedLine == "" {
			continue
		}

		if isCommentOnly(sourceLines[lineIndex]) {
			continue
		}

		return lineIndex + 1
	}

	return 0
}