Class AbstractFormatJob

java.lang.Object
de.funfried.netbeans.plugins.external.formatter.AbstractFormatJob
All Implemented Interfaces:
FormatJob
Direct Known Subclasses:
AbstractEclipseFormatJob

public abstract class AbstractFormatJob extends Object implements FormatJob
Abstract base implementation of a FormatJob which is called by the FormatterService where this implementation belongs to.
Author:
bahlef
  • Field Details

  • Constructor Details

    • AbstractFormatJob

      protected AbstractFormatJob(StyledDocument document, SortedSet<Pair<Integer,Integer>> changedElements)
      Constructor which has to be used by subclasses.
      Parameters:
      document - the StyledDocument from which the content should be formatted
      changedElements - SortedSet containing document offset ranges which should be formatted or null to format the whole document
  • Method Details

    • setFormattedCode

      protected boolean setFormattedCode(String code, String formattedContent) throws BadLocationException
      Applies the given formattedContent to the document.
      Parameters:
      code - the previous (unformatted) content
      formattedContent - the formatted code
      Returns:
      true if and only if the given formattedContent was set to the document, if due to any circumstances (old code equals formatted code, thrown exceptions, ...) the formattedContent wasn't applied false is returned
      Throws:
      BadLocationException - if there is an issue while applying the formatted code
    • getCode

      protected String getCode()
      Returns the content of the document.
      Returns:
      The content of the document
    • getFormatableSections

      @NonNull protected SortedSet<Pair<Integer,Integer>> getFormatableSections(String code)
      Returns a SortedSet within ranges as Pairs of Integers which describe the start and end offsets that can be formatted, it automatically checks for guarded sections and removes them before returning the SortedSet, this means if an empty SortedSet was removed nothing can be formatted, because all ranges in the changedElements are in guarded sections.
      Parameters:
      code - the current unformatted content of the document
      Returns:
      A SortedSet within ranges as Pairs of Integers which describe the start and end offsets which can be formatted or an empty SortedSet if nothing of the changedElements can be formatted
    • avoidGuardedSection

      protected SortedSet<Pair<Integer,Integer>> avoidGuardedSection(Pair<Integer,Integer> section, Iterable<GuardedSection> guardedSections)
      Checks if a given section interferes with the given guardedSections and if so splits the given section into multiple sections and returns them as a SortedSet.
      Parameters:
      section - the section that should be checked
      guardedSections - the guarded sections of the document
      Returns:
      A SortedSet containing the splitted sections or just the initial section itself if there was no interference with the given guardedSections