From 07c37910d70f20e0c54948f9f2230455bf97f01d Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 7 Apr 2021 16:33:00 -0400 Subject: [PATCH] Rename editor.document to not collide with import --- editor/document/row.go | 6 ++++-- editor/draw.go | 22 +++++++++++----------- editor/editor.go | 32 ++++++++++++++++---------------- editor/editor_test.go | 4 ++-- editor/input.go | 22 +++++++++++----------- editor/search.go | 12 ++++++------ 6 files changed, 50 insertions(+), 48 deletions(-) diff --git a/editor/document/row.go b/editor/document/row.go index 07be2b2..54873ca 100644 --- a/editor/document/row.go +++ b/editor/document/row.go @@ -108,12 +108,14 @@ func (r *Row) update() { func (r *Row) updateSyntax() { i := 0 + r.Hl = make([]int, r.RenderSize()) + for i < r.RenderSize() { ch := r.render[i] if unicode.IsDigit(ch) { - r.Hl = append(r.Hl, highlight.Number) + r.Hl[i] = highlight.Number } else { - r.Hl = append(r.Hl, highlight.Normal) + r.Hl[i] = highlight.Normal } i++ diff --git a/editor/draw.go b/editor/draw.go index d84faef..c996322 100644 --- a/editor/draw.go +++ b/editor/draw.go @@ -36,8 +36,8 @@ func (e *editor) RefreshScreen() { func (e *editor) scroll() { e.renderX = 0 - if e.cursor.Y < e.document.RowCount() { - e.renderX = e.document.GetRow(e.cursor.Y).CursorXToRenderX(e.cursor.X) + if e.cursor.Y < e.doc.RowCount() { + e.renderX = e.doc.GetRow(e.cursor.Y).CursorXToRenderX(e.cursor.X) } if e.cursor.Y < e.offset.Y { @@ -61,10 +61,10 @@ func (e *editor) drawRows(ab *gilo.Buffer) { for y := 0; y < e.screen.Rows; y++ { fileRow := y + e.offset.Y - if fileRow >= e.document.RowCount() { + if fileRow >= e.doc.RowCount() { e.drawPlaceholderRow(y, ab) } else { - rawRow := e.document.GetRow(fileRow) + rawRow := e.doc.GetRow(fileRow) // If the column offset is greater than the length of the row, // just display an empty row @@ -83,7 +83,7 @@ func (e *editor) drawRows(ab *gilo.Buffer) { func (e *editor) drawFileRow(fileRow int, ab *gilo.Buffer) { currentColor := terminal.DefaultFGColor - row := e.document.GetRow(fileRow) + row := e.doc.GetRow(fileRow) for i, ch := range row.Render(e.offset) { if row.Hl[i] == highlight.Normal { @@ -108,7 +108,7 @@ func (e *editor) drawFileRow(fileRow int, ab *gilo.Buffer) { } func (e *editor) drawPlaceholderRow(y int, ab *gilo.Buffer) { - if e.document.RowCount() == 0 && y == e.screen.Rows/3 { + if e.doc.RowCount() == 0 && y == e.screen.Rows/3 { welcome := fmt.Sprintf("Gilo editor -- version %s", gilo.Version) if len(welcome) > e.screen.Cols { welcome = gilo.Truncate(welcome, e.screen.Cols) @@ -137,15 +137,15 @@ func (e *editor) drawStatusBar(ab *gilo.Buffer) { ab.Append(terminal.InvertColor) fileName := "[No Name]" - if e.document.Filename != "" { - fileName = e.document.Filename + if e.doc.Filename != "" { + fileName = e.doc.Filename } modified := "" - if e.document.IsDirty() { + if e.doc.IsDirty() { modified = "(modified)" } - leftStatus := fmt.Sprintf("%.20s - %d lines %s", fileName, e.document.RowCount(), modified) + leftStatus := fmt.Sprintf("%.20s - %d lines %s", fileName, e.doc.RowCount(), modified) length := len(leftStatus) if length > cols { leftStatus = gilo.Truncate(leftStatus, cols) @@ -156,7 +156,7 @@ func (e *editor) drawStatusBar(ab *gilo.Buffer) { return } - rightStatus := fmt.Sprintf("%d/%d", e.cursor.Y+1, e.document.RowCount()) + rightStatus := fmt.Sprintf("%d/%d", e.cursor.Y+1, e.doc.RowCount()) rlength := len(rightStatus) statusLength := length + rlength diff --git a/editor/editor.go b/editor/editor.go index 259dfdf..91dee01 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -4,7 +4,7 @@ package editor import ( "fmt" "time" - doc "timshome.page/gilo/editor/document" + "timshome.page/gilo/editor/document" "timshome.page/gilo/gilo" "timshome.page/gilo/key" "timshome.page/gilo/terminal" @@ -23,7 +23,7 @@ type editor struct { screen *terminal.Screen cursor *gilo.Point offset *gilo.Point - document *doc.Document + doc *document.Document status *statusMsg search *search quitTimes uint8 @@ -38,7 +38,7 @@ func NewEditor() *editor { cursor := gilo.DefaultPoint() offset := gilo.DefaultPoint() - document := doc.NewDocument() + d := document.NewDocument() status := &statusMsg{ "", time.Now(), @@ -48,7 +48,7 @@ func NewEditor() *editor { screen, cursor, offset, - document, + d, status, newSearch(), gilo.QuitTimes, @@ -57,7 +57,7 @@ func NewEditor() *editor { } func (e *editor) Open(filename string) { - e.document.Open(filename) + e.doc.Open(filename) } func (e *editor) SetStatusMessage(template string, a ...interface{}) { @@ -74,14 +74,14 @@ func (e *editor) ProcessKeypress() bool { } func (e *editor) save() { - if e.document.Filename == "" { - e.document.Filename = e.prompt("Save as: %s (ESC to cancel)", nil) - if e.document.Filename == "" { + if e.doc.Filename == "" { + e.doc.Filename = e.prompt("Save as: %s (ESC to cancel)", nil) + if e.doc.Filename == "" { e.SetStatusMessage("Save aborted") } } - size := e.document.Save() + size := e.doc.Save() if size > 0 { e.SetStatusMessage("%d bytes written to disk", size) @@ -141,16 +141,16 @@ func (e *editor) prompt(prompt string, callback func(string, string)) string { } func (e *editor) insertChar(ch rune) { - if e.cursor.Y == e.document.RowCount() { - e.document.AppendRow("") + if e.cursor.Y == e.doc.RowCount() { + e.doc.AppendRow("") } - e.document.InsertChar(e.cursor, ch) + e.doc.InsertChar(e.cursor, ch) e.cursor.X += 1 } func (e *editor) delChar() { - if e.cursor.Y == e.document.RowCount() { + if e.cursor.Y == e.doc.RowCount() { return } @@ -161,13 +161,13 @@ func (e *editor) delChar() { if e.cursor.X > 0 { at := e.cursor at.X -= 1 - e.document.DelChar(at) + e.doc.DelChar(at) } else { // Move cursor to the current end of the previous line - e.cursor.X = e.document.GetRow(e.cursor.Y - 1).Size() + e.cursor.X = e.doc.GetRow(e.cursor.Y - 1).Size() // Move the contents of the current row to the previous - e.document.MergeRows(e.cursor.Y-1, e.cursor.Y) + e.doc.MergeRows(e.cursor.Y-1, e.cursor.Y) e.cursor.Y -= 1 } diff --git a/editor/editor_test.go b/editor/editor_test.go index 333d8f6..e6c143a 100644 --- a/editor/editor_test.go +++ b/editor/editor_test.go @@ -14,11 +14,11 @@ func TestInsertChar(t *testing.T) { e := NewEditor() e.insertChar('q') - if e.document.RowCount() != 1 { + if e.doc.RowCount() != 1 { t.Errorf("A row was not created when the character was inserted") } - row := e.document.GetRow(0) + row := e.doc.GetRow(0) if row.Size() != 1 { t.Errorf("Failed to add character to row. Row: %v", row) } diff --git a/editor/input.go b/editor/input.go index d00f5d4..1c536cb 100644 --- a/editor/input.go +++ b/editor/input.go @@ -29,7 +29,7 @@ const ( func (e *editor) processKeypressChar(ch rune) bool { switch ch { case key.Ctrl('q'): - if e.document.IsDirty() && e.quitTimes > 0 { + if e.doc.IsDirty() && e.quitTimes > 0 { e.SetStatusMessage("WARNING!!! File has unsaved changes. Press Ctrl-Q %d more times to quit.", e.quitTimes) e.quitTimes -= 1 @@ -48,7 +48,7 @@ func (e *editor) processKeypressChar(ch rune) bool { e.find() case key.Enter: - e.document.InsertNewline(e.cursor) + e.doc.InsertNewline(e.cursor) e.cursor.Y += 1 e.cursor.X = 0 @@ -106,10 +106,10 @@ func (e *editor) processKeypressStr(key string) { func (e *editor) moveCursor(key string) { var row *document.Row - if e.cursor.Y >= e.document.RowCount() { + if e.cursor.Y >= e.doc.RowCount() { row = nil } else { - row = e.document.GetRow(e.cursor.Y) + row = e.doc.GetRow(e.cursor.Y) } switch key { @@ -121,7 +121,7 @@ func (e *editor) moveCursor(key string) { // Move from beginning of current row to end of previous row if e.cursor.Y > 0 { e.cursor.Y -= 1 - e.cursor.X = e.document.GetRow(e.cursor.Y).Size() + e.cursor.X = e.doc.GetRow(e.cursor.Y).Size() } case keyRight: if row != nil && e.cursor.X < row.Size() { @@ -129,7 +129,7 @@ func (e *editor) moveCursor(key string) { } // Move from end of current line to beginning of next line - if row != nil && e.cursor.X == row.Size() && e.cursor.Y < e.document.RowCount()-1 { + if row != nil && e.cursor.X == row.Size() && e.cursor.Y < e.doc.RowCount()-1 { e.cursor.Y += 1 e.cursor.X = 0 } @@ -138,7 +138,7 @@ func (e *editor) moveCursor(key string) { e.cursor.Y -= 1 } case keyDown: - if e.cursor.Y < e.document.RowCount() { + if e.cursor.Y < e.doc.RowCount() { e.cursor.Y += 1 } case keyPageUp: @@ -149,10 +149,10 @@ func (e *editor) moveCursor(key string) { } case keyPageDown: - if e.cursor.Y+e.screen.Rows > e.document.RowCount() { + if e.cursor.Y+e.screen.Rows > e.doc.RowCount() { e.cursor.Y += e.screen.Rows } else { - e.cursor.Y = e.document.RowCount() - 1 + e.cursor.Y = e.doc.RowCount() - 1 } case keyHome: @@ -163,8 +163,8 @@ func (e *editor) moveCursor(key string) { } } - if e.cursor.Y < e.document.RowCount() { - row = e.document.GetRow(e.cursor.Y) + if e.cursor.Y < e.doc.RowCount() { + row = e.doc.GetRow(e.cursor.Y) rowLen := row.Size() // Snap to the end of a shorter line from a longer one diff --git a/editor/search.go b/editor/search.go index d5418e4..b4f8b45 100644 --- a/editor/search.go +++ b/editor/search.go @@ -46,7 +46,7 @@ func (e *editor) find() { func (e *editor) findCallback(query string, ch string) { if e.search.hlLine != -1 && e.search.hl != nil { - staleRow := e.document.GetRow(e.search.hlLine) + staleRow := e.doc.GetRow(e.search.hlLine) for i, val := range e.search.hl { staleRow.Hl[i] = val } @@ -77,16 +77,16 @@ func (e *editor) findCallback(query string, ch string) { current := e.search.lastMatch - for i := 0; i < e.document.RowCount(); i++ { + for i := 0; i < e.doc.RowCount(); i++ { current += e.search.direction if current == -1 { - current = e.document.RowCount() - 1 - } else if current == e.document.RowCount() { + current = e.doc.RowCount() - 1 + } else if current == e.doc.RowCount() { current = 0 } - row := e.document.GetRow(current) + row := e.doc.GetRow(current) matchIndex := row.Search(query) if matchIndex == -1 { continue @@ -95,7 +95,7 @@ func (e *editor) findCallback(query string, ch string) { e.search.lastMatch = current e.cursor.Y = current e.cursor.X = row.RenderXtoCursorX(matchIndex) - e.offset.Y = e.document.RowCount() + e.offset.Y = e.doc.RowCount() // Update highlighting of search result e.search.hlLine = current