Add linting fixes

This commit is contained in:
Daisuke Maki 2026-02-14 07:48:23 +09:00
parent 8356007755
commit db63d9f380
24 changed files with 59 additions and 98 deletions

11
.golangci.yml Normal file
View file

@ -0,0 +1,11 @@
version: "2"
linters:
disable:
- errcheck
settings:
staticcheck:
checks:
- "all"
- "-QF1008"
- "-ST1000"
- "-ST1003"

View file

@ -17,7 +17,7 @@ func TestActionFunc(t *testing.T) {
af := ActionFunc(func(_ context.Context, _ *Peco, _ Event) {
called++
})
af.Execute(nil, nil, Event{})
af.Execute(context.TODO(), nil, Event{})
if !assert.Equal(t, called, 1, "Expected ActionFunc to be called once, but it got called %d times", called) {
return
}

View file

@ -122,9 +122,9 @@ func (mb *MemoryBuffer) Accept(ctx context.Context, in chan interface{}, _ pipel
}
return
case v := <-in:
switch v.(type) {
switch v := v.(type) {
case error:
if pipeline.IsEndMark(v.(error)) {
if pipeline.IsEndMark(v) {
if pdebug.Enabled {
pdebug.Printf("MemoryBuffer received end mark (read %d lines, %s since starting accept loop)", len(mb.lines), time.Since(start).String())
}
@ -132,7 +132,7 @@ func (mb *MemoryBuffer) Accept(ctx context.Context, in chan interface{}, _ pipel
}
case line.Line:
mb.mutex.Lock()
mb.lines = append(mb.lines, v.(line.Line))
mb.lines = append(mb.lines, v)
mb.mutex.Unlock()
}
}

View file

@ -15,7 +15,7 @@ import (
var homedirFunc = util.Homedir
// NewConfig creates a new Config
// Init initializes the Config with default values
func (c *Config) Init() error {
c.Keymap = make(map[string]string)
c.InitialMatcher = IgnoreCaseMatch

View file

@ -3,7 +3,6 @@ package peco
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -131,7 +130,7 @@ func TestStringsToStyle(t *testing.T) {
}
func TestLocateRcfile(t *testing.T) {
dir, err := ioutil.TempDir("", "peco-")
dir, err := os.MkdirTemp("", "peco-")
if !assert.NoError(t, err, "Failed to create temporary directory: %s", err) {
return
}

View file

@ -82,15 +82,14 @@ func acceptAndFilter(ctx context.Context, f filter.Filter, in chan interface{},
buf = buffer.GetLineListBuf()
}
case v := <-in:
switch v.(type) {
switch v := v.(type) {
case error:
if pipeline.IsEndMark(v.(error)) {
if pipeline.IsEndMark(v) {
if pdebug.Enabled {
pdebug.Printf("filter received end mark (read %d lines, %s since starting accept loop)", lines+len(buf), time.Since(start).String())
}
if len(buf) > 0 {
flush <- buf
buf = nil
}
}
return
@ -103,7 +102,7 @@ func acceptAndFilter(ctx context.Context, f filter.Filter, in chan interface{},
// process while we filter what we already have. The buffer
// size is fairly big, because this really only makes a
// difference if we have a lot of lines to process.
buf = append(buf, v.(line.Line))
buf = append(buf, v)
if len(buf) >= bufsiz {
flush <- buf
buf = buffer.GetLineListBuf()

View file

@ -136,5 +136,4 @@ func (ecf *ExternalCmd) Apply(ctx context.Context, buf []line.Line, out pipeline
out.Send(l)
}
}
return nil
}

View file

@ -16,7 +16,6 @@ var ErrFilterNotFound = errors.New("specified filter was not found")
var ignoreCaseFlags = regexpFlagList([]string{"i"})
var defaultFlags = regexpFlagList{}
var queryKey = &struct{}{}
var incomingBufferKey = &struct{}{}
// DefaultCustomFilterBufferThreshold is the default value
// for BufferThreshold setting on CustomFilters.
@ -57,8 +56,7 @@ type Regexp struct {
quotemeta bool
mutex sync.Mutex
name string
onEnd func()
outCh pipeline.ChanOutput
outCh pipeline.ChanOutput
}
type ExternalCmd struct {

View file

@ -28,7 +28,7 @@ func regexpFor(q string, flags []string, quotemeta bool) (*regexp.Regexp, error)
reTxt = regexp.QuoteMeta(q)
}
if flags != nil && len(flags) > 0 {
if len(flags) > 0 {
reTxt = fmt.Sprintf("(?%s)%s", strings.Join(flags, ""), reTxt)
}
@ -72,7 +72,7 @@ func NewRegexp() *Regexp {
}
}
// NewRegexp creates a new regexp based filter
// NewIRegexp creates a new case-insensitive regexp based filter
func NewIRegexp() *Regexp {
return &Regexp{
factory: &regexpQueryFactory{
@ -86,7 +86,7 @@ func NewIRegexp() *Regexp {
}
}
func (rf Regexp) BufSize() int {
func (rf *Regexp) BufSize() int {
return 0
}
@ -177,7 +177,7 @@ func (rf *Regexp) Apply(ctx context.Context, lines []line.Line, out pipeline.Cha
return nil
}
func (rf Regexp) String() string {
func (rf *Regexp) String() string {
return rf.name
}
@ -196,8 +196,8 @@ func NewCaseSensitive() *Regexp {
return rf
}
// SmartCase turns ON the ignore-case flag in the regexp
// if the query contains a upper-case character
// NewSmartCase creates a filter that turns ON the ignore-case flag in the regexp
// if the query contains no upper-case character
func NewSmartCase() *Regexp {
rf := NewRegexp()
rf.quotemeta = true

View file

@ -35,7 +35,7 @@ func (p payload) Done() {
p.done <- struct{}{}
}
// NewHub creates a new Hub struct
// New creates a new Hub struct
func New(bufsiz int) *Hub {
return &Hub{
isSync: false,
@ -75,12 +75,12 @@ var doneChPool = sync.Pool{
},
}
func (r *payload) waitDone() {
// MAKE SURE r.done is valid. XXX needs locking?
<-r.done
func (p *payload) waitDone() {
// MAKE SURE p.done is valid. XXX needs locking?
<-p.done
ch := r.done
r.done = nil
ch := p.done
p.done = nil
defer doneChPool.Put(ch)
}

View file

@ -132,9 +132,10 @@ type Keyseq interface {
InMiddleOfChain() bool
}
// PagingRequest can be sent to move the selection cursor
// PagingRequestType is the type of a paging request
type PagingRequestType int
// PagingRequest can be sent to move the selection cursor
type PagingRequest interface {
Type() PagingRequestType
}

View file

@ -19,7 +19,7 @@ func ReleaseLineListBuf(l []line.Line) {
return
}
l = l[0:0]
lineListPool.Put(l)
lineListPool.Put(l) //nolint:staticcheck // SA6002: converting to pointer-based pool breaks tests
}
func GetLineListBuf() []line.Line {

View file

@ -80,31 +80,3 @@ func TestTree(t *testing.T) {
n10 := n9.Get(NewKeyFromKey(KeyCtrlE))
checkNode(t, n10, 0, validData(KeyList{NewKeyFromKey(KeyCtrlA), NewKeyFromKey(KeyCtrlB), NewKeyFromKey(KeyCtrlC), NewKeyFromKey(KeyCtrlD), NewKeyFromKey(KeyCtrlE)}, 10, r))
}
func assertMatches(t *testing.T, exp, act []Match) {
if len(act) != len(exp) {
t.Errorf("[]Match length is not %d (%d)", len(exp), len(act))
t.Logf(" expected: %v", exp)
t.Logf(" actually: %v", act)
}
for i, e := range exp {
dump := false
a := act[i]
if a.Index != e.Index {
t.Errorf("Index not matched at #%d\n", i)
dump = true
}
if !a.Pattern.Equals(e.Pattern) {
t.Errorf("Pattern not matched at #%d\n", i)
dump = true
}
if a.Value != e.Value {
t.Errorf("Value not matched at #%d\n", i)
dump = true
}
if dump {
t.Logf(" expected: %+v", e)
t.Logf(" actually: %+v", a)
}
}
}

View file

@ -91,13 +91,13 @@ func (k Key) Compare(x Key) int {
return 0
}
func (k KeyList) Equals(x KeyList) bool {
if len(k) != len(x) {
func (kl KeyList) Equals(x KeyList) bool {
if len(kl) != len(x) {
return false
}
for i := 0; i < len(k); i++ {
if k[i].Compare(x[i]) != 0 {
for i := 0; i < len(kl); i++ {
if kl[i].Compare(x[i]) != 0 {
return false
}
}

View file

@ -2,14 +2,6 @@ package keyseq
import "testing"
func assertNilBoth(t *testing.T, n *TernaryNode) {
if n.low != nil {
t.Errorf("low node has value: %v", &n.low)
}
if n.high != nil {
t.Errorf("high node has value: %v", &n.high)
}
}
func TestBalance(t *testing.T) {
trie := NewTernaryTrie()

View file

@ -28,7 +28,7 @@ func ContainsUpper(query string) bool {
// Global var used to strips ansi sequences
var reANSIEscapeChars = regexp.MustCompile("\x1B\\[(?:[0-9]{1,2}(?:;[0-9]{1,2})?)*[a-zA-Z]")
// Function who strips ansi sequences
// StripANSISequence strips ANSI escape sequences from the given string
func StripANSISequence(s string) string {
return reANSIEscapeChars.ReplaceAllString(s, "")
}
@ -51,11 +51,11 @@ type exitStatuser interface {
func IsIgnorableError(err error) bool {
for e := err; e != nil; {
switch e.(type) {
switch v := e.(type) {
case ignorable:
return e.(ignorable).Ignorable()
return v.Ignorable()
case causer:
e = e.(causer).Cause()
e = v.Cause()
default:
return false
}
@ -65,11 +65,11 @@ func IsIgnorableError(err error) bool {
func IsCollectResultsError(err error) bool {
for e := err; e != nil; {
switch e.(type) {
switch v := e.(type) {
case collectResults:
return e.(collectResults).CollectResults()
return v.CollectResults()
case causer:
e = e.(causer).Cause()
e = v.Cause()
default:
return false
}

View file

@ -2,7 +2,6 @@ package peco
import (
"io"
"io/ioutil"
"os"
"testing"
"time"
@ -47,7 +46,7 @@ func TestIssue212_SanityCheck(t *testing.T) {
}
// Okay, this time create a dummy config file, and read that in
f, err := ioutil.TempFile("", "peco-test-config")
f, err := os.CreateTemp("", "peco-test-config")
if !assert.NoError(t, err, "Failed to create temporary config file: %s", err) {
return
}

View file

@ -24,7 +24,9 @@ func (km Keymap) Sequence() Keyseq {
return km.seq
}
const isTopLevelActionCall = "peco.isTopLevelActionCall"
type contextKey string
const isTopLevelActionCall contextKey = "peco.isTopLevelActionCall"
func (km Keymap) ExecuteAction(ctx context.Context, state *Peco, ev Event) (err error) {
if pdebug.Enabled {
@ -179,7 +181,3 @@ func (km *Keymap) ApplyKeybinding() error {
return errors.Wrap(k.Compile(), "failed to compile key binding patterns")
}
// TODO: this needs to be fixed.
func (km Keymap) hasModifierMaps() bool {
return false
}

View file

@ -598,7 +598,7 @@ func (l *ListArea) Draw(state *Peco, parent Layout, perPage int, options *DrawOp
XOffset: xOffset,
Fg: fgAttr,
Bg: bgAttr,
Msg: line[m[1]:len(line)],
Msg: line[m[1]:],
Fill: true,
})
}

View file

@ -4,7 +4,7 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"runtime"
"sync"
"testing"
@ -68,7 +68,7 @@ func (i *interceptor) record(name string, args []interface{}) {
}
func newConfig(s string) (string, error) {
f, err := ioutil.TempFile("", "peco-test-config-")
f, err := os.CreateTemp("", "peco-test-config-")
if err != nil {
return "", err
}
@ -250,12 +250,6 @@ func TestPeco(t *testing.T) {
}
}
type testCauser interface {
Cause() error
}
type testIgnorableError interface {
Ignorable() bool
}
func TestPecoHelp(t *testing.T) {
p := newPeco()

View file

@ -134,7 +134,7 @@ func (p *Pipeline) Run(ctx context.Context) (err error) {
// Setup the Acceptors, effectively chaining all nodes
// starting from the destination, working all the way
// up to the Source
var prevCh ChanOutput = ChanOutput(make(chan interface{}))
prevCh := ChanOutput(make(chan interface{}))
go p.dst.Accept(ctx, prevCh, nil)
for i := len(p.nodes) - 1; i >= 0; i-- {

View file

@ -17,7 +17,6 @@ func TestSelection(t *testing.T) {
t.Errorf("expected Len = 1, got %d", s.Len())
}
s.Add(line.NewRaw(i, "Bob", false))
i++
if s.Len() != 2 {
t.Errorf("expected Len = 2, got %d", s.Len())
}

View file

@ -13,7 +13,7 @@ import (
"github.com/peco/peco/pipeline"
)
// Creates a new Source. Does not start processing the input until you
// NewSource creates a new Source. Does not start processing the input until you
// call Setup()
func NewSource(name string, in io.Reader, isInfinite bool, idgen line.IDGenerator, capacity int, enableSep bool) *Source {
s := &Source{

View file

@ -52,16 +52,16 @@ func (v *View) Loop(ctx context.Context, cancel func()) error {
v.movePage(r, r.Data().(PagingRequest))
case r := <-h.DrawCh():
tmp := r.Data()
switch tmp.(type) {
switch tmp := tmp.(type) {
case string:
switch tmp.(string) {
switch tmp {
case "prompt":
v.drawPrompt(r)
case "purgeCache":
v.purgeDisplayCache(r)
}
case *DrawOptions:
v.drawScreen(r, tmp.(*DrawOptions))
v.drawScreen(r, tmp)
default:
v.drawScreen(r, nil)
}