1 package net.sourceforge.phpeclipse.editors;
3 import org.eclipse.jface.text.BadLocationException;
4 import org.eclipse.jface.text.DocumentEvent;
5 import org.eclipse.jface.text.IDocument;
6 import org.eclipse.jface.text.IRegion;
7 import org.eclipse.jface.text.ITypedRegion;
8 import org.eclipse.jface.text.Region;
9 import org.eclipse.jface.text.TextAttribute;
10 import org.eclipse.jface.text.TextPresentation;
11 import org.eclipse.jface.text.presentation.IPresentationDamager;
12 import org.eclipse.jface.text.presentation.IPresentationRepairer;
13 import org.eclipse.jface.util.Assert;
14 import org.eclipse.swt.custom.StyleRange;
16 public class NonRuleBasedDamagerRepairer
17 implements IPresentationDamager, IPresentationRepairer {
19 /** The document this object works on */
20 protected IDocument fDocument;
21 /** The default text attribute if non is returned as data by the current token */
22 protected TextAttribute fDefaultTextAttribute;
25 * Constructor for NonRuleBasedDamagerRepairer.
27 public NonRuleBasedDamagerRepairer(TextAttribute defaultTextAttribute) {
28 Assert.isNotNull(defaultTextAttribute);
30 fDefaultTextAttribute = defaultTextAttribute;
34 * @see IPresentationRepairer#setDocument(IDocument)
36 public void setDocument(IDocument document) {
41 * Returns the end offset of the line that contains the specified offset or
42 * if the offset is inside a line delimiter, the end offset of the next line.
44 * @param offset the offset whose line end offset must be computed
45 * @return the line end offset for the given offset
46 * @exception BadLocationException if offset is invalid in the current document
48 protected int endOfLineOf(int offset) throws BadLocationException {
50 IRegion info = fDocument.getLineInformationOfOffset(offset);
51 if (offset <= info.getOffset() + info.getLength())
52 return info.getOffset() + info.getLength();
54 int line = fDocument.getLineOfOffset(offset);
56 info = fDocument.getLineInformation(line + 1);
57 return info.getOffset() + info.getLength();
58 } catch (BadLocationException x) {
59 return fDocument.getLength();
64 * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent, boolean)
66 public IRegion getDamageRegion(
67 ITypedRegion partition,
69 boolean documentPartitioningChanged) {
70 if (!documentPartitioningChanged) {
74 fDocument.getLineInformationOfOffset(event.getOffset());
75 int start = Math.max(partition.getOffset(), info.getOffset());
79 + (event.getText() == null
81 : event.getText().length());
83 if (info.getOffset() <= end
84 && end <= info.getOffset() + info.getLength()) {
85 // optimize the case of the same line
86 end = info.getOffset() + info.getLength();
88 end = endOfLineOf(end);
92 partition.getOffset() + partition.getLength(),
94 return new Region(start, end - start);
96 } catch (BadLocationException x) {
104 * @see IPresentationRepairer#createPresentation(TextPresentation, ITypedRegion)
106 public void createPresentation(
107 TextPresentation presentation,
108 ITypedRegion region) {
113 fDefaultTextAttribute);
117 * Adds style information to the given text presentation.
119 * @param presentation the text presentation to be extended
120 * @param offset the offset of the range to be styled
121 * @param length the length of the range to be styled
122 * @param attr the attribute describing the style of the range to be styled
124 protected void addRange(
125 TextPresentation presentation,
128 TextAttribute attr) {
130 presentation.addStyleRange(
134 attr.getForeground(),
135 attr.getBackground(),