Skip to content

Commit

Permalink
Fix scrolling with wrapped lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Verseth committed Jul 24, 2023
1 parent ee09fb5 commit 2344bfa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ package main
import (
"fmt"
"github.com/elk-language/go-prompt"
pstrings "github.com/elk-language/go-prompt/strings"
)

func completer(d prompt.Document) []prompt.Suggest {
func completer(d prompt.Document) ([]prompt.Suggest, pstrings.RuneNumber, pstrings.RuneNumber) {
endIndex := d.CurrentRuneIndex()
w := d.GetWordBeforeCursor()
startIndex := endIndex - pstrings.RuneCount(w)

s := []prompt.Suggest{
{Text: "users", Description: "Store the username and age"},
{Text: "articles", Description: "Store the article text posted by user"},
{Text: "comments", Description: "Store the text commented to articles"},
}
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
return prompt.FilterHasPrefix(s, w, true), startIndex, endIndex
}

func main() {
Expand Down
2 changes: 2 additions & 0 deletions buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ func (b *Buffer) DeleteBeforeCursor(count istrings.RuneNumber, columns istrings.
cursorPosition: b.cursorPosition - istrings.RuneNumber(len([]rune(deleted))),
}, columns, rows)
}
b.RecalculateStartLine(columns, rows)
return
}

Expand All @@ -205,6 +206,7 @@ func (b *Buffer) Delete(count istrings.RuneNumber, col istrings.Width, row int)
)

deleted := string(deletedRunes)
b.RecalculateStartLine(col, row)
return deleted
}

Expand Down
8 changes: 4 additions & 4 deletions renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ func (r *Renderer) renderText(lexer Lexer, input string, startLine int) {
for _, char := range input {
if lineCharIndex >= col || char == '\n' {
lineNumber++
lineCharIndex = 0
if lineNumber-1 < startLine {
continue
}
Expand All @@ -258,7 +259,6 @@ func (r *Renderer) renderText(lexer Lexer, input string, startLine int) {
}
lineBuffer.WriteRune('\n')
r.renderLine(prefix, lineBuffer.String(), r.inputTextColor)
lineCharIndex = 0
lineBuffer.Reset()
if char != '\n' {
lineBuffer.WriteRune(char)
Expand All @@ -271,11 +271,11 @@ func (r *Renderer) renderText(lexer Lexer, input string, startLine int) {
continue
}

lineCharIndex += istrings.GetRuneWidth(char)
if lineNumber < startLine {
continue
}
lineBuffer.WriteRune(char)
lineCharIndex += istrings.GetRuneWidth(char)
}

r.renderLine(prefix, lineBuffer.String(), r.inputTextColor)
Expand Down Expand Up @@ -376,6 +376,7 @@ tokenLoop:
for _, char := range text {
if lineCharIndex >= col || char == '\n' {
lineNumber++
lineCharIndex = 0
if lineNumber-1 < startLine {
continue charLoop
}
Expand All @@ -385,7 +386,6 @@ tokenLoop:
lineBuffer = append(lineBuffer, '\n')
r.writeColor(lineBuffer, token.Color())
r.renderPrefix(multilinePrefix)
lineCharIndex = 0
lineBuffer = lineBuffer[:0]
if char != '\n' {
size := utf8.EncodeRune(runeBuffer, char)
Expand All @@ -395,12 +395,12 @@ tokenLoop:
continue charLoop
}

lineCharIndex += istrings.GetRuneWidth(char)
if lineNumber < startLine {
continue charLoop
}
size := utf8.EncodeRune(runeBuffer, char)
lineBuffer = append(lineBuffer, runeBuffer[:size]...)
lineCharIndex += istrings.GetRuneWidth(char)
}
if len(lineBuffer) > 0 {
r.writeColor(lineBuffer, token.Color())
Expand Down

0 comments on commit 2344bfa

Please sign in to comment.