2 * $RCSfile: NonRuleBasedDamagerRepairer.java,v $
5 * CH-1700 Fribourg, Switzerland
8 *========================================================================
9 * Modifications history
10 *========================================================================
11 * $Log: not supported by cvs2svn $
12 * Revision 1.1 2004/02/26 02:25:42 agfitzp
13 * renamed packages to match xml & css
15 * Revision 1.1 2004/02/05 03:10:08 agfitzp
18 * Revision 1.1.2.1 2003/12/12 21:37:24 agfitzp
19 * Experimental work for Classes view
21 * Revision 1.1 2003/05/28 15:17:11 agfitzp
22 * net.sourceforge.phpeclipse.js.core 0.0.1 code base
24 *========================================================================
27 package net.sourceforge.phpeclipse.js.core.parser;
29 import org.eclipse.jface.text.BadLocationException;
30 import org.eclipse.jface.text.DocumentEvent;
31 import org.eclipse.jface.text.IDocument;
32 import org.eclipse.jface.text.IRegion;
33 import org.eclipse.jface.text.ITypedRegion;
34 import org.eclipse.jface.text.Region;
35 import org.eclipse.jface.text.TextAttribute;
36 import org.eclipse.jface.text.TextPresentation;
37 import org.eclipse.jface.text.presentation.IPresentationDamager;
38 import org.eclipse.jface.text.presentation.IPresentationRepairer;
39 //import org.eclipse.jface.util.Assert;
40 import org.eclipse.swt.custom.StyleRange;
46 * @author $Author: jsurfer $, $Date: 2004-09-02 18:14:38 $
48 * @version $Revision: 1.1 $
50 public class NonRuleBasedDamagerRepairer implements IPresentationDamager, IPresentationRepairer
52 /** The document this object works on */
53 protected IDocument fDocument;
55 /** The default text attribute if non is returned as data by the current token */
56 protected TextAttribute fDefaultTextAttribute;
59 * Constructor for NonRuleBasedDamagerRepairer.
60 * @param defaultTextAttribute
62 public NonRuleBasedDamagerRepairer(TextAttribute defaultTextAttribute)
64 // Assert.isNotNull(defaultTextAttribute);
66 fDefaultTextAttribute = defaultTextAttribute;
70 * @see IPresentationRepairer#setDocument(IDocument)
72 public void setDocument(IDocument document)
78 * Returns the end offset of the line that contains the specified offset or if the offset is
79 * inside a line delimiter, the end offset of the next line.
81 * @param offset the offset whose line end offset must be computed
83 * @return the line end offset for the given offset
85 * @exception BadLocationException if offset is invalid in the current document
87 protected int endOfLineOf(int offset) throws BadLocationException
89 IRegion info = fDocument.getLineInformationOfOffset(offset);
91 if(offset <= info.getOffset() + info.getLength())
93 return info.getOffset() + info.getLength();
96 int line = fDocument.getLineOfOffset(offset);
100 info = fDocument.getLineInformation(line + 1);
102 return info.getOffset() + info.getLength();
104 catch(BadLocationException x)
106 return fDocument.getLength();
111 * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent, boolean)
113 public IRegion getDamageRegion(ITypedRegion partition, DocumentEvent event,
114 boolean documentPartitioningChanged)
116 if(!documentPartitioningChanged)
120 IRegion info = fDocument.getLineInformationOfOffset(event.getOffset());
121 int start = Math.max(partition.getOffset(), info.getOffset());
123 int end = event.getOffset() +
124 (event.getText() == null ? event.getLength()
125 : event.getText().length());
127 if(info.getOffset() <= end && end <= info.getOffset() + info.getLength())
129 // optimize the case of the same line
130 end = info.getOffset() + info.getLength();
134 end = endOfLineOf(end);
137 end = Math.min(partition.getOffset() + partition.getLength(), end);
139 return new Region(start, end - start);
141 catch(BadLocationException x)
150 * @see IPresentationRepairer#createPresentation(TextPresentation, ITypedRegion)
152 public void createPresentation(TextPresentation presentation, ITypedRegion region)
154 addRange(presentation, region.getOffset(), region.getLength(), fDefaultTextAttribute);
158 * Adds style information to the given text presentation.
160 * @param presentation the text presentation to be extended
161 * @param offset the offset of the range to be styled
162 * @param length the length of the range to be styled
163 * @param attr the attribute describing the style of the range to be styled
165 protected void addRange(TextPresentation presentation, int offset, int length,
170 presentation.addStyleRange(
171 new StyleRange(offset, length, attr.getForeground(), attr.getBackground(),