1 11 12 package org.eclipse.search.internal.ui.text; 13 14 import java.util.ArrayList ; 15 16 import org.eclipse.jface.contentassist.IContentAssistSubjectControl; 17 import org.eclipse.jface.contentassist.ISubjectControlContentAssistProcessor; 18 import org.eclipse.jface.contentassist.SubjectControlContextInformationValidator; 19 import org.eclipse.jface.text.ITextViewer; 20 import org.eclipse.jface.text.contentassist.CompletionProposal; 21 import org.eclipse.jface.text.contentassist.ICompletionProposal; 22 import org.eclipse.jface.text.contentassist.IContentAssistProcessor; 23 import org.eclipse.jface.text.contentassist.IContextInformation; 24 import org.eclipse.jface.text.contentassist.IContextInformationValidator; 25 26 31 final class RegExContentAssistProcessor implements IContentAssistProcessor, ISubjectControlContentAssistProcessor { 32 33 36 private static class ProposalComputer { 37 38 41 private final String fExpression; 42 45 private final int fDocumentOffset; 46 49 private final ArrayList fPriorityProposals; 50 53 private final ArrayList fProposals; 54 57 private final boolean fIsEscape; 58 59 64 public ProposalComputer(IContentAssistSubjectControl contentAssistSubjectControl, int documentOffset) { 65 fExpression= contentAssistSubjectControl.getDocument().get(); 66 fDocumentOffset= documentOffset; 67 fPriorityProposals= new ArrayList (); 68 fProposals= new ArrayList (); 69 70 boolean isEscape= false; 71 esc: for (int i= documentOffset - 1; i >= 0; i--) { 72 if (fExpression.charAt(i) == '\\') 73 isEscape= !isEscape; 74 else 75 break esc; 76 } 77 fIsEscape= isEscape; 78 } 79 80 84 public ICompletionProposal[] computeFindProposals() { 85 addBsProposal("\\\\", RegExMessages.displayString_bs_bs, RegExMessages.additionalInfo_bs_bs); addBracketProposal("\\0", 2, RegExMessages.displayString_bs_0, RegExMessages.additionalInfo_bs_0); addBracketProposal("\\x", 2, RegExMessages.displayString_bs_x, RegExMessages.additionalInfo_bs_x); addBracketProposal("\\u", 2, RegExMessages.displayString_bs_u, RegExMessages.additionalInfo_bs_u); addBsProposal("\\t", RegExMessages.displayString_bs_t, RegExMessages.additionalInfo_bs_t); addBsProposal("\\n", RegExMessages.displayString_bs_n, RegExMessages.additionalInfo_bs_n); addBsProposal("\\r", RegExMessages.displayString_bs_r, RegExMessages.additionalInfo_bs_r); addBsProposal("\\f", RegExMessages.displayString_bs_f, RegExMessages.additionalInfo_bs_f); addBsProposal("\\a", RegExMessages.displayString_bs_a, RegExMessages.additionalInfo_bs_a); addBsProposal("\\e", RegExMessages.displayString_bs_e, RegExMessages.additionalInfo_bs_e); addBsProposal("\\c", RegExMessages.displayString_bs_c, RegExMessages.additionalInfo_bs_c); 98 if (! fIsEscape) 99 addBracketProposal(".", 1, RegExMessages.displayString_dot, RegExMessages.additionalInfo_dot); addBsProposal("\\d", RegExMessages.displayString_bs_d, RegExMessages.additionalInfo_bs_d); addBsProposal("\\D", RegExMessages.displayString_bs_D, RegExMessages.additionalInfo_bs_D); addBsProposal("\\s", RegExMessages.displayString_bs_s, RegExMessages.additionalInfo_bs_s); addBsProposal("\\S", RegExMessages.displayString_bs_S, RegExMessages.additionalInfo_bs_S); addBsProposal("\\w", RegExMessages.displayString_bs_w, RegExMessages.additionalInfo_bs_w); addBsProposal("\\W", RegExMessages.displayString_bs_W, RegExMessages.additionalInfo_bs_W); 107 addBsProposal("\\", RegExMessages.displayString_bs_i, RegExMessages.additionalInfo_bs_i); 110 addBsProposal("\\", RegExMessages.displayString_bs, RegExMessages.additionalInfo_bs); addBsProposal("\\Q", RegExMessages.displayString_bs_Q, RegExMessages.additionalInfo_bs_Q); addBsProposal("\\E", RegExMessages.displayString_bs_E, RegExMessages.additionalInfo_bs_E); 115 if (! fIsEscape) { 117 addBracketProposal("[]", 1, RegExMessages.displayString_set, RegExMessages.additionalInfo_set); addBracketProposal("[^]", 2, RegExMessages.displayString_setExcl, RegExMessages.additionalInfo_setExcl); addBracketProposal("[-]", 1, RegExMessages.displayString_setRange, RegExMessages.additionalInfo_setRange); addProposal("&&", RegExMessages.displayString_setInter, RegExMessages.additionalInfo_setInter); } 122 if (! fIsEscape && fDocumentOffset > 0 && fExpression.charAt(fDocumentOffset - 1) == '\\') { 123 addProposal("\\p{}", 3, RegExMessages.displayString_posix, RegExMessages.additionalInfo_posix); addProposal("\\P{}", 3, RegExMessages.displayString_posixNot, RegExMessages.additionalInfo_posixNot); } else { 126 addBracketProposal("\\p{}", 3, RegExMessages.displayString_posix, RegExMessages.additionalInfo_posix); addBracketProposal("\\P{}", 3, RegExMessages.displayString_posixNot, RegExMessages.additionalInfo_posixNot); } 129 130 149 if (fDocumentOffset == 0) { 151 addPriorityProposal("^", RegExMessages.displayString_start, RegExMessages.additionalInfo_start); } else if (fDocumentOffset == 1 && fExpression.charAt(0) == '^') { 153 addBracketProposal("^", 1, RegExMessages.displayString_start, RegExMessages.additionalInfo_start); } 155 if (fDocumentOffset == fExpression.length()) { 156 addProposal("$", RegExMessages.displayString_end, RegExMessages.additionalInfo_end); } 158 addBsProposal("\\b", RegExMessages.displayString_bs_b, RegExMessages.additionalInfo_bs_b); addBsProposal("\\B", RegExMessages.displayString_bs_B, RegExMessages.additionalInfo_bs_B); addBsProposal("\\A", RegExMessages.displayString_bs_A, RegExMessages.additionalInfo_bs_A); addBsProposal("\\G", RegExMessages.displayString_bs_G, RegExMessages.additionalInfo_bs_G); addBsProposal("\\Z", RegExMessages.displayString_bs_Z, RegExMessages.additionalInfo_bs_Z); addBsProposal("\\z", RegExMessages.displayString_bs_z, RegExMessages.additionalInfo_bs_z); 165 if (! fIsEscape) { 166 addBracketProposal("()", 1, RegExMessages.displayString_group, RegExMessages.additionalInfo_group); 169 addBracketProposal("(?)", 2, RegExMessages.displayString_flag, RegExMessages.additionalInfo_flag); addBracketProposal("(?:)", 3, RegExMessages.displayString_flagExpr, RegExMessages.additionalInfo_flagExpr); 173 addBracketProposal("(?:)", 3, RegExMessages.displayString_nonCap, RegExMessages.additionalInfo_nonCap); addBracketProposal("(?>)", 3, RegExMessages.displayString_atomicCap, RegExMessages.additionalInfo_atomicCap); 177 addBracketProposal("(?=)", 3, RegExMessages.displayString_posLookahead, RegExMessages.additionalInfo_posLookahead); addBracketProposal("(?!)", 3, RegExMessages.displayString_negLookahead, RegExMessages.additionalInfo_negLookahead); addBracketProposal("(?<=)", 4, RegExMessages.displayString_posLookbehind, RegExMessages.additionalInfo_posLookbehind); addBracketProposal("(?<!)", 4, RegExMessages.displayString_negLookbehind, RegExMessages.additionalInfo_negLookbehind); 183 addBracketProposal("?", 1, RegExMessages.displayString_quest, RegExMessages.additionalInfo_quest); addBracketProposal("*", 1, RegExMessages.displayString_star, RegExMessages.additionalInfo_star); addBracketProposal("+", 1, RegExMessages.displayString_plus, RegExMessages.additionalInfo_plus); addBracketProposal("{}", 1, RegExMessages.displayString_exact, RegExMessages.additionalInfo_exact); addBracketProposal("{,}", 1, RegExMessages.displayString_least, RegExMessages.additionalInfo_least); addBracketProposal("{,}", 1, RegExMessages.displayString_count, RegExMessages.additionalInfo_count); 191 addBracketProposal("??", 1, RegExMessages.displayString_questLazy, RegExMessages.additionalInfo_questLazy); addBracketProposal("*?", 1, RegExMessages.displayString_starLazy, RegExMessages.additionalInfo_starLazy); addBracketProposal("+?", 1, RegExMessages.displayString_plusLazy, RegExMessages.additionalInfo_plusLazy); addBracketProposal("{}?", 1, RegExMessages.displayString_exactLazy, RegExMessages.additionalInfo_exactLazy); addBracketProposal("{,}?", 1, RegExMessages.displayString_leastLazy, RegExMessages.additionalInfo_leastLazy); addBracketProposal("{,}?", 1, RegExMessages.displayString_countLazy, RegExMessages.additionalInfo_countLazy); 199 addBracketProposal("?+", 1, RegExMessages.displayString_questPoss, RegExMessages.additionalInfo_questPoss); addBracketProposal("*+", 1, RegExMessages.displayString_starPoss, RegExMessages.additionalInfo_starPoss); addBracketProposal("++", 1, RegExMessages.displayString_plusPoss, RegExMessages.additionalInfo_plusPoss); addBracketProposal("{}+", 1, RegExMessages.displayString_exactPoss, RegExMessages.additionalInfo_exactPoss); addBracketProposal("{,}+", 1, RegExMessages.displayString_leastPoss, RegExMessages.additionalInfo_leastPoss); addBracketProposal("{,}+", 1, RegExMessages.displayString_countPoss, RegExMessages.additionalInfo_countPoss); 207 addBracketProposal("|", 1, RegExMessages.displayString_alt, RegExMessages.additionalInfo_alt); } 210 211 fPriorityProposals.addAll(fProposals); 212 return (ICompletionProposal[]) fPriorityProposals.toArray(new ICompletionProposal[fProposals.size()]); 213 } 214 215 219 public ICompletionProposal[] computeReplaceProposals() { 220 if (fDocumentOffset > 0 && '$' == fExpression.charAt(fDocumentOffset - 1)) { 221 addProposal("", RegExMessages.displayString_dollar, RegExMessages.additionalInfo_dollar); } else { 223 addProposal("$", RegExMessages.displayString_dollar, RegExMessages.additionalInfo_dollar); addBsProposal("\\", RegExMessages.displayString_replace_bs, RegExMessages.additionalInfo_replace_bs); addProposal("\t", RegExMessages.displayString_tab, RegExMessages.additionalInfo_tab); } 227 return (ICompletionProposal[]) fProposals.toArray(new ICompletionProposal[fProposals.size()]); 228 } 229 230 237 private void addProposal(String proposal, String displayString, String additionalInfo) { 238 fProposals.add(new CompletionProposal(proposal, fDocumentOffset, 0, proposal.length(), null, displayString, null, additionalInfo)); 239 } 240 241 250 private void addProposal(String proposal, int cursorPosition, String displayString, String additionalInfo) { 251 fProposals.add(new CompletionProposal(proposal, fDocumentOffset, 0, cursorPosition, null, displayString, null, additionalInfo)); 252 } 253 254 261 private void addPriorityProposal(String proposal, String displayString, String additionalInfo) { 262 fPriorityProposals.add(new CompletionProposal(proposal, fDocumentOffset, 0, proposal.length(), null, displayString, null, additionalInfo)); 263 } 264 265 274 private void addBracketProposal(String proposal, int cursorPosition, String displayString, String additionalInfo) { 275 String prolog= fExpression.substring(0, fDocumentOffset); 276 if (! fIsEscape && prolog.endsWith("\\") && proposal.startsWith("\\")) { fProposals.add(new CompletionProposal(proposal, fDocumentOffset, 0, cursorPosition, null, displayString, null, additionalInfo)); 278 return; 279 } 280 for (int i= 1; i <= cursorPosition; i++) { 281 String prefix= proposal.substring(0, i); 282 if (prolog.endsWith(prefix)) { 283 String postfix= proposal.substring(cursorPosition); 284 String epilog= fExpression.substring(fDocumentOffset); 285 if (epilog.startsWith(postfix)) { 286 fPriorityProposals.add(new CompletionProposal(proposal.substring(i, cursorPosition), fDocumentOffset, 0, cursorPosition - i, null, displayString, null, additionalInfo)); 287 } else { 288 fPriorityProposals.add(new CompletionProposal(proposal.substring(i), fDocumentOffset, 0, cursorPosition - i, null, displayString, null, additionalInfo)); 289 } 290 return; 291 } 292 } 293 fProposals.add(new CompletionProposal(proposal, fDocumentOffset, 0, cursorPosition, null, displayString, null, additionalInfo)); 294 } 295 296 303 private void addBsProposal(String proposal, String displayString, String additionalInfo) { 304 if (fIsEscape) { 305 fPriorityProposals.add(new CompletionProposal(proposal.substring(1), fDocumentOffset, 0, proposal.length() - 1, null, displayString, null, additionalInfo)); 306 } else { 307 addProposal(proposal, displayString, additionalInfo); 308 } 309 } 310 } 311 312 315 private IContextInformationValidator fValidator= new SubjectControlContextInformationValidator(this); 316 317 321 private final boolean fIsFind; 322 323 public RegExContentAssistProcessor(boolean isFind) { 324 fIsFind= isFind; 325 } 326 327 330 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) { 331 throw new UnsupportedOperationException ("ITextViewer not supported"); } 333 334 337 public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) { 338 throw new UnsupportedOperationException ("ITextViewer not supported"); } 340 341 344 public char[] getCompletionProposalAutoActivationCharacters() { 345 if (fIsFind) 346 return new char[] {'\\', '[', '('}; 347 348 return new char[] {'$'}; 349 } 350 351 354 public char[] getContextInformationAutoActivationCharacters() { 355 return new char[] { }; 356 } 357 358 361 public IContextInformationValidator getContextInformationValidator() { 362 return fValidator; 363 } 364 365 368 public String getErrorMessage() { 369 return null; 370 } 371 372 375 public ICompletionProposal[] computeCompletionProposals(IContentAssistSubjectControl contentAssistSubjectControl, int documentOffset) { 376 if (fIsFind) 377 return new ProposalComputer(contentAssistSubjectControl, documentOffset).computeFindProposals(); 378 379 return new ProposalComputer(contentAssistSubjectControl, documentOffset).computeReplaceProposals(); 380 } 381 382 385 public IContextInformation[] computeContextInformation(IContentAssistSubjectControl contentAssistSubjectControl, int documentOffset) { 386 return null; 387 } 388 } 389 | Popular Tags |