From 1d2e50714d7595e0cbfeecacfdfa52775a9261ca Mon Sep 17 00:00:00 2001 From: Joana Bergsiek Date: Fri, 24 Nov 2023 13:23:21 +0100 Subject: [PATCH 1/2] Moves diffing mechanics to new subclass --- .../SBDiffTabView.class.st | 141 ++++++++++++++++++ .../SBExploriants.class.st | 13 -- packages/Sandblocks-Core/SBTabView.class.st | 110 +------------- .../Sandblocks-Smalltalk/SBVariant.class.st | 2 +- 4 files changed, 146 insertions(+), 120 deletions(-) create mode 100644 packages/Sandblocks-Babylonian/SBDiffTabView.class.st diff --git a/packages/Sandblocks-Babylonian/SBDiffTabView.class.st b/packages/Sandblocks-Babylonian/SBDiffTabView.class.st new file mode 100644 index 00000000..2b6e70f5 --- /dev/null +++ b/packages/Sandblocks-Babylonian/SBDiffTabView.class.st @@ -0,0 +1,141 @@ +Class { + #name : #SBDiffTabView, + #superclass : #SBTabView, + #instVars : [ + 'isShowingDiff' + ], + #category : #'Sandblocks-Babylonian' +} + +{ #category : #callbacks } +SBDiffTabView >> artefactSaved: aMethodBlock [ + + (aMethodBlock = self containingArtefact and: [self isShowingDiff]) + ifTrue: [self updateSelectedTab] +] + +{ #category : #diffing } +SBDiffTabView >> buildDiffBlockFrom: aNamedBlock to: anotherNamedBlock given: aDiffText [ + + (aNamedBlock == anotherNamedBlock) ifTrue: [^ aNamedBlock block vResizing: #spaceFill]. + + ^ SBTextBubble multiLine + vResizing: #spaceFill; + contents: aDiffText, + (Text fromString: + '', Character cr, + '==========', Character cr, + (aNamedBlock name)) +] + +{ #category : #ui } +SBDiffTabView >> buildTabs [ + + self addMorphBack: (SBRow new + addAllMorphsBack: (self namedBlocks collect: [:block | self asTabButton: block]); + name: #tabs; + addMorphBack: self addButton; + addMorphFront: self diffButton; + changeTableLayout; + listDirection: #leftToRight; + hResizing: #shrinkWrap) +] + +{ #category : #ui } +SBDiffTabView >> buildView [ + + self addMorphBack: ((self isShowingDiff + ifTrue: [self diffForSelected] + ifFalse: [self activeBlock]) + hResizing: #spaceFill) +] + +{ #category : #ui } +SBDiffTabView >> diffButton [ + + ^ SBButton new + icon: (SBIcon iconCodeFork size: 12.0 sbScaled) + do: [self toggleDiffView]; + makeSmall; + balloonText: 'Toggle diff to others'; + cornerStyle: #squared; + cellGap: -1.0 sbScaled; + layoutInset: (4.0 @ 3.0) sbScaled +] + +{ #category : #diffing } +SBDiffTabView >> diffForSelected [ + + | diffs | + diffs := self paddedDiffTexts. + + "Some blocks are only able to print themselves with a block as parent (eg block bodies). + Thats why we don't use a SBRow here" + ^ SBBlock new + changeTableLayout; + listDirection: #leftToRight; + vResizing: #shrinkWrap; + hResizing: #shrinkWrap; + addAllMorphsBack: (self namedBlocks withIndexCollect: [:aNamedBlock :i | + self buildDiffBlockFrom: aNamedBlock to: self active given: (diffs at: i)]) +] + +{ #category : #initialization } +SBDiffTabView >> initialize [ + + super initialize. + + isShowingDiff := false. +] + +{ #category : #accessing } +SBDiffTabView >> isShowingDiff [ + + ^ isShowingDiff +] + +{ #category : #accessing } +SBDiffTabView >> isShowingDiff: aBoolean [ + + isShowingDiff := aBoolean +] + +{ #category : #diffing } +SBDiffTabView >> paddedDiffTexts [ + + | diffs maxLines | + diffs := self namedBlocks collect: [:aNamedBlock | + (TextDiffBuilder + from: (self sourceStringFor: aNamedBlock) + to: (self sourceStringFor: self active)) buildDisplayPatch]. + + maxLines := (diffs collect: [:aText | aText lineCount]) max. + + ^ diffs collect: [:aText | | paddedText | + paddedText := aText. + (maxLines - aText lineCount) timesRepeat: [paddedText := paddedText, ('', Character cr)]. + paddedText] +] + +{ #category : #diffing } +SBDiffTabView >> sourceStringFor: aNamedBlock [ + + ^ aNamedBlock block isBlockBody + ifFalse: [aNamedBlock block sourceString] + ifTrue: [ (aNamedBlock block statements collect: #sourceString) + fold: [:a :b | a, Character cr, b]] +] + +{ #category : #accessing } +SBDiffTabView >> tabs [ + + ^ (self submorphNamed: #tabs) submorphs viewAllButFirstAndLast +] + +{ #category : #actions } +SBDiffTabView >> toggleDiffView [ + + self isShowingDiff: self isShowingDiff not. + self view delete. + self buildView. +] diff --git a/packages/Sandblocks-Babylonian/SBExploriants.class.st b/packages/Sandblocks-Babylonian/SBExploriants.class.st index eb8d2935..37008cae 100644 --- a/packages/Sandblocks-Babylonian/SBExploriants.class.st +++ b/packages/Sandblocks-Babylonian/SBExploriants.class.st @@ -43,13 +43,6 @@ SBExploriants >> = other [ ^ self class = other class ] -{ #category : #ui } -SBExploriants >> buildTabs [ - - super buildTabs. - (self submorphNamed: #tabs) firstSubmorph delete. -] - { #category : #initialization } SBExploriants >> initialize [ @@ -89,12 +82,6 @@ SBExploriants >> selector [ ^ nil ] -{ #category : #accessing } -SBExploriants >> tabs [ - - ^ (self submorphNamed: #tabs) submorphs allButLast -] - { #category : #actions } SBExploriants >> visualize [ diff --git a/packages/Sandblocks-Core/SBTabView.class.st b/packages/Sandblocks-Core/SBTabView.class.st index dd898345..b02c9c26 100644 --- a/packages/Sandblocks-Core/SBTabView.class.st +++ b/packages/Sandblocks-Core/SBTabView.class.st @@ -3,8 +3,7 @@ Class { #superclass : #SBBlock, #instVars : [ 'namedBlocks', - 'activeIndex', - 'isShowingDiff' + 'activeIndex' ], #category : #'Sandblocks-Core' } @@ -119,6 +118,7 @@ SBTabView >> addButton [ do: [self addTab]; makeSmall; cornerStyle: #squared; + vResizing: #spaceFill; balloonText: 'Add'; cellGap: -1.0 sbScaled; layoutInset: (4.0 @ 5.0) sbScaled @@ -142,13 +142,6 @@ SBTabView >> addTab [ self add: (self active veryDeepCopy name: self activeName, '_i') ] -{ #category : #callbacks } -SBTabView >> artefactSaved: aMethodBlock [ - - (aMethodBlock = self containingArtefact and: [self isShowingDiff]) - ifTrue: [self updateSelectedTab] -] - { #category : #ui } SBTabView >> asTabButton: aNamedBlock [ @@ -175,20 +168,6 @@ SBTabView >> blockAt: anIndex [ ^ self namedBlocks at: anIndex ] -{ #category : #diffing } -SBTabView >> buildDiffBlockFrom: aNamedBlock to: anotherNamedBlock given: aDiffText [ - - (aNamedBlock == anotherNamedBlock) ifTrue: [^ aNamedBlock block vResizing: #spaceFill]. - - ^ SBTextBubble multiLine - vResizing: #spaceFill; - contents: aDiffText, - (Text fromString: - '', Character cr, - '==========', Character cr, - (aNamedBlock name)) -] - { #category : #ui } SBTabView >> buildTabs [ @@ -196,7 +175,6 @@ SBTabView >> buildTabs [ addAllMorphsBack: (self namedBlocks collect: [:block | self asTabButton: block]); name: #tabs; addMorphBack: self addButton; - addMorphFront: self diffButton; changeTableLayout; listDirection: #leftToRight; hResizing: #shrinkWrap) @@ -205,10 +183,7 @@ SBTabView >> buildTabs [ { #category : #ui } SBTabView >> buildView [ - self addMorphBack: ((self isShowingDiff - ifTrue: [self diffForSelected] - ifFalse: [self activeBlock]) - hResizing: #spaceFill) + self addMorphBack: (self activeBlock hResizing: #spaceFill) ] { #category : #ui } @@ -228,36 +203,6 @@ SBTabView >> deleteButtonFor: aNamedBlock [ ^ delete ] -{ #category : #ui } -SBTabView >> diffButton [ - - ^ SBButton new - icon: (SBIcon iconCodeFork size: 12.0 sbScaled) - do: [self toggleDiffView]; - makeSmall; - balloonText: 'Toggle diff to others'; - cornerStyle: #squared; - cellGap: -1.0 sbScaled; - layoutInset: (4.0 @ 3.0) sbScaled -] - -{ #category : #diffing } -SBTabView >> diffForSelected [ - - | diffs | - diffs := self paddedDiffTexts. - - "Some blocks are only able to print themselves with a block as parent (eg block bodies). - Thats why we don't use a SBRow here" - ^ SBBlock new - changeTableLayout; - listDirection: #leftToRight; - vResizing: #shrinkWrap; - hResizing: #shrinkWrap; - addAllMorphsBack: (self namedBlocks withIndexCollect: [:aNamedBlock :i | - self buildDiffBlockFrom: aNamedBlock to: self active given: (diffs at: i)]) -] - { #category : #initialization } SBTabView >> initialize [ @@ -265,7 +210,6 @@ SBTabView >> initialize [ namedBlocks := {SBNamedBlock new} asOrderedCollection. activeIndex := 1. - isShowingDiff := false. self changeTableLayout; @@ -274,18 +218,6 @@ SBTabView >> initialize [ vResizing: #shrinkWrap. ] -{ #category : #accessing } -SBTabView >> isShowingDiff [ - - ^ isShowingDiff -] - -{ #category : #accessing } -SBTabView >> isShowingDiff: aBoolean [ - - isShowingDiff := aBoolean -] - { #category : #'shortcut-utility' } SBTabView >> jumpToEightTab [ @@ -402,23 +334,6 @@ SBTabView >> namedBlocks: aCollectionOfSBNamedBlocks activeIndex: aNumber [ self rebuild ] -{ #category : #diffing } -SBTabView >> paddedDiffTexts [ - - | diffs maxLines | - diffs := self namedBlocks collect: [:aNamedBlock | - (TextDiffBuilder - from: (self sourceStringFor: aNamedBlock) - to: (self sourceStringFor: self active)) buildDisplayPatch]. - - maxLines := (diffs collect: [:aText | aText lineCount]) max. - - ^ diffs collect: [:aText | | paddedText | - paddedText := aText. - (maxLines - aText lineCount) timesRepeat: [paddedText := paddedText, ('', Character cr)]. - paddedText] -] - { #category : #ui } SBTabView >> rebuild [ @@ -468,15 +383,6 @@ SBTabView >> setActive: aNamedBlock [ (self switchCommandFor: (self namedBlocks indexOf: aNamedBlock ifAbsent: 1)) ] -{ #category : #diffing } -SBTabView >> sourceStringFor: aNamedBlock [ - - ^ aNamedBlock block isBlockBody - ifFalse: [aNamedBlock block sourceString] - ifTrue: [ (aNamedBlock block statements collect: #sourceString) - fold: [:a :b | a, Character cr, b]] -] - { #category : #commands } SBTabView >> switchCommandFor: aNumber [ @@ -507,15 +413,7 @@ SBTabView >> tabCount [ { #category : #accessing } SBTabView >> tabs [ - ^ (self submorphNamed: #tabs) submorphs viewAllButFirstAndLast -] - -{ #category : #actions } -SBTabView >> toggleDiffView [ - - self isShowingDiff: self isShowingDiff not. - self view delete. - self buildView. + ^ (self submorphNamed: #tabs) submorphs ] { #category : #ui } diff --git a/packages/Sandblocks-Smalltalk/SBVariant.class.st b/packages/Sandblocks-Smalltalk/SBVariant.class.st index f6320055..2f90e72c 100644 --- a/packages/Sandblocks-Smalltalk/SBVariant.class.st +++ b/packages/Sandblocks-Smalltalk/SBVariant.class.st @@ -199,7 +199,7 @@ SBVariant >> initialize [ emphasis: TextEmphasis italic; small); contents: 'Variant X'. - self widget: (SBTabView + self widget: (SBDiffTabView namedBlocks: {SBNamedBlock block: (SBStBlockBody emptyWithDeclarations: {'a'. 'c'}) named: 'Code'} activeIndex: 1). id := UUID new asString. From 334bcfaa671248b9f53c4bc2716beb9f3631a770 Mon Sep 17 00:00:00 2001 From: Joana Bergsiek Date: Fri, 24 Nov 2023 13:26:20 +0100 Subject: [PATCH 2/2] ignores add button in tabs --- packages/Sandblocks-Core/SBTabView.class.st | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/Sandblocks-Core/SBTabView.class.st b/packages/Sandblocks-Core/SBTabView.class.st index b02c9c26..a299ca6f 100644 --- a/packages/Sandblocks-Core/SBTabView.class.st +++ b/packages/Sandblocks-Core/SBTabView.class.st @@ -413,7 +413,7 @@ SBTabView >> tabCount [ { #category : #accessing } SBTabView >> tabs [ - ^ (self submorphNamed: #tabs) submorphs + ^ (self submorphNamed: #tabs) submorphs allButLast ] { #category : #ui }