Skip to content

Commit a6e3c2a

Browse files
Splitting draw out to getRuneStyle in bufwindow, removing @ for wide rune in bufwindow
1 parent 98ff79d commit a6e3c2a

File tree

1 file changed

+98
-75
lines changed

1 file changed

+98
-75
lines changed

internal/display/bufwindow.go

Lines changed: 98 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,87 @@ func (w *BufWindow) displayBuffer() {
494494
}
495495
bloc.X = bslice
496496

497-
draw := func(r rune, combc []rune, style tcell.Style, highlight bool, showcursor bool) {
497+
getRuneStyle := func(r rune, style tcell.Style, isplaceholder bool) (rune, tcell.Style, bool) {
498+
bgoverridable := true
499+
if nColsBeforeStart > 0 || vloc.Y < 0 || isplaceholder {
500+
return r, style, bgoverridable
501+
}
502+
503+
for _, mb := range matchingBraces {
504+
if mb.X == bloc.X && mb.Y == bloc.Y {
505+
if b.Settings["matchbracestyle"].(string) == "highlight" {
506+
if s, ok := config.Colorscheme["match-brace"]; ok {
507+
return r, s, bgoverridable
508+
} else {
509+
return r, style.Reverse(true), bgoverridable
510+
}
511+
} else {
512+
return r, style.Underline(true), bgoverridable
513+
}
514+
}
515+
}
516+
517+
if r != '\t' && r != ' '{
518+
return r, style, bgoverridable
519+
}
520+
521+
var returnrune rune
522+
if r == '\t' {
523+
indentrunes := []rune(b.Settings["indentchar"].(string))
524+
// if empty indentchar settings, use space
525+
if len(indentrunes) == 0 {
526+
indentrunes = []rune{' '}
527+
}
528+
529+
returnrune = indentrunes[0]
530+
if s, ok := config.Colorscheme["indent-char"]; ok && r != ' ' {
531+
fg, _, _ := s.Decompose()
532+
style = style.Foreground(fg)
533+
}
534+
535+
if s, ok := config.Colorscheme["indent-char"]; ok {
536+
fg, _, _ := s.Decompose()
537+
style = style.Foreground(fg)
538+
}
539+
}
540+
541+
if b.Settings["hltaberrors"].(bool) && bloc.X < leadingwsEnd {
542+
if s, ok := config.Colorscheme["tab-error"]; ok {
543+
if b.Settings["tabstospaces"].(bool) && r == '\t' {
544+
fg, _, _ := s.Decompose()
545+
style = style.Background(fg)
546+
bgoverridable = false
547+
} else if !b.Settings["tabstospaces"].(bool) && r == ' ' {
548+
fg, _, _ := s.Decompose()
549+
style = style.Background(fg)
550+
bgoverridable = false
551+
}
552+
}
553+
}
554+
555+
if b.Settings["hltrailingws"].(bool) {
556+
if s, ok := config.Colorscheme["trailingws"]; ok {
557+
if bloc.X >= trailingwsStart && bloc.X < blineLen {
558+
hl := true
559+
for _, c := range cursors {
560+
if c.NewTrailingWsY == bloc.Y {
561+
hl = false
562+
break
563+
}
564+
}
565+
if hl {
566+
fg, _, _ := s.Decompose()
567+
style = style.Background(fg)
568+
bgoverridable = false
569+
}
570+
}
571+
}
572+
}
573+
574+
return returnrune, style, bgoverridable
575+
}
576+
577+
draw := func(r rune, combc []rune, style tcell.Style, highlight bool, showcursor bool, bgoverridable bool) {
498578
if nColsBeforeStart <= 0 && vloc.Y >= 0 {
499579
if highlight {
500580
if w.Buf.HighlightSearch && w.Buf.SearchMatch(bloc) {
@@ -509,37 +589,8 @@ func (w *BufWindow) displayBuffer() {
509589

510590
// syntax or hlsearch highlighting with non-default background takes precedence
511591
// over cursor-line and color-column
512-
dontOverrideBackground := origBg != defBg
513-
514-
if b.Settings["hltaberrors"].(bool) {
515-
if s, ok := config.Colorscheme["tab-error"]; ok {
516-
isTab := (r == '\t') || (r == ' ' && !showcursor)
517-
if (b.Settings["tabstospaces"].(bool) && isTab) ||
518-
(!b.Settings["tabstospaces"].(bool) && bloc.X < leadingwsEnd && r == ' ' && !isTab) {
519-
fg, _, _ := s.Decompose()
520-
style = style.Background(fg)
521-
dontOverrideBackground = true
522-
}
523-
}
524-
}
525-
526-
if b.Settings["hltrailingws"].(bool) {
527-
if s, ok := config.Colorscheme["trailingws"]; ok {
528-
if bloc.X >= trailingwsStart && bloc.X < blineLen {
529-
hl := true
530-
for _, c := range cursors {
531-
if c.NewTrailingWsY == bloc.Y {
532-
hl = false
533-
break
534-
}
535-
}
536-
if hl {
537-
fg, _, _ := s.Decompose()
538-
style = style.Background(fg)
539-
dontOverrideBackground = true
540-
}
541-
}
542-
}
592+
if bgoverridable {
593+
bgoverridable = origBg == defBg
543594
}
544595

545596
for _, c := range cursors {
@@ -554,7 +605,7 @@ func (w *BufWindow) displayBuffer() {
554605
}
555606
}
556607

557-
if b.Settings["cursorline"].(bool) && w.active && !dontOverrideBackground &&
608+
if b.Settings["cursorline"].(bool) && w.active && bgoverridable &&
558609
!c.HasSelection() && c.Y == bloc.Y {
559610
if s, ok := config.Colorscheme["cursor-line"]; ok {
560611
fg, _, _ := s.Decompose()
@@ -571,40 +622,12 @@ func (w *BufWindow) displayBuffer() {
571622
}
572623
}
573624

574-
if r == '\t' {
575-
indentrunes := []rune(b.Settings["indentchar"].(string))
576-
// if empty indentchar settings, use space
577-
if len(indentrunes) == 0 {
578-
indentrunes = []rune{' '}
579-
}
580-
581-
r = indentrunes[0]
582-
if s, ok := config.Colorscheme["indent-char"]; ok && r != ' ' {
583-
fg, _, _ := s.Decompose()
584-
style = style.Foreground(fg)
585-
}
586-
}
587-
588625
if s, ok := config.Colorscheme["color-column"]; ok {
589-
if colorcolumn != 0 && vloc.X-w.gutterOffset+w.StartCol == colorcolumn && !dontOverrideBackground {
626+
if colorcolumn != 0 && vloc.X-w.gutterOffset+w.StartCol == colorcolumn && bgoverridable {
590627
fg, _, _ := s.Decompose()
591628
style = style.Background(fg)
592629
}
593630
}
594-
595-
for _, mb := range matchingBraces {
596-
if mb.X == bloc.X && mb.Y == bloc.Y {
597-
if b.Settings["matchbracestyle"].(string) == "highlight" {
598-
if s, ok := config.Colorscheme["match-brace"]; ok {
599-
style = s
600-
} else {
601-
style = style.Reverse(true)
602-
}
603-
} else {
604-
style = style.Underline(true)
605-
}
606-
}
607-
}
608631
}
609632

610633
screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, r, combc, style)
@@ -692,7 +715,7 @@ func (w *BufWindow) displayBuffer() {
692715
// If a word (or just a wide rune) does not fit in the window
693716
if vloc.X+wordwidth > maxWidth && vloc.X > w.gutterOffset {
694717
for vloc.X < maxWidth {
695-
draw(' ', nil, config.DefStyle, false, false)
718+
draw(' ', nil, config.DefStyle, false, false, true)
696719
}
697720

698721
// We either stop or we wrap to draw the word in the next line
@@ -708,18 +731,17 @@ func (w *BufWindow) displayBuffer() {
708731
}
709732

710733
for _, r := range word {
711-
draw(r.r, r.combc, r.style, true, true)
712-
713-
// Draw any extra characters either spaces for tabs or @ for incomplete wide runes
714-
if r.width > 1 {
715-
char := ' '
716-
if r.r != '\t' {
717-
char = '@'
718-
}
719-
720-
for i := 1; i < r.width; i++ {
721-
draw(char, nil, r.style, true, false)
734+
drawrune, drawstyle, bgoverridable := getRuneStyle(r.r, r.style, false)
735+
draw(drawrune, r.combc, drawstyle, true, true, bgoverridable)
736+
737+
// Draw extra characters for tabs or wide runes
738+
for i := 1; i < r.width; i++ {
739+
if r.r == '\t' {
740+
drawrune, drawstyle, bgoverridable = getRuneStyle('\t', r.style, false)
741+
} else {
742+
drawrune, drawstyle, bgoverridable = getRuneStyle(' ', r.style, true)
722743
}
744+
draw(drawrune, nil, drawstyle, true, false, bgoverridable)
723745
}
724746
bloc.X++
725747
}
@@ -764,7 +786,8 @@ func (w *BufWindow) displayBuffer() {
764786

765787
if vloc.X != maxWidth {
766788
// Display newline within a selection
767-
draw(' ', nil, config.DefStyle, true, true)
789+
drawrune, drawstyle, bgoverridable := getRuneStyle(' ', config.DefStyle, true)
790+
draw(drawrune, nil, drawstyle, true, true, bgoverridable)
768791
}
769792

770793
bloc.X = w.StartCol

0 commit comments

Comments
 (0)