Skip to content

Commit

Permalink
Refactor internal packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Verseth committed Jul 12, 2023
1 parent 14a5059 commit 116b367
Show file tree
Hide file tree
Showing 28 changed files with 241 additions and 249 deletions.
2 changes: 1 addition & 1 deletion _tools/vt100_debug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"syscall"

prompt "github.com/elk-language/go-prompt"
"github.com/elk-language/go-prompt/internal/term"
"github.com/elk-language/go-prompt/term"
)

func main() {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion internal/bisect/bisect_test.go → bisect/bisect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"math/rand"
"testing"

"github.com/elk-language/go-prompt/internal/bisect"
"github.com/elk-language/go-prompt/bisect"
)

func Example() {
Expand Down
30 changes: 15 additions & 15 deletions buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package prompt
import (
"strings"

"github.com/elk-language/go-prompt/internal/debug"
istrings "github.com/elk-language/go-prompt/internal/strings"
"github.com/elk-language/go-prompt/debug"
istrings "github.com/elk-language/go-prompt/strings"
)

// Buffer emulates the console buffer.
type Buffer struct {
workingLines []string // The working lines. Similar to history
workingIndex int // index of the current line
cursorPosition istrings.RuneIndex
cursorPosition istrings.RuneNumber
cacheDocument *Document
lastKeyStroke Key
}
Expand Down Expand Up @@ -49,31 +49,31 @@ func (b *Buffer) InsertText(text string, overwrite bool, moveCursor bool) {
if overwrite {
overwritten := string(currentTextRunes[cursor:])
if len(overwritten) >= int(cursor)+len(text) {
overwritten = string(currentTextRunes[cursor : cursor+istrings.RuneLen(text)])
overwritten = string(currentTextRunes[cursor : cursor+istrings.RuneCount(text)])
}
if i := strings.IndexAny(overwritten, "\n"); i != -1 {
overwritten = overwritten[:i]
}
b.setText(string(currentTextRunes[:cursor]) + text + string(currentTextRunes[cursor+istrings.RuneLen(overwritten):]))
b.setText(string(currentTextRunes[:cursor]) + text + string(currentTextRunes[cursor+istrings.RuneCount(overwritten):]))
} else {
b.setText(string(currentTextRunes[:cursor]) + text + string(currentTextRunes[cursor:]))
}

if moveCursor {
b.cursorPosition += istrings.RuneLen(text)
b.cursorPosition += istrings.RuneCount(text)
}
}

// SetText method to set text and update cursorPosition.
// (When doing this, make sure that the cursor_position is valid for this text.
// text/cursor_position should be consistent at any time, otherwise set a Document instead.)
func (b *Buffer) setText(text string) {
debug.Assert(b.cursorPosition <= istrings.RuneLen(text), "length of input should be shorter than cursor position")
debug.Assert(b.cursorPosition <= istrings.RuneCount(text), "length of input should be shorter than cursor position")
b.workingLines[b.workingIndex] = text
}

// Set cursor position. Return whether it changed.
func (b *Buffer) setCursorPosition(p istrings.RuneIndex) {
func (b *Buffer) setCursorPosition(p istrings.RuneNumber) {
if p > 0 {
b.cursorPosition = p
} else {
Expand All @@ -88,13 +88,13 @@ func (b *Buffer) setDocument(d *Document) {
}

// CursorLeft move to left on the current line.
func (b *Buffer) CursorLeft(count istrings.RuneCount) {
func (b *Buffer) CursorLeft(count istrings.RuneNumber) {
l := b.Document().GetCursorLeftPosition(count)
b.cursorPosition += l
}

// CursorRight move to right on the current line.
func (b *Buffer) CursorRight(count istrings.RuneCount) {
func (b *Buffer) CursorRight(count istrings.RuneNumber) {
l := b.Document().GetCursorRightPosition(count)
b.cursorPosition += l
}
Expand All @@ -114,7 +114,7 @@ func (b *Buffer) CursorDown(count int) {
}

// DeleteBeforeCursor delete specified number of characters before cursor and return the deleted text.
func (b *Buffer) DeleteBeforeCursor(count istrings.RuneCount) (deleted string) {
func (b *Buffer) DeleteBeforeCursor(count istrings.RuneNumber) (deleted string) {
debug.Assert(count >= 0, "count should be positive")
r := []rune(b.Text())

Expand All @@ -126,7 +126,7 @@ func (b *Buffer) DeleteBeforeCursor(count istrings.RuneCount) (deleted string) {
deleted = string(r[start:b.cursorPosition])
b.setDocument(&Document{
Text: string(r[:start]) + string(r[b.cursorPosition:]),
cursorPosition: b.cursorPosition - istrings.RuneIndex(len([]rune(deleted))),
cursorPosition: b.cursorPosition - istrings.RuneNumber(len([]rune(deleted))),
})
}
return
Expand All @@ -142,13 +142,13 @@ func (b *Buffer) NewLine(copyMargin bool) {
}

// Delete specified number of characters and Return the deleted text.
func (b *Buffer) Delete(count istrings.RuneCount) string {
func (b *Buffer) Delete(count istrings.RuneNumber) string {
r := []rune(b.Text())
if b.cursorPosition < istrings.RuneIndex(len(r)) {
if b.cursorPosition < istrings.RuneNumber(len(r)) {
textAfterCursor := b.Document().TextAfterCursor()
textAfterCursorRunes := []rune(textAfterCursor)
deletedRunes := textAfterCursorRunes[:count]
b.setText(string(r[:b.cursorPosition]) + string(r[b.cursorPosition+istrings.RuneCount(len(deletedRunes)):]))
b.setText(string(r[:b.cursorPosition]) + string(r[b.cursorPosition+istrings.RuneNumber(len(deletedRunes)):]))

deleted := string(deletedRunes)
return deleted
Expand Down
30 changes: 15 additions & 15 deletions buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"reflect"
"testing"

istrings "github.com/elk-language/go-prompt/internal/strings"
istrings "github.com/elk-language/go-prompt/strings"
)

func TestNewBuffer(t *testing.T) {
Expand All @@ -25,8 +25,8 @@ func TestBuffer_InsertText(t *testing.T) {
t.Errorf("Text should be %#v, got %#v", "some_text", b.Text())
}

if b.cursorPosition != istrings.RuneLen("some_text") {
t.Errorf("cursorPosition should be %#v, got %#v", istrings.RuneLen("some_text"), b.cursorPosition)
if b.cursorPosition != istrings.RuneCount("some_text") {
t.Errorf("cursorPosition should be %#v, got %#v", istrings.RuneCount("some_text"), b.cursorPosition)
}
}

Expand All @@ -38,8 +38,8 @@ func TestBuffer_InsertText_Overwrite(t *testing.T) {
t.Errorf("Text should be %#v, got %#v", "ABC", b.Text())
}

if b.cursorPosition != istrings.RuneLen("ABC") {
t.Errorf("cursorPosition should be %#v, got %#v", istrings.RuneLen("ABC"), b.cursorPosition)
if b.cursorPosition != istrings.RuneCount("ABC") {
t.Errorf("cursorPosition should be %#v, got %#v", istrings.RuneCount("ABC"), b.cursorPosition)
}

b.CursorLeft(1)
Expand Down Expand Up @@ -87,8 +87,8 @@ func TestBuffer_CursorMovement(t *testing.T) {
if b.Text() != "some_teAxt" {
t.Errorf("Text should be %#v, got %#v", "some_teAxt", b.Text())
}
if b.cursorPosition != istrings.RuneLen("some_teA") {
t.Errorf("Text should be %#v, got %#v", istrings.RuneLen("some_teA"), b.cursorPosition)
if b.cursorPosition != istrings.RuneCount("some_teA") {
t.Errorf("Text should be %#v, got %#v", istrings.RuneCount("some_teA"), b.cursorPosition)
}

// Moving over left character counts.
Expand All @@ -97,8 +97,8 @@ func TestBuffer_CursorMovement(t *testing.T) {
if b.Text() != "Asome_teAxt" {
t.Errorf("Text should be %#v, got %#v", "some_teAxt", b.Text())
}
if b.cursorPosition != istrings.RuneLen("A") {
t.Errorf("Text should be %#v, got %#v", istrings.RuneLen("some_teA"), b.cursorPosition)
if b.cursorPosition != istrings.RuneCount("A") {
t.Errorf("Text should be %#v, got %#v", istrings.RuneCount("some_teA"), b.cursorPosition)
}

// TODO: Going right already at right end.
Expand Down Expand Up @@ -147,17 +147,17 @@ func TestBuffer_CursorDown(t *testing.T) {

// Normally going down
b.CursorDown(1)
if b.Document().cursorPosition != istrings.RuneLen("line1\nlin") {
t.Errorf("Should be %#v, got %#v", istrings.RuneLen("line1\nlin"), b.Document().cursorPosition)
if b.Document().cursorPosition != istrings.RuneCount("line1\nlin") {
t.Errorf("Should be %#v, got %#v", istrings.RuneCount("line1\nlin"), b.Document().cursorPosition)
}

// Going down to a line that's storter.
b = NewBuffer()
b.InsertText("long line1\na\nb", false, true)
b.cursorPosition = 3
b.CursorDown(1)
if b.Document().cursorPosition != istrings.RuneLen("long line1\na") {
t.Errorf("Should be %#v, got %#v", istrings.RuneLen("long line1\na"), b.Document().cursorPosition)
if b.Document().cursorPosition != istrings.RuneCount("long line1\na") {
t.Errorf("Should be %#v, got %#v", istrings.RuneCount("long line1\na"), b.Document().cursorPosition)
}
}

Expand All @@ -173,8 +173,8 @@ func TestBuffer_DeleteBeforeCursor(t *testing.T) {
if deleted != "e" {
t.Errorf("Should be %#v, got %#v", deleted, "e")
}
if b.cursorPosition != istrings.RuneLen("some_t") {
t.Errorf("Should be %#v, got %#v", istrings.RuneLen("some_t"), b.cursorPosition)
if b.cursorPosition != istrings.RuneCount("some_t") {
t.Errorf("Should be %#v, got %#v", istrings.RuneCount("some_t"), b.cursorPosition)
}

// Delete over the characters length before cursor.
Expand Down
2 changes: 1 addition & 1 deletion completer/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"runtime"

prompt "github.com/elk-language/go-prompt"
"github.com/elk-language/go-prompt/internal/debug"
"github.com/elk-language/go-prompt/debug"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package prompt
import (
"strings"

"github.com/elk-language/go-prompt/internal/debug"
istrings "github.com/elk-language/go-prompt/internal/strings"
"github.com/elk-language/go-prompt/debug"
istrings "github.com/elk-language/go-prompt/strings"
runewidth "github.com/mattn/go-runewidth"
)

Expand Down
2 changes: 1 addition & 1 deletion completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"reflect"
"testing"

istrings "github.com/elk-language/go-prompt/internal/strings"
istrings "github.com/elk-language/go-prompt/strings"
)

func TestFormatShortSuggestion(t *testing.T) {
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 116b367

Please sign in to comment.