Skip to content

Commit d00ac11

Browse files
committed
use slices package to simplify slice operations
1 parent 4ca9713 commit d00ac11

10 files changed

Lines changed: 36 additions & 41 deletions

File tree

pkg/config/hooks.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"slices"
45
"strings"
56

67
"github.com/docker/docker-agent/pkg/config/latest"
@@ -74,18 +75,18 @@ func MergeHooks(base, cli *latest.HooksConfig) *latest.HooksConfig {
7475
}
7576

7677
merged := &latest.HooksConfig{
77-
PreToolUse: append(append([]latest.HookMatcherConfig{}, base.PreToolUse...), cli.PreToolUse...),
78-
PostToolUse: append(append([]latest.HookMatcherConfig{}, base.PostToolUse...), cli.PostToolUse...),
79-
SessionStart: append(append([]latest.HookDefinition{}, base.SessionStart...), cli.SessionStart...),
80-
TurnStart: append(append([]latest.HookDefinition{}, base.TurnStart...), cli.TurnStart...),
81-
BeforeLLMCall: append(append([]latest.HookDefinition{}, base.BeforeLLMCall...), cli.BeforeLLMCall...),
82-
AfterLLMCall: append(append([]latest.HookDefinition{}, base.AfterLLMCall...), cli.AfterLLMCall...),
83-
SessionEnd: append(append([]latest.HookDefinition{}, base.SessionEnd...), cli.SessionEnd...),
84-
OnUserInput: append(append([]latest.HookDefinition{}, base.OnUserInput...), cli.OnUserInput...),
85-
Stop: append(append([]latest.HookDefinition{}, base.Stop...), cli.Stop...),
86-
Notification: append(append([]latest.HookDefinition{}, base.Notification...), cli.Notification...),
87-
OnError: append(append([]latest.HookDefinition{}, base.OnError...), cli.OnError...),
88-
OnMaxIterations: append(append([]latest.HookDefinition{}, base.OnMaxIterations...), cli.OnMaxIterations...),
78+
PreToolUse: slices.Concat(base.PreToolUse, cli.PreToolUse),
79+
PostToolUse: slices.Concat(base.PostToolUse, cli.PostToolUse),
80+
SessionStart: slices.Concat(base.SessionStart, cli.SessionStart),
81+
TurnStart: slices.Concat(base.TurnStart, cli.TurnStart),
82+
BeforeLLMCall: slices.Concat(base.BeforeLLMCall, cli.BeforeLLMCall),
83+
AfterLLMCall: slices.Concat(base.AfterLLMCall, cli.AfterLLMCall),
84+
SessionEnd: slices.Concat(base.SessionEnd, cli.SessionEnd),
85+
OnUserInput: slices.Concat(base.OnUserInput, cli.OnUserInput),
86+
Stop: slices.Concat(base.Stop, cli.Stop),
87+
Notification: slices.Concat(base.Notification, cli.Notification),
88+
OnError: slices.Concat(base.OnError, cli.OnError),
89+
OnMaxIterations: slices.Concat(base.OnMaxIterations, cli.OnMaxIterations),
8990
}
9091
return merged
9192
}

