From 3982628653c503d9807f7e7193235a54f2a14817 Mon Sep 17 00:00:00 2001 From: "Luke I. Wilson" Date: Sun, 4 Apr 2021 13:15:31 -0500 Subject: [PATCH] Ran `go fmt *.go` on all sources --- main.go | 22 ++++++++-------- ui/buffer/buffer.go | 1 - ui/buffer/highlighter.go | 18 ++++++------- ui/buffer/rope.go | 14 +++++----- ui/buffer/rope_test.go | 2 +- ui/drawfunctions.go | 6 ++--- ui/fileselectordialog.go | 4 +-- ui/inputfield.go | 2 +- ui/menu.go | 24 ++++++++--------- ui/messagedialog.go | 8 +++--- ui/textedit.go | 56 ++++++++++++++++++++-------------------- 11 files changed, 78 insertions(+), 79 deletions(-) diff --git a/main.go b/main.go index 2116a09..5e4e321 100644 --- a/main.go +++ b/main.go @@ -4,9 +4,9 @@ import ( "errors" "flag" "fmt" - "log" "io/fs" "io/ioutil" + "log" "os" "runtime" "runtime/pprof" @@ -186,7 +186,7 @@ func main() { tabContainer.AddTab("noname", textEdit) changeFocus(tabContainer) - tabContainer.FocusTab(tabContainer.GetTabCount()-1) + tabContainer.FocusTab(tabContainer.GetTabCount() - 1) }}, &ui.ItemEntry{Name: "Open...", Shortcut: "Ctrl+O", Callback: func() { callback := func(filePaths []string) { var errOccurred bool @@ -214,7 +214,7 @@ func main() { dialog = nil // Hide the file selector changeFocus(tabContainer) if tabContainer.GetTabCount() > 0 { - tabContainer.FocusTab(tabContainer.GetTabCount()-1) + tabContainer.FocusTab(tabContainer.GetTabCount() - 1) } } } @@ -254,13 +254,13 @@ func main() { } } }}, &ui.ItemEntry{Name: "Save As...", QuickChar: 5, Callback: saveAs}, &ui.ItemSeparator{}, - &ui.ItemEntry{Name: "Close", Shortcut: "Ctrl+Q", Callback: func() { - if tabContainer.GetTabCount() > 0 { - tabContainer.RemoveTab(tabContainer.GetSelectedTabIdx()) - } else { // No tabs open; close the editor - closing = true - } - }}}) + &ui.ItemEntry{Name: "Close", Shortcut: "Ctrl+Q", Callback: func() { + if tabContainer.GetTabCount() > 0 { + tabContainer.RemoveTab(tabContainer.GetSelectedTabIdx()) + } else { // No tabs open; close the editor + closing = true + } + }}}) panelMenu := ui.NewMenu("Panel", 0, &theme) @@ -426,7 +426,7 @@ func main() { } } - if ev.Modifiers() & tcell.ModCtrl != 0 { + if ev.Modifiers()&tcell.ModCtrl != 0 { handled := menuBar.HandleEvent(ev) if handled { continue // Avoid passing the event to the focusedComponent diff --git a/ui/buffer/buffer.go b/ui/buffer/buffer.go index 9e1c48b..8a6998d 100644 --- a/ui/buffer/buffer.go +++ b/ui/buffer/buffer.go @@ -62,7 +62,6 @@ type Buffer interface { // the last rune before the line delimiter. ClampLineCol(line, col int) (int, int) - // LineColToPos returns the index of the byte at line, col. If line is less than // zero, or more than the number of available lines, the function will panic. If // col is less than zero, the function will panic. If col is greater than the diff --git a/ui/buffer/highlighter.go b/ui/buffer/highlighter.go index 29c7d29..e0e66e2 100755 --- a/ui/buffer/highlighter.go +++ b/ui/buffer/highlighter.go @@ -23,14 +23,14 @@ func (c *Colorscheme) GetStyle(s Syntax) tcell.Style { } } - return tcell.StyleDefault; // No value for Default; use default style. + return tcell.StyleDefault // No value for Default; use default style. } type RegexpRegion struct { Start *regexp.Regexp - End *regexp.Regexp // Should be "$" by default - Skip *regexp.Regexp // Optional - Error *regexp.Regexp // Optional + End *regexp.Regexp // Should be "$" by default + Skip *regexp.Regexp // Optional + Error *regexp.Regexp // Optional Specials []*regexp.Regexp // Optional (nil or zero len) } @@ -97,9 +97,9 @@ func (h *Highlighter) updateLines(startLine, endLine int) { bytes := h.Buffer.Slice(startLine, 0, endLine, endCol) for k, v := range h.Language.Rules { - var indexes [][]int // [][2]int + var indexes [][]int // [][2]int if k.End != nil && k.End.String() != "$" { // If this range might be a multiline range... - endIndexes := k.End.FindAllIndex(bytes, -1) // Attempt to find every ending match + endIndexes := k.End.FindAllIndex(bytes, -1) // Attempt to find every ending match startIndexes := k.Start.FindAllIndex(bytes, -1) // Attempt to find every starting match // ... _ = endIndexes @@ -111,9 +111,9 @@ func (h *Highlighter) updateLines(startLine, endLine int) { if indexes != nil { for i := range indexes { startLine, startCol := h.Buffer.PosToLineCol(indexes[i][0] + startPos) - endLine, endCol := h.Buffer.PosToLineCol(indexes[i][1]-1 + startPos) + endLine, endCol := h.Buffer.PosToLineCol(indexes[i][1] - 1 + startPos) - match := Match { startCol, endLine, endCol, v } + match := Match{startCol, endLine, endCol, v} h.lineMatches[startLine] = append(h.lineMatches[startLine], match) // Unsorted } @@ -138,7 +138,7 @@ func (h *Highlighter) UpdateInvalidatedLines(startLine, endLine int) { // Keep endLine clamped if endLine >= len(h.lineMatches) { - endLine = len(h.lineMatches)-1 + endLine = len(h.lineMatches) - 1 } // Move endLine back to first line at or before endLine with invalidated changes diff --git a/ui/buffer/rope.go b/ui/buffer/rope.go index 2b47cba..d92978d 100644 --- a/ui/buffer/rope.go +++ b/ui/buffer/rope.go @@ -57,7 +57,7 @@ func (b *RopeBuffer) LineColToPos(line, col int) int { // delimiter. line starts from zero. Data returned may or may not be a copy: do not // write it. func (b *RopeBuffer) Line(line int) []byte { - pos := b.getLineStartPos(line) + pos := b.getLineStartPos(line) bytes := 0 _rope := (*rope.Node)(b) @@ -99,7 +99,7 @@ func (b *RopeBuffer) Line(line int) []byte { func (b *RopeBuffer) Slice(startLine, startCol, endLine, endCol int) []byte { endPos := b.LineColToPos(endLine, endCol) if length := (*rope.Node)(b).Len(); endPos >= length { - endPos = length-1 + endPos = length - 1 } return (*rope.Node)(b).Slice(b.LineColToPos(startLine, startCol), endPos+1) } @@ -164,7 +164,7 @@ func (b *RopeBuffer) getLineStartPos(line int) int { if line > 0 { _rope.IndexAllFunc(0, _rope.Len(), []byte{'\n'}, func(idx int) bool { line-- - pos = idx + 1 // idx+1 = start of line after delimiter + pos = idx + 1 // idx+1 = start of line after delimiter if line <= 0 { // If pos is now the start of the line we're searching for... return true // Stop indexing } @@ -213,7 +213,7 @@ func (b *RopeBuffer) RunesInLineWithDelim(line int) int { count++ // Add the '\r' we previously thought was part of the delim. } } - + // Respect Utf-8 codepoint boundaries _, size := utf8.DecodeRune(data[i:]) i += size @@ -257,7 +257,7 @@ func (b *RopeBuffer) RunesInLine(line int) int { } } count++ - + // Respect Utf-8 codepoint boundaries _, size := utf8.DecodeRune(data[i:]) i += size @@ -275,8 +275,8 @@ func (b *RopeBuffer) RunesInLine(line int) int { func (b *RopeBuffer) ClampLineCol(line, col int) (int, int) { if line < 0 { line = 0 - } else if lines := b.Lines()-1; line > lines { - line = lines + } else if lines := b.Lines() - 1; line > lines { + line = lines } if col < 0 { diff --git a/ui/buffer/rope_test.go b/ui/buffer/rope_test.go index 510d03b..c67bf2e 100644 --- a/ui/buffer/rope_test.go +++ b/ui/buffer/rope_test.go @@ -19,7 +19,7 @@ func TestRopePosToLineCol(t *testing.T) { t.Errorf("Expected startCol == 0, got %v", startCol) } - endPos := buf.Len()-1 + endPos := buf.Len() - 1 endLine, endCol := buf.PosToLineCol(endPos) t.Logf("endPos = %v", endPos) if endLine != 3 { diff --git a/ui/drawfunctions.go b/ui/drawfunctions.go index 24d748b..8b86e6b 100644 --- a/ui/drawfunctions.go +++ b/ui/drawfunctions.go @@ -47,7 +47,7 @@ func DrawQuickCharStr(s tcell.Screen, x, y int, str string, quickCharIdx int, st sty := style if runeIdx == quickCharIdx { - sty = style.Underline(true) + sty = style.Underline(true) } s.SetContent(x+col, y, r, nil, sty) @@ -89,8 +89,8 @@ func DrawRectOutlineDefault(s tcell.Screen, x, y, width, height int, style tcell func DrawWindow(s tcell.Screen, x, y, width, height int, title string, theme *Theme) { headerStyle := theme.GetOrDefault("WindowHeader") - DrawRect(s, x, y, width, 1, ' ', headerStyle) // Draw header background - DrawStr(s, x + width/2 - len(title)/2, y, title, headerStyle) // Draw header title + DrawRect(s, x, y, width, 1, ' ', headerStyle) // Draw header background + DrawStr(s, x+width/2-len(title)/2, y, title, headerStyle) // Draw header title DrawRect(s, x, y+1, width, height-1, ' ', theme.GetOrDefault("Window")) // Draw body } diff --git a/ui/fileselectordialog.go b/ui/fileselectordialog.go index a55967b..b071ecc 100644 --- a/ui/fileselectordialog.go +++ b/ui/fileselectordialog.go @@ -11,7 +11,7 @@ import ( type FileSelectorDialog struct { MustExist bool // Whether the dialog should have a user select an existing file. FilesChosenCallback func([]string) // Returns slice of filenames selected. nil if user canceled. - Theme *Theme + Theme *Theme title string x, y int @@ -31,7 +31,7 @@ func NewFileSelectorDialog(screen *tcell.Screen, title string, mustExist bool, t MustExist: mustExist, FilesChosenCallback: filesChosenCallback, Theme: theme, - title: title, + title: title, } dialog.inputField = NewInputField(screen, []byte{}, theme.GetOrDefault("Window")) // Use window's theme for InputField diff --git a/ui/inputfield.go b/ui/inputfield.go index 82a5078..c4b2210 100644 --- a/ui/inputfield.go +++ b/ui/inputfield.go @@ -81,7 +81,7 @@ func (f *InputField) insert(dst []byte, at int, src ...byte) []byte { copy(dstn[at:], src) return dstn } - dstn := make([]byte, len(dst) + len(src)) + dstn := make([]byte, len(dst)+len(src)) copy(dstn, dst[:at]) copy(dstn[at:], src) copy(dstn[at+len(src):], dst[at:]) diff --git a/ui/menu.go b/ui/menu.go index 0ec1aca..5f8c1c4 100644 --- a/ui/menu.go +++ b/ui/menu.go @@ -77,7 +77,7 @@ type MenuBar struct { x, y int width, height int focused bool - selected int // Index of selection in MenuBar + selected int // Index of selection in MenuBar menusVisible bool // Whether to draw the selected menu Theme *Theme @@ -115,7 +115,7 @@ func (b *MenuBar) ActivateMenuUnderCursor() { } func (b *MenuBar) CursorLeft() { - if b.menusVisible { + if b.menusVisible { b.menus[b.selected].SetFocused(false) // Unfocus current menu } @@ -158,7 +158,7 @@ func (b *MenuBar) Draw(s tcell.Screen) { DrawRect(s, b.x, b.y, b.width, 1, ' ', normalStyle) col := b.x + 1 for i, item := range b.menus { - sty := normalStyle + sty := normalStyle if b.focused && b.selected == i { sty = b.Theme.GetOrDefault("MenuBarSelected") // Use special style for selected item } @@ -242,7 +242,7 @@ func (b *MenuBar) HandleEvent(event tcell.Event) bool { if !b.menusVisible { // If menus are not visible... b.ActivateMenuUnderCursor() } else { // The selected Menu is visible, send the event to it - return b.menus[b.selected].HandleEvent(event) + return b.menus[b.selected].HandleEvent(event) } case tcell.KeyLeft: b.CursorLeft() @@ -261,14 +261,14 @@ func (b *MenuBar) HandleEvent(event tcell.Event) bool { for i, m := range b.menus { r := QuickCharInString(m.Name, m.QuickChar) if r != 0 && r == ev.Rune() { - b.selected = i // Select menu at i + b.selected = i // Select menu at i b.ActivateMenuUnderCursor() // Show menu break } } } else { return b.menus[b.selected].HandleEvent(event) // Have menu handle quick char event - } + } default: if b.menusVisible { @@ -299,10 +299,10 @@ type Menu struct { // New creates a new Menu. `items` can be `nil`. func NewMenu(name string, quickChar int, theme *Theme) *Menu { return &Menu{ - Name: name, + Name: name, QuickChar: quickChar, - Items: make([]Item, 0, 6), - Theme: theme, + Items: make([]Item, 0, 6), + Theme: theme, } } @@ -329,7 +329,7 @@ func (m *Menu) ActivateItemUnderCursor() { m.itemSelectedCallback() case *Menu: // TODO: implement sub-menus ... - } + } } func (m *Menu) CursorUp() { @@ -379,7 +379,7 @@ func (m *Menu) Draw(s tcell.Screen) { } else { sty = defaultStyle } - + nameCols := DrawQuickCharStr(s, m.x+1, m.y+1+i, item.GetName(), item.GetQuickCharIdx(), sty) str := strings.Repeat(" ", m.width-2-nameCols) // Fill space after menu names to border @@ -437,7 +437,7 @@ func (m *Menu) GetSize() (int, int) { } m.width = 1 + maxNameLen + shortcutsWidth + 1 // Add two for padding - m.height = 1 + len(m.Items) + 1 // And another two for the same reason ... + m.height = 1 + len(m.Items) + 1 // And another two for the same reason ... return m.width, m.height } diff --git a/ui/messagedialog.go b/ui/messagedialog.go index c5f90dc..28a326f 100755 --- a/ui/messagedialog.go +++ b/ui/messagedialog.go @@ -49,8 +49,8 @@ func NewMessageDialog(title string, message string, kind MessageDialogKind, opti } dialog := MessageDialog{ - Title: title, - Kind: kind, + Title: title, + Kind: kind, Callback: callback, theme: theme, @@ -117,8 +117,8 @@ func (d *MessageDialog) SetPos(x, y int) { func (d *MessageDialog) GetMinSize() (int, int) { lines := strings.Count(d.messageWrapped, "\n") + 1 - - return Max(len(d.Title)+2, 30), 2+lines+2 + + return Max(len(d.Title)+2, 30), 2 + lines + 2 } func (d *MessageDialog) GetSize() (int, int) { diff --git a/ui/textedit.go b/ui/textedit.go index 64b5b83..830d4db 100644 --- a/ui/textedit.go +++ b/ui/textedit.go @@ -9,9 +9,9 @@ import ( "strings" "unicode/utf8" - "github.com/mattn/go-runewidth" "github.com/fivemoreminix/qedit/ui/buffer" "github.com/gdamore/tcell/v2" + "github.com/mattn/go-runewidth" ) // A Selection represents a region of the buffer to be selected for text editing @@ -91,12 +91,12 @@ loop: t.Buffer = buffer.NewRopeBuffer(contents) // TODO: replace with automatic determination of language via filetype - lang := &buffer.Language { - Name: "Go", + lang := &buffer.Language{ + Name: "Go", Filetypes: []string{".go"}, - Rules: map[*buffer.RegexpRegion]buffer.Syntax { + Rules: map[*buffer.RegexpRegion]buffer.Syntax{ &buffer.RegexpRegion{Start: regexp.MustCompile("\\/\\/.*")}: buffer.Comment, - &buffer.RegexpRegion{Start: regexp.MustCompile("\".*?\"")}: buffer.String, + &buffer.RegexpRegion{Start: regexp.MustCompile("\".*?\"")}: buffer.String, &buffer.RegexpRegion{ Start: regexp.MustCompile("\\b(var|const|if|else|range|for|switch|fallthrough|case|default|break|continue|go|func|return|defer|import|type|package)\\b"), }: buffer.Keyword, @@ -115,7 +115,7 @@ loop: }, } - colorscheme := &buffer.Colorscheme { + colorscheme := &buffer.Colorscheme{ buffer.Default: tcell.Style{}.Foreground(tcell.ColorLightGray).Background(tcell.ColorBlack), buffer.Comment: tcell.Style{}.Foreground(tcell.ColorGray).Background(tcell.ColorBlack), buffer.String: tcell.Style{}.Foreground(tcell.ColorOlive).Background(tcell.ColorBlack), @@ -275,7 +275,7 @@ func (t *TextEdit) SetLineCol(line, col int) { line, col = t.Buffer.ClampLineCol(line, col) // Handle hard tabs - tabOffset := t.getTabCountInLineAtCol(line, col) * (t.TabSize-1) // Offset for the current line from hard tabs (temporary; purely visual) + tabOffset := t.getTabCountInLineAtCol(line, col) * (t.TabSize - 1) // Offset for the current line from hard tabs (temporary; purely visual) // Scroll the screen when going to lines out of view if line >= t.scrolly+t.height-1 { // If the new line is below view... @@ -386,7 +386,7 @@ func (t *TextEdit) Draw(s tcell.Screen) { selectedStyle := t.Theme.GetOrDefault("TextEditSelected") columnStyle := t.Theme.GetOrDefault("TextEditColumn") - t.Highlighter.UpdateInvalidatedLines(t.scrolly, t.scrolly + (t.height-1)) + t.Highlighter.UpdateInvalidatedLines(t.scrolly, t.scrolly+(t.height-1)) var tabBytes []byte if t.UseHardTabs { @@ -410,30 +410,30 @@ func (t *TextEdit) Draw(s tcell.Screen) { // When iterating lineTabs: the value at i is // the rune index the tab was found at. -// var lineTabs [128]int // Rune index for each hard tab '\t' in lineBytes -// var tabs int // Length of lineTabs (number of hard tabs) + // var lineTabs [128]int // Rune index for each hard tab '\t' in lineBytes + // var tabs int // Length of lineTabs (number of hard tabs) if t.UseHardTabs { -// var ri int // rune index -// var i int -// for i < len(lineBytes) { -// r, size := utf8.DecodeRune(lineBytes[i:]) -// if r == '\t' { -// lineTabs[tabs] = ri -// tabs++ -// } -// i += size -// ri++ -// } + // var ri int // rune index + // var i int + // for i < len(lineBytes) { + // r, size := utf8.DecodeRune(lineBytes[i:]) + // if r == '\t' { + // lineTabs[tabs] = ri + // tabs++ + // } + // i += size + // ri++ + // } lineBytes = bytes.ReplaceAll(lineBytes, []byte{'\t'}, tabBytes) } lineHighlightData := t.Highlighter.GetLineMatches(line) var lineHighlightDataIdx int - var byteIdx int // Byte index of lineStr + var byteIdx int // Byte index of lineStr // X offset we draw the next rune at (some runes can be 2 cols wide) col := t.x + columnWidth - var runeIdx int // Index into lineStr (as runes) we draw the next character at + var runeIdx int // Index into lineStr (as runes) we draw the next character at // REWRITE OF SCROLL FUNC: for runeIdx < t.scrollx && byteIdx < len(lineBytes) { @@ -444,7 +444,7 @@ func (t *TextEdit) Draw(s tcell.Screen) { tabOffsetAtRuneIdx := func(idx int) int { var count int - var i int + var i int for i < len(origLineBytes) { r, size := utf8.DecodeRune(origLineBytes[i:]) if r == '\t' { @@ -459,7 +459,7 @@ func (t *TextEdit) Draw(s tcell.Screen) { // not affected by the hard tabs becoming 4 or 8 spaces. origRuneIdx := func(idx int) int { // returns the idx that is not mutated by hard tabs var ridx int // new rune idx - var i int // byte index + var i int // byte index for idx > 0 { r, size := utf8.DecodeRune(origLineBytes[i:]) if r == '\t' { @@ -475,9 +475,9 @@ func (t *TextEdit) Draw(s tcell.Screen) { return ridx } - for col < t.x + t.width { // For each column in view... - var r rune = ' ' // Rune to draw this iteration - var size int = 1 // Size of the rune (in bytes) + for col < t.x+t.width { // For each column in view... + var r rune = ' ' // Rune to draw this iteration + var size int = 1 // Size of the rune (in bytes) var selected bool // Whether this rune should be styled as selected tabOffsetAtRuneIdx := tabOffsetAtRuneIdx(runeIdx)