Debian Codegraph @

internal/display/softwrap.go

raw
1 package display
2
3 import (
4 runewidth "github.com/mattn/go-runewidth"
5 "github.com/zyedidia/micro/v2/internal/buffer"
6 "github.com/zyedidia/micro/v2/internal/util"
7 )
8
9 // SLoc represents a vertical scrolling location, i.e. a location of a visual line
10 // in the buffer. When softwrap is enabled, a buffer line may be displayed as
11 // multiple visual lines (rows). So SLoc stores a number of a line in the buffer
12 // and a number of a row within this line.
13 type SLoc struct {
14 Line, Row int
15 }
16
17 // LessThan returns true if s is less b
18 func (s SLoc) LessThan(b SLoc) bool {
19 if s.Line < b.Line {
20 return true
21 }
22 return s.Line == b.Line && s.Row < b.Row
23 }
24
25 // GreaterThan returns true if s is bigger than b
26 func (s SLoc) GreaterThan(b SLoc) bool {
27 if s.Line > b.Line {
28 return true
29 }
30 return s.Line == b.Line && s.Row > b.Row
31 }
32
33 // LessEqual returns true if s is less than or equal to b
34 func (s SLoc) LessEqual(b SLoc) bool {
35 if s.Line < b.Line {
36 return true
37 }
38 if s.Line == b.Line && s.Row < b.Row {
39 return true
40 }
41 return s == b
42 }
43
44 // GreaterEqual returns true if s is bigger than or equal to b
45 func (s SLoc) GreaterEqual(b SLoc) bool {
46 if s.Line > b.Line {
47 return true
48 }
49 if s.Line == b.Line && s.Row > b.Row {
50 return true
51 }
52 return s == b
53 }
54
55 // VLoc represents a location in the buffer as a visual location in the
56 // linewrapped buffer.
57 type VLoc struct {
58 SLoc
59 VisualX int
60 }
61
62 type SoftWrap interface {
63 Scroll(s SLoc, n int) SLoc
64 Diff(s1, s2 SLoc) int
65 SLocFromLoc(loc buffer.Loc) SLoc
66 VLocFromLoc(loc buffer.Loc) VLoc
67 LocFromVLoc(vloc VLoc) buffer.Loc
68 }
69
70 func (w *BufWindow) getVLocFromLoc(loc buffer.Loc) VLoc {
71 vloc := VLoc{SLoc: SLoc{loc.Y, 0}, VisualX: 0}
72
73 if loc.X <= 0 {
74 return vloc
75 }
76
77 if w.bufWidth <= 0 {
78 return vloc
79 }
80
81 wordwrap := w.Buf.Settings["wordwrap"].(bool)
82 tabsize := util.IntOpt(w.Buf.Settings["tabsize"])
83
84 line := w.Buf.LineBytes(loc.Y)
85 x := 0
86 totalwidth := 0
87
88 wordwidth := 0
89 wordoffset := 0
90
91 for len(line) > 0 {
92 r, _, size := util.DecodeCharacter(line)
93 line = line[size:]
94
95 width := 0
96 switch r {
97 case '\t':
98 ts := tabsize - (totalwidth % tabsize)
99 width = util.Min(ts, w.bufWidth-vloc.VisualX)
100 totalwidth += ts
101 default:
102 width = runewidth.RuneWidth(r)
103 totalwidth += width
104 }
105
106 wordwidth += width
107
108 // Collect a complete word to know its width.
109 // If wordwrap is off, every single character is a complete "word".
110 if wordwrap {
111 if !util.IsWhitespace(r) && len(line) > 0 && wordwidth < w.bufWidth {
112 if x < loc.X {
113 wordoffset += width
114 x++
115 }
116 continue
117 }
118 }
119
120 // If a word (or just a wide rune) does not fit in the window
121 if vloc.VisualX+wordwidth > w.bufWidth && vloc.VisualX > 0 {
122 vloc.Row++
123 vloc.VisualX = 0
124 }
125
126 if x == loc.X {
127 vloc.VisualX += wordoffset
128 return vloc
129 }
130 x++
131
132 vloc.VisualX += wordwidth
133
134 wordwidth = 0
135 wordoffset = 0
136
137 if vloc.VisualX >= w.bufWidth {
138 vloc.Row++
139 vloc.VisualX = 0
140 }
141 }
142 return vloc
143 }
144
145 func (w *BufWindow) getLocFromVLoc(svloc VLoc) buffer.Loc {
146 loc := buffer.Loc{X: 0, Y: svloc.Line}
147
148 if w.bufWidth <= 0 {
149 return loc
150 }
151
152 wordwrap := w.Buf.Settings["wordwrap"].(bool)
153 tabsize := util.IntOpt(w.Buf.Settings["tabsize"])
154
155 line := w.Buf.LineBytes(svloc.Line)
156 vloc := VLoc{SLoc: SLoc{svloc.Line, 0}, VisualX: 0}
157
158 totalwidth := 0
159
160 var widths []int
161 if wordwrap {
162 widths = make([]int, 0, w.bufWidth)
163 } else {
164 widths = make([]int, 0, 1)
165 }
166 wordwidth := 0
167
168 for len(line) > 0 {
169 r, _, size := util.DecodeCharacter(line)
170 line = line[size:]
171
172 width := 0
173 switch r {
174 case '\t':
175 ts := tabsize - (totalwidth % tabsize)
176 width = util.Min(ts, w.bufWidth-vloc.VisualX)
177 totalwidth += ts
178 default:
179 width = runewidth.RuneWidth(r)
180 totalwidth += width
181 }
182
183 widths = append(widths, width)
184 wordwidth += width
185
186 // Collect a complete word to know its width.
187 // If wordwrap is off, every single character is a complete "word".
188 if wordwrap {
189 if !util.IsWhitespace(r) && len(line) > 0 && wordwidth < w.bufWidth {
190 continue
191 }
192 }
193
194 // If a word (or just a wide rune) does not fit in the window
195 if vloc.VisualX+wordwidth > w.bufWidth && vloc.VisualX > 0 {
196 if vloc.Row == svloc.Row {
197 if wordwrap {
198 // it's a word, not a wide rune
199 loc.X--
200 }
201 return loc
202 }
203 vloc.Row++
204 vloc.VisualX = 0
205 }
206
207 for i := range widths {
208 vloc.VisualX += widths[i]
209 if vloc.Row == svloc.Row && vloc.VisualX > svloc.VisualX {
210 return loc
211 }
212 loc.X++
213 }
214
215 widths = widths[:0]
216 wordwidth = 0
217
218 if vloc.VisualX >= w.bufWidth {
219 vloc.Row++
220 vloc.VisualX = 0
221 }
222 }
223 return loc
224 }
225
226 func (w *BufWindow) getRowCount(line int) int {
227 eol := buffer.Loc{X: util.CharacterCount(w.Buf.LineBytes(line)), Y: line}
228 return w.getVLocFromLoc(eol).Row + 1
229 }
230
231 func (w *BufWindow) scrollUp(s SLoc, n int) SLoc {
232 for n > 0 {
233 if n <= s.Row {
234 s.Row -= n
235 n = 0
236 } else if s.Line > 0 {
237 s.Line--
238 n -= s.Row + 1
239 s.Row = w.getRowCount(s.Line) - 1
240 } else {
241 s.Row = 0
242 break
243 }
244 }
245 return s
246 }
247
248 func (w *BufWindow) scrollDown(s SLoc, n int) SLoc {
249 for n > 0 {
250 rc := w.getRowCount(s.Line)
251 if n < rc-s.Row {
252 s.Row += n
253 n = 0
254 } else if s.Line < w.Buf.LinesNum()-1 {
255 s.Line++
256 n -= rc - s.Row
257 s.Row = 0
258 } else {
259 s.Row = rc - 1
260 break
261 }
262 }
263 return s
264 }
265
266 func (w *BufWindow) scroll(s SLoc, n int) SLoc {
267 if n < 0 {
268 return w.scrollUp(s, -n)
269 }
270 return w.scrollDown(s, n)
271 }
272
273 func (w *BufWindow) diff(s1, s2 SLoc) int {
274 n := 0
275 for s1.LessThan(s2) {
276 if s1.Line < s2.Line {
277 n += w.getRowCount(s1.Line) - s1.Row
278 s1.Line++
279 s1.Row = 0
280 } else {
281 n += s2.Row - s1.Row
282 s1.Row = s2.Row
283 }
284 }
285 return n
286 }
287
288 // Scroll returns the location which is n visual lines below the location s
289 // i.e. the result of scrolling n lines down. n can be negative,
290 // which means scrolling up. The returned location is guaranteed to be
291 // within the buffer boundaries.
292 func (w *BufWindow) Scroll(s SLoc, n int) SLoc {
293 if !w.Buf.Settings["softwrap"].(bool) {
294 s.Line = util.Clamp(s.Line+n, 0, w.Buf.LinesNum()-1)
295 return s
296 }
297 return w.scroll(s, n)
298 }
299
300 // Diff returns the difference (the vertical distance) between two SLocs.
301 func (w *BufWindow) Diff(s1, s2 SLoc) int {
302 if !w.Buf.Settings["softwrap"].(bool) {
303 return s2.Line - s1.Line
304 }
305 if s1.GreaterThan(s2) {
306 return -w.diff(s2, s1)
307 }
308 return w.diff(s1, s2)
309 }
310
311 // SLocFromLoc takes a position in the buffer and returns the location
312 // of the visual line containing this position.
313 func (w *BufWindow) SLocFromLoc(loc buffer.Loc) SLoc {
314 if !w.Buf.Settings["softwrap"].(bool) {
315 return SLoc{loc.Y, 0}
316 }
317 return w.getVLocFromLoc(loc).SLoc
318 }
319
320 // VLocFromLoc takes a position in the buffer and returns the corresponding
321 // visual location in the linewrapped buffer.
322 func (w *BufWindow) VLocFromLoc(loc buffer.Loc) VLoc {
323 if !w.Buf.Settings["softwrap"].(bool) {
324 tabsize := util.IntOpt(w.Buf.Settings["tabsize"])
325
326 visualx := util.StringWidth(w.Buf.LineBytes(loc.Y), loc.X, tabsize)
327 return VLoc{SLoc{loc.Y, 0}, visualx}
328 }
329 return w.getVLocFromLoc(loc)
330 }
331
332 // LocFromVLoc takes a visual location in the linewrapped buffer and returns
333 // the position in the buffer corresponding to this visual location.
334 func (w *BufWindow) LocFromVLoc(vloc VLoc) buffer.Loc {
335 if !w.Buf.Settings["softwrap"].(bool) {
336 tabsize := util.IntOpt(w.Buf.Settings["tabsize"])
337
338 x := util.GetCharPosInLine(w.Buf.LineBytes(vloc.Line), vloc.VisualX, tabsize)
339 return buffer.Loc{x, vloc.Line}
340 }
341 return w.getLocFromVLoc(vloc)
342 }