1 11 12 package org.eclipse.jdt.internal.ui.text.spelling; 13 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.List ; 17 import java.util.Locale ; 18 19 import org.eclipse.core.runtime.IProgressMonitor; 20 21 import org.eclipse.jface.text.BadLocationException; 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.TextUtilities; 26 import org.eclipse.jface.text.TypedRegion; 27 28 import org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector; 29 30 import org.eclipse.jdt.ui.PreferenceConstants; 31 32 import org.eclipse.jdt.internal.ui.JavaPlugin; 33 import org.eclipse.jdt.internal.ui.propertiesfileeditor.IPropertiesFilePartitions; 34 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellChecker; 35 36 41 public class PropertiesFileSpellingEngine extends SpellingEngine { 42 43 46 protected void check(IDocument document, IRegion[] regions, ISpellChecker checker, ISpellingProblemCollector collector, IProgressMonitor monitor) { 47 SpellEventListener listener= new SpellEventListener(collector, document); 48 boolean isIgnoringAmpersand= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.SPELLING_IGNORE_AMPERSAND_IN_PROPERTIES); 49 try { 50 checker.addListener(listener); 51 List partitionList= new ArrayList (); 52 for (int i= 0; i < regions.length; i++) 53 partitionList.addAll(Arrays.asList(TextUtilities.computePartitioning(document, IPropertiesFilePartitions.PROPERTIES_FILE_PARTITIONING, regions[i].getOffset(), regions[i].getLength(), false))); 54 ITypedRegion[] partitions= (ITypedRegion[]) partitionList.toArray(new ITypedRegion[partitionList.size()]); 55 56 for (int i= 0; i < partitions.length; i++) { 57 if (monitor != null && monitor.isCanceled()) 58 return; 59 if (listener.isProblemsThresholdReached()) 60 return; 61 62 ITypedRegion partition= partitions[i]; 63 if (IPropertiesFilePartitions.COMMENT.equals(partition.getType())) { 64 for (; i < partitions.length - 1; i++) { 65 ITypedRegion next= partitions[i+1]; 66 int gapOffset= partition.getOffset() + partition.getLength(); 67 int gapLength= next.getOffset() - gapOffset; 68 if ((IPropertiesFilePartitions.COMMENT.equals(next.getType()) || isWhitespace(document, next.getOffset(), next.getLength())) && isWhitespace(document, gapOffset, gapLength)) 69 partition= new TypedRegion(partition.getOffset(), next.getOffset() + next.getLength() - partition.getOffset(), partition.getType()); 70 else 71 break; 72 } 73 } 74 String partitionType= partition.getType(); 75 if (IPropertiesFilePartitions.COMMENT.equals(partitionType) || (!isIgnoringAmpersand && IPropertiesFilePartitions.PROPERTY_VALUE.equals(partitionType))) { 76 Locale locale= checker.getLocale(); 77 checker.execute(new SpellCheckIterator(document, partition, locale)); 78 } else if (isIgnoringAmpersand && IPropertiesFilePartitions.PROPERTY_VALUE.equals(partitionType)) { 79 Locale locale= checker.getLocale(); 80 checker.execute(new PropertiesFileSpellCheckIterator(document, partition, locale)); 81 } 82 } 83 } catch (BadLocationException x) { 84 JavaPlugin.log(x); 85 } finally { 86 checker.removeListener(listener); 87 } 88 } 89 90 100 private boolean isWhitespace(IDocument document, int offset, int length) { 101 try { 102 for (int i= 0; i < length; i++) 103 if (!Character.isWhitespace(document.getChar(offset + i))) 104 return false; 105 return true; 106 } catch (BadLocationException x) { 107 JavaPlugin.log(x); 108 return false; 109 } 110 } 111 } 112 | Popular Tags |