Rename editor.document to not collide with import
Some checks failed
timw4mail/gilo/pipeline/head There was a failure building this commit

This commit is contained in:
Timothy Warren 2021-04-07 16:33:00 -04:00
parent f78bbdabb2
commit 07c37910d7
6 changed files with 50 additions and 48 deletions

View File

@ -108,12 +108,14 @@ func (r *Row) update() {
func (r *Row) updateSyntax() { func (r *Row) updateSyntax() {
i := 0 i := 0
r.Hl = make([]int, r.RenderSize())
for i < r.RenderSize() { for i < r.RenderSize() {
ch := r.render[i] ch := r.render[i]
if unicode.IsDigit(ch) { if unicode.IsDigit(ch) {
r.Hl = append(r.Hl, highlight.Number) r.Hl[i] = highlight.Number
} else { } else {
r.Hl = append(r.Hl, highlight.Normal) r.Hl[i] = highlight.Normal
} }
i++ i++

View File

@ -36,8 +36,8 @@ func (e *editor) RefreshScreen() {
func (e *editor) scroll() { func (e *editor) scroll() {
e.renderX = 0 e.renderX = 0
if e.cursor.Y < e.document.RowCount() { if e.cursor.Y < e.doc.RowCount() {
e.renderX = e.document.GetRow(e.cursor.Y).CursorXToRenderX(e.cursor.X) e.renderX = e.doc.GetRow(e.cursor.Y).CursorXToRenderX(e.cursor.X)
} }
if e.cursor.Y < e.offset.Y { 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++ { for y := 0; y < e.screen.Rows; y++ {
fileRow := y + e.offset.Y fileRow := y + e.offset.Y
if fileRow >= e.document.RowCount() { if fileRow >= e.doc.RowCount() {
e.drawPlaceholderRow(y, ab) e.drawPlaceholderRow(y, ab)
} else { } else {
rawRow := e.document.GetRow(fileRow) rawRow := e.doc.GetRow(fileRow)
// If the column offset is greater than the length of the row, // If the column offset is greater than the length of the row,
// just display an empty 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) { func (e *editor) drawFileRow(fileRow int, ab *gilo.Buffer) {
currentColor := terminal.DefaultFGColor currentColor := terminal.DefaultFGColor
row := e.document.GetRow(fileRow) row := e.doc.GetRow(fileRow)
for i, ch := range row.Render(e.offset) { for i, ch := range row.Render(e.offset) {
if row.Hl[i] == highlight.Normal { 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) { 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) welcome := fmt.Sprintf("Gilo editor -- version %s", gilo.Version)
if len(welcome) > e.screen.Cols { if len(welcome) > e.screen.Cols {
welcome = gilo.Truncate(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) ab.Append(terminal.InvertColor)
fileName := "[No Name]" fileName := "[No Name]"
if e.document.Filename != "" { if e.doc.Filename != "" {
fileName = e.document.Filename fileName = e.doc.Filename
} }
modified := "" modified := ""
if e.document.IsDirty() { if e.doc.IsDirty() {
modified = "(modified)" 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) length := len(leftStatus)
if length > cols { if length > cols {
leftStatus = gilo.Truncate(leftStatus, cols) leftStatus = gilo.Truncate(leftStatus, cols)
@ -156,7 +156,7 @@ func (e *editor) drawStatusBar(ab *gilo.Buffer) {
return 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) rlength := len(rightStatus)
statusLength := length + rlength statusLength := length + rlength

View File

@ -4,7 +4,7 @@ package editor
import ( import (
"fmt" "fmt"
"time" "time"
doc "timshome.page/gilo/editor/document" "timshome.page/gilo/editor/document"
"timshome.page/gilo/gilo" "timshome.page/gilo/gilo"
"timshome.page/gilo/key" "timshome.page/gilo/key"
"timshome.page/gilo/terminal" "timshome.page/gilo/terminal"
@ -23,7 +23,7 @@ type editor struct {
screen *terminal.Screen screen *terminal.Screen
cursor *gilo.Point cursor *gilo.Point
offset *gilo.Point offset *gilo.Point
document *doc.Document doc *document.Document
status *statusMsg status *statusMsg
search *search search *search
quitTimes uint8 quitTimes uint8
@ -38,7 +38,7 @@ func NewEditor() *editor {
cursor := gilo.DefaultPoint() cursor := gilo.DefaultPoint()
offset := gilo.DefaultPoint() offset := gilo.DefaultPoint()
document := doc.NewDocument() d := document.NewDocument()
status := &statusMsg{ status := &statusMsg{
"", "",
time.Now(), time.Now(),
@ -48,7 +48,7 @@ func NewEditor() *editor {
screen, screen,
cursor, cursor,
offset, offset,
document, d,
status, status,
newSearch(), newSearch(),
gilo.QuitTimes, gilo.QuitTimes,
@ -57,7 +57,7 @@ func NewEditor() *editor {
} }
func (e *editor) Open(filename string) { func (e *editor) Open(filename string) {
e.document.Open(filename) e.doc.Open(filename)
} }
func (e *editor) SetStatusMessage(template string, a ...interface{}) { func (e *editor) SetStatusMessage(template string, a ...interface{}) {
@ -74,14 +74,14 @@ func (e *editor) ProcessKeypress() bool {
} }
func (e *editor) save() { func (e *editor) save() {
if e.document.Filename == "" { if e.doc.Filename == "" {
e.document.Filename = e.prompt("Save as: %s (ESC to cancel)", nil) e.doc.Filename = e.prompt("Save as: %s (ESC to cancel)", nil)
if e.document.Filename == "" { if e.doc.Filename == "" {
e.SetStatusMessage("Save aborted") e.SetStatusMessage("Save aborted")
} }
} }
size := e.document.Save() size := e.doc.Save()
if size > 0 { if size > 0 {
e.SetStatusMessage("%d bytes written to disk", size) 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) { func (e *editor) insertChar(ch rune) {
if e.cursor.Y == e.document.RowCount() { if e.cursor.Y == e.doc.RowCount() {
e.document.AppendRow("") e.doc.AppendRow("")
} }
e.document.InsertChar(e.cursor, ch) e.doc.InsertChar(e.cursor, ch)
e.cursor.X += 1 e.cursor.X += 1
} }
func (e *editor) delChar() { func (e *editor) delChar() {
if e.cursor.Y == e.document.RowCount() { if e.cursor.Y == e.doc.RowCount() {
return return
} }
@ -161,13 +161,13 @@ func (e *editor) delChar() {
if e.cursor.X > 0 { if e.cursor.X > 0 {
at := e.cursor at := e.cursor
at.X -= 1 at.X -= 1
e.document.DelChar(at) e.doc.DelChar(at)
} else { } else {
// Move cursor to the current end of the previous line // 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 // 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 e.cursor.Y -= 1
} }

View File

@ -14,11 +14,11 @@ func TestInsertChar(t *testing.T) {
e := NewEditor() e := NewEditor()
e.insertChar('q') 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") 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 { if row.Size() != 1 {
t.Errorf("Failed to add character to row. Row: %v", row) t.Errorf("Failed to add character to row. Row: %v", row)
} }

View File

@ -29,7 +29,7 @@ const (
func (e *editor) processKeypressChar(ch rune) bool { func (e *editor) processKeypressChar(ch rune) bool {
switch ch { switch ch {
case key.Ctrl('q'): 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.SetStatusMessage("WARNING!!! File has unsaved changes. Press Ctrl-Q %d more times to quit.", e.quitTimes)
e.quitTimes -= 1 e.quitTimes -= 1
@ -48,7 +48,7 @@ func (e *editor) processKeypressChar(ch rune) bool {
e.find() e.find()
case key.Enter: case key.Enter:
e.document.InsertNewline(e.cursor) e.doc.InsertNewline(e.cursor)
e.cursor.Y += 1 e.cursor.Y += 1
e.cursor.X = 0 e.cursor.X = 0
@ -106,10 +106,10 @@ func (e *editor) processKeypressStr(key string) {
func (e *editor) moveCursor(key string) { func (e *editor) moveCursor(key string) {
var row *document.Row var row *document.Row
if e.cursor.Y >= e.document.RowCount() { if e.cursor.Y >= e.doc.RowCount() {
row = nil row = nil
} else { } else {
row = e.document.GetRow(e.cursor.Y) row = e.doc.GetRow(e.cursor.Y)
} }
switch key { switch key {
@ -121,7 +121,7 @@ func (e *editor) moveCursor(key string) {
// Move from beginning of current row to end of previous row // Move from beginning of current row to end of previous row
if e.cursor.Y > 0 { if e.cursor.Y > 0 {
e.cursor.Y -= 1 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: case keyRight:
if row != nil && e.cursor.X < row.Size() { 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 // 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.Y += 1
e.cursor.X = 0 e.cursor.X = 0
} }
@ -138,7 +138,7 @@ func (e *editor) moveCursor(key string) {
e.cursor.Y -= 1 e.cursor.Y -= 1
} }
case keyDown: case keyDown:
if e.cursor.Y < e.document.RowCount() { if e.cursor.Y < e.doc.RowCount() {
e.cursor.Y += 1 e.cursor.Y += 1
} }
case keyPageUp: case keyPageUp:
@ -149,10 +149,10 @@ func (e *editor) moveCursor(key string) {
} }
case keyPageDown: 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 e.cursor.Y += e.screen.Rows
} else { } else {
e.cursor.Y = e.document.RowCount() - 1 e.cursor.Y = e.doc.RowCount() - 1
} }
case keyHome: case keyHome:
@ -163,8 +163,8 @@ func (e *editor) moveCursor(key string) {
} }
} }
if e.cursor.Y < e.document.RowCount() { if e.cursor.Y < e.doc.RowCount() {
row = e.document.GetRow(e.cursor.Y) row = e.doc.GetRow(e.cursor.Y)
rowLen := row.Size() rowLen := row.Size()
// Snap to the end of a shorter line from a longer one // Snap to the end of a shorter line from a longer one

View File

@ -46,7 +46,7 @@ func (e *editor) find() {
func (e *editor) findCallback(query string, ch string) { func (e *editor) findCallback(query string, ch string) {
if e.search.hlLine != -1 && e.search.hl != nil { 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 { for i, val := range e.search.hl {
staleRow.Hl[i] = val staleRow.Hl[i] = val
} }
@ -77,16 +77,16 @@ func (e *editor) findCallback(query string, ch string) {
current := e.search.lastMatch current := e.search.lastMatch
for i := 0; i < e.document.RowCount(); i++ { for i := 0; i < e.doc.RowCount(); i++ {
current += e.search.direction current += e.search.direction
if current == -1 { if current == -1 {
current = e.document.RowCount() - 1 current = e.doc.RowCount() - 1
} else if current == e.document.RowCount() { } else if current == e.doc.RowCount() {
current = 0 current = 0
} }
row := e.document.GetRow(current) row := e.doc.GetRow(current)
matchIndex := row.Search(query) matchIndex := row.Search(query)
if matchIndex == -1 { if matchIndex == -1 {
continue continue
@ -95,7 +95,7 @@ func (e *editor) findCallback(query string, ch string) {
e.search.lastMatch = current e.search.lastMatch = current
e.cursor.Y = current e.cursor.Y = current
e.cursor.X = row.RenderXtoCursorX(matchIndex) e.cursor.X = row.RenderXtoCursorX(matchIndex)
e.offset.Y = e.document.RowCount() e.offset.Y = e.doc.RowCount()
// Update highlighting of search result // Update highlighting of search result
e.search.hlLine = current e.search.hlLine = current