tools/JavaLintService.st
branchdevelopment
changeset 2734 f56049613ff3
child 2735 e20dd8496371
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/JavaLintService.st	Mon Sep 16 14:09:52 2013 +0100
@@ -0,0 +1,351 @@
+"
+ COPYRIGHT (c) 2006 by eXept Software AG
+	      All Rights Reserved
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+"
+"{ Package: 'stx:libjava/tools' }"
+
+Tools::BackgroundSourceProcessingService subclass:#JavaLintService
+	instanceVariableNames:'highlighter problems'
+	classVariableNames:'Debugging'
+	poolDictionaries:''
+	category:'Languages-Java-Tools-Editor-Lint'
+!
+
+!JavaLintService class methodsFor:'documentation'!
+
+copyright
+"
+ COPYRIGHT (c) 2006 by eXept Software AG
+	      All Rights Reserved
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+"
+! !
+
+!JavaLintService class methodsFor:'initialization'!
+
+initialize
+
+    "
+    | map |
+    map := Screen current keyboardMap.
+    map bindValue:#CodeCompletion to: #'Ctrl '.
+    map bindValue:#CodeCompletion to: #'Ctrlspace'.
+    "
+
+    Smalltalk isInitialized ifTrue:[
+        Smalltalk addStartBlock:[
+            Screen current notNil ifTrue:[
+                | map |
+                map := Screen current keyboardMap.
+                (map keyAtValue:#CodeCompletion) isNil ifTrue:[
+                    map bindValue:#CodeCompletion to: #'Ctrl '.
+                    map bindValue:#CodeCompletion to: #'Ctrlspace'.
+                ].
+           ]
+        ]
+    ]
+
+    "Modified: / 15-08-2013 / 15:19:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaLintService class methodsFor:'accessing'!
+
+debugging
+    ^Debugging == true
+
+    "Created: / 17-02-2012 / 00:48:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+debugging: aBoolean
+
+    Debugging := aBoolean
+
+    "Created: / 16-02-2012 / 16:22:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+label
+
+    "Answers short label - for UI"
+
+    ^'Java - Static Checking'
+
+    "Created: / 07-03-2010 / 14:00:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 16-09-2013 / 10:01:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+priority
+    "Answers a priority of the service. Services with higher priority
+     will get the event notification before ones with lower priority.
+     Therefore, a lower-priority service might not get the event if high
+     priority service processes it"
+
+    ^ 10
+
+    "Created: / 01-02-2012 / 10:29:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaLintService class methodsFor:'queries'!
+
+isUsefulFor:aCodeView
+    "this filters useful services.
+     must be redefined to return true in subclasses (but each class must do it only
+     for itself - not for subclasses"
+
+    ^ self == JavaLintService
+
+    "Created: / 24-07-2013 / 11:35:59 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 16-09-2013 / 09:56:34 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaLintService class methodsFor:'testing'!
+
+isAvailable
+
+    ^UserPreferences current smallSenseEnabled
+
+    "Created: / 04-02-2012 / 22:20:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaLintService methodsFor:'accessing'!
+
+syntaxHighlighter
+    "Returns a syntax highligter class or nil. The highlighting
+     process gather all syntaxHighlighterClasses from all services
+     and then use them one by one to highlight the text. Individual
+     services may override this method to provide additional
+     highliging of the source code"
+
+    | lang |
+
+    JavaCompiler isNil ifTrue: [
+        ^ nil
+    ].
+    lang := codeView language.
+    ^ (lang notNil and: [ lang isJava and:[ problems notEmptyOrNil ]])
+        ifTrue: [ highlighter ]
+        ifFalse: [ nil ]
+
+    "Created: / 05-08-2011 / 10:59:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 16-09-2013 / 13:33:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaLintService methodsFor:'change & update'!
+
+sourceChanged:force
+    | lang |
+
+    ((lang := codeView language) isNil or:[lang isJava not]) ifTrue:[ ^ self ].
+    highlighter reset.
+    super sourceChanged:force.
+
+    "Created: / 18-02-2012 / 22:50:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 16-09-2013 / 09:41:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaLintService methodsFor:'event handling'!
+
+buttonPress:button x:x y:y in:view 
+    |lineNr|
+
+    view == gutterView ifTrue:[
+        button == 1 ifTrue:[
+            lineNr := textView yVisibleToLineNr:y.
+            lineNr notNil ifTrue:[ 
+                ^ self showInfoWindowForLine: lineNr 
+            ].
+            ^ false.
+        ].
+    ].
+    ^ false
+
+    "Created: / 30-01-2012 / 21:04:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 16-09-2013 / 09:41:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaLintService methodsFor:'initialization'!
+
+initialize
+
+    super initialize.
+
+    highlighter := JavaLintHighlighter new.
+
+    "Created: / 05-08-2011 / 11:53:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 16-09-2013 / 10:13:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaLintService methodsFor:'private'!
+
+annotationAtLine: lineNo
+    | annotations |        
+
+    (annotations := highlighter annotations) isEmptyOrNil ifTrue:[ ^ nil ].
+    annotations do:[:a|
+        | line |
+
+        line := a line.    
+        line > lineNo ifTrue:[ ^ nil ].
+        line == lineNo ifTrue:[ ^ a ].
+    ].
+    ^nil
+
+    "Created: / 30-01-2012 / 21:06:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 16-09-2013 / 13:38:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+process
+    "(Re)starts the processing job. Should be called whenever a source 
+     must be (re)processed."
+
+    codeView language isNil ifTrue: [ ^ self ].
+    codeView language isJava ifFalse: [ ^ self ]. 
+
+    ^super process.
+
+    "Created: / 24-01-2012 / 12:43:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 16-09-2013 / 09:42:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+process: delayed
+    "Do the real source processing. If delayed is true, actuall data update must
+     be done within the event queue using #pushUserEvent:...
+    "
+    | cls oldCodeList oldCode |
+
+    codeView language isNil ifTrue: [ ^ self ].
+    codeView language isJava ifFalse: [ ^ self ]. 
+    done := false.
+    modified := false.
+
+"/    cls := codeView klass.
+"/        cls isNil ifTrue:[^ self ].    
+
+
+    Delay waitForMilliseconds: 200."Give user some time to write"
+
+    (cls notNil and:[cls isObsolete]) ifTrue:[
+        cls isMeta ifTrue:[
+            cls := (Smalltalk at:cls theNonMetaclass name) class
+        ] ifFalse:[
+            cls := Smalltalk at:cls name
+        ].
+    ].
+
+    "textView" modified ifFalse:[
+        oldCodeList := textView list copy.
+        oldCodeList isEmptyOrNil ifTrue: [ ^ self ].
+        "textView" modified ifFalse:[
+            oldCodeList isNil ifFalse:[
+                oldCode := oldCodeList asStringWithoutEmphasis.
+                oldCode isEmptyOrNil ifTrue:[ ^ self ].
+                "textView" modified ifFalse:[
+                    Screen currentScreenQuerySignal answer:codeView device
+                    do:[
+                        Error handle:[:ex |
+                            |errMsg|
+
+                            errMsg := ex description asStringCollection first asString.
+
+                            Debugging == true ifTrue:[
+                                Debugging := false.    
+                                ex pass.
+                            ].
+
+                            "/ Transcript topView raiseDeiconified.
+                            "/ Transcript showCR:'ParseError: ', ex description.
+"/ self halt.
+                            "/ self showInfo:(errMsg colorizeAllWith:Color red).
+                        ] do:[
+                            | compiler |
+
+                            compiler := JavaCompiler new.
+                            problems := compiler check: oldCode.
+                            highlighter problem: problems ? #()
+                        ].
+                        delayed ifTrue:[
+                            codeView sensor pushUserEvent:#rehighlight: for:self withArgument: true.
+                        ] ifFalse:[
+                            self rehighlight: true.
+                        ]
+                    ]
+                ]
+            ]
+        ]
+    ]
+
+    "Created: / 24-01-2012 / 12:44:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 16-09-2013 / 10:37:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+rehighlight: delayed
+
+    | service |
+    service := self service: #'SmallSense::SyntaxHighlightingService'.
+    service isNil ifTrue:[
+        service := self service: Tools::CodeHighlightingService name
+    ].
+    service notNil ifTrue:[
+        highlighter reset.
+        highlighter problems: problems.
+        service sourceChanged: true.
+    ]
+
+    "Created: / 27-01-2012 / 17:06:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 16-09-2013 / 13:33:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+showInfoWindowForLine: lineNo 
+    | ann |
+
+    ann := self annotationAtLine: lineNo.
+    ann isNil ifTrue: [
+        ^ false
+    ].
+    (JavaLintPopupWindow new)
+        problem: ann problem;
+        codeView: codeView;
+        allButOpen;
+        openWindowAt: (Screen current pointerPosition - (20 @ 20)).
+    ^ true
+
+    "Created: / 30-01-2012 / 21:04:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 16-09-2013 / 13:40:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaLintService methodsFor:'redrawing'!
+
+drawLine:lineNo in:view atX:x y:y width:w height:h from:startCol to:endColOrNil with:fg and:bg 
+    "Called by both gutterView and textView (well, not yet) to
+     allow services to draw custom things on text view.
+     Ask JV what the args means if unsure (I'm lazy to document
+     them, now it is just an experiment...)"
+
+    | lang annotation |
+
+    ((lang := codeView language) isNil or:[lang isJava not]) ifTrue:[ ^ self ].
+
+    annotation :=  self annotationAtLine: lineNo.
+    annotation notNil ifTrue:[
+        self drawAnnotationIcon: (ToolbarIconLibrary smalllintWarning16x16)
+                atX: x y: y  width: w height: h.
+    ].
+
+    "Created: / 30-01-2012 / 15:11:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 16-09-2013 / 13:36:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+
+JavaLintService initialize!