mirror of
https://github.com/Hopiu/micro.git
synced 2026-04-16 12:21:13 +00:00
Add buffer.WordAt (#2070)
Add buffer.WordAt function returning the word around a given location in the buffer. Useful for plugins.
This commit is contained in:
parent
c5798b5b8c
commit
1f73f8587c
1 changed files with 21 additions and 0 deletions
|
|
@ -545,6 +545,27 @@ func (b *Buffer) RuneAt(loc Loc) rune {
|
|||
return '\n'
|
||||
}
|
||||
|
||||
// WordAt returns the word around a given location in the buffer
|
||||
func (b *Buffer) WordAt(loc Loc) []byte {
|
||||
if len(b.LineBytes(loc.Y)) == 0 || !util.IsWordChar(b.RuneAt(loc)) {
|
||||
return []byte{}
|
||||
}
|
||||
|
||||
start := loc
|
||||
end := loc.Move(1, b)
|
||||
|
||||
for start.X > 0 && util.IsWordChar(b.RuneAt(start.Move(-1, b))) {
|
||||
start.X--
|
||||
}
|
||||
|
||||
lineLen := util.CharacterCount(b.LineBytes(loc.Y))
|
||||
for end.X < lineLen && util.IsWordChar(b.RuneAt(end)) {
|
||||
end.X++
|
||||
}
|
||||
|
||||
return b.Substr(start, end)
|
||||
}
|
||||
|
||||
// Modified returns if this buffer has been modified since
|
||||
// being opened
|
||||
func (b *Buffer) Modified() bool {
|
||||
|
|
|
|||
Loading…
Reference in a new issue