File: align.go

package info (click to toggle)
golang-github-dsnet-golib 0.0~git20171103.1ea1667-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 240 kB
  • sloc: makefile: 2
file content (204 lines) | stat: -rw-r--r-- 5,793 bytes parent folder | download | duplicates (2)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright 2017, Joe Tsai. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.md file.

package jsonfmt

import (
	"bytes"
	"fmt"
	"regexp"
	"strings"
)

// tokenRegex is a regexp for any valid JSON token (not including comments).
var tokenRegex = regexp.MustCompile(fmt.Sprintf("^(%s)",
	strings.Join([]string{stringRx, numberRx, literalRx, spaceRx}, "|")))

// alignJSON inserts spaces into certain lines in the input to
// to achieve some form of vertical alignment across related lines.
func alignJSON(in []byte, alignLimit int) (out []byte) {
	if alignLimit <= 0 {
		return in
	}

	// Parse the JSON structure on a line-by-line basis.
	// TODO: Should we just record this information in format?
	lines := bytes.Split(in, newlineBytes)
	infos := make([]lineInfo, len(lines))
	var inBlockComment bool
	for i, line := range lines {
		nl := len(line)
		info := &infos[i]
		if inBlockComment {
			info.numIndents = -1 // Invalidate alignment for this line
		} else {
			info.numIndents = len(indentPrefix(line))
		}

		var hasTokens bool
		var lastComma bool
		for len(line) > 0 {
			if inBlockComment {
				if j := bytes.Index(line, []byte("*/")); j >= 0 {
					inBlockComment = false
					line = line[j+len("*/"):]
				} else {
					line = nil
				}
				continue
			}
			if b := tokenRegex.Find(line); len(b) > 0 {
				hasTokens = true
				line = line[len(b):] // Ignore any other JSON token
				continue
			}
			switch line[0] {
			case '/': // Handle comments
				inBlockComment = len(line) > 1 && line[1] == '*'
				if !inBlockComment {
					if hasTokens { // First comment is not "end" comment
						info.commentOffset = nl - len(line)
					}
					line = nil // Rest of line is comment
				}
				continue
			case '{', '[': // Record open braces
				info.bracesBalance++
			case '}', ']': // Record close braces
				info.bracesBalance--
			case ':', ',': // Record alignment markers
				if len(info.alignOffsets) > 0 && info.alignBracesLevel == info.bracesBalance {
					info.alignOffsets = append(info.alignOffsets, nl-len(line)+1)
					lastComma = line[0] == ','
				} else if len(info.alignOffsets) == 0 && line[0] == ':' {
					info.alignOffsets = append(info.alignOffsets, nl-len(line)+1)
					info.alignBracesLevel = info.bracesBalance
				}
			default:
				return in // Invalid JSON
			}
			line = line[1:]
		}
		if lastComma {
			info.alignOffsets = info.alignOffsets[:len(info.alignOffsets)-1]
		}
	}

	// For each indented group, align according to each marker and end comment.
	for i := 0; i < len(infos); {
		infoGroup := nextGroup(infos[i:])
		if len(infoGroup) > 1 {
			var maxAligns int // Maximum number of align markers
			for _, li := range infoGroup {
				if len(li.alignOffsets) > maxAligns {
					maxAligns = len(li.alignOffsets)
				}
			}
			lineGroup := lines[i : i+len(infoGroup)]
			for j := 0; j < maxAligns; j++ {
				alignPositions(lineGroup, infoGroup, j, alignLimit) // Align markers
			}
			alignPositions(lineGroup, infoGroup, -1, alignLimit) // Align comments
		}
		i += len(infoGroup)
	}
	return bytes.Join(lines, newlineBytes)
}

type lineInfo struct {
	numIndents       int   // Number of preceding indents
	bracesBalance    int   // Incremented for '{' or '['; decremented for '}' or ']'
	alignBracesLevel int   // Value of bracesBalance at first append
	alignOffsets     []int // Offset of ':' and ',' in same brace level
	commentOffset    int   // Offset of trailing comment
}

// alignOffset returns the offset into the line for the i-th align marker.
// A negative i returns the offset of the end comment.
// An offset of zero implies that it is invalid.
func (li *lineInfo) alignOffset(i int) int {
	if i < 0 {
		return li.commentOffset
	}
	if i < len(li.alignOffsets) {
		return li.alignOffsets[i]
	}
	return 0
}

// insertSpaces inserts n spaces at pos into line.
// This also updates lineInfo to account for the shifted offsets.
func (li *lineInfo) insertSpaces(line []byte, pos, n int) []byte {
	if n <= 0 || pos >= len(line) {
		return line
	}
	for i, p := range li.alignOffsets {
		if p > pos {
			li.alignOffsets[i] = p + n
		}
	}
	if li.commentOffset > pos {
		li.commentOffset += n
	}
	// TODO: This is inefficient because of repeated copying.
	line = append(line[:pos:pos], append(bytes.Repeat(spaceBytes, n), line[pos:]...)...)
	return line
}

// nextGroup returns the next set of infos that are grouped together
// based on their indent level. This always returns a non-empty slice
// if the input is non-empty.
func nextGroup(infos []lineInfo) []lineInfo {
	if len(infos) <= 1 {
		return infos
	}
	li0 := infos[0]
	n := 1
	if li0.numIndents < 0 || li0.bracesBalance != 0 {
		return infos[:n]
	}
	for _, li := range infos[1:] {
		if li.numIndents != li0.numIndents || li.bracesBalance != 0 {
			return infos[:n]
		}
		n++
	}
	return infos[:n]
}

// alignPositions takes in a set of lines (and their associated lineInfos)
// and aligns each line for the idx-th (-1 for end comment) align marker.
// The limit controls the maximum number of spaces added to achieve alignment.
func alignPositions(lines [][]byte, infos []lineInfo, idx int, limit int) {
	for len(infos) > 1 {
		li0 := infos[0]
		pos0 := li0.alignOffset(idx)
		if pos0 == 0 {
			infos, lines = infos[1:], lines[1:]
			continue
		}

		// Cluster infos into a subgroup to align together.
		n := 1
		maxPos := pos0
		for _, li := range infos[1:] {
			pos := li.alignOffset(idx)
			if pos == 0 || pos > pos0+limit || pos < pos0-limit {
				break
			}
			if maxPos < pos {
				maxPos = pos
			}
			n++
		}

		// Align the subgroup.
		for i, li := range infos[:n] {
			pos := li.alignOffset(idx)
			lines[i] = infos[i].insertSpaces(lines[i], pos, maxPos-pos)
		}

		infos, lines = infos[n:], lines[n:]
	}
}