forked from limetext/commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcase.go
117 lines (105 loc) · 2.45 KB
/
case.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2013 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package commands
import (
"strings"
"unicode"
"github.com/limetext/backend"
)
type (
// TitleCase Command transforms all selections
// to be in Title Case. For instance, the text:
// "this is some sample text"
// turns in to:
// "This Is Some Sample Text".
TitleCase struct {
backend.DefaultCommand
}
// SwapCase Command transforms all selections
// so that each character in the selection
// is the opposite case. For example, the text:
// "Hello, World!"
// turns in to:
// "hELLO, wORLD!".
SwapCase struct {
backend.DefaultCommand
}
// UpperCase Command transforms all selections
// so that each character in the selection
// is in its upper case equivalent (if any.)
UpperCase struct {
backend.DefaultCommand
}
// LowerCase Command transforms all selections
// so that each character in the selection
// is in its lower case equivalent.
LowerCase struct {
backend.DefaultCommand
}
)
// Run executes the TitleCase command.
func (c *TitleCase) Run(v *backend.View, e *backend.Edit) error {
sel := v.Sel()
for i := 0; i < sel.Len(); i++ {
r := sel.Get(i)
if r.Size() != 0 {
t := v.Substr(r)
v.Replace(e, r, strings.Title(t))
}
}
return nil
}
// Run executes the SwapCase command.
func (c *SwapCase) Run(v *backend.View, e *backend.Edit) error {
sel := v.Sel()
for i := 0; i < sel.Len(); i++ {
r := sel.Get(i)
if r.Size() == 0 {
continue
}
text := v.Substr(r)
swapped := make([]rune, 0)
for _, c := range text {
if unicode.IsUpper(c) {
swapped = append(swapped, unicode.ToLower(c))
} else {
swapped = append(swapped, unicode.ToUpper(c))
}
}
v.Replace(e, r, string(swapped))
}
return nil
}
// Run executes the UpperCase command.
func (c *UpperCase) Run(v *backend.View, e *backend.Edit) error {
sel := v.Sel()
for i := 0; i < sel.Len(); i++ {
r := sel.Get(i)
if r.Size() != 0 {
t := v.Substr(r)
v.Replace(e, r, strings.ToUpper(t))
}
}
return nil
}
// Run executes the LowerCase command.
func (c *LowerCase) Run(v *backend.View, e *backend.Edit) error {
sel := v.Sel()
for i := 0; i < sel.Len(); i++ {
r := sel.Get(i)
if r.Size() != 0 {
t := v.Substr(r)
v.Replace(e, r, strings.ToLower(t))
}
}
return nil
}
func init() {
register([]backend.Command{
&TitleCase{},
&SwapCase{},
&UpperCase{},
&LowerCase{},
})
}