Skip to content

Commit

Permalink
Moves diffing mechanics to new subclass
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeAtHPI committed Nov 24, 2023
1 parent a0a3b16 commit 1d2e507
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 120 deletions.
141 changes: 141 additions & 0 deletions packages/Sandblocks-Babylonian/SBDiffTabView.class.st
Original file line number Diff line number Diff line change
@@ -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.
]
13 changes: 0 additions & 13 deletions packages/Sandblocks-Babylonian/SBExploriants.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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 [

Expand Down Expand Up @@ -89,12 +82,6 @@ SBExploriants >> selector [
^ nil
]

{ #category : #accessing }
SBExploriants >> tabs [

^ (self submorphNamed: #tabs) submorphs allButLast
]

{ #category : #actions }
SBExploriants >> visualize [

Expand Down
110 changes: 4 additions & 106 deletions packages/Sandblocks-Core/SBTabView.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ Class {
#superclass : #SBBlock,
#instVars : [
'namedBlocks',
'activeIndex',
'isShowingDiff'
'activeIndex'
],
#category : #'Sandblocks-Core'
}
Expand Down Expand Up @@ -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
Expand All @@ -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 [

Expand All @@ -175,28 +168,13 @@ 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 [

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)
Expand All @@ -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 }
Expand All @@ -228,44 +203,13 @@ 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 [

super initialize.

namedBlocks := {SBNamedBlock new} asOrderedCollection.
activeIndex := 1.
isShowingDiff := false.

self
changeTableLayout;
Expand All @@ -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 [

Expand Down Expand Up @@ -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 [

Expand Down Expand Up @@ -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 [

Expand Down Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion packages/Sandblocks-Smalltalk/SBVariant.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 1d2e507

Please sign in to comment.