1 11 12 package org.eclipse.ui.texteditor; 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 38 private static class ProposalComputer { 39 40 43 private final String fExpression; 44 47 private final int fDocumentOffset; 48 51 private final ArrayList fPriorityProposals; 52 55 private final ArrayList fProposals; 56 59 private final boolean fIsEscape; 60 61 66 public ProposalComputer(IContentAssistSubjectControl contentAssistSubjectControl, int documentOffset) { 67 fExpression= contentAssistSubjectControl.getDocument().get(); 68 fDocumentOffset= documentOffset; 69 fPriorityProposals= new ArrayList (); 70 fProposals= new ArrayList (); 71 72 boolean isEscape= false; 73 esc: for (int i= documentOffset - 1; i >= 0; i--) { 74 if (fExpression.charAt(i) == '\\') 75 isEscape= !isEscape; 76 else 77 break esc; 78 } 79 fIsEscape= isEscape; 80 } 81 82 86 public ICompletionProposal[] computeFindProposals() { 87 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); 100 if (! fIsEscape) 101 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); 109 addBsProposal("\\", RegExMessages.displayString_bs_i, RegExMessages.additionalInfo_bs_i); 112 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); 117 if (! fIsEscape) { 119 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); } 124 if (! fIsEscape && fDocumentOffset > 0 && fExpression.charAt(fDocumentOffset - 1) == '\\') { 125 addProposal("\\p{}", 3, RegExMessages.displayString_posix, RegExMessages.additionalInfo_posix); addProposal("\\P{}", 3, RegExMessages.displayString_posixNot, RegExMessages.additionalInfo_posixNot); } else { 128 addBracketProposal("\\p{}", 3, RegExMessages.displayString_posix, RegExMessages.additionalInfo_posix); addBracketProposal("\\P{}", 3, RegExMessages.displayString_posixNot, RegExMessages.additionalInfo_posixNot); } 131 132 151 if (fDocumentOffset == 0) { 153 addPriorityProposal("^", RegExMessages.displayString_start, RegExMessages.additionalInfo_start); } else if (fDocumentOffset == 1 && fExpression.charAt(0) == '^') { 155 addBracketProposal("^", 1, RegExMessages.displayString_start, RegExMessages.additionalInfo_start); } 157 if (fDocumentOffset == fExpression.length()) { 158 addProposal("$", RegExMessages.displayString_end, RegExMessages.additionalInfo_end); } 160 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); 167 if (! fIsEscape) { 168 addBracketProposal("()", 1, RegExMessages.displayString_group, RegExMessages.additionalInfo_group); 171 addBracketProposal("(?)", 2, RegExMessages.displayString_flag, RegExMessages.additionalInfo_flag); addBracketProposal("(?:)", 3, RegExMessages.displayString_flagExpr, RegExMessages.additionalInfo_flagExpr); 175 addBracketProposal("(?:)", 3, RegExMessages.displayString_nonCap, RegExMessages.additionalInfo_nonCap); addBracketProposal("(?>)", 3, RegExMessages.displayString_atomicCap, RegExMessages.additionalInfo_atomicCap); 179 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); 185 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); 193 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); 201 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); 209 addBracketProposal("|", 1, RegExMessages.displayString_alt, RegExMessages.additionalInfo_alt); } 212 213 fPriorityProposals.addAll(fProposals); 214 return (ICompletionProposal[]) fPriorityProposals.toArray(new ICompletionProposal[fProposals.size()]); 215 } 216 217 221 public ICompletionProposal[] computeReplaceProposals() { 222 if (fDocumentOffset > 0 && '$' == fExpression.charAt(fDocumentOffset - 1)) { 223 addProposal("", RegExMessages.displayString_dollar, RegExMessages.additionalInfo_dollar); } else { 225 addProposal("$", RegExMessages.displayString_dollar, RegExMessages.additionalInfo_dollar); addBsProposal("\\", RegExMessages.displayString_replace_bs, RegExMessages.additionalInfo_replace_bs); addProposal("\t", RegExMessages.displayString_tab, RegExMessages.additionalInfo_tab); } 229 return (ICompletionProposal[]) fProposals.toArray(new ICompletionProposal[fProposals.size()]); 230 } 231 232 239 private void addProposal(String proposal, String displayString, String additionalInfo) { 240 fProposals.add(new CompletionProposal(proposal, fDocumentOffset, 0, proposal.length(), null, displayString, null, additionalInfo)); 241 } 242 243 252 private void addProposal(String proposal, int cursorPosition, String displayString, String additionalInfo) { 253 fProposals.add(new CompletionProposal(proposal, fDocumentOffset, 0, cursorPosition, null, displayString, null, additionalInfo)); 254 } 255 256 263 private void addPriorityProposal(String proposal, String displayString, String additionalInfo) { 264 fPriorityProposals.add(new CompletionProposal(proposal, fDocumentOffset, 0, proposal.length(), null, displayString, null, additionalInfo)); 265 } 266 267 276 private void addBracketProposal(String proposal, int cursorPosition, String displayString, String additionalInfo) { 277 String prolog= fExpression.substring(0, fDocumentOffset); 278 if (! fIsEscape && prolog.endsWith("\\") && proposal.startsWith("\\")) { fProposals.add(new CompletionProposal(proposal, fDocumentOffset, 0, cursorPosition, null, displayString, null, additionalInfo)); 280 return; 281 } 282 for (int i= 1; i <= cursorPosition; i++) { 283 String prefix= proposal.substring(0, i); 284 if (prolog.endsWith(prefix)) { 285 String postfix= proposal.substring(cursorPosition); 286 String epilog= fExpression.substring(fDocumentOffset); 287 if (epilog.startsWith(postfix)) { 288 fPriorityProposals.add(new CompletionProposal(proposal.substring(i, cursorPosition), fDocumentOffset, 0, cursorPosition - i, null, displayString, null, additionalInfo)); 289 } else { 290 fPriorityProposals.add(new CompletionProposal(proposal.substring(i), fDocumentOffset, 0, cursorPosition - i, null, displayString, null, additionalInfo)); 291 } 292 return; 293 } 294 } 295 fProposals.add(new CompletionProposal(proposal, fDocumentOffset, 0, cursorPosition, null, displayString, null, additionalInfo)); 296 } 297 298 305 private void addBsProposal(String proposal, String displayString, String additionalInfo) { 306 if (fIsEscape) { 307 fPriorityProposals.add(new CompletionProposal(proposal.substring(1), fDocumentOffset, 0, proposal.length() - 1, null, displayString, null, additionalInfo)); 308 } else { 309 addProposal(proposal, displayString, additionalInfo); 310 } 311 } 312 } 313 314 317 private IContextInformationValidator fValidator= new SubjectControlContextInformationValidator(this); 318 319 324 private final boolean fIsFind; 325 326 329 public RegExContentAssistProcessor(boolean isFind) { 330 fIsFind= isFind; 331 } 332 333 336 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) { 337 return null; 338 } 339 340 343 public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) { 344 return null; 345 } 346 347 350 public char[] getCompletionProposalAutoActivationCharacters() { 351 if (fIsFind) 352 return new char[] {'\\', '[', '('}; 353 354 return new char[] {'$'}; 355 } 356 357 360 public char[] getContextInformationAutoActivationCharacters() { 361 return new char[] { }; 362 } 363 364 367 public IContextInformationValidator getContextInformationValidator() { 368 return fValidator; 369 } 370 371 374 public String getErrorMessage() { 375 return null; 376 } 377 378 381 public ICompletionProposal[] computeCompletionProposals(IContentAssistSubjectControl contentAssistSubjectControl, int documentOffset) { 382 if (fIsFind) 383 return new ProposalComputer(contentAssistSubjectControl, documentOffset).computeFindProposals(); 384 385 return new ProposalComputer(contentAssistSubjectControl, documentOffset).computeReplaceProposals(); 386 } 387 388 391 public IContextInformation[] computeContextInformation(IContentAssistSubjectControl contentAssistSubjectControl, int documentOffset) { 392 return null; 393 } 394 } 395 | Popular Tags |