Difference between revisions of "FAQ How do I support formatting in my editor?"
|Line 48:||Line 48:|
== See Also: ==
== See Also: ==
Latest revision as of 04:01, 15 January 2007
The JFace source viewer has infrastructure for supporting content formatters. A content formatters job is primarily to adjust the whitespace between words in a document to match a configured style. A JFace formatter can be configured to operate on an entire document or on a region within a document. Typically, if a document contains several content types, a different formatting strategy will be used for each type. As usual, a formatter is installed from your subclass of SourceViewerConfiguration. To provide a configured formatter instance, override the method getContentFormatter. Most of the time, you can create an instance of the standard formatting class, MultiPassContentFormatter. This class requires that you specify a single master formatting strategy and optionally a slave formatting strategy for each partition in your document.
The following snippet from the Java source configuration installs a master strategy (JavaFormattingStrategy) that is used to format Java code and a slave formatting strategy for formatting comments:
MultiPassContentFormatter formatter= new MultiPassContentFormatter( getConfiguredDocumentPartitioning(viewer), IDocument.DEFAULT_CONTENT_TYPE); formatter.setMasterStrategy( new JavaFormattingStrategy()); formatter.setSlaveStrategy( new CommentFormattingStrategy(...), IJavaPartitions.JAVA_DOC);
The work of formatting the characters in the document is performed by the formatting-strategy classes that are installed on the formatter. JFace doesnt provide much common infrastructure for doing this formatting as it is based largely on the syntax of the language you are formatting.
Finally, you will need to create an action that invokes the formatter. No generic formatting action is defined by the text infrastructure, but it is quite easy to create one of your own. The actions run method can simply call the following on the source viewer to invoke the formatter:
This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.