remove operação remove, corrige bug de focus

This commit is contained in:
2026-09-08 11:16:15 -03:00
parent bf46c56b36
commit 94833d6f43
5 changed files with 128 additions and 38 deletions
+9 -15
View File
@@ -18,7 +18,6 @@ type Kind string
// The supported operation kinds.
const (
KindReplace Kind = "Replace"
KindRemove Kind = "Remove"
KindInsert Kind = "Insert"
KindIncrement Kind = "Increment"
KindLower Kind = "To lowercase"
@@ -29,7 +28,6 @@ const (
// Kinds lists every supported operation kind, in menu order.
var Kinds = []Kind{
KindReplace,
KindRemove,
KindInsert,
KindIncrement,
KindLower,
@@ -48,9 +46,9 @@ const ToLast = "to-last"
type Config struct {
Kind Kind
Target string // Replace, Remove: the text or pattern to look for.
Target string // Replace: the text or pattern to look for.
Replacement string // Replace: the text to put in its place.
Limit string // Replace, Remove: max number of matches, empty or 0 for all.
Limit string // Replace: max number of matches, empty or 0 for all.
Text string // Insert: the text to insert.
Index string // Insert: character position, or ToLast for the end.
Prefix string // Increment: text placed before the counter.
@@ -59,9 +57,9 @@ type Config struct {
First string // Truncate: first character index, or ToLast.
Last string // Truncate: last character index, or ToLast.
FromStart bool // Replace, Remove: apply Limit to the first matches instead of the last.
CaseSensitive bool // Replace, Remove: match letter case exactly.
Regex bool // Replace, Remove: treat Target as a regular expression.
FromStart bool // Replace: apply Limit to the first matches instead of the last.
CaseSensitive bool // Replace: match letter case exactly.
Regex bool // Replace: treat Target as a regular expression.
KeepBetween bool // Truncate: keep the selected range instead of removing it.
PreserveExt bool // All kinds: leave the file extension untouched.
}
@@ -98,7 +96,7 @@ type Operation interface {
// cannot be parsed or a regular expression does not compile.
func New(cfg Config) (Operation, error) {
switch cfg.Kind {
case KindReplace, KindRemove:
case KindReplace:
return newSubstitute(cfg)
case KindInsert:
return newInsert(cfg)
@@ -185,8 +183,8 @@ func compileTarget(cfg Config) (*regexp.Regexp, error) {
return re, nil
}
// substituteOp implements both [KindReplace] and [KindRemove]; a removal is a
// replacement with empty text.
// substituteOp implements [KindReplace]. Setting the replacement to an empty
// string removes matching text.
type substituteOp struct {
cfg Config
re *regexp.Regexp
@@ -253,11 +251,7 @@ func (o *substituteOp) substitute(s string) string {
func (o *substituteOp) Summary() string {
var b strings.Builder
if o.cfg.Kind == KindRemove {
fmt.Fprintf(&b, "Remove %q", o.cfg.Target)
} else {
fmt.Fprintf(&b, "Replace %q with %q", o.cfg.Target, o.cfg.Replacement)
}
fmt.Fprintf(&b, "Replace %q with %q", o.cfg.Target, o.cfg.Replacement)
flags := []string{}
if o.limit > 0 {
where := "last"
+23 -4
View File
@@ -103,7 +103,7 @@ func TestReplace(t *testing.T) {
}
}
func TestRemove(t *testing.T) {
func TestReplaceWithEmptyReplacementRemovesMatches(t *testing.T) {
tests := []struct {
name string
cfg func(c *ops.Config)
@@ -134,7 +134,7 @@ func TestRemove(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := ops.DefaultConfig(ops.KindRemove)
cfg := ops.DefaultConfig(ops.KindReplace)
tt.cfg(&cfg)
if got := apply(t, cfg, tt.in, 0); got != tt.want {
t.Errorf("Apply(%q) = %q, want %q", tt.in, got, tt.want)
@@ -282,7 +282,7 @@ func TestNewRejectsBadInput(t *testing.T) {
},
{
name: "negative limit",
cfg: ops.Config{Kind: ops.KindRemove, Target: "a", Limit: "-2"},
cfg: ops.Config{Kind: ops.KindReplace, Target: "a", Limit: "-2"},
want: "must not be negative",
},
{
@@ -321,7 +321,7 @@ func TestNewRejectsBadInput(t *testing.T) {
}
func TestPreviewAppliesOperationsInOrder(t *testing.T) {
remove := ops.DefaultConfig(ops.KindRemove)
remove := ops.DefaultConfig(ops.KindReplace)
remove.Target = "IMG_"
lower := ops.DefaultConfig(ops.KindUpper)
increment := ops.DefaultConfig(ops.KindIncrement)
@@ -399,3 +399,22 @@ func TestDefaultConfig(t *testing.T) {
})
}
}
func TestKindsContainsOnlySupportedMenuOperations(t *testing.T) {
want := []ops.Kind{
ops.KindReplace,
ops.KindInsert,
ops.KindIncrement,
ops.KindLower,
ops.KindUpper,
ops.KindTruncate,
}
if len(ops.Kinds) != len(want) {
t.Fatalf("Kinds = %v, want %v", ops.Kinds, want)
}
for i := range want {
if ops.Kinds[i] != want[i] {
t.Errorf("Kinds[%d] = %q, want %q", i, ops.Kinds[i], want[i])
}
}
}