Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

options: Add bracepairs to control the start and end character of the pairs #2857

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions internal/action/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1293,19 +1293,24 @@ func (h *BufPane) paste(clip string) {
// JumpToMatchingBrace moves the cursor to the matching brace if it is
// currently on a brace
func (h *BufPane) JumpToMatchingBrace() bool {
for _, bp := range buffer.BracePairs {
r := h.Cursor.RuneUnder(h.Cursor.X)
rl := h.Cursor.RuneUnder(h.Cursor.X - 1)
if r == bp[0] || r == bp[1] || rl == bp[0] || rl == bp[1] {
matchingBrace, left, found := h.Buf.FindMatchingBrace(bp, h.Cursor.Loc)
if found {
if left {
h.Cursor.GotoLoc(matchingBrace)
} else {
h.Cursor.GotoLoc(matchingBrace.Move(1, h.Buf))
for _, pair := range h.Buf.Settings["bracepairs"].([]interface{}) {
strPair := pair.(string)
if util.CharacterCountInString(strPair) == 2 {
runes := []rune(strPair)
bp := [2]rune{runes[0], runes[1]}
r := h.Cursor.RuneUnder(h.Cursor.X)
rl := h.Cursor.RuneUnder(h.Cursor.X - 1)
if r == bp[0] || r == bp[1] || rl == bp[0] || rl == bp[1] {
matchingBrace, left, found := h.Buf.FindMatchingBrace(bp, h.Cursor.Loc)
if found {
if left {
h.Cursor.GotoLoc(matchingBrace)
} else {
h.Cursor.GotoLoc(matchingBrace.Move(1, h.Buf))
}
h.Relocate()
return true
}
h.Relocate()
return true
}
}
}
Expand Down
6 changes: 0 additions & 6 deletions internal/buffer/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,12 +974,6 @@ func (b *Buffer) MoveLinesDown(start int, end int) {
)
}

var BracePairs = [][2]rune{
{'(', ')'},
{'{', '}'},
{'[', ']'},
}

// FindMatchingBrace returns the location in the buffer of the matching bracket
// It is given a brace type containing the open and closing character, (for example
// '{' and '}') as well as the location to match from
Expand Down
24 changes: 23 additions & 1 deletion internal/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func init() {
// Options with validators
var optionValidators = map[string]optionValidator{
"autosave": validateNonNegativeValue,
"bracepairs": validateBracePairs,
"clipboard": validateClipboard,
"tabsize": validatePositiveValue,
"scrollmargin": validateNonNegativeValue,
Expand Down Expand Up @@ -89,7 +90,7 @@ func ReadSettings() error {
func verifySetting(option string, value reflect.Type, def reflect.Type) bool {
var interfaceArr []interface{}
switch option {
case "pluginrepos", "pluginchannels":
case "bracepairs", "pluginrepos", "pluginchannels":
return value.AssignableTo(reflect.TypeOf(interfaceArr))
default:
return def.AssignableTo(value)
Expand Down Expand Up @@ -277,6 +278,7 @@ var defaultCommonSettings = map[string]interface{}{
"backup": true,
"backupdir": "",
"basename": false,
"bracepairs": []interface{}{"()", "{}", "[]"},
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a lot of trouble to get it somehow working with the default and a user-defined configuration in case it was defined as []string{"()", "{}", "[]"}, because then the whole type conversion kicks in.

"colorcolumn": float64(0),
"cursorline": true,
"diffgutter": false,
Expand Down Expand Up @@ -413,6 +415,16 @@ func GetNativeValue(option string, realValue interface{}, value string) (interfa
return nil, ErrInvalidValue
}
native = float64(i)
} else if kind == reflect.Slice {
var s []interface{}
parts := strings.Split(value, " ")
if len(parts) == 0 {
return nil, ErrInvalidValue
}
for _, pair := range parts {
s = append(s, pair)
}
native = s
} else {
return nil, ErrInvalidValue
}
Expand Down Expand Up @@ -462,6 +474,16 @@ func validateNonNegativeValue(option string, value interface{}) error {
return nil
}

func validateBracePairs(option string, value interface{}) error {
_, ok := value.([]interface{})
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, value.([]string) was too tricky so far.


if !ok {
return errors.New("Expected slice of strings")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...and thus error message doesn't fit exactly, because now it expects slice of interfaces.

}

return nil
}

func validateColorscheme(option string, value interface{}) error {
colorscheme, ok := value.(string)

Expand Down
42 changes: 23 additions & 19 deletions internal/display/bufwindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,26 +390,30 @@ func (w *BufWindow) displayBuffer() {
}

var matchingBraces []buffer.Loc
// bracePairs is defined in buffer.go
if b.Settings["matchbrace"].(bool) {
for _, bp := range buffer.BracePairs {
for _, c := range b.GetCursors() {
if c.HasSelection() {
continue
}
curX := c.X
curLoc := c.Loc

r := c.RuneUnder(curX)
rl := c.RuneUnder(curX - 1)
if r == bp[0] || r == bp[1] || rl == bp[0] || rl == bp[1] {
mb, left, found := b.FindMatchingBrace(bp, curLoc)
if found {
matchingBraces = append(matchingBraces, mb)
if !left {
matchingBraces = append(matchingBraces, curLoc)
} else {
matchingBraces = append(matchingBraces, curLoc.Move(-1, b))
for _, pair := range b.Settings["bracepairs"].([]interface{}) {
strPair := pair.(string)
if util.CharacterCountInString(strPair) == 2 {
runes := []rune(strPair)
bp := [2]rune{runes[0], runes[1]}
for _, c := range b.GetCursors() {
if c.HasSelection() {
continue
}
curX := c.X
curLoc := c.Loc

r := c.RuneUnder(curX)
rl := c.RuneUnder(curX - 1)
if r == bp[0] || r == bp[1] || rl == bp[0] || rl == bp[1] {
mb, left, found := b.FindMatchingBrace(bp, curLoc)
if found {
matchingBraces = append(matchingBraces, mb)
if !left {
matchingBraces = append(matchingBraces, curLoc)
} else {
matchingBraces = append(matchingBraces, curLoc.Move(-1, b))
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions runtime/help/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ Here are the available options:

default value: `false`

* `bracepairs`: controls which characters shall be handled as start and end
of brace pairs.

default value: `"()", "{}", "[]"`

* `clipboard`: specifies how micro should access the system clipboard.
Possible values are:
* `external`: accesses clipboard via an external tool, such as xclip/xsel
Expand Down