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

The following examples show how to use com.intellij.openapi.util.text.StringUtil#replace() . 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: ExtendedRegexFilter.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
private static String substituteMacrosWithRegexps(String expression) {
    int filePathIndex = expression.indexOf(FILE_PATH_MACROS);
    int lineIndex = expression.indexOf(LINE_MACROS);
    int columnIndex = expression.indexOf(COLUMN_MACROS);

    if (filePathIndex == -1) {
        throw new InvalidExpressionException("Expression must contain " + FILE_PATH_MACROS + " macros.");
    }

    expression = StringUtil.replace(expression, FILE_PATH_MACROS, FILE_PATH_REGEXP);

    if (lineIndex != -1) {
        expression = StringUtil.replace(expression, LINE_MACROS, NUMBER_REGEXP);
    }

    if (columnIndex != -1) {
        expression = StringUtil.replace(expression, COLUMN_MACROS, NUMBER_REGEXP);
    }
    return expression;
}
 
Example 2
Source File: HighlightCommand.java    From CppTools with Apache License 2.0 6 votes vote down vote up
private static String getMessageText(int messageId, String message, String params) {
  MessageInfo messageInfo = myIndexToErrorInfo.get(messageId);
  String completeMessage = messageInfo != null ? messageInfo.message:null;

  if (completeMessage != null) {
    message = completeMessage;
    int offset = 0;
    int previousOffset = 0;
    for(int i = 0; i < messageInfo.parameterCount; ++i) {
      previousOffset = offset;
      offset = offset != params.length() ? Communicator.findDelimiter(params,offset):params.length();
      message = StringUtil.replace(
        message,
        "%"+i,
        previousOffset != params.length() ?
          BuildingCommandHelper.unquote(params.substring(previousOffset, offset)):""
      );
    }
  }
  return message;
}
 
Example 3
Source File: DiffUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static JPanel createMessagePanel(@Nonnull String message) {
  String text = StringUtil.replace(message, "\n", "<br>");
  JLabel label = new JBLabel(text) {
    @Override
    public Dimension getMinimumSize() {
      Dimension size = super.getMinimumSize();
      size.width = Math.min(size.width, 200);
      size.height = Math.min(size.height, 100);
      return size;
    }
  }.setCopyable(true);
  label.setForeground(UIUtil.getInactiveTextColor());

  return new CenteredPanel(label, JBUI.Borders.empty(5));
}
 
Example 4
Source File: DesktopContainerPathManager.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Contract("null, _ -> null")
public static String substituteVars(String s, String ideaHomePath) {
  if (s == null) return null;
  if (s.startsWith("..")) {
    s = ideaHomePath + File.separatorChar + BIN_FOLDER + File.separatorChar + s;
  }
  s = StringUtil.replace(s, "${idea.home}", ideaHomePath);
  final Properties props = System.getProperties();
  final Set keys = props.keySet();
  for (final Object key1 : keys) {
    String key = (String)key1;
    String value = props.getProperty(key);
    s = StringUtil.replace(s, "${" + key + "}", value);
  }
  return s;
}
 
Example 5
Source File: FoldingTestCase.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
protected void runTestInternal() throws Throwable {
  String filePath = myFullDataPath + "/" + getTestName(false) + "." + myExtension;
  File file = new File(filePath);

  String expectedContent;
  try {
    expectedContent = FileUtil.loadFile(file);
  }
  catch (IOException e) {
    throw new RuntimeException(e);
  }
  Assert.assertNotNull(expectedContent);

  expectedContent = StringUtil.replace(expectedContent, "\r", "");
  final String cleanContent = expectedContent.replaceAll(START_FOLD, "").replaceAll(END_FOLD, "");
  final String actual = getFoldingDescription(cleanContent, file.getName(), myDoCheckCollapseStatus);

  Assert.assertEquals(expectedContent, actual);
}
 
