Skip to content

Commit

Permalink
And and Or operations implemented efficiently
Browse files Browse the repository at this point in the history
  • Loading branch information
carolahp committed Oct 23, 2023
1 parent 9a4f79e commit 40bc008
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 34 deletions.
7 changes: 3 additions & 4 deletions src/NewTools-Scopes-Browser/ScopeNode.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ Class {
#superclass : 'Object',
#instVars : [
'value',
'packages',
'children'
],
#category : 'NewTools-Scopes-Browser-Nodes',
Expand All @@ -28,7 +27,7 @@ ScopeNode class >> on: aRBEnvironment [
{ #category : 'operations' }
ScopeNode >> & anotherNode [

^ self value & anotherNode value
^ ScopesManager newScopeFrom: self value and: anotherNode value
]

{ #category : 'accessing' }
Expand Down Expand Up @@ -92,7 +91,7 @@ ScopeNode >> name [
{ #category : 'operations' }
ScopeNode >> not [

self value not
^ ScopesManager newScopeFromNot: self value
]

{ #category : 'accessing' }
Expand Down Expand Up @@ -131,5 +130,5 @@ ScopeNode >> value: aPackageOrClass [
{ #category : 'operations' }
ScopeNode >> | anotherNode [

^ self value | anotherNode value
^ ScopesManager newScopeFrom: self value or: anotherNode value
]
20 changes: 18 additions & 2 deletions src/NewTools-Scopes-Browser/ScopeScopeNode.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ I am a model for having scopes in the ScopeTreePresenter
Class {
#name : 'ScopeScopeNode',
#superclass : 'ScopeNode',
#instVars : [
'classes',
'packages'
],
#category : 'NewTools-Scopes-Browser-Nodes',
#package : 'NewTools-Scopes-Browser',
#tag : 'Nodes'
Expand All @@ -18,8 +22,8 @@ ScopeScopeNode >> children [

self value isCompositeEnvironment ifTrue: [
chldrn := Set new.
envPackages := self value packages.
envClasses := self value classes reject: [:c | c isMeta ].
envPackages := self packages.
envClasses := self classes reject: [:c | c isMeta ].

"packages whose classes were all selected"
fullPackages := ScopesManager
Expand Down Expand Up @@ -62,6 +66,12 @@ ScopeScopeNode >> children [

]

{ #category : 'accessing' }
ScopeScopeNode >> classes [

^ classes ifNil: [ classes := value classes ]
]

{ #category : 'testing' }
ScopeScopeNode >> isScopeNode [
^ true
Expand All @@ -77,6 +87,12 @@ ScopeScopeNode >> packageNode [
self shouldNotImplement
]

{ #category : 'accessing' }
ScopeScopeNode >> packages [

^ packages ifNil: [ packages := value packages ]
]

{ #category : 'accessing' }
ScopeScopeNode >> scopesIconName [
^ #smallPushpin
Expand Down
24 changes: 19 additions & 5 deletions src/NewTools-Scopes-Browser/ScopesPresenter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,17 @@ ScopesPresenter >> argumentsListMenu [
anItem
name: 'AND';
visibleIf: [ scopesTree selectedItem isScopeNode ];
action: [ self andWithScope: scopesTree selectedItem value ] ];
action: [ self andWithScope: scopesTree selectedItem ] ];
addItem: [ :anItem |
anItem
name: 'OR';
visibleIf: [ scopesTree selectedItem isScopeNode ];
action: [ self orWithScope: scopesTree selectedItem value ] ];
action: [ self orWithScope: scopesTree selectedItem ] ];
addItem: [ :anItem |
anItem
name: 'NOT';
visibleIf: [ scopesTree selectedItem isScopeNode ];
action: [ self notWithScope: scopesTree selectedItem value ] ] ]
action: [ self notWithScope: scopesTree selectedItem ] ] ]
]

{ #category : 'menu commands' }
Expand Down Expand Up @@ -291,12 +291,26 @@ ScopesPresenter >> orWithScope: anItem [
{ #category : 'operations' }
ScopesPresenter >> performOperation: aSymbol with: anItem [

| anEnvironment newScope newName |
| anEnvironment newScope newName existingNames index indexes |
anEnvironment := self selectAnotherScopeWhitout: anItem.
anEnvironment ifNil: [ ^ self ].
newScope := anItem perform: aSymbol with: anEnvironment.
newName := anItem label , aSymbol , anEnvironment label.
newScope label: newName.
existingNames := model scopes collect: #label.

index := ''.

(existingNames includes: newName) ifTrue: [
indexes := existingNames
collect: [ :each |
index := (each reverse copyUpToSubstring: '-') reverse
asInteger ]
thenReject: [ :each | each isNil ].
index := indexes
ifEmpty: [ '-1' ]
ifNotEmpty: [ '-' , (indexes max + 1) asString ] ].

newScope label: newName , index.

self scopesManagerClass singleInstance addScope: newScope.
self refresh
Expand Down
79 changes: 56 additions & 23 deletions src/NewTools-Scopes/ScopesManager.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,60 @@ ScopesManager class >> invalidNames [
^ self scopes collect: [ :e | e label ]
]

{ #category : 'operations' }
ScopesManager class >> newScopeFrom: aSet [

| packageScope classScope classes packages orphanClasses fullPackages compositeScope |
classes := (aSet select: [ :each | each isClassOrTrait ]) reject: [ :each | each isMeta ] .
packages := aSet select: [ :each | each isKindOf: RPackage ].

"packages whose classes were all selected"
fullPackages := self fullPackagesIn: packages having: classes.
"classes whose package hasn't all of its classes selected"
orphanClasses := self
orphanClassesIn: classes
havingPackages: packages
fullPackages: fullPackages.

classScope := RBClassEnvironment classes: orphanClasses.
packageScope := RBPackageEnvironment packages: fullPackages.

(classScope isNotEmpty and: [ packageScope isNotEmpty ]) ifTrue: [
compositeScope := packageScope | classScope ].

compositeScope ifNotNil: [ ^ compositeScope ].
packageScope isEmpty ifFalse: [ ^ packageScope ].
classScope isEmpty ifFalse: [ ^ classScope ].
self error: 'Impossible to create scope from nodes'
]

{ #category : 'operations' }
ScopesManager class >> newScopeFrom: scopeA and: scopeB [

| classesA classesB classesInterAB packages fullPackages |

classesA := scopeA classes asSet.
classesB := scopeB classes.

classesInterAB := (classesA, classesB) select: [ :each |
(classesA includes: each) and: [
classesB includes: each ] ].
packages := classesInterAB collect: [ :each | each package ].
fullPackages := self fullPackagesIn: packages having: classesInterAB.

^ self newScopeFrom: fullPackages , classesInterAB
]

{ #category : 'operations' }
ScopesManager class >> newScopeFrom: scopeA or: scopeB [
^ self newScopeFrom: scopeA classes, scopeB classes, scopeA packages, scopeB packages
]

{ #category : 'operations' }
ScopesManager class >> newScopeFromNot: scope [
self halt.
]

{ #category : 'private' }
ScopesManager class >> orphanClassesIn: classes havingPackages: packages fullPackages: fullPackages [

Expand Down Expand Up @@ -138,30 +192,9 @@ ScopesManager >> initialize [
]

{ #category : 'private' }
ScopesManager >> newScopeFrom: aSet [

| packageScope classScope classes packages orphanClasses fullPackages compositeScope |
classes := aSet select: [ :each | each isClassOrTrait ].
packages := aSet select: [ :each | each isKindOf: RPackage ].

"packages whose classes were all selected"
fullPackages := self class fullPackagesIn: packages having: classes.
"classes whose package hasn't all of its classes selected"
orphanClasses := self class
orphanClassesIn: classes
havingPackages: packages
fullPackages: fullPackages.

classScope := RBClassEnvironment classes: orphanClasses.
packageScope := RBPackageEnvironment packages: fullPackages.

(classScope isNotEmpty and: [ packageScope isNotEmpty ]) ifTrue: [
compositeScope := packageScope | classScope ].
ScopesManager >> newScopeFrom: aSetOfClassesAndPackages [

compositeScope ifNotNil: [ ^ compositeScope ].
packageScope isEmpty ifFalse: [ ^ packageScope ].
classScope isEmpty ifFalse: [ ^ classScope ].
self error: 'Impossible to create scope from nodes'
^ self class newScopeFrom: aSetOfClassesAndPackages
]

{ #category : 'private' }
Expand Down

0 comments on commit 40bc008

Please sign in to comment.