From 40bc0085867f4521367e35d7c4b248c7a6003643 Mon Sep 17 00:00:00 2001 From: Carolina Hernandez Date: Mon, 23 Oct 2023 03:40:04 -0300 Subject: [PATCH] And and Or operations implemented efficiently --- .../ScopeNode.class.st | 7 +- .../ScopeScopeNode.class.st | 20 ++++- .../ScopesPresenter.class.st | 24 ++++-- src/NewTools-Scopes/ScopesManager.class.st | 79 +++++++++++++------ 4 files changed, 96 insertions(+), 34 deletions(-) diff --git a/src/NewTools-Scopes-Browser/ScopeNode.class.st b/src/NewTools-Scopes-Browser/ScopeNode.class.st index 1fac611b4..ba329ce43 100644 --- a/src/NewTools-Scopes-Browser/ScopeNode.class.st +++ b/src/NewTools-Scopes-Browser/ScopeNode.class.st @@ -6,7 +6,6 @@ Class { #superclass : 'Object', #instVars : [ 'value', - 'packages', 'children' ], #category : 'NewTools-Scopes-Browser-Nodes', @@ -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' } @@ -92,7 +91,7 @@ ScopeNode >> name [ { #category : 'operations' } ScopeNode >> not [ - self value not + ^ ScopesManager newScopeFromNot: self value ] { #category : 'accessing' } @@ -131,5 +130,5 @@ ScopeNode >> value: aPackageOrClass [ { #category : 'operations' } ScopeNode >> | anotherNode [ - ^ self value | anotherNode value + ^ ScopesManager newScopeFrom: self value or: anotherNode value ] diff --git a/src/NewTools-Scopes-Browser/ScopeScopeNode.class.st b/src/NewTools-Scopes-Browser/ScopeScopeNode.class.st index 5a46b8815..409435680 100644 --- a/src/NewTools-Scopes-Browser/ScopeScopeNode.class.st +++ b/src/NewTools-Scopes-Browser/ScopeScopeNode.class.st @@ -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' @@ -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 @@ -62,6 +66,12 @@ ScopeScopeNode >> children [ ] +{ #category : 'accessing' } +ScopeScopeNode >> classes [ + + ^ classes ifNil: [ classes := value classes ] +] + { #category : 'testing' } ScopeScopeNode >> isScopeNode [ ^ true @@ -77,6 +87,12 @@ ScopeScopeNode >> packageNode [ self shouldNotImplement ] +{ #category : 'accessing' } +ScopeScopeNode >> packages [ + + ^ packages ifNil: [ packages := value packages ] +] + { #category : 'accessing' } ScopeScopeNode >> scopesIconName [ ^ #smallPushpin diff --git a/src/NewTools-Scopes-Browser/ScopesPresenter.class.st b/src/NewTools-Scopes-Browser/ScopesPresenter.class.st index 592c8ec87..9a2a921dc 100644 --- a/src/NewTools-Scopes-Browser/ScopesPresenter.class.st +++ b/src/NewTools-Scopes-Browser/ScopesPresenter.class.st @@ -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' } @@ -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 diff --git a/src/NewTools-Scopes/ScopesManager.class.st b/src/NewTools-Scopes/ScopesManager.class.st index 7cefd55a6..c25b2d0fe 100644 --- a/src/NewTools-Scopes/ScopesManager.class.st +++ b/src/NewTools-Scopes/ScopesManager.class.st @@ -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 [ @@ -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' }