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/09/02 18:14:38 jsurfer
13 * intial source from ttp://www.sf.net/projects/wdte
15 * Revision 1.1 2004/02/26 02:25:42 agfitzp
16 * renamed packages to match xml & css
18 * Revision 1.1 2004/02/05 03:10:08 agfitzp
21 * Revision 1.1.2.1 2003/12/12 21:37:24 agfitzp
22 * Experimental work for Classes view
24 * Revision 1.1 2003/05/28 15:17:11 agfitzp
25 * net.sourceforge.phpeclipse.js.core 0.0.1 code base
27 *========================================================================
30 package net.sourceforge.phpeclipse.js.core.parser;
32 import org.eclipse.jface.text.BadLocationException;
33 import org.eclipse.jface.text.DocumentEvent;
34 import org.eclipse.jface.text.IDocument;
35 import org.eclipse.jface.text.IRegion;
36 import org.eclipse.jface.text.ITypedRegion;
37 import org.eclipse.jface.text.Region;
38 import org.eclipse.jface.text.TextAttribute;
39 import org.eclipse.jface.text.TextPresentation;
40 import org.eclipse.jface.text.presentation.IPresentationDamager;
41 import org.eclipse.jface.text.presentation.IPresentationRepairer;
42 import org.eclipse.swt.custom.StyleRange;
48 * @author $Author: stefanbjarni $, $Date: 2005-05-06 00:58:28 $
50 * @version $Revision: 1.2 $
52 public class NonRuleBasedDamagerRepairer implements IPresentationDamager, IPresentationRepairer
54 /** The document this object works on */
55 protected IDocument fDocument;
57 /** The default text attribute if non is returned as data by the current token */
58 protected TextAttribute fDefaultTextAttribute;
61 * Constructor for NonRuleBasedDamagerRepairer.
62 * @param defaultTextAttribute
64 public NonRuleBasedDamagerRepairer(TextAttribute defaultTextAttribute)
66 // Assert.isNotNull(defaultTextAttribute);
68 fDefaultTextAttribute = defaultTextAttribute;
72 * @see IPresentationRepairer#setDocument(IDocument)
74 public void setDocument(IDocument document)
80 * Returns the end offset of the line that contains the specified offset or if the offset is
81 * inside a line delimiter, the end offset of the next line.
83 * @param offset the offset whose line end offset must be computed
85 * @return the line end offset for the given offset
87 * @exception BadLocationException if offset is invalid in the current document
89 protected int endOfLineOf(int offset) throws BadLocationException
91 IRegion info = fDocument.getLineInformationOfOffset(offset);
93 if(offset <= info.getOffset() + info.getLength())
95 return info.getOffset() + info.getLength();
98 int line = fDocument.getLineOfOffset(offset);
102 info = fDocument.getLineInformation(line + 1);
104 return info.getOffset() + info.getLength();
106 catch(BadLocationException x)
108 return fDocument.getLength();
113 * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent, boolean)
115 public IRegion getDamageRegion(ITypedRegion partition, DocumentEvent event,
116 boolean documentPartitioningChanged)
118 if(!documentPartitioningChanged)
122 IRegion info = fDocument.getLineInformationOfOffset(event.getOffset());
123 int start = Math.max(partition.getOffset(), info.getOffset());
125 int end = event.getOffset() +
126 (event.getText() == null ? event.getLength()
127 : event.getText().length());
129 if(info.getOffset() <= end && end <= info.getOffset() + info.getLength())
131 // optimize the case of the same line
132 end = info.getOffset() + info.getLength();
136 end = endOfLineOf(end);
139 end = Math.min(partition.getOffset() + partition.getLength(), end);
141 return new Region(start, end - start);
143 catch(BadLocationException x)
152 * @see IPresentationRepairer#createPresentation(TextPresentation, ITypedRegion)
154 public void createPresentation(TextPresentation presentation, ITypedRegion region)
156 addRange(presentation, region.getOffset(), region.getLength(), fDefaultTextAttribute);
160 * Adds style information to the given text presentation.
162 * @param presentation the text presentation to be extended
163 * @param offset the offset of the range to be styled
164 * @param length the length of the range to be styled
165 * @param attr the attribute describing the style of the range to be styled
167 protected void addRange(TextPresentation presentation, int offset, int length,
172 presentation.addStyleRange(
173 new StyleRange(offset, length, attr.getForeground(), attr.getBackground(),