Skip to content

Commit 1c1a35a

Browse files
committed
added LocVoid() and Loc.IsVoid()
1 parent e2ae140 commit 1c1a35a

5 files changed

Lines changed: 23 additions & 13 deletions

File tree

cmd/micro/micro.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func LoadInput(args []string) []*buffer.Buffer {
165165
}
166166

167167
files := make([]string, 0, len(args))
168-
flagStartPos := buffer.Loc{-1, -1}
168+
flagStartPos := buffer.LocVoid()
169169
flagr := regexp.MustCompile(`^\+(\d+)(?::(\d+))?$`)
170170
for _, a := range args {
171171
match := flagr.FindStringSubmatch(a)

internal/action/command.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ func (h *BufPane) ReplaceCmd(args []string) {
971971
return l.GreaterEqual(start) && l.LessEqual(end)
972972
}
973973

974-
lastMatchEnd := buffer.Loc{-1, -1}
974+
lastMatchEnd := buffer.LocVoid()
975975
var doReplacement func()
976976
doReplacement = func() {
977977
locs := h.Buf.FindDown(rgrp, searchLoc, end)
@@ -986,7 +986,7 @@ func (h *BufPane) ReplaceCmd(args []string) {
986986
// skip empty match right after previous match
987987
if searchLoc == end {
988988
searchLoc = start
989-
lastMatchEnd = buffer.Loc{-1, -1}
989+
lastMatchEnd = buffer.LocVoid()
990990
} else {
991991
searchLoc = searchLoc.Move(1, h.Buf)
992992
}

internal/buffer/buffer.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,18 +217,18 @@ type Buffer struct {
217217
}
218218

219219
// NewBufferFromFileAtLoc opens a new buffer with a given cursor location
220-
// If cursorLoc is {-1, -1} the location does not overwrite what the cursor location
220+
// If cursorLoc is void, the location does not overwrite what the cursor location
221221
// would otherwise be (start of file, or saved cursor position if `savecursor` is
222222
// enabled)
223223
func NewBufferFromFileAtLoc(path string, btype BufType, cursorLoc Loc) (*Buffer, error) {
224224
var err error
225225
filename := path
226-
if config.GetGlobalOption("parsecursor").(bool) && cursorLoc.X == -1 && cursorLoc.Y == -1 {
226+
if config.GetGlobalOption("parsecursor").(bool) && cursorLoc.IsVoid() {
227227
var cursorPos []string
228228
filename, cursorPos = util.GetPathAndCursorPosition(filename)
229229
cursorLoc, err = ParseCursorLocation(cursorPos)
230230
if err != nil {
231-
cursorLoc = Loc{-1, -1}
231+
cursorLoc = LocVoid()
232232
}
233233
}
234234

@@ -280,7 +280,7 @@ func NewBufferFromFileAtLoc(path string, btype BufType, cursorLoc Loc) (*Buffer,
280280
// It will return an empty buffer if the path does not exist
281281
// and an error if the file is a directory
282282
func NewBufferFromFile(path string, btype BufType) (*Buffer, error) {
283-
return NewBufferFromFileAtLoc(path, btype, Loc{-1, -1})
283+
return NewBufferFromFileAtLoc(path, btype, LocVoid())
284284
}
285285

286286
// NewBufferFromStringAtLoc creates a new buffer containing the given string with a cursor loc
@@ -290,7 +290,7 @@ func NewBufferFromStringAtLoc(text, path string, btype BufType, cursorLoc Loc) *
290290

291291
// NewBufferFromString creates a new buffer containing the given string
292292
func NewBufferFromString(text, path string, btype BufType) *Buffer {
293-
return NewBuffer(strings.NewReader(text), int64(len(text)), path, Loc{-1, -1}, btype)
293+
return NewBuffer(strings.NewReader(text), int64(len(text)), path, LocVoid(), btype)
294294
}
295295

296296
// NewBuffer creates a new buffer from a given reader with a given path

internal/buffer/loc.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ type Loc struct {
99
X, Y int
1010
}
1111

12+
// LocVoid returns a Loc strictly smaller then any valid buffer location
13+
func LocVoid() Loc {
14+
return Loc{-1, -1}
15+
}
16+
17+
// IsVoid returns true if the location l is void
18+
func (l Loc) IsVoid() bool {
19+
return l == LocVoid()
20+
}
21+
1222
// LessThan returns true if b is smaller
1323
func (l Loc) LessThan(b Loc) bool {
1424
if l.Y < b.Y {

internal/buffer/search.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (b *Buffer) findDownFunc(rgrp RegexpGroup, start, end Loc, find bytesFind)
9393
}
9494
return Loc{x, i}
9595
} else { // start or end of unused submatch
96-
return Loc{-1, -1}
96+
return LocVoid()
9797
}
9898
})
9999
}
@@ -113,7 +113,7 @@ func (b *Buffer) FindDown(rgrp RegexpGroup, start, end Loc) []Loc {
113113
// FindDownSubmatch returns a slice containing the start and end positions
114114
// of the first match of `rgrp` between `start` and `end` plus those
115115
// of all submatches (capturing groups), or nil if no match exists.
116-
// The start and end positions of an unused submatch are `Loc{-1, -1}`.
116+
// The start and end positions of an unused submatch are void.
117117
func (b *Buffer) FindDownSubmatch(rgrp RegexpGroup, start, end Loc) []Loc {
118118
return b.findDownFunc(rgrp, start, end, (*regexp.Regexp).FindSubmatchIndex)
119119
}
@@ -169,7 +169,7 @@ func (b *Buffer) FindUp(rgrp RegexpGroup, start, end Loc) []Loc {
169169
// FindUpSubmatch returns a slice containing the start and end positions
170170
// of the last match of `rgrp` between `start` and `end` plus those
171171
// of all submatches (capturing groups), or nil if no match exists.
172-
// The start and end positions of an unused submatch are `Loc{-1, -1}`.
172+
// The start and end positions of an unused submatch are void.
173173
func (b *Buffer) FindUpSubmatch(rgrp RegexpGroup, start, end Loc) []Loc {
174174
return b.findUpFunc(rgrp, start, end, func(re *regexp.Regexp, l []byte) []int {
175175
allMatches := re.FindAllSubmatchIndex(l, -1)
@@ -259,7 +259,7 @@ func (b *Buffer) FindAllSubmatch(s string, start, end Loc) ([][]Loc, error) {
259259
func (b *Buffer) MatchedStrings(locs []Loc) []string {
260260
strs := make([]string, len(locs)/2)
261261
for i := 0; 2*i < len(locs); i += 2 {
262-
if locs[2*i] != (Loc{-1, -1}) {
262+
if !locs[2*i].IsVoid() {
263263
strs[i] = string(b.Substr(locs[2*i], locs[2*i+1]))
264264
}
265265
}
@@ -310,7 +310,7 @@ func (b *Buffer) FindNext(s string, start, end, from Loc, down bool, useRegex bo
310310
func (b *Buffer) replaceAllFuncFunc(s string, start, end Loc, find bufferFind, repl func(match []Loc) []byte) (int, Loc, error) {
311311
rgrp, err := NewRegexpGroup(s)
312312
if err != nil {
313-
return -1, Loc{-1, -1}, err
313+
return -1, LocVoid(), err
314314
}
315315

316316
charsEnd := util.CharacterCount(b.LineBytes(end.Y))

0 commit comments

Comments
 (0)