diff options
author | Dani Megert | 2005-05-19 16:33:35 +0000 |
---|---|---|
committer | Dani Megert | 2005-05-19 16:33:35 +0000 |
commit | b5bfb0add595c2935e7b472f598921cd272803b5 (patch) | |
tree | 77f342bc4ae156c85d5999d5b7b2e3a4866666c2 /org.eclipse.text | |
parent | e5e2c7d39f0a3e3ce56af3a0bd26860a95ce64bc (diff) | |
download | eclipse.platform.text-b5bfb0add595c2935e7b472f598921cd272803b5.tar.gz eclipse.platform.text-b5bfb0add595c2935e7b472f598921cd272803b5.tar.xz eclipse.platform.text-b5bfb0add595c2935e7b472f598921cd272803b5.zip |
Added support for default line delimiter
Diffstat (limited to 'org.eclipse.text')
5 files changed, 131 insertions, 34 deletions
diff --git a/org.eclipse.text/projection/org/eclipse/jface/text/projection/ProjectionDocument.java b/org.eclipse.text/projection/org/eclipse/jface/text/projection/ProjectionDocument.java index fb6e7008fae..f5d771359b9 100644 --- a/org.eclipse.text/projection/org/eclipse/jface/text/projection/ProjectionDocument.java +++ b/org.eclipse.text/projection/org/eclipse/jface/text/projection/ProjectionDocument.java @@ -21,12 +21,14 @@ import org.eclipse.jface.text.DefaultLineTracker; import org.eclipse.jface.text.DocumentEvent; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IDocumentExtension; +import org.eclipse.jface.text.IDocumentExtension4; import org.eclipse.jface.text.IDocumentListener; import org.eclipse.jface.text.ILineTracker; import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.ITextStore; import org.eclipse.jface.text.Position; import org.eclipse.jface.text.Region; +import org.eclipse.jface.text.TextUtilities; /** @@ -194,7 +196,17 @@ public class ProjectionDocument extends AbstractDocument { public IDocument getMasterDocument() { return fMasterDocument; } - + + /* + * @see org.eclipse.jface.text.IDocumentExtension4#getDefaultLineDelimiter() + * @since 3.1 + */ + public String getDefaultLineDelimiter() { + if (fMasterDocument instanceof IDocumentExtension4) + return ((IDocumentExtension4)fMasterDocument).getDefaultLineDelimiter(); + return TextUtilities.getDefaultLineDelimiter(this); + } + /** * Initializes the projection document from the master document based on * the master's fragments. diff --git a/org.eclipse.text/src/org/eclipse/jface/text/AbstractDocument.java b/org.eclipse.text/src/org/eclipse/jface/text/AbstractDocument.java index 050badafac0..c59a59fdeed 100644 --- a/org.eclipse.text/src/org/eclipse/jface/text/AbstractDocument.java +++ b/org.eclipse.text/src/org/eclipse/jface/text/AbstractDocument.java @@ -155,6 +155,11 @@ public abstract class AbstractDocument implements IDocument, IDocumentExtension, * @since 3.1 */ private long fModificationStamp= IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP; + /** + * This document's default line delimiter. + * @since 3.1 + */ + private String fInitialLineDelimiter; /** @@ -803,6 +808,51 @@ public abstract class AbstractDocument implements IDocument, IDocumentExtension, public String[] getLegalLineDelimiters() { return getTracker().getLegalLineDelimiters(); } + + /* + * @see org.eclipse.jface.text.IDocumentExtension4#getDefaultLineDelimiter() + * @since 3.1 + */ + public String getDefaultLineDelimiter() { + + String lineDelimiter= null; + + try { + lineDelimiter= getLineDelimiter(0); + } catch (BadLocationException x) { + } + + if (lineDelimiter != null) + return lineDelimiter; + + if (fInitialLineDelimiter != null) + return fInitialLineDelimiter; + + String sysLineDelimiter= System.getProperty("line.separator"); //$NON-NLS-1$ + String[] delimiters= getLegalLineDelimiters(); + Assert.isTrue(delimiters.length > 0); + for (int i= 0; i < delimiters.length; i++) { + if (delimiters[i].equals(sysLineDelimiter)) { + lineDelimiter= sysLineDelimiter; + break; + } + } + + if (lineDelimiter == null) + lineDelimiter= delimiters[0]; + + return lineDelimiter; + + } + + /* + * @see org.eclipse.jface.text.IDocumentExtension4#setInitialLineDelimiter(java.lang.String) + * @since 3.1 + */ + public void setInitialLineDelimiter(String lineDelimiter) { + Assert.isNotNull(lineDelimiter); + fInitialLineDelimiter= lineDelimiter; + } /* * @see org.eclipse.jface.text.IDocument#getLineLength(int) diff --git a/org.eclipse.text/src/org/eclipse/jface/text/IDocument.java b/org.eclipse.text/src/org/eclipse/jface/text/IDocument.java index b024953e26e..326c1704379 100644 --- a/org.eclipse.text/src/org/eclipse/jface/text/IDocument.java +++ b/org.eclipse.text/src/org/eclipse/jface/text/IDocument.java @@ -27,7 +27,7 @@ package org.eclipse.jface.text; * a document provides the <code>replace</code> method which substitutes a given * string for a specified text range in the document. On each document change, all * registered document listeners are informed exactly once. - * + * <p> * Positions are stickers to the document's text that are updated when the * document is changed. Positions are updated by {@link org.eclipse.jface.text.IPositionUpdater}s. Position * updaters are managed as a list. The list defines the sequence in which position @@ -36,8 +36,8 @@ package org.eclipse.jface.text; * the document defines the order of position in a category based on the position's offset * based on the implementation of the method <code>computeIndexInCategory</code>. * Each document must support a default position category whose name is specified by this - * interface.<p> - * + * interface.</p> + * <p> * A document can be considered consisting of a sequence of not overlapping partitions. * A partition is defined by its offset, its length, and its type. Partitions are * updated on every document manipulation and ensured to be up-to-date when the document @@ -49,13 +49,17 @@ package org.eclipse.jface.text; * informed exactly once. The extension interface {@link org.eclipse.jface.text.IDocumentExtension3} * introduced in version 3.0 extends the concept of partitions and allows a document to * not only manage one but multiple partitioning. Each partitioning has an id which must - * be used to refer to a particular partitioning.<p> - * + * be used to refer to a particular partitioning.</p> + * <p> * An <code>IDocument</code> provides methods to map line numbers and character * positions onto each other based on the document's line delimiters. When moving text * between documents using different line delimiters, the text must be converted to - * use the target document's line delimiters.<p> - * + * use the target document's line delimiters.</p> + * <p> + * An <code>IDocument</code> does not care about mixed line delimiters. Clients who + * who want to ensure a single line delimiter in their document should use the line + * delimiter returned by {@link org.eclipse.jface.text.TextUtilities#getDefaultLineDelimiter(IDocument)}.</p> + * <p> * <code>IDocument</code> throws <code>BadLocationException</code> if the parameters of * queries or manipulation requests are not inside the bounds of the document. The purpose * of this style of exception handling is @@ -63,11 +67,11 @@ package org.eclipse.jface.text; * <li> prepare document for multi-thread access * <li> allow clients to implement backtracking recovery methods * <li> prevent clients from up-front contract checking when dealing with documents. - * </ul> - * + * </ul></p> + * <p> * A document support for searching has deprecated since version 3.0. The recommended way - * for searching is to use a {@link org.eclipse.jface.text.FindReplaceDocumentAdapter}.<p> - * + * for searching is to use a {@link org.eclipse.jface.text.FindReplaceDocumentAdapter}.</p> + * <p> * In order to provide backward compatibility for clients of <code>IDocument</code>, extension * interfaces are used to provide a means of evolution. The following extension interfaces * exist: @@ -82,15 +86,17 @@ package org.eclipse.jface.text; * length partitions in conjunction with the distinction between open and closed partitions. </li> * <li> {@link org.eclipse.jface.text.IDocumentExtension4} since version 3.1 introducing the * concept of rewrite sessions. A rewrite session is a sequence of document replace operations - * that form a semantic unit.</li> - * </ul> - * + * that form a semantic unit. It also introduces a modification stamp and the ability to + * set the initial line delimiter and to query the default line delimiter.</li> + * </ul></p> + * <p> * Clients may implement this interface and its extension interfaces or use the default - * implementation provided by <code>AbstractDocument</code> and <code>Document</code>. - * + * implementation provided by <code>AbstractDocument</code> and <code>Document</code>.</p> + * * @see org.eclipse.jface.text.IDocumentExtension * @see org.eclipse.jface.text.IDocumentExtension2 * @see org.eclipse.jface.text.IDocumentExtension3 + * @see org.eclipse.jface.text.IDocumentExtension4 * @see org.eclipse.jface.text.Position * @see org.eclipse.jface.text.IPositionUpdater * @see org.eclipse.jface.text.IDocumentPartitioner diff --git a/org.eclipse.text/src/org/eclipse/jface/text/IDocumentExtension4.java b/org.eclipse.text/src/org/eclipse/jface/text/IDocumentExtension4.java index 9b41466cad1..112b8f7470e 100644 --- a/org.eclipse.text/src/org/eclipse/jface/text/IDocumentExtension4.java +++ b/org.eclipse.text/src/org/eclipse/jface/text/IDocumentExtension4.java @@ -11,10 +11,16 @@ package org.eclipse.jface.text; /** - * Extension interface for {@link org.eclipse.jface.text.IDocument}. Adds the - * concept of rewrite sessions introduces a modification stamp on the document. - * A rewrite session is a sequence of replace operations that form a semantic unit. - * + * Extension interface for {@link org.eclipse.jface.text.IDocument}. It adds the + * following concepts: + * <ul> + * <li>Rewrite sessions. A rewrite session is a sequence of replace operations + * that form a semantic unit.</li> + * <li>A modification stamp on the document</li> + * <li>The ability to set the initial line delimiter and to query the default + * line delimiter</li> + * </ul> + * * @since 3.1 */ public interface IDocumentExtension4 { @@ -125,4 +131,23 @@ public interface IDocumentExtension4 { * @return the modification stamp of this document or <code>UNKNOWN_MODIFICATION_STAMP</code> */ long getModificationStamp(); + + /** + * Returns this document's default line delimiter. + * <p> + * This default line delimiter should be used by clients who + * want unique delimiters (e.g. 'CR's) in the document.</p> + * + * @return the default line delimiter or <code>null</code> if none + */ + String getDefaultLineDelimiter(); + + /** + * Sets this document's initial line delimiter i.e. the one + * which is returned by <code>getDefaultLineDelimiter</code> + * if the document does not yet contain any line delimiter. + * + * @param lineDelimiter the default line delimiter + */ + void setInitialLineDelimiter(String lineDelimiter); } diff --git a/org.eclipse.text/src/org/eclipse/jface/text/TextUtilities.java b/org.eclipse.text/src/org/eclipse/jface/text/TextUtilities.java index 13fad800ef1..88327f15df0 100644 --- a/org.eclipse.text/src/org/eclipse/jface/text/TextUtilities.java +++ b/org.eclipse.text/src/org/eclipse/jface/text/TextUtilities.java @@ -473,27 +473,31 @@ public class TextUtilities { */ public static String getDefaultLineDelimiter(IDocument document) { + if (document instanceof IDocumentExtension4) + return ((IDocumentExtension4)document).getDefaultLineDelimiter(); + String lineDelimiter= null; - + try { lineDelimiter= document.getLineDelimiter(0); } catch (BadLocationException x) { } - if (lineDelimiter == null) { - String sysLineDelimiter= System.getProperty("line.separator"); //$NON-NLS-1$ - String[] delimiters= document.getLegalLineDelimiters(); - Assert.isTrue(delimiters.length > 0); - for (int i= 0; i < delimiters.length; i++) { - if (delimiters[i].equals(sysLineDelimiter)) { - lineDelimiter= sysLineDelimiter; - break; - } + if (lineDelimiter != null) + return lineDelimiter; + + String sysLineDelimiter= System.getProperty("line.separator"); //$NON-NLS-1$ + String[] delimiters= document.getLegalLineDelimiters(); + Assert.isTrue(delimiters.length > 0); + for (int i= 0; i < delimiters.length; i++) { + if (delimiters[i].equals(sysLineDelimiter)) { + lineDelimiter= sysLineDelimiter; + break; } - - if (lineDelimiter == null) - lineDelimiter= delimiters[0]; } + + if (lineDelimiter == null) + lineDelimiter= delimiters[0]; return lineDelimiter; } |