1 11 12 package org.eclipse.jface.text.rules; 13 14 15 import org.eclipse.swt.custom.StyleRange; 16 import org.eclipse.swt.SWT; 17 18 import org.eclipse.core.runtime.Assert; 19 20 import org.eclipse.jface.text.BadLocationException; 21 import org.eclipse.jface.text.DocumentEvent; 22 import org.eclipse.jface.text.IDocument; 23 import org.eclipse.jface.text.IRegion; 24 import org.eclipse.jface.text.ITypedRegion; 25 import org.eclipse.jface.text.Region; 26 import org.eclipse.jface.text.TextAttribute; 27 import org.eclipse.jface.text.TextPresentation; 28 import org.eclipse.jface.text.presentation.IPresentationDamager; 29 import org.eclipse.jface.text.presentation.IPresentationRepairer; 30 31 32 42 public class DefaultDamagerRepairer implements IPresentationDamager, IPresentationRepairer { 43 44 45 46 protected IDocument fDocument; 47 48 protected ITokenScanner fScanner; 49 50 protected TextAttribute fDefaultTextAttribute; 51 52 62 public DefaultDamagerRepairer(ITokenScanner scanner, TextAttribute defaultTextAttribute) { 63 64 Assert.isNotNull(defaultTextAttribute); 65 66 fScanner= scanner; 67 fDefaultTextAttribute= defaultTextAttribute; 68 } 69 70 76 public DefaultDamagerRepairer(ITokenScanner scanner) { 77 78 Assert.isNotNull(scanner); 79 80 fScanner= scanner; 81 fDefaultTextAttribute= new TextAttribute(null); 82 } 83 84 88 public void setDocument(IDocument document) { 89 fDocument= document; 90 } 91 92 93 95 103 protected int endOfLineOf(int offset) throws BadLocationException { 104 105 IRegion info= fDocument.getLineInformationOfOffset(offset); 106 if (offset <= info.getOffset() + info.getLength()) 107 return info.getOffset() + info.getLength(); 108 109 int line= fDocument.getLineOfOffset(offset); 110 try { 111 info= fDocument.getLineInformation(line + 1); 112 return info.getOffset() + info.getLength(); 113 } catch (BadLocationException x) { 114 return fDocument.getLength(); 115 } 116 } 117 118 121 public IRegion getDamageRegion(ITypedRegion partition, DocumentEvent e, boolean documentPartitioningChanged) { 122 123 if (!documentPartitioningChanged) { 124 try { 125 126 IRegion info= fDocument.getLineInformationOfOffset(e.getOffset()); 127 int start= Math.max(partition.getOffset(), info.getOffset()); 128 129 int end= e.getOffset() + (e.getText() == null ? e.getLength() : e.getText().length()); 130 131 if (info.getOffset() <= end && end <= info.getOffset() + info.getLength()) { 132 end= info.getOffset() + info.getLength(); 134 } else 135 end= endOfLineOf(end); 136 137 end= Math.min(partition.getOffset() + partition.getLength(), end); 138 return new Region(start, end - start); 139 140 } catch (BadLocationException x) { 141 } 142 } 143 144 return partition; 145 } 146 147 149 152 public void createPresentation(TextPresentation presentation, ITypedRegion region) { 153 154 if (fScanner == null) { 155 addRange(presentation, region.getOffset(), region.getLength(), fDefaultTextAttribute); 157 return; 158 } 159 160 int lastStart= region.getOffset(); 161 int length= 0; 162 boolean firstToken= true; 163 IToken lastToken= Token.UNDEFINED; 164 TextAttribute lastAttribute= getTokenTextAttribute(lastToken); 165 166 fScanner.setRange(fDocument, lastStart, region.getLength()); 167 168 while (true) { 169 IToken token= fScanner.nextToken(); 170 if (token.isEOF()) 171 break; 172 173 TextAttribute attribute= getTokenTextAttribute(token); 174 if (lastAttribute != null && lastAttribute.equals(attribute)) { 175 length += fScanner.getTokenLength(); 176 firstToken= false; 177 } else { 178 if (!firstToken) 179 addRange(presentation, lastStart, length, lastAttribute); 180 firstToken= false; 181 lastToken= token; 182 lastAttribute= attribute; 183 lastStart= fScanner.getTokenOffset(); 184 length= fScanner.getTokenLength(); 185 } 186 } 187 188 addRange(presentation, lastStart, length, lastAttribute); 189 } 190 191 200 protected TextAttribute getTokenTextAttribute(IToken token) { 201 Object data= token.getData(); 202 if (data instanceof TextAttribute) 203 return (TextAttribute) data; 204 return fDefaultTextAttribute; 205 } 206 207 215 protected void addRange(TextPresentation presentation, int offset, int length, TextAttribute attr) { 216 if (attr != null) { 217 int style= attr.getStyle(); 218 int fontStyle= style & (SWT.ITALIC | SWT.BOLD | SWT.NORMAL); 219 StyleRange styleRange= new StyleRange(offset, length, attr.getForeground(), attr.getBackground(), fontStyle); 220 styleRange.strikeout= (style & TextAttribute.STRIKETHROUGH) != 0; 221 styleRange.underline= (style & TextAttribute.UNDERLINE) != 0; 222 styleRange.font= attr.getFont(); 223 presentation.addStyleRange(styleRange); 224 } 225 } 226 } 227 228 229 | Popular Tags |