Skip to main content

Notice: This Wiki is now read only and edits are no longer 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 use the text document model?"

 
Line 40: Line 40:
 
[[FAQ_How_do_I_insert_text_in_the_active_text_editor%3F]]
 
[[FAQ_How_do_I_insert_text_in_the_active_text_editor%3F]]
  
Go to '''Platform Plug-in Developer Guide > Programmer's Guide >Editors > Documents and partitions''' in online help, or at [https://help.eclipse.org/latest/topic/org.eclipse.platform.doc.isv/guide/editors_documents.htm help.eclipse.org].
+
Go to '''Platform Plug-in Developer Guide > Programmer's Guide >Editors > Documents and partitions''' in Eclipse help, or at [https://help.eclipse.org/latest/topic/org.eclipse.platform.doc.isv/guide/editors_documents.htm help.eclipse.org].
  
 
<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>
 
<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>

Latest revision as of 12:37, 26 June 2021

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