SmallSense__JavaEditSupport.st
changeset 134 e34ee6ceb7c8
parent 133 bd659b67811c
child 139 bf1538a4e7ce
--- a/SmallSense__JavaEditSupport.st	Sun Oct 20 03:10:44 2013 +0100
+++ b/SmallSense__JavaEditSupport.st	Tue Oct 22 03:29:26 2013 +0100
@@ -35,6 +35,83 @@
 
     "Created: / 03-10-2013 / 17:45:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
     "Modified: / 20-10-2013 / 02:53:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+scannerClass
+    "Returns a class to use for scanning lines. If nil, scanning is
+     not supported and scanLine* methods will return an empty array."
+
+    ^ (Smalltalk at: #JavaScanner)
+
+    "Created: / 22-10-2013 / 00:38:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaEditSupport methodsFor:'editing'!
+
+insertElectricSnippet
+    | tokens lastToken0 |
+
+    tokens := self scanLineAtCursor.
+    tokens isEmptyOrNil ifTrue:[ ^ false ].
+    lastToken0 := tokens at: (tokens size - 3).
+    lastToken0 = 'Error' ifTrue:[ ^ false ].
+    (tokens last > service textView cursorCol) ifTrue:[ ^ false ].
+
+    "/ Insert try { ... }
+    lastToken0 == #try ifTrue:[
+        (tokens size == 4 or:[(tokens size > 4) and:[(tokens at: tokens size -7) ~~ $.]]) ifTrue:[
+            self insertElectricBlockOpenedBy: ' {' closedBy: '}'. 
+            ^ true.
+        ]
+    ].
+
+    "/ Insert catch (  ) { ... }
+    lastToken0 == #catch ifTrue:[
+        (tokens size > 4 and:[(tokens at: tokens size -7) == $}]) ifTrue:[
+            | cursorLine cursorCol |
+            cursorLine := service textView cursorLine.
+            cursorCol := service textView cursorCol.
+            self insertElectricBlockOpenedBy: ' (  ) {' closedBy: '}'. 
+            service textView cursorLine: cursorLine  col: cursorCol + 3.
+            ^ true.
+        ].
+    ].
+
+    "/ Insert finally { ... }
+    lastToken0 == #finally ifTrue:[
+        (tokens size > 4 and:[(tokens at: tokens size -7) == $}]) ifTrue:[
+            self insertElectricBlockOpenedBy: ' {' closedBy: '}'. 
+            ^ true.
+        ].
+    ].
+
+    "/ Insert if/while/synchronized ( )  { ... }
+    (#(if while synchronized) includes: lastToken0) ifTrue:[
+        (tokens size = 4) ifTrue:[
+            | cursorLine cursorCol |
+            cursorLine := service textView cursorLine.
+            cursorCol := service textView cursorCol.
+            self insertElectricBlockOpenedBy: ' (  ) {' closedBy: '}'. 
+            service textView cursorLine: cursorLine  col: cursorCol + 3.
+            ^ true.
+        ].
+    ].
+
+    "/ Insert for ( ; ; )  { ... }
+    (lastToken0 == #for) ifTrue:[
+        (tokens size = 4) ifTrue:[
+            | cursorLine cursorCol |
+            cursorLine := service textView cursorLine.
+            cursorCol := service textView cursorCol.
+            self insertElectricBlockOpenedBy: ' ( ; ; ) {' closedBy: '}'. 
+            service textView cursorLine: cursorLine  col: cursorCol + 3.
+            ^ true.
+        ].
+    ].
+
+    ^ false
+
+    "Created: / 22-10-2013 / 01:53:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !JavaEditSupport methodsFor:'event handling'!
@@ -59,31 +136,44 @@
     ^ 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>"
+    "Modified: / 22-10-2013 / 01:55:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 keyPressOpenCurly
-    | line token i |
+    | line tokens 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.
-        ].
+            ^ false.        ].
     ] ifFalse:[
         self insertElectricBlockOpenedBy: '{' closedBy: '}'. 
         ^ true
     ].
 
-    token := self tokenAtCursorLine.
-    (token isNil or:[token == #String]) ifTrue:[ ^ false ].
+    tokens := self scanLineAtCursor.
+    tokens notEmptyOrNil ifTrue:[
+        | column |
+
+        column := service textView cursorCol.
+        (tokens at: tokens size - 3) = 'Error' ifTrue:[ ^ false ].
+        1 to: tokens size - 3 by: 4 do:[:i |
+            (column between: (tokens at: i + 2) and: (tokens at: i + 3)) ifTrue:[
+                (tokens at: i) == #String ifTrue:[ ^ false ].
+
+                self insertElectricBlockOpenedBy: '{' closedBy: '}'. 
+                ^ true.
+            ].
+        ].
+    ].
 
     self insertElectricBlockOpenedBy: '{' closedBy: '}'. 
     ^ true
 
     "Created: / 04-08-2013 / 01:54:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 22-10-2013 / 00:55:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !JavaEditSupport methodsFor:'initialization'!