RB Code Highlighting
Last published: April 2, 2013 by 'nross'
Defines 11 Classes
Extends 25 Classes
This adds syntax highlighting to the Refactoring Browser, and optionally to workspaces and debuggers. Through the TextHighlighting package, it runs a background process that parses the code in the text area and attempts to apply various styles to lexical elements of the parsed text, by language. For reasonable size methods and reasonable speed machines, it can usually keep up with typing. In addition to most of the syntactic constructs, it also highlights syntax errors, some compile time errors (e.g., redefined variables), and some runtime errors (e.g., probable DNU errors):
You can customize the particular emphases applied in the settings tool, using the "Highlighter" pages. These normally apply various colors along with standard kinds of text emphases. Alternative color schemes are also available through settings.
To reuse the highlighter in your application model, make sure #addHighlighters is invoked in its #postBuildWith: method, and add an extension method along these lines:
CodeHighlighter on: (self widgetAt: #TheIdOfTheWidgetYouWantToCodeFormat) controller for: self
The argument "self" can be any object that will actually request the highlighting from the CodeHighlightingParser. This delegation is required to correctly configure the parser with class and environment for message send context. The delegation object must understand both #highlight:from: and #unhighlight:from:, in accordance with the implementation of DelegateHighlighter.
You can use methods in the CodeHighlighter configuring protocol to control when the highlighter works through settings. By default, it uses the "forSource" method, which restricts only by the master code highlighting switch. If you wanted to tie this, for example, to the debugger switch, your extension method would look like this:
(CodeHighlighter on: (self widgetAt: #TheIdOfTheWidgetYouWantToCodeFormat) controller for: self) forDebugger
You can add your own code highlighting restriction controls and settings using the configuring methods as examples.
With this version, the application of individual emphases for specific situations made pluggable and is accomplished by selected emphasizers. The code parser defers to a [computed] list of emphasizers, where each does the actual styling for any language element, according to its purpose. This allows complete control of what visual characteristics are applied. Emphasizers are configured for each parser as it is created through the use of pragmas. The method containing the pragma is invoked during parser initialization. If the method returns nil, it is ignored. Otherwise, the method should return an instance of a CodeEmphasizer configured on the parser. Since emphasizers and the resulting styling are orthogonal, no provision is made for ordering the emphasizers. For example, the language colorizer is enabled as follows (in the configuring protocol of CodeHighlightingParser):
^CodeHighlighter markUpLanguage ifTrue: [LexicalCodeEmphasizer on: self]
The latest version can be found in the Cincom Public Store Repository (see http://www.cincomsmalltalk.com). See also http://wiki.cs.uiuc.edu/VisualWorks/RB+Code+Highlighter for more information.