Small optimization

This commit is contained in:
Zachary Yedidia
2016-05-05 11:23:20 -04:00
parent f6dc983823
commit 87d9221a73
5 changed files with 27 additions and 25 deletions

View File

@@ -791,10 +791,10 @@ func (v *View) Start() bool {
// End moves the viewport to the end of the buffer
func (v *View) End() bool {
if v.height > len(v.buf.lines) {
if v.height > v.buf.numLines {
v.topline = 0
} else {
v.topline = len(v.buf.lines) - v.height
v.topline = v.buf.numLines - v.height
}
return false
}
@@ -811,10 +811,10 @@ func (v *View) PageUp() bool {
// PageDown scrolls the view down a page
func (v *View) PageDown() bool {
if len(v.buf.lines)-(v.topline+v.height) > v.height {
if v.buf.numLines-(v.topline+v.height) > v.height {
v.ScrollDown(v.height)
} else if len(v.buf.lines) >= v.height {
v.topline = len(v.buf.lines) - v.height
} else if v.buf.numLines >= v.height {
v.topline = v.buf.numLines - v.height
}
return false
}
@@ -831,11 +831,11 @@ func (v *View) HalfPageUp() bool {
// HalfPageDown scrolls the view down half a page
func (v *View) HalfPageDown() bool {
if len(v.buf.lines)-(v.topline+v.height) > v.height/2 {
if v.buf.numLines-(v.topline+v.height) > v.height/2 {
v.ScrollDown(v.height / 2)
} else {
if len(v.buf.lines) >= v.height {
v.topline = len(v.buf.lines) - v.height
if v.buf.numLines >= v.height {
v.topline = v.buf.numLines - v.height
}
}
return false
@@ -865,12 +865,12 @@ func (v *View) JumpLine() bool {
return false
}
// Move cursor and view if possible.
if lineint < len(v.buf.lines) {
if lineint < v.buf.numLines {
v.cursor.x = 0
v.cursor.y = lineint
return true
}
messenger.Error("Only ", len(v.buf.lines), " lines to jump")
messenger.Error("Only ", v.buf.numLines, " lines to jump")
return false
}

View File

@@ -27,7 +27,8 @@ type Buffer struct {
// Provide efficient and easy access to text and lines so the rope String does not
// need to be constantly recalculated
// These variables are updated in the update() function
lines []string
lines []string
numLines int
// Syntax highlighting rules
rules []SyntaxRule
@@ -70,6 +71,7 @@ func (b *Buffer) String() string {
// Update fetches the string from the rope and updates the `text` and `lines` in the buffer
func (b *Buffer) Update() {
b.lines = strings.Split(b.String(), "\n")
b.numLines = len(b.lines)
}
// Save saves the buffer to its default path

View File

@@ -112,7 +112,7 @@ func (c *Cursor) SelectLine() {
c.Start()
c.curSelection[0] = c.Loc()
c.End()
if len(c.v.buf.lines)-1 > c.y {
if c.v.buf.numLines-1 > c.y {
c.curSelection[1] = c.Loc() + 1
} else {
c.curSelection[1] = c.Loc()
@@ -282,7 +282,7 @@ func (c *Cursor) Up() {
// Down moves the cursor down one line (if possible)
func (c *Cursor) Down() {
if c.y < len(c.v.buf.lines)-1 {
if c.y < c.v.buf.numLines-1 {
c.y++
runes := []rune(c.v.buf.lines[c.y])
@@ -361,8 +361,8 @@ func (c *Cursor) GetVisualX() int {
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
} else if c.y >= c.v.buf.numLines {
c.y = c.v.buf.numLines - 1
}
if c.x < 0 {

View File

@@ -386,8 +386,8 @@ func Match(v *View) SyntaxMatches {
viewStart := v.topline
viewEnd := v.topline + v.height
if viewEnd > len(buf.lines) {
viewEnd = len(buf.lines)
if viewEnd > buf.numLines {
viewEnd = buf.numLines
}
lines := buf.lines[viewStart:viewEnd]
@@ -403,8 +403,8 @@ func Match(v *View) SyntaxMatches {
if totalStart < 0 {
totalStart = 0
}
if totalEnd > len(buf.lines) {
totalEnd = len(buf.lines)
if totalEnd > buf.numLines {
totalEnd = buf.numLines
}
str := strings.Join(buf.lines[totalStart:totalEnd], "\n")

View File

@@ -121,9 +121,9 @@ func (v *View) ScrollUp(n int) {
// ScrollDown scrolls the view down n lines (if possible)
func (v *View) ScrollDown(n int) {
// Try to scroll by n but if it would overflow, scroll by 1
if v.topline+n <= len(v.buf.lines)-v.height {
if v.topline+n <= v.buf.numLines-v.height {
v.topline += n
} else if v.topline < len(v.buf.lines)-v.height {
} else if v.topline < v.buf.numLines-v.height {
v.topline++
}
}
@@ -223,8 +223,8 @@ func (v *View) MoveToMouseClick(x, y int) {
v.ScrollDown(1)
y = v.height + v.topline - 1
}
if y >= len(v.buf.lines) {
y = len(v.buf.lines) - 1
if y >= v.buf.numLines {
y = v.buf.numLines - 1
}
if y < 0 {
y = 0
@@ -391,7 +391,7 @@ func (v *View) DisplayView() {
// Convert the length of buffer to a string, and get the length of the string
// We are going to have to offset by that amount
maxLineLength := len(strconv.Itoa(len(v.buf.lines)))
maxLineLength := len(strconv.Itoa(v.buf.numLines))
// + 1 for the little space after the line number
if settings["ruler"] == true {
v.lineNumOffset = maxLineLength + 1
@@ -408,7 +408,7 @@ func (v *View) DisplayView() {
var x int
// If the buffer is smaller than the view height
// and we went too far, break
if lineN+v.topline >= len(v.buf.lines) {
if lineN+v.topline >= v.buf.numLines {
break
}
line := v.buf.lines[lineN+v.topline]