Skip to content

Commit

Permalink
fix highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Nov 18, 2024
1 parent 9658b1a commit 6ca1fa5
Show file tree
Hide file tree
Showing 21 changed files with 788 additions and 190 deletions.
2 changes: 1 addition & 1 deletion config/language_servers.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Language Server Protocol configuration
# For a list of available language servers, see: https://langserver.org/#implementations-server

use_servers = { only = ['gopls', 'golangci-lint', 'superhtml'], except = [] }
use_servers = { only = ['gopls'], except = [] }

[language_servers.gopls]
command = 'gopls'
Expand Down
6 changes: 3 additions & 3 deletions config/queries/go/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@

"func" @keyword.function

"return" @keyword.return
"return" @keyword.control.return

[
"import"
Expand All @@ -183,9 +183,9 @@
"case"
"switch"
"if"
] @keyword.conditional
] @keyword.control.conditional

"for" @keyword.repeat
"for" @keyword.control.repeat

[
"var"
Expand Down
22 changes: 22 additions & 0 deletions config/themes/dark.toml
Original file line number Diff line number Diff line change
Expand Up @@ -261,28 +261,50 @@ unnecessary_char = { curly_underline = true, underline_color = '$red' }
"constant.numeric.float" = { foreground = '$yellow' }
"constant.builtin" = { foreground = '$bright_red' }
"constant.builtin.boolean" = { foreground = '$yellow' }
"constant.character" = { foreground = '$yellow' }
"constant.character.escape" = { foreground = '$cyan', bold = true }
"constant.other.placeholder" = { foreground = '$cyan', bold = true }

"function" = { foreground = '$bright_cyan' }
"function.builtin" = { foreground = '$bright_cyan' }
"function.method" = { foreground = '$bright_cyan' }
"function.method.private" = { foreground = '$bright_cyan' }
"function.macro" = { foreground = '$bright_cyan' }

"constructor" = { foreground = '$bright_cyan' }

"keyword" = { foreground = '$bright_magenta', italic = true }
"keyword.control" = { foreground = '$bright_magenta', italic = true }
"keyword.control.conditional" = { foreground = '$bright_magenta', italic = true }
"keyword.control.repeat" = { foreground = '$bright_magenta', italic = true }
"keyword.control.import" = { foreground = '$bright_magenta', italic = true }
"keyword.control.return" = { foreground = '$bright_magenta', italic = true }
"keyword.control.exception" = { foreground = '$bright_magenta', italic = true }
"keyword.operator" = { foreground = '$bright_magenta', italic = true }
"keyword.directive" = { foreground = '$bright_magenta', italic = true }
"keyword.function" = { foreground = '$bright_magenta', italic = true }
"keyword.storage" = { foreground = '$bright_magenta', italic = true }
"keyword.storage.type" = { foreground = '$bright_magenta', italic = true }
"keyword.storage.modifier" = { foreground = '$bright_magenta', italic = true }

"section" = { foreground = '$bright_magenta' }

"punctuation.delimiter" = { foreground = '$white' }
"punctuation.special" = { foreground = '$bright_magenta' }
"punctuation.bracket" = { foreground = '$white' }

"operator" = { foreground = '$white' }

"special" = { foreground = '$bright_cyan' }

"string" = { foreground = '$bright_green', bold = true }
"string.special" = { foreground = '$yellow' }
"string.special.path" = { foreground = '$bright_green', bold = true }
"string.special.url" = { foreground = '$blue' }

"type" = { foreground = '$bright_yellow', bold = true }
"type.builtin" = { foreground = '$bright_yellow', bold = true }
"type.parameter" = { foreground = '$bright_yellow', bold = true }

"markup.heading" = { foreground = '$cyan' }
"markup.heading.marker" = { foreground = '$bright_magenta' }
Expand Down
12 changes: 12 additions & 0 deletions gopad/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ var Actions = []Action{
return OpenLSPOverlay
},
},
{
Name: "Stop LSP",
Run: func() tea.Cmd {
return OpenLSPOverlay
},
},
{
Name: "Format Document",
Run: func() tea.Cmd {
return editor.FormatAction
},
},
}

type Action struct {
Expand Down
2 changes: 2 additions & 0 deletions gopad/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
Keys Keymap
Keymaps []KeymapConfig
Theme ThemeStyles
CodeTheme *CodeStyles
Themes []ThemeConfig
)

