Organized imports
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.js.core / src / net / sourceforge / phpeclipse / js / core / parser / NonRuleBasedDamagerRepairer.java
1 /*
2  * $RCSfile: NonRuleBasedDamagerRepairer.java,v $
3  *
4  * Copyright 2002
5  * CH-1700 Fribourg, Switzerland
6  * All rights reserved.
7  *
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
14  *
15  * Revision 1.1  2004/02/26 02:25:42  agfitzp
16  * renamed packages to match xml & css
17  *
18  * Revision 1.1  2004/02/05 03:10:08  agfitzp
19  * Initial Submission
20  *
21  * Revision 1.1.2.1  2003/12/12 21:37:24  agfitzp
22  * Experimental work for Classes view
23  *
24  * Revision 1.1  2003/05/28 15:17:11  agfitzp
25  * net.sourceforge.phpeclipse.js.core 0.0.1 code base
26  *
27  *========================================================================
28 */
29
30 package net.sourceforge.phpeclipse.js.core.parser;
31
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;
43
44
45 /**
46  * 
47  *
48  * @author $Author: stefanbjarni $, $Date: 2005-05-06 00:58:28 $
49  *
50  * @version $Revision: 1.2 $
51  */
52 public class NonRuleBasedDamagerRepairer implements IPresentationDamager, IPresentationRepairer
53 {
54    /** The document this object works on */
55    protected IDocument fDocument;
56
57    /** The default text attribute if non is returned as data by the current token */
58    protected TextAttribute fDefaultTextAttribute;
59
60    /**
61     * Constructor for NonRuleBasedDamagerRepairer.
62     * @param defaultTextAttribute
63     */
64    public NonRuleBasedDamagerRepairer(TextAttribute defaultTextAttribute)
65    {
66 //      Assert.isNotNull(defaultTextAttribute);
67
68       fDefaultTextAttribute = defaultTextAttribute;
69    }
70
71    /**
72     * @see IPresentationRepairer#setDocument(IDocument)
73     */
74    public void setDocument(IDocument document)
75    {
76       fDocument = document;
77    }
78
79    /**
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.
82     * 
83     * @param offset the offset whose line end offset must be computed
84     * 
85     * @return the line end offset for the given offset
86     * 
87     * @exception BadLocationException if offset is invalid in the current document
88     */
89    protected int endOfLineOf(int offset) throws BadLocationException
90    {
91       IRegion info = fDocument.getLineInformationOfOffset(offset);
92
93       if(offset <= info.getOffset() + info.getLength())
94       {
95          return info.getOffset() + info.getLength();
96       }
97
98       int line = fDocument.getLineOfOffset(offset);
99
100       try
101       {
102          info = fDocument.getLineInformation(line + 1);
103
104          return info.getOffset() + info.getLength();
105       }
106       catch(BadLocationException x)
107       {
108          return fDocument.getLength();
109       }
110    }
111
112    /**
113     * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent, boolean)
114     */
115    public IRegion getDamageRegion(ITypedRegion partition, DocumentEvent event, 
116                                   boolean documentPartitioningChanged)
117    {
118       if(!documentPartitioningChanged)
119       {
120          try
121          {
122             IRegion info = fDocument.getLineInformationOfOffset(event.getOffset());
123             int start = Math.max(partition.getOffset(), info.getOffset());
124
125             int end = event.getOffset() + 
126                       (event.getText() == null ? event.getLength()
127                                                : event.getText().length());
128
129             if(info.getOffset() <= end && end <= info.getOffset() + info.getLength())
130             {
131                // optimize the case of the same line
132                end = info.getOffset() + info.getLength();
133             }
134             else
135             {
136                end = endOfLineOf(end);
137             }
138
139             end = Math.min(partition.getOffset() + partition.getLength(), end);
140
141             return new Region(start, end - start);
142          }
143          catch(BadLocationException x)
144          {
145          }
146       }
147
148       return partition;
149    }
150
151    /**
152     * @see IPresentationRepairer#createPresentation(TextPresentation, ITypedRegion)
153     */
154    public void createPresentation(TextPresentation presentation, ITypedRegion region)
155    {
156       addRange(presentation, region.getOffset(), region.getLength(), fDefaultTextAttribute);
157    }
158
159    /**
160     * Adds style information to the given text presentation.
161     * 
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
166     */
167    protected void addRange(TextPresentation presentation, int offset, int length, 
168                            TextAttribute attr)
169    {
170       if(attr != null)
171       {
172          presentation.addStyleRange(
173                 new StyleRange(offset, length, attr.getForeground(), attr.getBackground(), 
174                                attr.getStyle()));
175       }
176    }
177 }