Java Code Examples for com.intellij.openapi.util.text.StringUtil#escapeToRegexp()

The following examples show how to use com.intellij.openapi.util.text.StringUtil#escapeToRegexp() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StringPattern.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
public StringPattern matches(@NonNls @Nonnull final String s) {
  final String escaped = StringUtil.escapeToRegexp(s);
  if (escaped.equals(s)) {
    return equalTo(s);
  }
  // may throw PatternSyntaxException here
  final Pattern pattern = Pattern.compile(s);
  return with(new ValuePatternCondition<String>("matches") {
    @Override
    public boolean accepts(@Nonnull final String str, final ProcessingContext context) {
      return pattern.matcher(newBombedCharSequence(str)).matches();
    }

    @Override
    public Collection<String> getValues() {
      return Collections.singleton(s);
    }
  });
}
 
Example 2
Source File: FindManagerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
private static FindModel normalizeIfMultilined(@Nonnull FindModel findmodel) {
  if (findmodel.isMultiline()) {
    final FindModel model = new FindModel();
    model.copyFrom(findmodel);
    final String s = model.getStringToFind();
    String newStringToFind;

    if (findmodel.isRegularExpressions()) {
      newStringToFind = StringUtil.replace(s, "\n", "\\n\\s*"); // add \\s* for convenience
    }
    else {
      newStringToFind = StringUtil.escapeToRegexp(s);
      model.setRegularExpressions(true);
    }
    model.setStringToFind(newStringToFind);

    return model;
  }
  return findmodel;
}
 
Example 3
Source File: BlazeJUnitTestFilterFlags.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static String testFilterForLocation(Location<?> location) {
  PsiElement psi = location.getPsiElement();
  assert (psi instanceof PsiMethod);
  String methodName = ((PsiMethod) psi).getName();
  if (location instanceof PsiMemberParameterizedLocation) {
    return methodName
        + StringUtil.escapeToRegexp(
            ((PsiMemberParameterizedLocation) location).getParamSetName());
  }
  return methodName;
}
 
Example 4
Source File: StringPattern.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public StringPattern matchesBrics(@NonNls @Nonnull final String s) {
  final String escaped = StringUtil.escapeToRegexp(s);
  if (escaped.equals(s)) {
    return equalTo(s);
  }

  StringBuilder sb = new StringBuilder(s.length()*5);
  for (int i = 0; i < s.length(); i++) {
    final char c = s.charAt(i);
    if(c == ' ') {
      sb.append("<whitespace>");
    }
    else
    //This is really stupid and inconvenient builder - it breaks any normal pattern with uppercase
    if(Character.isUpperCase(c)) {
      sb.append('[').append(Character.toUpperCase(c)).append(Character.toLowerCase(c)).append(']');
    }
    else
    {
      sb.append(c);
    }
  }
  final RegExp regExp = new RegExp(sb.toString());
  final Automaton automaton = regExp.toAutomaton(new DatatypesAutomatonProvider());
  final RunAutomaton runAutomaton = new RunAutomaton(automaton, true);

  return with(new ValuePatternCondition<String>("matchesBrics") {
    @Override
    public boolean accepts(@Nonnull String str, final ProcessingContext context) {
      if (!str.isEmpty() && (str.charAt(0) == '"' || str.charAt(0) == '\'')) str = str.substring(1);
      return runAutomaton.run(str);
    }

    @Override
    public Collection<String> getValues() {
      return Collections.singleton(s);
    }
  });
}
 
Example 5
Source File: HighlightUsagesHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void doRangeHighlighting(Editor editor, Project project) {
  if (!editor.getSelectionModel().hasSelection()) return;

  final String text = editor.getSelectionModel().getSelectedText();
  if (text == null) return;

  if (editor instanceof EditorWindow) {
    // highlight selection in the whole editor, not injected fragment only
    editor = ((EditorWindow)editor).getDelegate();
  }

  EditorSearchSession oldSearch = EditorSearchSession.get(editor);
  if (oldSearch != null) {
    if (oldSearch.hasMatches()) {
      String oldText = oldSearch.getTextInField();
      if (!oldSearch.getFindModel().isRegularExpressions()) {
        oldText = StringUtil.escapeToRegexp(oldText);
        oldSearch.getFindModel().setRegularExpressions(true);
      }

      String newText = oldText + '|' + StringUtil.escapeToRegexp(text);
      oldSearch.setTextInField(newText);
      return;
    }
  }

  EditorSearchSession.start(editor, project).getFindModel().setRegularExpressions(false);
}