Expand Down Expand Up @@ -109,6 +110,7 @@ func Load(name string, defaultConfigs embed.FS) error {
}
}
Theme = theme.Theme()
CodeTheme = NewCodeStyles(Theme.CodeStyles)

return nil
}
Expand Down
6 changes: 6 additions & 0 deletions gopad/config/languages.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type LanguageConfig struct {
AutoPairs []LanguageAutoPairs `toml:"auto_pairs"`
Indent Indent `toml:"indent"`
Grammar *GrammarConfig `toml:"grammar"`
Formatter *FormatterConfig `toml:"formatter"`
}

type BlockCommentToken struct {
Expand Down Expand Up @@ -108,3 +109,8 @@ const (
RefTypeCommit RefType = "commit"
RefTypeTag RefType = "tag"
)

type FormatterConfig struct {
Command string `toml:"command"`
Args []string `toml:"args"`
}
36 changes: 36 additions & 0 deletions gopad/config/styles.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"fmt"
"image/color"

"github.com/charmbracelet/lipgloss/v2"
Expand All @@ -17,6 +18,41 @@ import (
"go.gopad.dev/gopad/internal/bubbles/textinput"
)

func NewCodeStyles(styles map[string]lipgloss.Style) *CodeStyles {
var scopes []string

for key := range styles {
scopes = append(scopes, key)
}

return &CodeStyles{
styles: styles,
scopes: scopes,
}
}

type CodeStyles struct {
styles map[string]lipgloss.Style
scopes []string
}

func (t *CodeStyles) Highlight(i int, languageName string) lipgloss.Style {
scope := t.scopes[i]
style, ok := t.styles[fmt.Sprintf("%s.%s", scope, languageName)]
if ok {
return style
}
return t.styles[scope]
}

func (t *CodeStyles) Scope(i int) string {
return t.scopes[i]
}

func (t *CodeStyles) Scopes() []string {
return t.scopes
}

type ThemeStyles struct {
Name string
Foreground color.Color
Expand Down
10 changes: 7 additions & 3 deletions gopad/editor/buffer/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ type Buffer interface {
// Rune returns the rune at the given index.
Rune(i int) rune

// ByteIndex returns the byte index in the buffer for the rune index.
// ByteIndex converts the rune index to a byte index.
ByteIndex(i int) int
// RuneIndex converts the byte index to a rune index.
RuneIndex(i int) int
// ByteIndexByPoint returns the byte index in the buffer for the given point.
ByteIndexByPoint(p Point) int
// Position returns the point for the given byte index.
Expand Down Expand Up @@ -103,8 +105,10 @@ type Line interface {
Len() int
// BytesLen returns the byte length of the line.
BytesLen() int
// Index returns the byte index in the line for the given rune index.
Index(i int) int
// ByteIndex converts the rune index to a byte index.
ByteIndex(i int) int
// RuneIndex converts the byte index to a rune index.
RuneIndex(i int) int

// Rune returns the rune at the given index.
Rune(i int) rune
Expand Down
10 changes: 7 additions & 3 deletions gopad/editor/buffer/byte_line.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ func (l byteLine) BytesLen() int {
return len(l.data)
}

func (l byteLine) Index(index int) int {
func (l byteLine) Rune(index int) rune {
return xbytes.Rune(l.data, index)
}

func (l byteLine) ByteIndex(index int) int {
return xbytes.RuneIndex(l.data, index)
}

func (l byteLine) Rune(index int) rune {
return xbytes.Rune(l.data, index)
func (l byteLine) RuneIndex(index int) int {
return xbytes.RuneIndex(l.data, index)
}

func (l byteLine) Runes() []rune {
Expand Down
17 changes: 14 additions & 3 deletions gopad/editor/buffer/line_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,24 @@ func (b *lineBuffer) Rune(i int) rune {
return 0
}

// ByteIndex returns the byte index in the buffer for the rune index.
func (b *lineBuffer) ByteIndex(i int) int {
var n int
for _, line := range b.lines {
byteLen := line.BytesLen()
runeLen := line.Len() + 1
if n+runeLen >= i {
return n + line.ByteIndex(i-n)
}
n += runeLen
}
return n
}

func (b *lineBuffer) RuneIndex(i int) int {
var n int
for _, line := range b.lines {
byteLen := len(line.Bytes())
if n+byteLen >= i {
return n + line.Index(i-n)
return n + line.RuneIndex(i-n)
}
n += byteLen + 1
}
Expand Down
Loading

0 comments on commit 6ca1fa5

Please sign in to comment.