#UI_ENHANCEMENT by exept
class: EditTextView
added: #handleReturnPressed:
changed: #doKeyPress:x:y:
shift-return AND autoindent: move cursor to col
--- a/EditTextView.st Tue Aug 13 10:35:24 2019 +0200
+++ b/EditTextView.st Wed Aug 14 16:10:16 2019 +0200
@@ -5841,11 +5841,7 @@
].
(key == #Return) ifTrue:[
- shiftPressed ifTrue:[
- self unselect. self cursorReturn.
- ^ self
- ].
- self handleReturnPressed.
+ self handleReturnPressed:shiftPressed.
^ self
].
@@ -6183,6 +6179,128 @@
"Modified: / 18-07-2019 / 08:28:52 / Claus Gittinger"
!
+handleReturnPressed:shiftPressed
+ "return key pressed - handle autoIndent"
+
+ |i leftPart rightPart startIndent oldIndent newIndent language
+ bracketStrings endsWithOpeningBracket startsWithClosingBracket
+ cursorColBefore|
+
+ shiftPressed ifTrue:[
+ self unselect.
+ self cursorReturn.
+ self autoIndent ifTrue:[
+ i := self leftIndentOfLine:cursorLine.
+ self cursorCol:(i+1 max:1)
+ ].
+ ^ self
+ ].
+
+ self isReadOnly ifTrue:[
+ self unselect; makeCursorVisible.
+ self cursorReturn.
+ ^ self.
+ ].
+
+ self isInInsertMode ifFalse:[
+ self cursorReturn:true.
+ self autoIndent ifTrue:[
+ i := self leftIndentForLine:(cursorLine + 1).
+ (self listAt:cursorLine) isEmptyOrNil ifTrue:[
+ self cursorCol:(i+1 max:1)
+ ]
+ ].
+ ^ self.
+ ].
+
+ cursorColBefore := cursorCol.
+
+ "/ old version just unselected ...
+ "/ self unselect; makeCursorVisible.
+
+ "/ new version deletes ...
+ typeOfSelection == #paste ifTrue:[
+ self unselect; makeCursorVisible.
+ ] ifFalse:[
+ self copyAndDeleteSelection.
+ ].
+
+ startIndent := self leftIndentOfLine:cursorLine.
+
+ "/ split the line
+ leftPart := ((self listAt:cursorLine to:cursorCol-1) ? '') string.
+ rightPart := ((self listAt:cursorLine from:cursorCol) ? '') string.
+ self insertCharAtCursor:(Character cr).
+ self autoIndent ifFalse:[^ self].
+ cursorColBefore == 1 ifTrue:[^ self].
+
+ endsWithOpeningBracket := startsWithClosingBracket := [:line | false].
+
+ (language := self editedLanguage) notNil ifTrue:[
+ bracketStrings := language bracketStrings ? #().
+ endsWithOpeningBracket :=
+ [:line |
+ bracketStrings contains:[:pair | line endsWith:(pair first)].
+ ].
+ startsWithClosingBracket :=
+ [:line |
+ bracketStrings contains:[:pair | line startsWith:(pair second)].
+ ].
+ ].
+
+ (rightPart isEmpty and:[cursorCol ~~ 1]) ifTrue:[
+ "/ nothing to do.
+ ] ifFalse:[
+ i := self leftIndentForLine:cursorLine.
+
+ leftPart := leftPart withoutSeparators.
+ rightPart := rightPart withoutSeparators.
+ oldIndent := self leftIndentOfLine:cursorLine.
+
+ rightPart isEmptyOrNil ifTrue:[
+ (endsWithOpeningBracket value:leftPart) ifTrue:[
+ newIndent := i. "/ leftIndentForLine already adjusted the value if ending in an opening bracket
+ ] ifFalse:[
+ (false "(left endsWith:']')" or:[(startsWithClosingBracket value:rightPart)]) ifTrue:[
+ newIndent := (self prevTabBefore:i)-1.
+ ] ifFalse:[
+ newIndent := i. "/ leftIndentForLine returns the previous line's indent
+ ]
+ ].
+ ] ifFalse:[
+ (endsWithOpeningBracket value:leftPart) ifTrue:[
+ newIndent := i.
+ (startsWithClosingBracket value:rightPart) ifTrue:[
+ "/ entered return inside [] - insert another empty line
+ self insertCharAtCursor:(Character cr).
+ self indentFromLine:cursorLine toLine:cursorLine by:startIndent.
+ self cursorUp.
+ ]
+ ] ifFalse:[
+ (startsWithClosingBracket value:rightPart) ifTrue:[
+ leftPart notEmptyOrNil ifTrue:[
+ newIndent := (self prevTabBefore:i)-1.
+ ]
+ ] ifFalse:[
+ newIndent := i
+ ]
+ ]
+ ].
+ ].
+ newIndent notNil ifTrue:[
+ self indentFromLine:cursorLine toLine:cursorLine by:(newIndent-oldIndent).
+ self st80EditMode ifTrue:[
+ (self listAt:cursorLine) size < newIndent ifTrue:[
+ self insertStringAtCursor:(String new:((newIndent-oldIndent) max:0)).
+ ].
+ ].
+ self cursorCol:(newIndent+1 max:1)
+ ].
+
+ "Created: / 26-06-2019 / 21:57:50 / Claus Gittinger"
+ "Modified: / 18-07-2019 / 08:28:52 / Claus Gittinger"
+!
+
keyPress:key x:x y:y
"handle keyboard input"