Tools__CodeHighlightingService.st
changeset 18040 8f6de437a2df
parent 17716 406658241c77
child 18042 3fecdf0454b7
equal deleted inserted replaced
18039:1b9aabb9f18a 18040:8f6de437a2df
       
     1 "{ Encoding: utf8 }"
       
     2 
     1 "
     3 "
     2  COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague
     4  COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague
     3               All Rights Reserved
     5               All Rights Reserved
     4 
     6 
     5 Permission is hereby granted, free of charge, to any person
     7 Permission is hereby granted, free of charge, to any person
    61 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    63 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    62 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    64 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    63 OTHER DEALINGS IN THE SOFTWARE.
    65 OTHER DEALINGS IN THE SOFTWARE.
    64 "
    66 "
    65 ! !
    67 ! !
    66 
       
    67 
    68 
    68 !CodeHighlightingService class methodsFor:'accessing'!
    69 !CodeHighlightingService class methodsFor:'accessing'!
    69 
    70 
    70 label
    71 label
    71     "Answers a short label - for UI"
    72     "Answers a short label - for UI"
   209     "Modified: / 26-09-2011 / 15:40:23 / cg"
   210     "Modified: / 26-09-2011 / 15:40:23 / cg"
   210     "Created: / 24-01-2012 / 12:11:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   211     "Created: / 24-01-2012 / 12:11:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   211 !
   212 !
   212 
   213 
   213 process: delayed
   214 process: delayed
   214     |oldCode oldCodeList newCode elements cls mthd highlighterClasses|
   215     |oldCode oldCodeList oldCodeWithoutEmphasis newCode elements cls mthd highlighterClasses|
   215 
   216 
   216     "/ codeView topView class == DebugView ifTrue:[^ self].
   217     "/ codeView topView class == DebugView ifTrue:[^ self].
   217 
   218 
   218     done := false.
   219     done := false.
   219     modified := false.
   220     modified := false.
   230         ] ifFalse:[
   231         ] ifFalse:[
   231             cls := Smalltalk at:cls name
   232             cls := Smalltalk at:cls name
   232         ].
   233         ].
   233     ].
   234     ].
   234     mthd := codeView method.
   235     mthd := codeView method.
       
   236     mthd isNil ifTrue:[
       
   237         cls isNil ifTrue:[
       
   238             self halt.
       
   239             ^ self.
       
   240         ]
       
   241     ].
   235 
   242 
   236     "textView" modified ifFalse:[
   243     "textView" modified ifFalse:[
   237         "/ bad bad bad: textView's list may change, while we copy!!!!!!!!!!
   244         "/ bad bad bad: textView's list may change, while we copy!!!!!!!!!!
   238         [
   245         [
   239             oldCodeList := textView list copy.
   246             oldCodeList := textView list copy.
   240         ] valueUninterruptably.
   247         ] valueUninterruptably.
   241 
   248 
   242         "textView" modified ifFalse:[
   249         "textView" modified ifFalse:[
   243             oldCodeList isNil ifFalse:[
   250             oldCodeList isNil ifFalse:[
   244                 oldCode := oldCodeList asStringCollection asStringWithoutEmphasis.
   251                 oldCode := oldCodeList asStringCollection.
       
   252                 oldCodeWithoutEmphasis := oldCode asStringWithoutEmphasis.
   245                 "textView" modified ifFalse:[
   253                 "textView" modified ifFalse:[
   246                     Screen currentScreenQuerySignal answer:codeView device
   254                     Screen currentScreenQuerySignal answer:codeView device
   247                     do:[
   255                     do:[
   248                         "/ cg: not a good idea to handle it here;
   256                         "/ cg: not a good idea to handle it here;
   249                         "/ Syntaxhighlighter does it also, and will generate a better colorized string
   257                         "/ Syntaxhighlighter does it also, and will generate a better colorized string
   259 "/                            newCode := nil.
   267 "/                            newCode := nil.
   260 "/                        ] do:[
   268 "/                        ] do:[
   261                             | codeAspect |
   269                             | codeAspect |
   262                             
   270                             
   263                             elements := ParseTreeIndex new.
   271                             elements := ParseTreeIndex new.
   264                             newCode := oldCode asText.
   272                             newCode := oldCodeWithoutEmphasis asText.
   265                             codeAspect := codeView codeAspect.
   273                             codeAspect := codeView codeAspect.
   266                             codeAspect == SyntaxHighlighter codeAspectMethod ifTrue:[
   274                             codeAspect == SyntaxHighlighter codeAspectMethod ifTrue:[
   267                                 highlighterClasses do:[:e|newCode := e formatMethod:mthd source:newCode in:cls using:syntaxPreferences elementsInto: elements].
   275                                 highlighterClasses do:[:e|newCode := e formatMethod:mthd source:newCode in:cls using:syntaxPreferences elementsInto: elements].
   268                             ] ifFalse:[codeAspect == SyntaxHighlighter codeAspectExpression ifTrue:[
   276                             ] ifFalse:[codeAspect == SyntaxHighlighter codeAspectExpression ifTrue:[
   269                                 highlighterClasses do:[:e|newCode := e formatExpression:newCode in:cls elementsInto: elements].
   277                                 highlighterClasses do:[:e|newCode := e formatExpression:newCode in:cls elementsInto: elements].
   276                             ]]]]].
   284                             ]]]]].
   277 "/                        ]
   285 "/                        ]
   278                     ].
   286                     ].
   279                     newCode notNil ifTrue:[
   287                     newCode notNil ifTrue:[
   280                         "textView" modified ifFalse:[
   288                         "textView" modified ifFalse:[
   281                             newCode ~= oldCodeList ifTrue:[
   289                             newCode ~= oldCode ifTrue:[
   282                                 newCode := newCode asStringCollection.
   290                                 newCode := newCode asStringCollection.
   283                                 "textView" modified ifFalse:[
   291                                 "textView" modified ifFalse:[
   284                                     done := true.
   292                                     done := true.
   285                                     textView notNil ifTrue:[
   293                                     textView notNil ifTrue:[
   286                                         "/ must add this event - and not been interrupted
   294                                         "/ must add this event - and not been interrupted
   339 
   347 
   340     firstShown := textView firstLineShown.
   348     firstShown := textView firstLineShown.
   341     lastShown := textView lastLineShown.
   349     lastShown := textView lastLineShown.
   342 
   350 
   343     replaceAction := [:lNr :line |
   351     replaceAction := [:lNr :line |
   344             |oldLine|
   352             |oldLine nonSepIndex charEmphasis|
   345 
   353 
   346             oldLine :=  list at:lNr ifAbsent:nil.
   354             oldLine :=  list at:lNr ifAbsent:nil.
   347             oldLine notNil ifTrue:[
   355             oldLine notNil ifTrue:[
   348                 line notNil ifTrue:[
   356                 line notNil ifTrue:[
   349                     "/ this check is needed - there is a race
   357                     "/ this check is needed - there is a race
   350                     "/ when the text is converted. This detects the
   358                     "/ when the text is converted. This detects the
   351                     "/ resulting error.
   359                     "/ resulting error.
   352                     "/ Certainly a kludge.
   360                     "/ Certainly a kludge.
   353 
   361 
   354                     oldLine string = line string ifTrue:[
   362                     oldLine string = line string ifTrue:[
   355                         | i |
       
   356 
       
   357                         "JV@2012-02-01: Remove any emphasis on leading whitespace"
   363                         "JV@2012-02-01: Remove any emphasis on leading whitespace"
   358                         "(presumably created by LintHighlighter)"
   364                         "(presumably created by LintHighlighter)"
   359                         i := line string indexOfNonSeparator.
   365                         nonSepIndex := line string indexOfNonSeparator.
   360                         i > 1 ifTrue:[
   366                         nonSepIndex > 1 ifTrue:[
   361                             | e |
   367                             (charEmphasis := (line emphasisAt:nonSepIndex - 1)) notNil ifTrue:[
   362 
   368                                 line emphasisFrom:1 to:(nonSepIndex - 1) remove:charEmphasis.
   363                             (e := (line emphasisAt: i - 1)) notNil ifTrue:[
       
   364                                 line emphasisFrom: 1 to: i - 1 remove: e.
       
   365                             ]
   369                             ]
   366                         ].
   370                         ].
   367 
   371 
   368                         oldLine emphasis ~= line emphasis ifTrue:[
   372                         oldLine emphasis ~= line emphasis ifTrue:[
   369                             textView modifiedChannel removeDependent:self.
   373                             textView modifiedChannel removeDependent:self.