Skip to content

Commit

Permalink
minor changes on selecter
Browse files Browse the repository at this point in the history
  • Loading branch information
plantec committed Mar 16, 2024
1 parent 47ba7bf commit 7bbf9f5
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ I move a cursor to the beginning of the current word when in the middle of a wor
Class {
#name : #AlbTextEditorNavigatorMoveToStartPreviousWordOperation,
#superclass : #AlbTextEditorNavigatorOperation,
#category : 'Album-Model-Operators'
#category : #'Album-Model-Operators'
}

{ #category : #matching }
Expand All @@ -13,5 +13,5 @@ AlbTextEditorNavigatorMoveToStartPreviousWordOperation >> performOn: aNavigator

aNavigator
privateRegisterForMove: self cursor
to: (aNavigator findWordStartBefore: self cursor position)
to: (aNavigator findWordEnlargedStartBefore: self cursor position)
]
84 changes: 63 additions & 21 deletions src/Album/AlbTextEditorOperator.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Class {
'subscriptions',
'context'
],
#category : 'Album-Model-Operators'
#category : #'Album-Model-Operators'
}

{ #category : #'api - processing' }
Expand Down Expand Up @@ -310,7 +310,25 @@ AlbTextEditorOperator >> findWhitespaceOfLineBefore: aTextIndex [
]

{ #category : #private }
AlbTextEditorOperator >> findWordAt: aTextIndex [
AlbTextEditorOperator >> findWordEndAfter: aTextIndex [
"Find a text index of the end of the next word after a given text index"
| textIterator newPosition aText |

newPosition := aTextIndex.
aText := self text.

textIterator := aText iterator: (newPosition max: 1) to: aText size.
[ textIterator hasNext and: [ newPosition = aTextIndex ] ] whileTrue: [
| skip |
skip := textIterator peek isSeparator.
textIterator nextWord.
skip ifFalse: [ newPosition := textIterator position ] ].

^ newPosition
]

{ #category : #private }
AlbTextEditorOperator >> findWordEnlargedIntervalAt: aTextIndex [
"Find a word text interval or a whitespace at a given text index"
<return: #Interval>
| aTextIterator hasWordToTheLeft hasWordToTheRight shouldBeWord aWordStart aWordEnd aDesiredType |
Expand Down Expand Up @@ -344,25 +362,7 @@ AlbTextEditorOperator >> findWordAt: aTextIndex [
]

{ #category : #private }
AlbTextEditorOperator >> findWordEndAfter: aTextIndex [
"Find a text index of the end of the next word after a given text index"
| textIterator newPosition aText |

newPosition := aTextIndex.
aText := self text.

textIterator := aText iterator: (newPosition max: 1) to: aText size.
[ textIterator hasNext and: [ newPosition = aTextIndex ] ] whileTrue: [
| skip |
skip := textIterator peek isSeparator.
textIterator nextWord.
skip ifFalse: [ newPosition := textIterator position ] ].

^ newPosition
]

{ #category : #private }
AlbTextEditorOperator >> findWordStartBefore: aTextIndex [
AlbTextEditorOperator >> findWordEnlargedStartBefore: aTextIndex [
"Find a text index of start of the previous word before a given text index"
| newPosition aText |

Expand All @@ -377,6 +377,48 @@ AlbTextEditorOperator >> findWordStartBefore: aTextIndex [
^ newPosition
]

{ #category : #private }
AlbTextEditorOperator >> findWordIntervalAt: aTextIndex [
"Find a word text interval or a whitespace at a given text index"
<return: #Interval>
| aTextIterator hasWordToTheLeft hasWordToTheRight shouldBeWord aWordStart aWordEnd aDesiredType |

aTextIterator := self text iterator skip: aTextIndex.
hasWordToTheLeft := aTextIterator hasPrevious and: [ self isWordChar: aTextIterator peer ].
hasWordToTheRight := aTextIterator hasNext and: [ self isWordChar: aTextIterator peek ].
shouldBeWord := hasWordToTheLeft.

aWordStart := aTextIndex.
aWordEnd := aTextIndex.
shouldBeWord ifFalse: [ ^ aWordStart to: aWordEnd - 1 ].
aTextIterator := self text iterator skip: aTextIndex.
aTextIterator hasPrevious ifTrue: [
aTextIterator
previousSegment: [ :eachCharacter | self characterType: eachCharacter ]
indicesDo: [ :aStart :anEnd :aType |
((aType = #word and: [ shouldBeWord ]) or: [ (aDesiredType isNil or: [ aType = aDesiredType ] ) ])
ifTrue: [ aWordStart := aStart ] ] ].

^ aWordStart to: aWordEnd
]

{ #category : #private }
AlbTextEditorOperator >> findWordStartBefore: aTextIndex [
"Find a text index of start of the previous word before a given text index"

| newPosition aText |
aText := self text.
newPosition := aTextIndex - 1.
newPosition := newPosition max: 0.

[ newPosition = 0 or: [ (aText at: newPosition) isSeparator ] ]
whileFalse: [ newPosition := newPosition - 1 ].

^ newPosition = 0
ifTrue: [ newPosition ]
ifFalse: [ newPosition + 1 ]
]

{ #category : #private }
AlbTextEditorOperator >> hasSpaceAroundCursor: aCursor [
"Answer a boolean indicating whether the characters either side of the cursor are separators (or end of string)"
Expand Down
6 changes: 3 additions & 3 deletions src/Album/AlbTextEditorSelecter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Class {
'shouldDeselectExisting',
'commands'
],
#category : 'Album-Model-Operators'
#category : #'Album-Model-Operators'
}

{ #category : #'api - configuration' }
Expand Down Expand Up @@ -99,7 +99,7 @@ AlbTextEditorSelecter >> extendToPreviousWordStart [
theCommands := self cursors collect: [ :aCursor |
| aDelta |

aDelta := aCursor position - (self findWordStartBefore: aCursor position).
aDelta := aCursor position - (self findWordEnlargedStartBefore: aCursor position).
(self
privateExtendSelectionLeftAt: aCursor position
by: aDelta)
Expand Down Expand Up @@ -529,6 +529,6 @@ AlbTextEditorSelecter >> wordAt: aTextIndex [
"Select a word or a whitespace at a given text index"
| aWordInterval |

aWordInterval := self findWordAt: aTextIndex.
aWordInterval := self findWordEnlargedIntervalAt: aTextIndex.
self from: aWordInterval first to: aWordInterval last
]
8 changes: 7 additions & 1 deletion src/Album/AlbTextInsertedEvent.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Class {
'text',
'index'
],
#category : 'Album-Model-Events'
#category : #'Album-Model-Events'
}

{ #category : #'instance creation' }
Expand All @@ -17,6 +17,12 @@ AlbTextInsertedEvent class >> text: aBlText at: anIndex [
index: anIndex
]

{ #category : #accessing }
AlbTextInsertedEvent >> endIndex [

^ self index + self text size
]

{ #category : #accessing }
AlbTextInsertedEvent >> index [
^ index
Expand Down

0 comments on commit 7bbf9f5

Please sign in to comment.