Example 6
Source File: DartVmServiceValue.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean computeVarHavingStringValuePresentation(@NotNull final XValueNode node) {
  // getValueAsString() is provided for the instance kinds: Null, Bool, Double, Int, String (value may be truncated), Float32x4, Float64x2, Int32x4, StackTrace
  switch (myInstanceRef.getKind()) {
    case Null:
    case Bool:
      node.setPresentation(getIcon(), new XKeywordValuePresentation(myInstanceRef.getValueAsString()), false);
      break;
    case Double:
    case Int:
      node.setPresentation(getIcon(), new XNumericValuePresentation(myInstanceRef.getValueAsString()), false);
      break;
    case String:
      final String presentableValue = StringUtil.replace(myInstanceRef.getValueAsString(), "\"", "\\\"");
      node.setPresentation(getIcon(), new XStringValuePresentation(presentableValue), false);

      if (myInstanceRef.getValueAsStringIsTruncated()) {
        addFullStringValueEvaluator(node, myInstanceRef);
      }
      break;
    case Float32x4:
    case Float64x2:
    case Int32x4:
    case StackTrace:
      node.setFullValueEvaluator(new ImmediateFullValueEvaluator("Click to see stack trace...", myInstanceRef.getValueAsString()));
      node.setPresentation(getIcon(), myInstanceRef.getClassRef().getName(), "", true);
      break;
    default:
      return false;
  }
  return true;
}
 
Example 7
Source File: BaseApplication.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void unmarkThreadNameInStackTrace() {
  String id = id();

  if (id != null) {
    final Thread thread = Thread.currentThread();
    String name = thread.getName();
    name = StringUtil.replace(name, id, "");
    thread.setName(name);
  }
}
 
Example 8
Source File: CommandLineUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static List<String> toCommandLine(@Nonnull String command, @Nonnull List<String> parameters, @Nonnull FilePathSeparator filePathSeparator) {
  List<String> commandLine = ContainerUtil.newArrayListWithCapacity(parameters.size() + 1);

  commandLine.add(FileUtilRt.toSystemDependentName(command, filePathSeparator.fileSeparator));

  boolean isWindows = filePathSeparator == FilePathSeparator.WINDOWS;
  boolean winShell = isWindows && isWinShell(command);

  for (String parameter : parameters) {
    if (isWindows) {
      if (parameter.contains("\"")) {
        parameter = StringUtil.replace(parameter, "\"", "\\\"");
      }
      else if (parameter.isEmpty()) {
        parameter = "\"\"";
      }
    }

    if (winShell && StringUtil.containsAnyChar(parameter, WIN_SHELL_SPECIALS)) {
      parameter = quote(parameter, SPECIAL_QUOTE);
    }

    if (isQuoted(parameter, SPECIAL_QUOTE)) {
      parameter = quote(parameter.substring(1, parameter.length() - 1), '"');
    }

    commandLine.add(parameter);
  }

  return commandLine;
}
 
Example 9
Source File: FileTemplateManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static String normalizeText(@Nonnull String text) {
  text = StringUtil.convertLineSeparators(text);
  text = StringUtil.replace(text, "$NAME$", "${NAME}");
  text = StringUtil.replace(text, "$PACKAGE_NAME$", "${PACKAGE_NAME}");
  text = StringUtil.replace(text, "$DATE$", "${DATE}");
  text = StringUtil.replace(text, "$TIME$", "${TIME}");
  text = StringUtil.replace(text, "$USER$", "${USER}");
  return text;
}
 
