]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/cellview.go
allow optionally brace matching with the closing brace to the left of the cursor
[micro.git] / cmd / micro / cellview.go
index 2cab7a0896d05e7e0e99427fa016b883fb853d48..9c50856e4ab50ac5eefee8ba12c18395d02429d6 100644 (file)
@@ -65,12 +65,38 @@ 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 {
+                       if buf.Cursor.RuneUnder(buf.Cursor.X) == bp[0] {
+                               matchingBrace = buf.FindMatchingBrace(bp, buf.Cursor.Loc)
+                               break
+                       }
+                       left := buf.Cursor.Loc.X
+                       if buf.Settings["matchbraceleft"].(bool) {
+                               left -= 1
+                               if left < 0 {
+                                       left = 0
+                               }
+                       }
+                       if buf.Cursor.RuneUnder(left) == bp[1] {
+                               matchingBrace = buf.FindMatchingBrace(
+                                       bp, Loc{X: left, Y: buf.Cursor.Loc.Y})
+                       }
+               }
+       }
+
        tabsize := int(buf.Settings["tabsize"].(float64))
        softwrap := buf.Settings["softwrap"].(bool)
        indentrunes := []rune(buf.Settings["indentchar"].(string))
        // if empty indentchar settings, use space
        if indentrunes == nil || len(indentrunes) == 0 {
-               indentrunes = []rune(" ")
+               indentrunes = []rune{' '}
        }
        indentchar := indentrunes[0]
 
@@ -133,7 +159,13 @@ func (c *CellView) Draw(buf *Buffer, top, height, left, width int) {
                        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' {
                                charWidth := tabsize - (viewCol+left)%tabsize
@@ -142,7 +174,8 @@ func (c *CellView) Draw(buf *Buffer, top, height, left, width int) {
                                        c.lines[viewLine][viewCol].width = charWidth
 
                                        indentStyle := curStyle
-                                       if group, ok := colorscheme["indent-char"]; ok {
+                                       ch := buf.Settings["indentchar"].(string)
+                                       if group, ok := colorscheme["indent-char"]; ok && !IsStrWhitespace(ch) && ch != "" {
                                                indentStyle = group
                                        }
 
@@ -151,7 +184,7 @@ func (c *CellView) Draw(buf *Buffer, top, height, left, width int) {
 
                                for i := 1; i < charWidth; i++ {
                                        viewCol++
-                                       if viewCol >= 0 && viewCol < lineLength {
+                                       if viewCol >= 0 && viewCol < lineLength && viewCol < len(c.lines[viewLine]) {
                                                c.lines[viewLine][viewCol] = &Char{Loc{viewCol, viewLine}, Loc{colN, lineN}, char, ' ', curStyle, 1}
                                        }
                                }
@@ -163,7 +196,7 @@ func (c *CellView) Draw(buf *Buffer, top, height, left, width int) {
                                }
                                for i := 1; i < charWidth; i++ {
                                        viewCol++
-                                       if viewCol >= 0 && viewCol < lineLength {
+                                       if viewCol >= 0 && viewCol < lineLength && viewCol < len(c.lines[viewLine]) {
                                                c.lines[viewLine][viewCol] = &Char{Loc{viewCol, viewLine}, Loc{colN, lineN}, char, ' ', curStyle, 1}
                                        }
                                }