]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/cellview.go
Update readme
[micro.git] / cmd / micro / cellview.go
index 0a64ed9e2ce1706f9ac38fec26916e7da2eb2221..b0cb6523c3602312b9ba8470d7cae65e83e12ed6 100644 (file)
@@ -2,7 +2,6 @@ package main
 
 import (
        "github.com/mattn/go-runewidth"
-       "github.com/zyedidia/micro/cmd/micro/highlight"
        "github.com/zyedidia/tcell"
 )
 
@@ -24,7 +23,7 @@ func visualToCharPos(visualIndex int, lineN int, str string, buf *Buffer, tabsiz
                // width := StringWidth(str[:i], tabsize)
 
                if group, ok := buf.Match(lineN)[charPos]; ok {
-                       s := GetColor(highlight.GetGroup(group))
+                       s := GetColor(group.String())
                        style = &s
                }
 
@@ -39,7 +38,7 @@ func visualToCharPos(visualIndex int, lineN int, str string, buf *Buffer, tabsiz
                lastWidth = width
                rw = 0
                if c == '\t' {
-                       rw := tabsize - (lineIdx % tabsize)
+                       rw = tabsize - (lineIdx % tabsize)
                        width += rw
                } else {
                        rw = runewidth.RuneWidth(c)
@@ -66,12 +65,41 @@ type CellView struct {
 }
 
 func (c *CellView) Draw(buf *Buffer, top, height, left, width int) {
+       if width <= 0 {
+               return
+       }
+
+       matchingBrace := Loc{-1, -1}
+       // bracePairs is defined in buffer.go
+       if buf.Settings["matchbrace"].(bool) {
+               for _, bp := range bracePairs {
+                       curX := buf.Cursor.X
+                       curLoc := buf.Cursor.Loc
+                       if buf.Settings["matchbraceleft"].(bool) {
+                               if curX > 0 {
+                                       curX--
+                                       curLoc = curLoc.Move(-1, buf)
+                               }
+                       }
+
+                       r := buf.Cursor.RuneUnder(curX)
+                       if r == bp[0] || r == bp[1] {
+                               matchingBrace = buf.FindMatchingBrace(bp, curLoc)
+                       }
+               }
+       }
+
        tabsize := int(buf.Settings["tabsize"].(float64))
        softwrap := buf.Settings["softwrap"].(bool)
-       indentchar := []rune(buf.Settings["indentchar"].(string))[0]
+       indentrunes := []rune(buf.Settings["indentchar"].(string))
+       // if empty indentchar settings, use space
+       if indentrunes == nil || len(indentrunes) == 0 {
+               indentrunes = []rune{' '}
+       }
+       indentchar := indentrunes[0]
 
        start := buf.Cursor.Y
-       if buf.Settings["syntax"].(bool) {
+       if buf.Settings["syntax"].(bool) && buf.syntaxDef != nil {
                if start > 0 && buf.lines[start-1].rehighlight {
                        buf.highlighter.ReHighlightLine(buf, start-1)
                        buf.lines[start-1].rehighlight = false
@@ -123,36 +151,50 @@ func (c *CellView) Draw(buf *Buffer, top, height, left, width int) {
                                break
                        }
                        if group, ok := buf.Match(lineN)[colN]; ok {
-                               curStyle = GetColor(highlight.GetGroup(group))
+                               curStyle = GetColor(group.String())
                        }
 
                        char := line[colN]
 
                        if viewCol >= 0 {
-                               c.lines[viewLine][viewCol] = &Char{Loc{viewCol, viewLine}, Loc{colN, lineN}, char, char, curStyle, 1}
+                               st := curStyle
+                               if colN == matchingBrace.X && lineN == matchingBrace.Y && !buf.Cursor.HasSelection() {
+                                       st = curStyle.Reverse(true)
+                               }
+                               if viewCol < len(c.lines[viewLine]) {
+                                       c.lines[viewLine][viewCol] = &Char{Loc{viewCol, viewLine}, Loc{colN, lineN}, char, char, st, 1}
+                               }
                        }
                        if char == '\t' {
-                               width := tabsize - (viewCol+left)%tabsize
+                               charWidth := tabsize - (viewCol+left)%tabsize
                                if viewCol >= 0 {
                                        c.lines[viewLine][viewCol].drawChar = indentchar
-                                       c.lines[viewLine][viewCol].width = width
+                                       c.lines[viewLine][viewCol].width = charWidth
+
+                                       indentStyle := curStyle
+                                       ch := buf.Settings["indentchar"].(string)
+                                       if group, ok := colorscheme["indent-char"]; ok && !IsStrWhitespace(ch) && ch != "" {
+                                               indentStyle = group
+                                       }
+
+                                       c.lines[viewLine][viewCol].style = indentStyle
                                }
 
-                               for i := 1; i < width; i++ {
+                               for i := 1; i < charWidth; i++ {
                                        viewCol++
-                                       if viewCol >= 0 {
+                                       if viewCol >= 0 && viewCol < lineLength && viewCol < len(c.lines[viewLine]) {
                                                c.lines[viewLine][viewCol] = &Char{Loc{viewCol, viewLine}, Loc{colN, lineN}, char, ' ', curStyle, 1}
                                        }
                                }
                                viewCol++
                        } else if runewidth.RuneWidth(char) > 1 {
-                               width := runewidth.RuneWidth(char)
+                               charWidth := runewidth.RuneWidth(char)
                                if viewCol >= 0 {
-                                       c.lines[viewLine][viewCol].width = width
+                                       c.lines[viewLine][viewCol].width = charWidth
                                }
-                               for i := 1; i < width; i++ {
+                               for i := 1; i < charWidth; i++ {
                                        viewCol++
-                                       if viewCol >= 0 {
+                                       if viewCol >= 0 && viewCol < lineLength && viewCol < len(c.lines[viewLine]) {
                                                c.lines[viewLine][viewCol] = &Char{Loc{viewCol, viewLine}, Loc{colN, lineN}, char, ' ', curStyle, 1}
                                        }
                                }
@@ -179,7 +221,7 @@ func (c *CellView) Draw(buf *Buffer, top, height, left, width int) {
 
                }
                if group, ok := buf.Match(lineN)[len(line)]; ok {
-                       curStyle = GetColor(highlight.GetGroup(group))
+                       curStyle = GetColor(group.String())
                }
 
                // newline
@@ -188,6 +230,9 @@ func (c *CellView) Draw(buf *Buffer, top, height, left, width int) {
        }
 
        for i := top; i < top+height; i++ {
+               if i >= buf.NumLines {
+                       break
+               }
                buf.SetMatch(i, nil)
        }
 }