EditorFormattingAPIUpgrade

Migrating Modules From The Old Formatting API

Contents


This page describes common situations that module owners are likely to encounter when migrating their modules from the old editor formatting API. Please note that the vast majority of Netbeans modules from trunk and contrib repositories has already been migrated and use the new formatting API. The few modules that still need to be migrated may find the information here useful.

If you are a module owner and have problems with rewriting your module and this page doesn't help you, please send a question to dev (at) editor (dot) netbeans (dot) org.

The code examples in the sections below that show the usage of the new formatting API may seem to be much more complex than the code snippets with the old formatting API code. This is because the new formatting API examples show the full and correct usage of the API including the locking mechanism. In fact if your old code was written correctly it must contain similar locking as well.


Getting a formatter/indenter for a document

  • Typical old API usage:
  BaseDocument doc = ...
  Formatter f = doc.getFormatter();
  f.reformat(...);
  • New API usage:
  BaseDocument doc = ...
  Reformat f = Reformat.get(doc);
  ...

  // Or when you just need to indent lines
  Indent i = Indent.get(doc);
  ...


Calculating the formatted area length

  • Typical old API usage:
  int formatLength = doc.getFormatter().reformat(doc, startOffset, endOffset);
  • New API usage:
  final int [] formatLength = new int[1];
  final Reformat reformat = Reformat.get(doc);
  reformat.lock();
  try {
    doc.runAtomic(new Runnable() {
      public void run() {
        try {
          Position endPos = doc.createPosition(endOffset);
          reformat.reformat(startOffset, endOffset);
          formatLength[0] = Math.max(0, endPos.getOffset() - startOffset);
        } catch (BadLocationException ble) {
          Exceptions.printStackTrace(ble);
        }
      }
    });
  } finally {
    reformat.unlock();
  }


Getting the caret offset for a new indented line

  • Typical old API usage:
int newCaretOffset = formatter.indentNewLine(doc, offset);
  • New API usage:
  final int [] newCaretOffset = new int[1];
  final Indent indenter = Indent.get(doc);
  indenter.lock();
  try {
    doc.runAtomic(new Runnable() {
      public void run() {
        try {
          newCaretOffset[0] = indenter.indentNewLine(offset);
        } catch (BadLocationException ble) {
          Exceptions.printStackTrace(ble);
        }
      }
    });
  } finally {
    indenter.unlock();
  }


Rewriting FormatLayer implementation

TBD


Updating Unit Tests

In some situations tests that need an indenter/formatter for you language may not be able to find it anymore. It's hard to say what exactly is wrong without knowing your test setup, but these are typical thing that you should check:

  • If your language indenter/formatter is implemented using the new API make sure that your Reformat.Factory and Indent.Factory are present in MimeLookup for your language's mime type. For example if you are using MockMimeLookup make sure that the factory instances are added to it.
  • If your indenter/formatter is implemented using the old API (eg. org.netbeant.editor.Formatter) you will need the real MimeLookup (not MockMimeLookup) and the real module layer loaded. This means that your tests will have to depend on editor.mimelookup.impl and editor.deprecated.pre65formatting modules and you will have to somehow set up org.openide.filesystems.Repository to load system filesystem from the module layers with your language registrations (eg. your EditorKit implementation, etc).
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo