Skip to content

Commit

Permalink
Merge pull request #1112 from pharo-spec/dev-1.0
Browse files Browse the repository at this point in the history
select line before doit
  • Loading branch information
estebanlm authored Jul 9, 2021
2 parents a958b2a + 38c9bc9 commit c3a8511
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 26 deletions.
18 changes: 14 additions & 4 deletions src/Spec2-Adapters-Morphic/RubTextEditor.extension.st
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@ Extension { #name : #RubTextEditor }

{ #category : #'*Spec2-Adapters-Morphic' }
RubTextEditor >> lineAtCursorPosition [
| string |

string := self text asString ifEmpty: [ ^ '' ].
^ self lineIntervalAtCursorPosition
ifNotNil: [ :anInterval | string copyFrom: anInterval first to: anInterval last ]
ifNil: [ '' ]
]

{ #category : #'*Spec2-Adapters-Morphic' }
RubTextEditor >> lineIntervalAtCursorPosition [
| string lastEnd index lastStart |

index := self pointIndex.
string := self text asString ifEmpty: [^''].
string := self text asString ifEmpty: [ ^ nil ].
string lineIndicesDo: [ :start :endWithoutDelimiters :end |
index <= end ifTrue: [
^ string copyFrom: start to: endWithoutDelimiters ].
^ start to: endWithoutDelimiters ].
lastStart := start.
lastEnd := end ].

"evaluate the case where the cursor is placed at the end of the text (there will not
be delimiter, but there will be a line to answer anyway (maybe empty)"
^ lastEnd + 1 <= index
ifTrue: [ string copyFrom: lastStart to: lastEnd ]
ifFalse: [ '' ]
ifTrue: [ lastStart to: lastEnd ]
ifFalse: [ nil ]
]
3 changes: 2 additions & 1 deletion src/Spec2-Adapters-Morphic/SpMorphicBaseTextAdapter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ SpMorphicBaseTextAdapter >> selectFrom: nodeStart to: nodeStop [
{ #category : #'spec protocol' }
SpMorphicBaseTextAdapter >> selectLine [

self widgetDo: [ :w | w textArea editor selectLine ]
self widgetDo: [ :w |
self selectionInterval: w textArea editor lineIntervalAtCursorPosition ]
]

{ #category : #accessing }
Expand Down
3 changes: 2 additions & 1 deletion src/Spec2-Code/SpCodePresenter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,8 @@ SpCodePresenter >> selectedSelector [
SpCodePresenter >> selectedTextOrLine [

self selectedText ifNotEmpty: [ :aString | ^ aString ].
^ self lineAtCursorPosition
self selectLine.
^ self selectedText
]

{ #category : #private }
Expand Down
100 changes: 81 additions & 19 deletions src/Spec2-Core/SpFilteringSelectableListPresenter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,38 @@ Class {
'displayBlock',
'displayIconBlock',
'selectedItems',
'previousSelectedIndex'
'previousSelectedIndex',
'onActivation',
'onDeactivation'
],
#category : #'Spec2-Core-Widgets-Advanced'
}

{ #category : #private }
SpFilteringSelectableListPresenter >> activateAll [

self selectItems: self items.

self onActivation ifNotNil: [ :valuable |
self items \ selectedItems do: [ :item | valuable cull: item ] ]
]

{ #category : #private }
SpFilteringSelectableListPresenter >> activateItem: anObject [

selectedItems add: anObject.

self onActivation ifNotNil: [ :valuable | valuable cull: anObject ]
]

{ #category : #private }
SpFilteringSelectableListPresenter >> deactivateItem: anObject [

selectedItems remove: anObject.

self onDeactivation ifNotNil: [ :valuable | valuable cull: anObject ]
]

{ #category : #api }
SpFilteringSelectableListPresenter >> display [

Expand Down Expand Up @@ -46,38 +73,42 @@ SpFilteringSelectableListPresenter >> initialize [
SpFilteringSelectableListPresenter >> initializePresenters [

super initializePresenters.
listPresenter
bindKeyCombination: Character space
listPresenter
bindKeyCombination: Character space
toAction: [ self toggleSelection ].

listPresenter selection whenSelectedIndexChangedDo: [ :newIndex :prevIndex |
previousSelectedIndex := prevIndex ].


listPresenter selection whenSelectedIndexChangedDo: [
:newIndex
:prevIndex | previousSelectedIndex := prevIndex ].

listPresenter eventHandler whenMouseDownDo: [ :event |
event shiftPressed
ifTrue: [
self
self
selectFrom: previousSelectedIndex
to: listPresenter selection selectedIndex ]
ifFalse: [
self toggleSelection ] ].

ifFalse: [ self toggleSelection ] ].

listPresenter
bindKeyCombination: $a meta toAction: [ self selectItems: self items ]
bindKeyCombination: $a meta
toAction: [ self activateAll ]
]

{ #category : #private }
SpFilteringSelectableListPresenter >> listColumns [
| column |

column := SpCompositeTableColumn new.

column addColumn: (selectColumn := (SpCheckBoxTableColumn new
evaluated: [ :anObject | selectedItems includes: anObject ];
onActivation: [ :anObject | selectedItems add: anObject ];
onDeactivation: [ :anObject | selectedItems remove: anObject ];
onActivation: [ :anObject | self activateItem: anObject ];
onDeactivation: [ :anObject |self deactivateItem: anObject ];
beNotExpandable)).

displayIconBlock ifNotNil: [
column addColumn: (SpImageTableColumn evaluated: displayIconBlock) beNotExpandable ].

column addColumn: (SpStringTableColumn evaluated: self display).

^ { column }
Expand All @@ -93,12 +124,43 @@ SpFilteringSelectableListPresenter >> newListToFilter [
^ table
]

{ #category : #api }
SpFilteringSelectableListPresenter >> onActivation [

^ onActivation
]

{ #category : #api }
SpFilteringSelectableListPresenter >> onActivation: aBlock [

onActivation := aBlock
]

{ #category : #api }
SpFilteringSelectableListPresenter >> onDeactivation [

^ onDeactivation
]

{ #category : #api }
SpFilteringSelectableListPresenter >> onDeactivation: aBlock [

onDeactivation := aBlock
]

{ #category : #private }
SpFilteringSelectableListPresenter >> selectFrom: fromIndex to: toIndex [

selectedItems addAll: (fromIndex < toIndex
| newItems |
newItems := fromIndex < toIndex
ifTrue: [ listPresenter items copyFrom: fromIndex to: toIndex ]
ifFalse:[ listPresenter items copyFrom: toIndex to: fromIndex ]).
ifFalse: [ listPresenter items copyFrom: toIndex to: fromIndex ].

selectedItems addAll: newItems.

self onActivation ifNotNil: [ :valuable |
newItems do: [ :item | valuable cull: item ] ].

listPresenter refresh
]

Expand All @@ -123,11 +185,11 @@ SpFilteringSelectableListPresenter >> selectedItems [

{ #category : #private }
SpFilteringSelectableListPresenter >> toggleSelection [
| selectedItem |

| selectedItem |
selectedItem := listPresenter selectedItem.
(selectedItems includes: selectedItem)
ifTrue: [ selectedItems remove: selectedItem ]
ifFalse: [ selectedItems add: selectedItem ].
ifTrue: [ self deactivateItem: selectedItem ]
ifFalse: [ self activateItem: selectedItem ].
listPresenter refresh
]
21 changes: 21 additions & 0 deletions src/Spec2-Examples/SpFilteringSelectableListPresenter.extension.st
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ SpFilteringSelectableListPresenter class >> example [
^ example
]

{ #category : #'*Spec2-Examples' }
SpFilteringSelectableListPresenter class >> exampleWithActions [

| example |
example := self new.
example items: RBBrowserEnvironment default classes asArray.
example displayIcon: [ :aClass | aClass systemIcon ].
example onActivation: [ :class | self crTrace: '+ ' , class name ].
example onDeactivation: [ :class | self crTrace: '- ' , class name ].

Transcript
open;
clear.

example openWithSpec.
example withWindowDo: [ :window |
window title: self name asString , ' example' ].

^ example
]

{ #category : #'*Spec2-Examples' }
SpFilteringSelectableListPresenter class >> exampleWithInitializedFilter [
| example |
Expand Down
17 changes: 16 additions & 1 deletion src/Spec2-Tests/SpTextPresenterTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,23 @@ SpTextPresenterTest >> testPropagateNaturalWidth [

{ #category : #tests }
SpTextPresenterTest >> testSelectLine [
self initializationText.

presenter text: 'Text for tests.
with multiple
lines'.
self openInstance.
presenter selectLine.
self assert: presenter selectionInterval equals: (1 to: 15)
]

{ #category : #tests }
SpTextPresenterTest >> testSelectLineSecondLine [

presenter text: 'Text for tests.
with multiple
lines'.
self openInstance.
presenter cursorPositionIndex: 17.
presenter selectLine.
self assert: presenter selectionInterval equals: (17 to: 29)
]

0 comments on commit c3a8511

Please sign in to comment.