From 5fdd37f4917649cf61bc9e8b2f21414d3a1fcac4 Mon Sep 17 00:00:00 2001 From: zjl Date: Sun, 3 Nov 2024 00:58:38 +0800 Subject: [PATCH] feat(ui): add custom cursorline color support - Add Cursorline field to ColorConfig struct - Implement HexToRGB function for color conversion - Update cursorline color setting in UI - Add debug logging for color processing --- pkg/lsp/tests/md/test.md | 18 ++++++++++++++++++ pkg/ui/config.go | 9 +++++---- pkg/ui/uicolortheme.go | 36 +++++++++++++++++++++++++++++++----- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/pkg/lsp/tests/md/test.md b/pkg/lsp/tests/md/test.md index 11f589b0..ad424a15 100644 --- a/pkg/lsp/tests/md/test.md +++ b/pkg/lsp/tests/md/test.md @@ -3,6 +3,24 @@ ### 22222 ## b ## c +~~~c +#pragma once +#include +#include +class class_c { +public: + class_c() {} + void run_class_c(); + int run_class_1(int a,int c){ + return 0; + } + void call_1(int a,int b) {} + void call_2() { + call_1(1,2); + } +} + +~~~ ~~~go func (t *Tree) load_ts_lang(cb chan<- bool) error {} for i := range tree_sitter_lang_map { diff --git a/pkg/ui/config.go b/pkg/ui/config.go index 3346ceb1..d211660c 100644 --- a/pkg/ui/config.go +++ b/pkg/ui/config.go @@ -14,8 +14,9 @@ import ( type highlight struct { Search string `yaml:"search"` } -type color struct { - Highlight highlight `yaml:"highlight"` +type ColorConfig struct { + Highlight *highlight `yaml:"highlight,omitempty"` + Cursorline *string `yaml:"cursorline,omitempty"` } type vimmode struct { Leadkey string `yaml:"leadkey,omitempty"` @@ -25,7 +26,7 @@ type LspviConfig struct { Colorscheme string `yaml:"colorscheme"` Wrap bool `yaml:"wrap"` Lsp lspcore.LspConfig `yaml:"lsp"` - Color color `yaml:"color"` + Color ColorConfig `yaml:"color"` Vim *vimmode `yaml:"vim,omitempty"` enablevim bool Keyboard lspvi_command_map `yaml:"keyboard"` @@ -63,7 +64,7 @@ func NewLspviconfig() *LspviConfig { default_ret := LspviConfig{ Colorscheme: "darcula", Wrap: false, - Color: color{}, + Color: ColorConfig{}, enablevim: false, } return &default_ret diff --git a/pkg/ui/uicolortheme.go b/pkg/ui/uicolortheme.go index 57af5268..83bb8f4e 100644 --- a/pkg/ui/uicolortheme.go +++ b/pkg/ui/uicolortheme.go @@ -8,6 +8,7 @@ import ( "fmt" "path/filepath" "strconv" + "strings" rgb "image/color" @@ -16,6 +17,7 @@ import ( "github.com/pgavlin/femto/runtime" "github.com/rivo/tview" "github.com/tectiv3/go-lsp" + "zen108.com/lspvi/pkg/debug" "zen108.com/lspvi/pkg/treesittertheme" // lspcore "zen108.com/lspvi/pkg/lsp" ) @@ -95,16 +97,40 @@ func IntToRGB(colorInt tcell.Color) rgb.RGBA { r, g, b := colorInt.RGB() return rgb.RGBA{uint8(r), uint8(g), uint8(b), 255} // 默认Alpha通道为255(完全不透明) } +func HexToRGB(hexString string) (int, int, int, error) { + // 去掉前缀 # + hexString = strings.TrimPrefix(hexString, "#") + + // 将十六进制字符串转换为整数 + value, err := strconv.ParseUint(hexString, 16, 32) + if err != nil { + return 0, 0, 0, err + } + + // 提取红色、绿色和蓝色的分量 + blue := int(value & 255) + green := int((value >> 8) & 255) + red := int((value >> 16) & 255) + + return red, green, blue, nil +} func (mgr *symbol_colortheme) set_currsor_line() { if ret := mgr.get_color("cursorline"); ret != nil { _, bg, _ := ret.Decompose() - x := lightenColor(IntToRGB(bg), 0.0) + ss := bg.Hex() + debug.DebugLogf("color", "#%x %s", ss, mgr.name) + x := lightenColor(IntToRGB(bg), 0.2) v := ColorToCellColor(x) s := ret.Background(v) - mgr.colorscheme["cursor-line"] = s //*ret - // if _, ok := mgr.colorscheme["selection"]; !ok { - // mgr.colorscheme["selection"] = *ret - // } + _, bg, _ = s.Decompose() + debug.DebugLogf("color", "#%x %s", bg.Hex(), mgr.name) + mgr.colorscheme["cursor-line"] = s + } + if bg := global_config.Color.Cursorline; bg != nil { + if r, g, b, err := hexToRGB(*bg); err == nil { + s := tcell.StyleDefault.Background(tcell.NewRGBColor(r, g, b)) + mgr.colorscheme["cursor-line"] = s + } } if ret := mgr.get_color("visual"); ret != nil { mgr.colorscheme["selection"] = *ret