Relocate cursor on reOpen()

This commit is contained in:
Zachary Yedidia 2016-04-24 17:26:42 -04:00
parent e1c1372f8f
commit 20ab7a44ab
2 changed files with 17 additions and 5 deletions

View file

@ -304,6 +304,22 @@ func (c *Cursor) GetVisualX() int {
return c.x + NumOccurences(string(runes[:c.x]), '\t')*(tabSize-1)
}
// Relocate makes sure that the cursor is inside the bounds of the buffer
// If it isn't, it moves it to be within the buffer's lines
func (c *Cursor) Relocate() {
if c.y < 0 {
c.y = 0
} else if c.y >= len(c.v.buf.lines) {
c.y = len(c.v.buf.lines) - 1
}
if c.x < 0 {
c.x = 0
} else if c.x > Count(c.v.buf.lines[c.y]) {
c.x = Count(c.v.buf.lines[c.y])
}
}
// Display draws the cursor to the screen at the correct position
func (c *Cursor) Display() {
// Don't draw the cursor if it is out of the viewport or if it has a selection

View file

@ -183,15 +183,11 @@ func (v *View) reOpen() {
buf := NewBuffer(string(file), filename)
v.buf = buf
v.matches = Match(v)
v.cursor.Relocate()
v.Relocate()
}
}
// OpenFile opens a new file in the current view
// It makes sure that the current buffer can be closed first (unsaved changes)
func (v *View) OpenFile() {
}
// Relocate moves the view window so that the cursor is in view
// This is useful if the user has scrolled far away, and then starts typing
func (v *View) Relocate() bool {