From 87d9221a7314533926e5288dda83d4d21c8c0970 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Thu, 5 May 2016 11:23:20 -0400 Subject: [PATCH] Small optimization --- cmd/micro/bindings.go | 20 ++++++++++---------- cmd/micro/buffer.go | 4 +++- cmd/micro/cursor.go | 8 ++++---- cmd/micro/highlighter.go | 8 ++++---- cmd/micro/view.go | 12 ++++++------ 5 files changed, 27 insertions(+), 25 deletions(-) diff --git a/cmd/micro/bindings.go b/cmd/micro/bindings.go index 13308f02..437cb618 100644 --- a/cmd/micro/bindings.go +++ b/cmd/micro/bindings.go @@ -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 } diff --git a/cmd/micro/buffer.go b/cmd/micro/buffer.go index 1f3e8686..34ef3d1a 100644 --- a/cmd/micro/buffer.go +++ b/cmd/micro/buffer.go @@ -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 diff --git a/cmd/micro/cursor.go b/cmd/micro/cursor.go index db94cc36..88287ac4 100644 --- a/cmd/micro/cursor.go +++ b/cmd/micro/cursor.go @@ -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 { diff --git a/cmd/micro/highlighter.go b/cmd/micro/highlighter.go index 50bdc6e1..bcf0aabd 100644 --- a/cmd/micro/highlighter.go +++ b/cmd/micro/highlighter.go @@ -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") diff --git a/cmd/micro/view.go b/cmd/micro/view.go index 264a8b77..175799f6 100644 --- a/cmd/micro/view.go +++ b/cmd/micro/view.go @@ -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]