Skip to content

Commit

Permalink
prepare real cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Nov 20, 2024
1 parent a9a31ac commit 539c8c0
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 30 deletions.
2 changes: 1 addition & 1 deletion config/keymaps/linux.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ search = 'ctrl+f'
open_outline = 'alt+7'

refresh_syntax_highlight = 'f1'
toggle_tree_sitter_debug = 'f2'
toggle_debug = 'f2'
debug_tree_sitter_nodes = 'f3'

[keys.editor.file]
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require (
github.com/charmbracelet/lipgloss v1.0.0 // indirect
github.com/charmbracelet/x/cellbuf v0.0.5 // indirect
github.com/charmbracelet/x/term v0.2.1 // indirect
github.com/charmbracelet/x/wcwidth v0.0.0-20241113152101-0af7d04e9f32 // indirect
github.com/charmbracelet/x/wcwidth v0.0.0-20241119170951-e919f77bf23b // indirect
github.com/charmbracelet/x/windows v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ github.com/charmbracelet/x/cellbuf v0.0.5 h1:LYmOqjZqoLeL4bgafJu+HZesy5zIdjgSYFk
github.com/charmbracelet/x/cellbuf v0.0.5/go.mod h1:FaDNlvrPSNCEnih536+xi5f8iqxRQfDA20TVTs30CPU=
github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ=
github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg=
github.com/charmbracelet/x/wcwidth v0.0.0-20241113152101-0af7d04e9f32 h1:14czE6R5CgOlvONsJYa2B1uTyLvXzGXpBqw2AyZeTh4=
github.com/charmbracelet/x/wcwidth v0.0.0-20241113152101-0af7d04e9f32/go.mod h1:hyua5CY63kyl7IfyIxv1SjVEqoKze/XmDkEglItuVjA=
github.com/charmbracelet/x/wcwidth v0.0.0-20241119170951-e919f77bf23b h1:TF0bhQRbHgu+zGe5Ix6tFam8A5hp3S3QAkw/5TxXHu8=
github.com/charmbracelet/x/wcwidth v0.0.0-20241119170951-e919f77bf23b/go.mod h1:hyua5CY63kyl7IfyIxv1SjVEqoKze/XmDkEglItuVjA=
github.com/charmbracelet/x/windows v0.2.0 h1:ilXA1GJjTNkgOm94CLPeSz7rar54jtFatdmoiONPuEw=
github.com/charmbracelet/x/windows v0.2.0/go.mod h1:ZibNFR49ZFqCXgP76sYanisxRyC+EYrBE7TTknD8s1s=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
Expand Down
12 changes: 6 additions & 6 deletions gopad/config/keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type EditorKeyMap struct {
OpenOutline key.Binding

RefreshSyntaxHighlight key.Binding
ToggleTreeSitterDebug key.Binding
ToggleDebug key.Binding
DebugTreeSitterNodes key.Binding

File EditorFileKeyMap
Expand All @@ -114,7 +114,7 @@ func (k EditorKeyMap) HelpView() []help.KeyMapCategory {
k.OpenOutline,
emptyKeyBind,
k.RefreshSyntaxHighlight,
k.ToggleTreeSitterDebug,
k.ToggleDebug,
k.DebugTreeSitterNodes,
},
},
Expand Down Expand Up @@ -513,7 +513,7 @@ type EditorKeyConfig struct {
OpenOutline string `toml:"open_outline"`

RefreshSyntaxHighlight string `toml:"refresh_syntax_highlight"`
ToggleTreeSitterDebug string `toml:"toggle_tree_sitter_debug"`
ToggleDebug string `toml:"toggle_debug"`
DebugTreeSitterNodes string `toml:"debug_tree_sitter_nodes"`

File struct {
Expand Down Expand Up @@ -630,9 +630,9 @@ func (k EditorKeyConfig) KeyMap() EditorKeyMap {
key.WithKeys(k.RefreshSyntaxHighlight),
key.WithHelp(k.RefreshSyntaxHighlight, "refresh syntax highlight"),
),
ToggleTreeSitterDebug: key.NewBinding(
key.WithKeys(k.ToggleTreeSitterDebug),
key.WithHelp(k.ToggleTreeSitterDebug, "toggle tree-sitter debug"),
ToggleDebug: key.NewBinding(
key.WithKeys(k.ToggleDebug),
key.WithHelp(k.ToggleDebug, "toggle tree-sitter debug"),
),
DebugTreeSitterNodes: key.NewBinding(
key.WithKeys(k.DebugTreeSitterNodes),
Expand Down
12 changes: 10 additions & 2 deletions gopad/editor/document_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,15 @@ func (v DocumentView) Update(msg tea.Msg) (DocumentView, tea.Cmd) {
v.file.Autocomplete.ClearCompletions()
return v, tea.Batch(cmds...)
case key.Matches(msg, config.Keys.Editor.RefreshSyntaxHighlight):
// TODO: refresh syntax highlight
if v.file.Syntax != nil {
syntax, err := file.NewSyntax(v.file.Syntax.Language, v.file.Buffer.Bytes())
if err != nil {
cmds = append(cmds, notifications.Addf("failed to refresh syntax highlight: %s", err.Error()))
return v, tea.Batch(cmds...)
}
v.file.Syntax = syntax
cmds = append(cmds, notifications.Add("Syntax highlight refreshed"))
}
case key.Matches(msg, config.Keys.Editor.Diagnostic.Show):
v.ShowCurrentDiagnostic()
case key.Matches(msg, config.Keys.Cancel) && v.ShowsCurrentDiagnostic():
Expand Down Expand Up @@ -769,7 +777,7 @@ func (v DocumentView) renderLine(ln int, lineCode []byte, prefixWidth int, width
return borderStyle(prefix+codeLineStyle.Render(string(lineCode))) + "\n"
}

func (v DocumentView) View(width int, height int, border bool, debug bool) string {
func (v DocumentView) View(width int, height int, border bool, debug bool, offsetX int, offsetY int) string {
prefixWidth := lipgloss.Width(strconv.Itoa(v.file.Buffer.LinesLen()))
width = max(width-prefixWidth-config.Theme.UI.FileView.BorderStyle.GetHorizontalFrameSize()-3, 0)

Expand Down
24 changes: 14 additions & 10 deletions gopad/editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ type Editor struct {
activeFile int
fileOffset int

focus ModelType
treeSitterDebug bool
focus ModelType
debug bool
}

func (e Editor) Init() (Editor, tea.Cmd) {
Expand Down Expand Up @@ -339,8 +339,8 @@ func (e *Editor) HasChanges() bool {
return false
}

func (e *Editor) ToggleTreeSitterDebug() {
e.treeSitterDebug = !e.treeSitterDebug
func (e *Editor) ToggleDebug() {
e.debug = !e.debug
}

func (e Editor) Update(msg tea.Msg) (Editor, tea.Cmd) {
Expand Down Expand Up @@ -619,8 +619,8 @@ func (e Editor) Update(msg tea.Msg) (Editor, tea.Cmd) {
cmds = append(cmds, Focus(ModelTypeFile))
}
return e, tea.Batch(cmds...)
case key.Matches(msg, config.Keys.Editor.ToggleTreeSitterDebug):
e.ToggleTreeSitterDebug()
case key.Matches(msg, config.Keys.Editor.ToggleDebug):
e.ToggleDebug()
return e, tea.Batch(cmds...)
case key.Matches(msg, config.Keys.Editor.DebugTreeSitterNodes):
v := e.FileView()
Expand Down Expand Up @@ -700,11 +700,13 @@ func (e Editor) Update(msg tea.Msg) (Editor, tea.Cmd) {
return e, tea.Batch(cmds...)
}

func (e *Editor) View(width int, height int) string {
func (e *Editor) View(width int, height int, offsetX int, offsetY int) string {
var tree string
if e.fileTree.Visible() {
tree = e.fileTree.View(height)
width -= lipgloss.Width(tree)
treeWidth := lipgloss.Width(tree)
width -= treeWidth
offsetX += treeWidth
}

f := e.FileView()
Expand All @@ -730,10 +732,12 @@ func (e *Editor) View(width int, height int) string {
if tree != "" {
search = config.Theme.UI.FileView.BorderStyle.Render(search)
}
height -= lipgloss.Height(search)
searchHeight := lipgloss.Height(search)
height -= searchHeight
offsetY += searchHeight
}

editor := f.View(width, height, e.fileTree.Visible(), e.treeSitterDebug)
editor := f.View(width, height, e.fileTree.Visible(), e.debug, offsetX, offsetY)

if search != "" {
editor = lipgloss.JoinVertical(lipgloss.Left, search, editor)
Expand Down
16 changes: 8 additions & 8 deletions gopad/gopad.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,12 @@ func (g Gopad) Init() (tea.Model, tea.Cmd) {
}

func (g Gopad) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if _, ok := msg.(cursor.BlinkMsg); !ok {
log.Printf("Msg: %T: %v\n", msg, msg)
}
// now := time.Now()
// defer func() {
// log.Printf("Update time: %s\nMessage: %T", time.Since(now), msg)
// }()
now := time.Now()
defer func() {
if _, ok := msg.(cursor.BlinkMsg); !ok {
log.Printf("Update time: %s, Msg: %T", time.Since(now), msg)
}
}()

var cmds []tea.Cmd

Expand Down Expand Up @@ -209,7 +208,8 @@ func (g Gopad) View() string {

appBar := g.AppBar()
codeBar := g.CodeBar()
codeEditor := g.editor.View(g.width, height-lipgloss.Height(appBar)-lipgloss.Height(codeBar))
appBarHeight := lipgloss.Height(appBar)
codeEditor := g.editor.View(g.width, height-appBarHeight-lipgloss.Height(codeBar), 0, appBarHeight)
view := fmt.Sprintf("%s\n%s\n%s", appBar, codeEditor, codeBar)

if g.overlays.Focused() {
Expand Down

0 comments on commit 539c8c0

Please sign in to comment.