I'm thinking to create a simple highlighting parser for BASIC from the
existing mechanism in VisualWorks. I am not thinking to hook this into my BASIC parser. I only need to detect certain kinds of tokens such as keywords, valid variable names, numeric and string literals, etc. and give each kind its own color. I do this already in VSE for Liberty BASIC and it is sufficient for my purposes. What would be the minimal approach to implementing this in VW? Thanks for any suggestions. -Carl Gundel http://www.libertybasic.com http://www.runbasic.com _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc |
Hi Carl,
i just did a similar thing two weeks ago and it's actually quite easy. here's what you need to do: create a subclass of UI.Highlighter. Then you need to add that to the texteditorcontroller you like to have highlighting on. To do so you add that to your postBuildWith: sourceController := (aBuilder componentAt:#sourceEditor) widget controller. sourceController addHighlighter: YourHighlighter new.
In YourHighlighter you need to implement: #highlight: which gets a Text passed. You need to return a Text in that method and that text will be displayed. How you parse the text is up to you. I've used the Scanner and just kept scanning for tokens until everything was parsed. While doing that i kept asking the scanner for the token, the tokenType and the mark. But I guess you already have your own parser for Basic that provides a different interface than the Smalltalk Scanner. To add a certain style to the Text, call: #addEmphasis:removeEmphasis:allowDuplicates:from:to: The styles that you can use can all be found when inspecting: StyleDescription codeNames. So you could for example emphasise a keyword using: resultText addEmphasis: #code_keywordMessageSend removeEmphasis: nil allowDuplicates: true from: start to: end Kind Regards Karsten Karsten Kusche - Dipl. Inf. - [hidden email] Georg Heeg eK - Köthen Handelsregister: Amtsgericht Dortmund A 12812 Am Samstag, 4. Februar 2012 um 17:37 schrieb Carl Gundel:
_______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc |
That was easy thanks, but for some reason my highlighted Text object
only appears as highlighted if I view it in an inspector. It doesn't highlight where I am typing the code in. I'm not copying it, and I'm returning the same instance from the highlight: method. Any ideas? Do I need to set some flag or use a special subclass of something? -Carl On Mon, Feb 6, 2012 at 8:59 AM, Karsten Kusche <[hidden email]> wrote: > Hi Carl, > > i just did a similar thing two weeks ago and it's actually quite easy. > here's what you need to do: > > create a subclass of UI.Highlighter. Then you need to add that to the > texteditorcontroller you like to have highlighting on. To do so you add that > to your postBuildWith: > > sourceController := (aBuilder componentAt:#sourceEditor) widget controller. > sourceController addHighlighter: YourHighlighter new. > > In YourHighlighter you need to implement: #highlight: which gets a Text > passed. You need to return a Text in that method and that text will be > displayed. > How you parse the text is up to you. I've used the Scanner and just kept > scanning for tokens until everything was parsed. While doing that i kept > asking the scanner for the token, the tokenType and the mark. But I guess > you already have your own parser for Basic that provides a different > interface than the Smalltalk Scanner. > > To add a certain style to the Text, > call: #addEmphasis:removeEmphasis:allowDuplicates:from:to: > > The styles that you can use can all be found when > inspecting: StyleDescription codeNames. > So you could for example emphasise a keyword using: > > resultText > addEmphasis: #code_keywordMessageSend > removeEmphasis: nil > allowDuplicates: true > from: start > to: end > > Kind Regards > Karsten > > > -- > Karsten Kusche - Dipl. Inf. - [hidden email] > Georg Heeg eK - Köthen > Handelsregister: Amtsgericht Dortmund A 12812 > > Am Samstag, 4. Februar 2012 um 17:37 schrieb Carl Gundel: > > I'm thinking to create a simple highlighting parser for BASIC from the > existing mechanism in VisualWorks. I am not thinking to hook this > into my BASIC parser. I only need to detect certain kinds of tokens > such as keywords, valid variable names, numeric and string literals, > etc. and give each kind its own color. I do this already in VSE for > Liberty BASIC and it is sufficient for my purposes. What would be the > minimal approach to implementing this in VW? > > Thanks for any suggestions. > > -Carl Gundel > http://www.libertybasic.com > http://www.runbasic.com > _______________________________________________ > vwnc mailing list > [hidden email] > http://lists.cs.uiuc.edu/mailman/listinfo/vwnc _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc |
Hi Carl,
not sure what's wrong there. I've published the package HighlightingExample to the public store where you can see who i implemented it. It's really just what i wrote in the previous mail, but maybe it helps you understanding where the error is. Kind Regards Karsten Karsten Kusche - Dipl. Inf. - [hidden email] Georg Heeg eK - Köthen Handelsregister: Amtsgericht Dortmund A 12812 Am Montag, 6. Februar 2012 um 22:49 schrieb Carl Gundel:
_______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc |
The problem seems to be that I set the text style of the text editor when I opened it. If I remove that then my code highlighting works (yay!) but the courier font I was using is not there. :-( The other thing is that for programs that are more than a few K bytes the whole process is painfully sluggish, but I think I may be able to fix that. -Carl Gundel Liberty BASIC for Windows - http://www.libertybasic.com Run BASIC, easy web programming - http://www.runbasic.com
_______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc |
Free forum by Nabble | Edit this page |