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() {
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++

View File

@ -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

View File

@ -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
}

View File

@ -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)
}

View File

@ -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

View File

@ -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