1 11 12 package org.eclipse.ui.texteditor; 13 14 import java.util.ArrayList ; 15 16 import org.eclipse.jface.fieldassist.IContentProposal; 17 import org.eclipse.jface.fieldassist.IContentProposalProvider; 18 19 27 final class RegExContentProposalProvider implements IContentProposalProvider { 28 29 30 33 private static class ProposalComputer { 34 35 private static class Proposal implements IContentProposal { 36 37 private String fContent; 38 private String fLabel; 39 private String fDescription; 40 private int fCursorPosition; 41 42 Proposal(String content, String label, String description, int cursorPosition) { 43 fContent= content; 44 fLabel= label; 45 fDescription= description; 46 fCursorPosition= cursorPosition; 47 } 48 49 public String getContent() { 50 return fContent; 51 } 52 53 public String getLabel() { 54 return fLabel; 55 } 56 57 public String getDescription() { 58 return fDescription; 59 } 60 61 public int getCursorPosition() { 62 return fCursorPosition; 63 } 64 } 65 66 67 70 private final String fExpression; 71 74 private final int fDocumentOffset; 75 78 private final ArrayList fPriorityProposals; 79 82 private final ArrayList fProposals; 83 86 private final boolean fIsEscape; 87 88 93 public ProposalComputer(String contents, int position) { 94 fExpression= contents; 95 fDocumentOffset= position; 96 fPriorityProposals= new ArrayList (); 97 fProposals= new ArrayList (); 98 99 boolean isEscape= false; 100 esc: for (int i= position - 1; i >= 0; i--) { 101 if (fExpression.charAt(i) == '\\') 102 isEscape= !isEscape; 103 else 104 break esc; 105 } 106 fIsEscape= isEscape; 107 } 108 109 113 public IContentProposal[] computeFindProposals() { 114 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); 127 if (! fIsEscape) 128 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); 136 addBsProposal("\\", RegExMessages.displayString_bs_i, RegExMessages.additionalInfo_bs_i); 139 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); 144 if (! fIsEscape) { 146 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); } 151 if (! fIsEscape && fDocumentOffset > 0 && fExpression.charAt(fDocumentOffset - 1) == '\\') { 152 addProposal("\\p{}", 3, RegExMessages.displayString_posix, RegExMessages.additionalInfo_posix); addProposal("\\P{}", 3, RegExMessages.displayString_posixNot, RegExMessages.additionalInfo_posixNot); } else { 155 addBracketProposal("\\p{}", 3, RegExMessages.displayString_posix, RegExMessages.additionalInfo_posix); addBracketProposal("\\P{}", 3, RegExMessages.displayString_posixNot, RegExMessages.additionalInfo_posixNot); } 158 159 178 if (fDocumentOffset == 0) { 180 addPriorityProposal("^", RegExMessages.displayString_start, RegExMessages.additionalInfo_start); } else if (fDocumentOffset == 1 && fExpression.charAt(0) == '^') { 182 addBracketProposal("^", 1, RegExMessages.displayString_start, RegExMessages.additionalInfo_start); } 184 if (fDocumentOffset == fExpression.length()) { 185 addProposal("$", RegExMessages.displayString_end, RegExMessages.additionalInfo_end); } 187 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); 194 if (! fIsEscape) { 195 addBracketProposal("()", 1, RegExMessages.displayString_group, RegExMessages.additionalInfo_group); 198 addBracketProposal("(?)", 2, RegExMessages.displayString_flag, RegExMessages.additionalInfo_flag); addBracketProposal("(?:)", 3, RegExMessages.displayString_flagExpr, RegExMessages.additionalInfo_flagExpr); 202 addBracketProposal("(?:)", 3, RegExMessages.displayString_nonCap, RegExMessages.additionalInfo_nonCap); addBracketProposal("(?>)", 3, RegExMessages.displayString_atomicCap, RegExMessages.additionalInfo_atomicCap); 206 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); 212 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); 220 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); 228 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); 236 addBracketProposal("|", 1, RegExMessages.displayString_alt, RegExMessages.additionalInfo_alt); } 239 240 fPriorityProposals.addAll(fProposals); 241 return (IContentProposal[]) fPriorityProposals.toArray(new IContentProposal[fProposals.size()]); 242 } 243 244 248 public IContentProposal[] computeReplaceProposals() { 249 if (fDocumentOffset > 0 && '$' == fExpression.charAt(fDocumentOffset - 1)) { 250 addProposal("", RegExMessages.displayString_dollar, RegExMessages.additionalInfo_dollar); } else { 252 addProposal("$", RegExMessages.displayString_dollar, RegExMessages.additionalInfo_dollar); addBsProposal("\\", RegExMessages.displayString_replace_bs, RegExMessages.additionalInfo_replace_bs); addProposal("\t", RegExMessages.displayString_tab, RegExMessages.additionalInfo_tab); } 256 return (IContentProposal[]) fProposals.toArray(new IContentProposal[fProposals.size()]); 257 } 258 259 266 private void addProposal(String proposal, String displayString, String additionalInfo) { 267 fProposals.add(new Proposal(proposal, displayString, additionalInfo, proposal.length())); 268 } 269 270 279 private void addProposal(String proposal, int cursorPosition, String displayString, String additionalInfo) { 280 fProposals.add(new Proposal(proposal, displayString, additionalInfo, cursorPosition)); 281 } 282 283 290 private void addPriorityProposal(String proposal, String displayString, String additionalInfo) { 291 fPriorityProposals.add(new Proposal(proposal, displayString, additionalInfo, proposal.length())); 292 } 293 294 303 private void addBracketProposal(String proposal, int cursorPosition, String displayString, String additionalInfo) { 304 String prolog= fExpression.substring(0, fDocumentOffset); 305 if (! fIsEscape && prolog.endsWith("\\") && proposal.startsWith("\\")) { fProposals.add(new Proposal(proposal, displayString, additionalInfo, cursorPosition)); 307 return; 308 } 309 for (int i= 1; i <= cursorPosition; i++) { 310 String prefix= proposal.substring(0, i); 311 if (prolog.endsWith(prefix)) { 312 String postfix= proposal.substring(cursorPosition); 313 String epilog= fExpression.substring(fDocumentOffset); 314 if (epilog.startsWith(postfix)) { 315 fPriorityProposals.add(new Proposal(proposal.substring(i, cursorPosition), displayString, additionalInfo, cursorPosition-i)); 316 } else { 317 fPriorityProposals.add(new Proposal(proposal.substring(i), displayString, additionalInfo, cursorPosition-i)); 318 } 319 return; 320 } 321 } 322 fProposals.add(new Proposal(proposal, displayString, additionalInfo, cursorPosition)); 323 } 324 325 333 private void addBsProposal(String proposal, String displayString, String additionalInfo) { 334 String prolog= fExpression.substring(0, fDocumentOffset); 335 int position= proposal.length(); 336 if (prolog.endsWith("\\")) { position--; 339 proposal= proposal.substring(1); 340 } 341 342 if (fIsEscape) { 343 fPriorityProposals.add(new Proposal(proposal, displayString, additionalInfo, position)); 344 } else { 345 addProposal(proposal, position, displayString, additionalInfo); 346 } 347 } 348 } 349 350 354 private final boolean fIsFind; 355 356 357 363 public RegExContentProposalProvider(boolean isFind) { 364 fIsFind= isFind; 365 } 366 367 370 public IContentProposal [] getProposals(String contents, int position) { 371 if (fIsFind) 372 return new ProposalComputer(contents, position).computeFindProposals(); 373 return new ProposalComputer(contents, position).computeReplaceProposals(); 374 } 375 } 376 | Popular Tags |