Example 10
Source File: FileSetTestCase.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected void runTest() throws Throwable {
  String content = FileUtil.loadFile(myTestFile);
  assertNotNull(content);

  List<String> input = new ArrayList<String>();

  int separatorIndex;

  content = StringUtil.replace(content, "\r", "");

  while ((separatorIndex = content.indexOf(getDelimiter())) >= 0) {
    input.add(content.substring(0, separatorIndex));
    content = content.substring(separatorIndex);
    while (StringUtil.startsWithChar(content, '-') || StringUtil.startsWithChar(content, '\n')) content = content.substring(1);
  }

  String result = content;

  assertTrue("No data found in source file", input.size() > 0);

  while (StringUtil.startsWithChar(result, '-') || StringUtil.startsWithChar(result, '\n') || StringUtil.startsWithChar(result, '\r')) {
    result = result.substring(1);
  }
  final String transformed;
  FileSetTestCase.this.myProject = getProject();
  String testName = myTestFile.getName();
  final int dotIdx = testName.indexOf('.');
  if (dotIdx >= 0) {
    testName = testName.substring(0, dotIdx);
  }

  transformed = StringUtil.replace(transform(testName, ArrayUtil.toStringArray(input)), "\r", "");
  result = StringUtil.replace(result, "\r", "");

  assertEquals(result.trim(),transformed.trim());
}
 
Example 11
Source File: CreateRTAction.java    From react-templates-plugin with MIT License 5 votes vote down vote up
private static String getControllerTemplate(String name, String modules) {
        String s = "";
        try {
            String ext = RTRunner.TYPESCRIPT.equals(modules) ? "ts" : "js";
            String tplName = "/fileTemplates/internal/RT Controller File " + modules + '.' + ext + ".ft";
            s = FileUtil.loadTextAndClose(CreateRTAction.class.getResourceAsStream(tplName));
            s = StringUtil.replace(s, "$name$", name);
        } catch (IOException e) {
//      throw new IncorrectOperationException(RTBundle.message("error.cannot.read", formName), (Throwable)e);
        }
        return StringUtil.convertLineSeparators(s);
    }
 
Example 12
Source File: DefaultChooseByNameItemProvider.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private static MatchResult matchQualifiedName(ChooseByNameModel model, MinusculeMatcher fullMatcher, @Nonnull Object element) {
  String fullName = model.getFullName(element);
  if (fullName == null) return null;

  for (String separator : model.getSeparators()) {
    fullName = StringUtil.replace(fullName, separator, UNIVERSAL_SEPARATOR);
  }
  return matchName(fullMatcher, fullName);
}
 
Example 13
Source File: HighlightInfo.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * Encodes \p tooltip so that substrings equal to a \p description
 * are replaced with the special placeholder to reduce size of the
 * tooltip. If encoding takes place, <html></html> tags are
 * stripped of the tooltip.
 *
 * @param tooltip     - html text
 * @param description - plain text (not escaped)
 * @return encoded tooltip (stripped html text with one or more placeholder characters)
 * or tooltip without changes.
 */
private static String encodeTooltip(String tooltip, String description) {
  if (tooltip == null || description == null || description.isEmpty()) return tooltip;

  String encoded = StringUtil.replace(tooltip, XmlStringUtil.escapeString(description), DESCRIPTION_PLACEHOLDER);
  //noinspection StringEquality
  if (encoded == tooltip) {
    return tooltip;
  }
  if (encoded.equals(DESCRIPTION_PLACEHOLDER)) encoded = DESCRIPTION_PLACEHOLDER;
  return XmlStringUtil.stripHtml(encoded);
}
 
Example 14
Source File: EventLog.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void appendText(Document document, String text) {
  text = StringUtil.replace(text, "&nbsp;", " ");
  text = StringUtil.replace(text, "&raquo;", ">>");
  text = StringUtil.replace(text, "&laquo;", "<<");
  text = StringUtil.replace(text, "&hellip;", "...");
  document.insertString(document.getTextLength(), StringUtil.unescapeXml(text));
}
 
Example 15
Source File: HighlightInfo.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public String getToolTip() {
  String toolTip = this.toolTip;
  String description = this.description;
  if (toolTip == null || description == null || !toolTip.contains(DESCRIPTION_PLACEHOLDER)) return toolTip;
  String decoded = StringUtil.replace(toolTip, DESCRIPTION_PLACEHOLDER, XmlStringUtil.escapeString(description));
  return XmlStringUtil.wrapInHtml(decoded);
}
 
