Skip to content

Instantly share code, notes, and snippets.

@peteruhnak
Last active February 27, 2018 11:26
Show Gist options
  • Save peteruhnak/5082cbba8a22551bf0c70738de5107e9 to your computer and use it in GitHub Desktop.
Save peteruhnak/5082cbba8a22551bf0c70738de5107e9 to your computer and use it in GitHub Desktop.
Calypso XML tool

Tool build is based on XMLNode>>gtInspectorSourceIn:inContext:, so basically it shows a Glamour presentation.

dependencies: XMLParser must be installed.

Installation

CodeImporter evaluateString: 'https://gist.githubusercontent.com/peteruhnak/5082cbba8a22551bf0c70738de5107e9/raw/MyClyGlmXmlEditorTool.st' asZnUrl retrieveContents.

I've tried using ClyTextEditorTool as a base, but couldn't figure out how to color it, as ClyTextEditorTool uses shout highlighting, whilst Glamour uses RubParagraphDecorator-based highlighting.

  1. editing the method itself doesn't update the XML tool
  2. the XML tool contents is not updated after saving (saving X will produce reformatted X, but the editor won't show the reformatted veresion)
'From Pharo6.0 of 13 May 2016 [Latest update: #60530] on 21 February 2018 at 9:43:37.179327 am'!
ClyBrowserTool subclass: #MyClyGlmXmlEditorTool
instanceVariableNames: 'editingMethod'
classVariableNames: ''
poolDictionaries: ''
category: 'Cly-Editors'!
!MyClyGlmXmlEditorTool commentStamp: 'PeterUhnak 2/21/2018 09:43' prior: 0!
I am a Calypso tool for editing XML method containing <resource: 'xml'> pragma.
Select #exampleXml method in me.!
!MyClyGlmXmlEditorTool methodsFor: 'building' stamp: 'PeterUhnak 2/21/2018 09:34'!
acceptChanges: aString
| node newNode newContent |
editingMethod ifNil: [ ^ false ].
newContent := aString parseXML removeAllFormattingNodes prettyPrinted.
node := self editingNode.
newNode := RBParser parseExpression: newContent printString.
node replaceWith: newNode.
editingMethod methodClass compile: node methodNode formattedCode.
^ true! !
!MyClyGlmXmlEditorTool methodsFor: 'building' stamp: 'PeterUhnak 2/21/2018 09:34'!
build
| composite presentation morph currentTextStylerDecorator customPresentation |
composite := GLMCompositePresentation new.
customPresentation := GLMRubricHighlightedTextPresentation new
editingMode: [ currentTextStylerDecorator := GLMXMLDocumentHighlighterTextStylerDecorator new.
GLMHighlighterTextRubEditingMode withStylerDecorator: currentTextStylerDecorator ];
act: [ self acceptChanges: currentTextStylerDecorator text asString.
customPresentation clearUserEdits ]
icon: GLMUIThemeExtraIcons glamorousAccept
on: $s
entitled: 'Accept'.
presentation := composite custom: customPresentation.
presentation display: [ self editingText ].
morph := GLMMorphicRenderer new render: presentation.
morph hResizing: #spaceFill.
morph vResizing: #spaceFill.
self addMorph: morph! !
!MyClyGlmXmlEditorTool methodsFor: 'initialization' stamp: 'PeterUhnak 2/21/2018 09:33'!
setUpModelFromContext
editingMethod := context lastSelectedMethod! !
!MyClyGlmXmlEditorTool methodsFor: 'initialization' stamp: 'PeterUhnak 2/20/2018 22:12'!
defaultIconName
^ #smallLeftFlush! !
!MyClyGlmXmlEditorTool methodsFor: 'initialization' stamp: 'PeterUhnak 2/21/2018 09:33'!
editedMethod
^ editingMethod! !
!MyClyGlmXmlEditorTool methodsFor: 'initialization' stamp: 'PeterUhnak 2/20/2018 22:26'!
defaultTitle
^ 'XML'! !
!MyClyGlmXmlEditorTool methodsFor: 'examples' stamp: 'PeterUhnak 2/21/2018 09:25'!
exampleXml
<resource: 'xml'>
^ '<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmlns:uml="http://www.omg.org/spec/UML/20110701" xmlns:xmi="http://www.omg.org/spec/XMI/20110701">
<uml:Package xmi:type="uml:Package" xmi:id="123" name="Package">
<packagedElement xmi:type="uml:Class" xmi:id="456" name="Class" />
</uml:Package>
</xmi:XMI>'! !
!MyClyGlmXmlEditorTool methodsFor: 'accessing' stamp: 'PeterUhnak 2/21/2018 09:33'!
editingNode
^ editingMethod parseTree allChildren reversed detect: #isLiteralNode! !
!MyClyGlmXmlEditorTool methodsFor: 'accessing' stamp: 'PeterUhnak 2/20/2018 22:14'!
editingText
^ self editingNode sourceText trimBoth: [ :c | c = $' ]! !
!MyClyGlmXmlEditorTool methodsFor: 'testing' stamp: 'PeterUhnak 2/21/2018 09:33'!
isSimilarTo: anotherBrowserTool
(super isSimilarTo: anotherBrowserTool)
ifFalse: [ ^ false ].
^ editingMethod methodClass == anotherBrowserTool editedMethod methodClass
and: [ editingMethod selector == anotherBrowserTool editedMethod selector ]! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
MyClyGlmXmlEditorTool class
slots: { }!
!MyClyGlmXmlEditorTool class methodsFor: 'activation' stamp: 'PeterUhnak 2/21/2018 09:25'!
shouldBeActivatedInContext: aBrowserContext
^ aBrowserContext isMethodSelected
and: [ aBrowserContext lastSelectedMethod pragmas
anySatisfy: [ :each | each keyword = 'resource:' and: [ each arguments = #(xml) ] ] ]! !
!MyClyGlmXmlEditorTool class methodsFor: 'activation' stamp: 'PeterUhnak 2/20/2018 22:11'!
methodTabActivation
<classAnnotation>
^ClyTabActivationStrategy for: ClyMethod asCalypsoItemContext! !
!MyClyGlmXmlEditorTool class methodsFor: 'activation' stamp: 'PeterUhnak 2/20/2018 22:11'!
tabOrder
^ 100! !
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment