Ran go fmt *.go on all sources

This commit is contained in:
Luke I. Wilson 2021-04-04 13:15:31 -05:00
parent e115855491
commit 3982628653
11 changed files with 78 additions and 79 deletions

22
main.go
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 {

View File

@ -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 {

View File

@ -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
}

View File

@ -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

View File

@ -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:])

View File

@ -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
}

View File

@ -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) {

View File

@ -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)