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