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

FAQ How do I use the text document model?

The underlying model behind text editors is represented by the interface IDocument and its default implementation, Document, both of which are declared in the org.eclipse.jface.text package. You can use documents to manipulate text inside and outside a text editor. Documents are created with a simple constructor that optionally takes a string representing the initial input. The document contents can be obtained and replaced by using get() and set(String). The document model has a powerful search method and several methods for querying or replacing portions of the document. The following example uses a document to implement search and replace:

   String searchAndReplace(String input, String search, 
     String replace) throws BadLocationException {
      Document doc = new Document(input);
      int offset = 0;
      while (offset < doc.getLength()) {
         offset = doc.search(offset, search, true, true, true);
         if (offset < 0)
            break;
         doc.replace(offset, search.length(), replace);
         offset += replace.length();
      }
      return doc.get();
   }


This example only scratches the surface of the capabilities of the IDocument model. Documents also provide change notification, mapping between line numbers and character offsets, partitions, and much more. Other FAQs in this chapter dig into some of these concepts in more detail.


See Also:

FAQ_What_is_a_document_partition?

FAQ_How_do_I_insert_text_in_the_active_text_editor?

Go to Platform Plug-in Developer Guide > Programmer's Guide >Editors > Documents and partitions in Eclipse help, or at help.eclipse.org.


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