Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "FAQ How do I support formatting in my editor?"

 
(See Also:)
 
Line 48: Line 48:
 
== See Also: ==
 
== See Also: ==
  
[[FAQ_How_do_I_get_started_with_creating_a_custom_text_editor%3F]]
+
[[FAQ How do I get started with creating a custom text editor?]]
  
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>
+
{{Template: FAQ_Tagline}}

Latest revision as of 05:01, 15 January 2007

The JFace source viewer has infrastructure for supporting content formatters. A content formatter&#146;s 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 doesn&#146;t 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 action&#146;s run method can simply call the following on the source viewer to invoke the formatter:

   sourceViewer.doOperation(ISourceViewer.FORMAT);

See Also:

FAQ How do I get started with creating a custom text editor?


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.

Back to the top