pkg/fsx/collect_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package fsx
33
import (
44
"os"
55
"path/filepath"
6-
"sort"
6+
"slices"
77
"strings"
88
"testing"
99

@@ -379,7 +379,7 @@ func TestCollectFiles_SortedOutput(t *testing.T) {
379379

380380
// Note: CollectFiles doesn't guarantee order, but results should be consistent
381381
// Sort for comparison
382-
sort.Strings(got)
382+
slices.Sort(got)
383383
assert.True(t, strings.HasSuffix(got[0], "a.go"))
384384
assert.True(t, strings.HasSuffix(got[1], "m.go"))
385385
assert.True(t, strings.HasSuffix(got[2], "z.go"))

pkg/hooks/builtins/add_directory_listing.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"log/slog"
77
"os"
8-
"sort"
8+
"slices"
99
"strings"
1010

1111
"github.com/docker/docker-agent/pkg/hooks"
@@ -56,7 +56,7 @@ func addDirectoryListing(_ context.Context, in *hooks.Input, _ []string) (*hooks
5656
if len(names) == 0 {
5757
return nil, nil
5858
}
59-
sort.Strings(names)
59+
slices.Sort(names)
6060

6161
truncated := 0
6262
if len(names) > maxDirectoryEntries {

pkg/hooks/handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"os/exec"
1111
"path/filepath"
12+
"slices"
1213
"sync"
1314

1415
"github.com/docker/docker-agent/pkg/shellpath"
@@ -150,7 +151,7 @@ func hookEnv(base []string, overrides map[string]string) []string {
150151
if len(overrides) == 0 {
151152
return base
152153
}
153-
env := append([]string{}, base...)
154+
env := slices.Clone(base)
154155
if len(env) == 0 {
155156
env = os.Environ()
156157
}

pkg/runtime/runtime_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"io"
77
"reflect"
8+
"slices"
89
"strings"
910
"sync"
1011
"testing"
@@ -2082,7 +2083,7 @@ func (r *recordingProvider) CreateChatCompletionStream(_ context.Context, _ []ch
20822083
defer r.mu.Unlock()
20832084

20842085
// Record the tool names for this call.
2085-
r.recordedCalls = append(r.recordedCalls, append([]tools.Tool{}, toolList...))
2086+
r.recordedCalls = append(r.recordedCalls, slices.Clone(toolList))
20862087

20872088
if r.callIdx >= len(r.streams) {
20882089
return newStreamBuilder().AddStopWithUsage(1, 1).Build(), nil

pkg/session/migrations_unit_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"database/sql"
66
"errors"
7+
"slices"
78
"testing"
89

910
"github.com/stretchr/testify/assert"
@@ -156,7 +157,7 @@ func TestMigrationManager_CheckForUnknownMigrations_AllowsEqualOrOlderDB(t *test
156157
require.NoError(t, NewMigrationManagerWithMigrations(db, migrations).checkForUnknownMigrations(t.Context()))
157158

158159
// Newer binary that knows about more migrations than the DB has applied: fine.
159-
newer := append([]Migration{}, migrations...)
160+
newer := slices.Clone(migrations)
160161
newer = append(newer, Migration{ID: 2, Name: "002_b", UpSQL: `CREATE TABLE b (id INTEGER)`})
161162
require.NoError(t, NewMigrationManagerWithMigrations(db, newer).checkForUnknownMigrations(t.Context()))
162163
}

pkg/tui/components/editor/editor.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99
"regexp"
10+
"slices"
1011
"strings"
1112
"time"
1213

@@ -1506,7 +1507,7 @@ func (e *editor) removeLastNAttachments(n int) {
15061507
if !e.attachments[i].isTemp {
15071508
// Strip the placeholder text ("@/path/file.png ") that AttachFile inserted
15081509
value = strings.Replace(value, e.attachments[i].placeholder+" ", "", 1)
1509-
e.attachments = append(e.attachments[:i], e.attachments[i+1:]...)
1510+
e.attachments = slices.Delete(e.attachments, i, i+1)
15101511
removed++
15111512
}
15121513
}

pkg/tui/components/messages/messages.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,9 +1151,9 @@ func (m *model) AddLoadingMessage(description string) tea.Cmd {
11511151
func (m *model) ReplaceLoadingWithUser(content string, sessionPos int) tea.Cmd {
11521152
for i := len(m.messages) - 1; i >= 0; i-- {
11531153
if m.messages[i].Type == types.MessageTypeLoading {
1154-
m.messages = append(m.messages[:i], m.messages[i+1:]...)
1154+
m.messages = slices.Delete(m.messages, i, i+1)
11551155
if i < len(m.views) {
1156-
m.views = append(m.views[:i], m.views[i+1:]...)
1156+
m.views = slices.Delete(m.views, i, i+1)
11571157
}
11581158
m.invalidateAllItems()
11591159
break

pkg/tui/dialog/working_dir_picker.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,8 @@ func (d *workingDirPickerDialog) cycleSectionBackward() { d.cycleSection(-1) }
417417

418418
func (d *workingDirPickerDialog) cycleSection(delta int) {
419419
n := len(dirPickerSectionOrder)
420-
for i, s := range dirPickerSectionOrder {
421-
if s == d.section {
422-
d.section = dirPickerSectionOrder[(i+delta+n)%n]
423-
break
424-
}
420+
if i := slices.Index(dirPickerSectionOrder, d.section); i >= 0 {
421+
d.section = dirPickerSectionOrder[(i+delta+n)%n]
425422
}
426423
d.updateSectionFocus()
427424
}

pkg/tui/service/supervisor/supervisor.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"log/slog"
88
"path/filepath"
9+
"slices"
910
"sync"
1011

1112
tea "charm.land/bubbletea/v2"
@@ -234,12 +235,7 @@ func (s *Supervisor) buildTabInfoLocked() []messages.TabInfo {
234235

235236
// activeIndexLocked returns the index of the active tab (must be called with lock held).
236237
func (s *Supervisor) activeIndexLocked() int {
237-
for i, id := range s.order {
238-
if id == s.activeID {
239-
return i
240-
}
241-
}
242-
return 0
238+
return max(0, slices.Index(s.order, s.activeID))
243239
}
244240

245241
// SwitchTo switches to a different session.
@@ -375,12 +371,9 @@ func (s *Supervisor) CloseSession(sessionID string) (nextActiveID string) {
375371

376372
// Remove from order slice, remembering where it was.
377373
closedIdx := 0
378-
for i, id := range s.order {
379-
if id == sessionID {
380-
closedIdx = i
381-
s.order = append(s.order[:i], s.order[i+1:]...)
382-
break
383-
}
374+
if i := slices.Index(s.order, sessionID); i >= 0 {
375+
closedIdx = i
376+
s.order = slices.Delete(s.order, i, i+1)
384377
}
385378

386379
// If this was the active session, switch to the previous tab (or the
@@ -430,8 +423,8 @@ func (s *Supervisor) ReorderTab(fromIdx, toIdx int) {
430423
}
431424

432425
id := s.order[fromIdx]
433-
s.order = append(s.order[:fromIdx], s.order[fromIdx+1:]...)
434-
s.order = append(s.order[:toIdx], append([]string{id}, s.order[toIdx:]...)...)
426+
s.order = slices.Delete(s.order, fromIdx, fromIdx+1)
427+
s.order = slices.Insert(s.order, toIdx, id)
435428
s.notifyTabsUpdated()
436429
}
437430

0 commit comments

Comments
 (0)