SmallSense__JavaEditSupport.st
changeset 64 2257d7223898
child 67 020b7461b15e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SmallSense__JavaEditSupport.st	Sat Aug 24 22:15:09 2013 +0100
@@ -0,0 +1,104 @@
+"{ Package: 'jv:smallsense' }"
+
+"{ NameSpace: SmallSense }"
+
+EditSupport subclass:#JavaEditSupport
+	instanceVariableNames:'lastTypedKey0 lastTypedKey1 lastTypedKey2 lastTypedKey3'
+	classVariableNames:''
+	poolDictionaries:''
+	category:'SmallSense-Services'
+!
+
+!JavaEditSupport methodsFor:'accessing'!
+
+language
+    "superclass SmallSenseEditSupport says that I am responsible to implement this method"
+
+    ^ (Smalltalk at:#JavaLanguage) instance.
+
+    "Modified: / 04-08-2013 / 02:07:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaEditSupport methodsFor:'event handling'!
+
+keyPress: key x:x y:y in: view
+
+    "Handles an event in given view (a subview of codeView).
+     If the method returns true, the event will not be processed
+     by the view."
+
+    view ~~ textView ifTrue:[ ^ false ].
+
+    lastTypedKey3 := lastTypedKey2.
+    lastTypedKey2 := lastTypedKey1.
+    lastTypedKey1 := lastTypedKey0.
+    lastTypedKey0 := key.
+
+    key == ${ ifTrue:[
+        ^ self keyPressOpenCurly
+    ].
+
+    ^ super keyPress: key x:x y:y in: view
+
+    "Created: / 07-03-2010 / 09:36:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 04-08-2013 / 03:06:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+keyPressOpenCurly
+    | line token i |
+
+    line := service codeView listAt: service codeView cursorLine.
+    line notEmptyOrNil ifTrue:[
+        i := line size.
+        [ (line at: i) isSeparator and:[i > 0] ] whileTrue:[ i := i - 1 ].
+        (i ~~ 0 and:[service codeView cursorCol < i]) ifTrue:[
+            ^ false.
+        ].
+    ] ifFalse:[
+        self insertElectricBlockOpenedBy: '{' closedBy: '}'. 
+        ^ true
+    ].
+
+    token := self tokenAtCursorLine.
+    (token isNil or:[token == #String]) ifTrue:[ ^ false ].
+
+    self insertElectricBlockOpenedBy: '{' closedBy: '}'. 
+    ^ true
+
+    "Created: / 04-08-2013 / 01:54:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaEditSupport methodsFor:'private'!
+
+tokenAtCursorLine
+    | scanner token |
+
+    scanner := (Smalltalk at:#JavaScanner) for: (service textView listAt: service textView cursorLine) string.
+
+    [ 
+        [ 
+            token := scanner nextToken.
+            (token ~~ #EOF and:[ scanner tokenEndPosition + 1 < service textView cursorCol ])
+        ] whileTrue.
+    ] on: Error do:[
+        token := nil.
+    ].
+    ^ token
+
+    "Created: / 04-08-2013 / 02:00:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 04-08-2013 / 03:10:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+tokensAtCursorLine
+    | scanner token |
+
+    scanner := (Smalltalk at:#JavaScanner) for: (service textView listAt: service textView cursorLine) string.
+    ^ OrderedCollection streamContents:[:tokens |
+        [ token := scanner nextToken.token ~~ #EOF ] whileTrue:[
+            tokens nextPut: token.
+        ].
+    ].
+
+    "Created: / 04-08-2013 / 01:57:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+