keyInputInsertBracket handle input of opening bracket-like entity (paren, brace, bracket)
(kt events.Event)
| 394 | // keyInputInsertBracket handle input of opening bracket-like entity |
| 395 | // (paren, brace, bracket) |
| 396 | func (ed *Editor) keyInputInsertBracket(kt events.Event) { |
| 397 | pos := ed.CursorPos |
| 398 | match := true |
| 399 | newLine := false |
| 400 | curLn := ed.Buffer.Line(pos.Ln) |
| 401 | lnLen := len(curLn) |
| 402 | lp, _ := parse.LanguageSupport.Properties(ed.Buffer.ParseState.Known) |
| 403 | if lp != nil && lp.Lang != nil { |
| 404 | match, newLine = lp.Lang.AutoBracket(&ed.Buffer.ParseState, kt.KeyRune(), pos, curLn) |
| 405 | } else { |
| 406 | if kt.KeyRune() == '{' { |
| 407 | if pos.Ch == lnLen { |
| 408 | if lnLen == 0 || unicode.IsSpace(curLn[pos.Ch-1]) { |
| 409 | newLine = true |
| 410 | } |
| 411 | match = true |
| 412 | } else { |
| 413 | match = unicode.IsSpace(curLn[pos.Ch]) |
| 414 | } |
| 415 | } else { |
| 416 | match = pos.Ch == lnLen || unicode.IsSpace(curLn[pos.Ch]) // at end or if space after |
| 417 | } |
| 418 | } |
| 419 | if match { |
| 420 | ket, _ := lexer.BracePair(kt.KeyRune()) |
| 421 | if newLine && ed.Buffer.Options.AutoIndent { |
| 422 | ed.InsertAtCursor([]byte(string(kt.KeyRune()) + "\n")) |
| 423 | tbe, _, cpos := ed.Buffer.AutoIndent(ed.CursorPos.Ln) |
| 424 | if tbe != nil { |
| 425 | pos = lexer.Pos{Ln: tbe.Reg.End.Ln, Ch: cpos} |
| 426 | ed.SetCursorShow(pos) |
| 427 | } |
| 428 | ed.InsertAtCursor([]byte("\n" + string(ket))) |
| 429 | ed.Buffer.AutoIndent(ed.CursorPos.Ln) |
| 430 | } else { |
| 431 | ed.InsertAtCursor([]byte(string(kt.KeyRune()) + string(ket))) |
| 432 | pos.Ch++ |
| 433 | } |
| 434 | ed.lastAutoInsert = ket |
| 435 | } else { |
| 436 | ed.InsertAtCursor([]byte(string(kt.KeyRune()))) |
| 437 | pos.Ch++ |
| 438 | } |
| 439 | ed.SetCursorShow(pos) |
| 440 | ed.setCursorColumn(ed.CursorPos) |
| 441 | } |
| 442 | |
| 443 | // keyInputInsertRune handles the insertion of a typed character |
| 444 | func (ed *Editor) keyInputInsertRune(kt events.Event) { |
no test coverage detected