Skip to content

Commit

Permalink
improve updating of the example snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
syrel committed Jun 8, 2021
1 parent 528aec8 commit 274e6b6
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/Lepiter-Snippet-Example/LeExampleSnippet.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ LeExampleSnippet >> contentAsString [
^ '{{gtExample:', self asAnnotationString, '}}'
]

{ #category : #'api - accessing' }
LeExampleSnippet >> copyFrom: aLeExampleSnippet [
self codeExpanded: aLeExampleSnippet codeExpanded.
self exampleBehaviorName: aLeExampleSnippet exampleBehaviorName.
self exampleSelector: aLeExampleSnippet exampleSelector.
self noCode: aLeExampleSnippet noCode.
self previewExpanded: aLeExampleSnippet previewExpanded.
self previewHeight: aLeExampleSnippet previewHeight.
self previewShowSelector: aLeExampleSnippet previewShowSelector.
]

{ #category : #initialization }
LeExampleSnippet >> defaultCodeExpanded [
^ true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Class {
#name : #LeExampleSnippetAnnotationValidator,
#superclass : #Object,
#traits : 'TLeAnnotationParseNodeVisitor',
#classTraits : 'TLeAnnotationParseNodeVisitor classTrait',
#instVars : [
'isValid'
],
#category : #'Lepiter-Snippet-Example-Snippet'
}

{ #category : #initialization }
LeExampleSnippetAnnotationValidator >> initialize [
super initialize.

isValid := true
]

{ #category : #accessing }
LeExampleSnippetAnnotationValidator >> isValid [
^ isValid
]

{ #category : #accessing }
LeExampleSnippetAnnotationValidator >> validate: anAst [
isValid := true.
self accept: anAst.
^ self isValid
]

{ #category : #visiting }
LeExampleSnippetAnnotationValidator >> visitCodeExpandedArgument: aCodeExpandedArgument [
isValid := isValid and: [ aCodeExpandedArgument hasValidBooleanValue ]
]

{ #category : #generated }
LeExampleSnippetAnnotationValidator >> visitNoCodeArgument: aNoCodeArgument [
isValid := isValid and: [ aNoCodeArgument hasValidBooleanValue ]
]

{ #category : #generated }
LeExampleSnippetAnnotationValidator >> visitPreviewExpandedArgument: aPreviewExpandedArgument [
isValid := isValid and: [ aPreviewExpandedArgument hasValidBooleanValue ]
]
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ LeExampleSnippetAnnotationVisitor >> visitNoCodeArgument: aNoCodeArgument [
LeExampleSnippetAnnotationVisitor >> visitPreviewExpandedArgument: aPreviewExpandedArgument [
aPreviewExpandedArgument hasValidBooleanValue
ifFalse: [ ^ self ].

self exampleSnippet previewExpanded: aPreviewExpandedArgument booleanValue
]

Expand Down
22 changes: 17 additions & 5 deletions src/Lepiter-Snippet-Example/LeExampleSnippetViewModel.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -200,23 +200,35 @@ LeExampleSnippetViewModel >> notifyAnnotationCoderViewModelChanged [

{ #category : #'private - event handling' }
LeExampleSnippetViewModel >> onAnnotationAstChanged: anAstChangedAnnouncement [
| anAst |
| anAst aNewSnippet |

anAst := anAstChangedAnnouncement ast.
anAst
allNodesDo: [ :eachNode | eachNode isErrorNode ifTrue: [ ^ self ] ]
andTokensDo: [ :eachToken | ]
includeErrors: true.

aNewSnippet := LeExampleSnippet new.

"this will modify the properties of a default snippet while making sure that removing properties falls back to default"
LeExampleSnippetAnnotationVisitor new
exampleSnippet: aNewSnippet;
accept: anAst.

"if properties did not change, do nothing"
(self snippetModel similarTo: aNewSnippet)
ifTrue: [ ^ self ].

"it is possible that multiple parameters were changes as part of the ast update.
we should queue them up and request annotation update only once at the very end"
self optionAt: #doNotUpdateText put: true.
[
LeExampleSnippetAnnotationVisitor new
exampleSnippet: self;
accept: anAst.
self snippetModel copyFrom: aNewSnippet
] ensure: [ self optionAt: #doNotUpdateText put: false ].


(LeExampleSnippetAnnotationValidator new validate: anAst)
ifFalse: [ ^ self ].

self requestUpdateAnnotationText
]

Expand Down

0 comments on commit 274e6b6

Please sign in to comment.