Example 16
Source File: DartVmServiceValue.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean computeVarHavingStringValuePresentation(@NotNull final XValueNode node) {
  // getValueAsString() is provided for the instance kinds: Null, Bool, Double, Int, String (value may be truncated), Float32x4, Float64x2, Int32x4, StackTrace
  switch (myInstanceRef.getKind()) {
    case Null:
    case Bool:
      node.setPresentation(getIcon(), new XKeywordValuePresentation(myInstanceRef.getValueAsString()), false);
      break;
    case Double:
    case Int:
      node.setPresentation(getIcon(), new XNumericValuePresentation(myInstanceRef.getValueAsString()), false);
      break;
    case String:
      final String presentableValue = StringUtil.replace(myInstanceRef.getValueAsString(), "\"", "\\\"");
      node.setPresentation(getIcon(), new XStringValuePresentation(presentableValue), false);

      if (myInstanceRef.getValueAsStringIsTruncated()) {
        addFullStringValueEvaluator(node, myInstanceRef);
      }
      break;
    case Float32x4:
    case Float64x2:
    case Int32x4:
    case StackTrace:
      node.setFullValueEvaluator(new ImmediateFullValueEvaluator("Click to see stack trace...", myInstanceRef.getValueAsString()));
      node.setPresentation(getIcon(), myInstanceRef.getClassRef().getName(), "", true);
      break;
    default:
      return false;
  }
  return true;
}
 
Example 17
Source File: ProblemDescriptorUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static String renderDescriptionMessage(@Nonnull CommonProblemDescriptor descriptor, PsiElement element, @FlagConstant int flags) {
  String message = descriptor.getDescriptionTemplate();

  // no message. Should not be the case if inspection correctly implemented.
  // noinspection ConstantConditions
  if (message == null) return "";

  if ((flags & APPEND_LINE_NUMBER) != 0 &&
      descriptor instanceof ProblemDescriptor &&
      !message.contains("#ref") &&
      message.contains("#loc")) {
    final int lineNumber = ((ProblemDescriptor)descriptor).getLineNumber();
    if (lineNumber >= 0) {
      message = StringUtil
              .replace(message, "#loc", "(" + InspectionsBundle.message("inspection.export.results.at.line") + " " + lineNumber + ")");
    }
  }
  message = StringUtil.replace(message, "<code>", "'");
  message = StringUtil.replace(message, "</code>", "'");
  message = StringUtil.replace(message, "#loc ", "");
  message = StringUtil.replace(message, " #loc", "");
  message = StringUtil.replace(message, "#loc", "");
  if (message.contains("#ref")) {
    String ref = extractHighlightedText(descriptor, element);
    message = StringUtil.replace(message, "#ref", ref);
  }

  final int endIndex = (flags & TRIM_AT_END) != 0 ? message.indexOf("#end") :
                       (flags & TRIM_AT_TREE_END) != 0 ? message.indexOf("#treeend") : -1;
  if (endIndex > 0) {
    message = message.substring(0, endIndex);
  }
  message = StringUtil.replace(message, "#end", "");
  message = StringUtil.replace(message, "#treeend", "");

  if (message.contains(XML_CODE_MARKER.first)) {
    message = unescapeXmlCode(message);
  }
  else {
    message = StringUtil.unescapeXml(message).trim();
  }
  return message;
}
 
Example 18
Source File: URLUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static URL getJarEntryURL(@Nonnull File file, @Nonnull String pathInJar) throws MalformedURLException {
  String fileURL = StringUtil.replace(file.toURI().toASCIIString(), "!", "%21");
  return new URL(JAR_PROTOCOL + ':' + fileURL + JAR_SEPARATOR + StringUtil.trimLeading(pathInJar, '/'));
}
 
Example 19
Source File: DateFormatUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static String fixWindowsFormat(String format) {
  format = format.replaceAll("g+", "G");
  format = StringUtil.replace(format, "tt", "a");
  return format;
}
 
Example 20
Source File: ShStringUtil.java    From react-native-console with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@NotNull
public static String unquote(String afterSlash) {
  return StringUtil.replace(afterSlash, ENCODED, ORIGINS);
}