Class MultiPassContentFormatter

java.lang.Object
org.eclipse.jface.text.formatter.MultiPassContentFormatter
All Implemented Interfaces:
IContentFormatter, IContentFormatterExtension

public class MultiPassContentFormatter extends Object implements IContentFormatter, IContentFormatterExtension
Content formatter for edit-based formatting strategies.

Two kinds of formatting strategies can be registered with this formatter:

  • one master formatting strategy for the default content type
  • one formatting strategy for each non-default content type
The master formatting strategy always formats the whole region to be formatted in the first pass. In a second pass, all partitions of the region to be formatted that are not of master content type are formatted using the slave formatting strategy registered for the underlying content type. All formatting strategies must implement IFormattingStrategyExtension.

Regions to be formatted with the master formatting strategy always have an offset aligned to the line start. Regions to be formatted with slave formatting strategies are aligned on partition boundaries.

Since:
3.0
See Also:
  • Constructor Details

    • MultiPassContentFormatter

      public MultiPassContentFormatter(String partitioning, String type)
      Creates a new content formatter.
      Parameters:
      partitioning - the document partitioning for this formatter
      type - the default content type
  • Method Details

    • format

      public final void format(IDocument medium, IFormattingContext context)
      Description copied from interface: IContentFormatterExtension
      Formats the given region of the specified document.

      The formatter may safely assume that it is the only subject that modifies the document at this point in time. This method is fully reentrant, but not thread-safe.

      The formatting process performed by format(IDocument, IFormattingContext) happens as follows:

      • In a first pass the content formatter formats the range of the document to be formatted by using the master formatting strategy. This happens regardless of the content type of the underlying partition.
      • In the second pass, the range is formatted again, this time using the registered slave formatting strategies. For each partition contained in the range to be formatted, the content formatter determines its content type and formats the partition with the correct formatting strategy.
      Specified by:
      format in interface IContentFormatterExtension
      Parameters:
      medium - the document to be formatted
      context - the formatting context to pass to the formatting strategies. This argument must not be null.
    • format

      public final void format(IDocument medium, IRegion region)
      Description copied from interface: IContentFormatter
      Formats the given region of the specified document.The formatter may safely assume that it is the only subject that modifies the document at this point in time.
      Specified by:
      format in interface IContentFormatter
      Parameters:
      medium - the document to be formatted
      region - the region within the document to be formatted
    • formatMaster

      protected void formatMaster(IFormattingContext context, IDocument document, int offset, int length)
      Formats the document specified in the formatting context with the master formatting strategy.

      The master formatting strategy covers all regions of the document. The offset of the region to be formatted is aligned on line start boundaries, whereas the end index of the region remains the same. For this formatting type the document partitioning is not taken into account.

      Parameters:
      context - The formatting context to use
      document - The document to operate on
      offset - The offset of the region to format
      length - The length of the region to format
    • formatSlave

      protected void formatSlave(IFormattingContext context, IDocument document, int offset, int length, String type)
      Formats the document specified in the formatting context with the formatting strategy registered for the content type.

      For this formatting type only slave strategies are used. The region to be formatted is aligned on partition boundaries of the underlying content type. The exact formatting strategy is determined by the underlying content type of the document partitioning.

      Parameters:
      context - The formatting context to use
      document - The document to operate on
      offset - The offset of the region to format
      length - The length of the region to format
      type - The content type of the region to format
    • formatSlaves

      protected void formatSlaves(IFormattingContext context, IDocument document, int offset, int length)
      Formats the document specified in the formatting context with the slave formatting strategies.

      For each content type of the region to be formatted in the document partitioning, the registered slave formatting strategy is used to format that particular region. The region to be formatted is aligned on partition boundaries of the underlying content type. If the content type is the document's default content type, nothing happens.

      Parameters:
      context - The formatting context to use
      document - The document to operate on
      offset - The offset of the region to format
      length - The length of the region to format
    • getFormattingStrategy

      public final IFormattingStrategy getFormattingStrategy(String type)
      Description copied from interface: IContentFormatter
      Returns the formatting strategy registered for the given content type.
      Specified by:
      getFormattingStrategy in interface IContentFormatter
      Parameters:
      type - the content type for which to look up the formatting strategy
      Returns:
      the formatting strategy for the given content type, or null if there is no such strategy
    • setMasterStrategy

      public final void setMasterStrategy(IFormattingStrategy strategy)
      Registers a master formatting strategy.

      The strategy may already be registered with a certain content type as slave strategy. The master strategy is registered for the default content type of documents. If a master strategy has already been registered, it is overridden by the new one.

      Parameters:
      strategy - The master formatting strategy, must implement IFormattingStrategyExtension
    • setSlaveStrategy

      public final void setSlaveStrategy(IFormattingStrategy strategy, String type)
      Registers a slave formatting strategy for a certain content type.

      The strategy may already be registered as master strategy. An already registered slave strategy for the specified content type will be replaced. However, the same strategy may be registered with several content types. Slave strategies cannot be registered for the default content type of documents.

      Parameters:
      strategy - The slave formatting strategy
      type - The content type to register this strategy with, must implement IFormattingStrategyExtension