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

The following examples show how to use com.intellij.openapi.util.text.StringUtil#convertLineSeparators() . 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: XDebuggerEvaluationDialog.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void switchToMode(EvaluationMode mode, XExpression text) {
  if (myMode == mode) return;

  myMode = mode;

  if (mode == EvaluationMode.EXPRESSION) {
    text = new XExpressionImpl(StringUtil.convertLineSeparators(text.getExpression(), " "), text.getLanguage(), text.getCustomInfo());
  }

  myInputComponent = createInputComponent(mode, text);
  myMainPanel.removeAll();
  myInputComponent.addComponent(myMainPanel, myResultPanel);

  setTitle(myInputComponent.getTitle());
  mySwitchModeAction.putValue(Action.NAME, getSwitchButtonText(mode));
  getInputEditor().requestFocusInEditor();
}
 
Example 2
Source File: FormatterTestCase.java    From consulo with Apache License 2.0 6 votes vote down vote up
protected String loadFile(String name, String resultNumber) throws Exception {
  String fullName = getTestDataPath() + File.separatorChar + getBasePath() + File.separatorChar + name;
  String text = FileUtil.loadFile(new File(fullName));
  text = StringUtil.convertLineSeparators(text);
  if (resultNumber == null) {
    return prepareText(text);
  }
  else {
    String beginLine = "<<<" + resultNumber + ">>>";
    String endLine = "<<</" + resultNumber + ">>>";
    int beginPos = text.indexOf(beginLine);
    assertTrue(beginPos >= 0);
    int endPos = text.indexOf(endLine);
    assertTrue(endPos >= 0);

    return prepareText(text.substring(beginPos + beginLine.length(), endPos).trim());

  }
}
 
Example 3
Source File: AssertEx.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static void assertSameLinesWithFile(String filePath, String actualText) {
  String fileText;
  try {
    if (OVERWRITE_TESTDATA) {
      FileUtil.writeToFile(new File(filePath), actualText);
      System.out.println("File " + filePath + " created.");
    }
    fileText = FileUtil.loadFile(new File(filePath));
  }
  catch (IOException e) {
    throw new RuntimeException(e);
  }
  String expected = StringUtil.convertLineSeparators(fileText.trim());
  String actual = StringUtil.convertLineSeparators(actualText.trim());
  if (!Comparing.equal(expected, actual)) {
    throw new FileComparisonFailure(null, expected, actual, filePath);
  }
}
 
Example 4
Source File: RTDocumentationProvider.java    From react-templates-plugin with MIT License 5 votes vote down vote up
private static String getDoc(String tag) {
        // return RTBundle.message("doc." + name);
        try {
            String s = FileUtil.loadTextAndClose(RTDocumentationProvider.class.getResourceAsStream("/tagDescriptions/" + tag + ".html"));
            return StringUtil.convertLineSeparators(s);
        } catch (IOException e) {
//      throw new IncorrectOperationException(RTBundle.message("error.cannot.read", formName), (Throwable)e);
            return "null";
        }
    }
 
Example 5
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 6
Source File: AbstractCreateFormAction.java    From react-templates-plugin with MIT License 5 votes vote down vote up
protected String createFormBody(@Nullable String fqn, @NonNls String formName, String layoutManager) throws IncorrectOperationException {
        String s = "";
        try {
            s = FileUtil.loadTextAndClose(getClass().getResourceAsStream(formName));
        } catch (IOException e) {
//      throw new IncorrectOperationException(RTBundle.message("error.cannot.read", formName), (Throwable)e);
        }
        s = fqn == null ? StringUtil.replace(s, "bind-to-class=\"$CLASS$\"", "") : StringUtil.replace(s, "$CLASS$", fqn);
        s = StringUtil.replace(s, "$LAYOUT$", layoutManager);
        return StringUtil.convertLineSeparators(s);
    }
 
Example 7
Source File: RTDocumentationProvider.java    From react-templates-plugin with MIT License 5 votes vote down vote up
private static String getDoc(String tag) {
        // return RTBundle.message("doc." + name);
        try {
            String s = FileUtil.loadTextAndClose(RTDocumentationProvider.class.getResourceAsStream("/tagDescriptions/" + tag + ".html"));
            return StringUtil.convertLineSeparators(s);
        } catch (IOException e) {
//      throw new IncorrectOperationException(RTBundle.message("error.cannot.read", formName), (Throwable)e);
            return "null";
        }
    }
 
Example 8
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 9
Source File: AbstractCreateFormAction.java    From react-templates-plugin with MIT License 5 votes vote down vote up
protected String createFormBody(@Nullable String fqn, @NonNls String formName, String layoutManager) throws IncorrectOperationException {
        String s = "";
        try {
            s = FileUtil.loadTextAndClose(getClass().getResourceAsStream(formName));
        } catch (IOException e) {
//      throw new IncorrectOperationException(RTBundle.message("error.cannot.read", formName), (Throwable)e);
        }
        s = fqn == null ? StringUtil.replace(s, "bind-to-class=\"$CLASS$\"", "") : StringUtil.replace(s, "$CLASS$", fqn);
        s = StringUtil.replace(s, "$LAYOUT$", layoutManager);
        return StringUtil.convertLineSeparators(s);
    }
 
Example 10
Source File: KillRingUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * Copies target region from the given offset to the kill ring, i.e. combines it with the previously
 * copied/cut adjacent text if necessary and puts to the clipboard.
 *
 * @param editor      target editor
 * @param startOffset start offset of the target region within the given editor
 * @param endOffset   end offset of the target region within the given editor
 * @param cut         flag that identifies if target text region will be cut from the given editor
 */
public static void copyToKillRing(@Nonnull final Editor editor, int startOffset, int endOffset, boolean cut) {
  Document document = editor.getDocument();
  String s = document.getCharsSequence().subSequence(startOffset, endOffset).toString();
  s = StringUtil.convertLineSeparators(s);
  CopyPasteManager.getInstance().setContents(new KillRingTransferable(s, document, startOffset, endOffset, cut));
  if (editor instanceof EditorEx) {
    EditorEx ex = (EditorEx)editor;
    if (ex.isStickySelection()) {
      ex.setStickySelection(false);
    }
  }
}
 
Example 11
Source File: EncodingAwareProperties.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void load(File file, String encoding) throws IOException{
  String propText = FileUtil.loadFile(file, encoding);
  propText = StringUtil.convertLineSeparators(propText);
  StringTokenizer stringTokenizer = new StringTokenizer(propText, "\n");
  while (stringTokenizer.hasMoreElements()){
    String line = stringTokenizer.nextElement();
    int i = line.indexOf('=');
    String propName = i == -1 ? line : line.substring(0,i);
    String propValue = i == -1 ? "" : line.substring(i+1);
    setProperty(propName, propValue);
  }
}
 
Example 12
Source File: ExternalDiffToolUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void apply() throws IOException {
  final String content = StringUtil.convertLineSeparators(FileUtil.loadFile(myLocalFile, myCharset));
  ApplicationManager.getApplication().runWriteAction(() -> {
    myDocument.setText(content);
  });
}
 
Example 13
Source File: FileTemplateUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static PsiElement createFromTemplate(@Nonnull final FileTemplate template,
                                            @NonNls @Nullable String fileName,
                                            @Nullable Map<String, Object> additionalProperties,
                                            @Nonnull final PsiDirectory directory,
                                            @Nullable ClassLoader classLoader) throws Exception {
  final Project project = directory.getProject();

  Map<String, Object> properties = new THashMap<>();
  FileTemplateManager.getInstance(project).fillDefaultVariables(properties);

  if(additionalProperties != null) {
    properties.putAll(additionalProperties);
  }

  FileTemplateManager.getInstance(project).addRecentName(template.getName());
  fillDefaultProperties(properties, directory);

  final CreateFromTemplateHandler handler = findHandler(template);
  if (fileName != null && properties.get(FileTemplate.ATTRIBUTE_NAME) == null) {
    properties.put(FileTemplate.ATTRIBUTE_NAME, fileName);
  }
  else if (fileName == null && handler.isNameRequired()) {
    fileName = (String)properties.get(FileTemplate.ATTRIBUTE_NAME);
    if (fileName == null) {
      throw new Exception("File name must be specified");
    }
  }

  //Set escaped references to dummy values to remove leading "\" (if not already explicitely set)
  String[] dummyRefs = calculateAttributes(template.getText(), properties, true, project);
  for (String dummyRef : dummyRefs) {
    properties.put(dummyRef, "");
  }

  handler.prepareProperties(properties);

  final String fileName_ = fileName;
  String mergedText =
          ClassLoaderUtil.runWithClassLoader(classLoader != null ? classLoader : FileTemplateUtil.class.getClassLoader(), (ThrowableComputable<String, IOException>)() -> template.getText(properties));
  final String templateText = StringUtil.convertLineSeparators(mergedText);
  final Exception[] commandException = new Exception[1];
  final PsiElement[] result = new PsiElement[1];
  CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> {
    try {
      result[0] = handler.createFromTemplate(project, directory, fileName_, template, templateText, properties);
    }
    catch (Exception ex) {
      commandException[0] = ex;
    }
  }), handler.commandName(template), null);
  if (commandException[0] != null) {
    throw commandException[0];
  }
  return result[0];
}
 
Example 14
Source File: CamelDocumentationProviderTestIT.java    From camel-idea-plugin with Apache License 2.0 4 votes vote down vote up
private String exampleHtmlFileText(String name) throws IOException {
    final File htmlPath = new File(getTestDataPath() + name + ".html");
    final String hmltDoc = StringUtil.convertLineSeparators(FileUtil.loadFile(htmlPath).trim(), "");
    return String.format(hmltDoc, CAMEL_VERSION);
}
 
Example 15
Source File: CodeInsightTestFixtureImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
static SelectionAndCaretMarkupLoader fromFile(String path, Project project, String charset) throws IOException {
  return new SelectionAndCaretMarkupLoader(project, StringUtil.convertLineSeparators(FileUtil.loadFile(new File(path), charset)), path);
}
 
Example 16
Source File: EventLog.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static boolean parseHtmlContent(String text,
                                        Notification notification,
                                        Document document,
                                        AtomicBoolean showMore,
                                        Map<RangeMarker, HyperlinkInfo> links,
                                        List<RangeMarker> lineSeparators) {
  String content = StringUtil.convertLineSeparators(text);

  int initialLen = document.getTextLength();
  boolean hasHtml = false;
  while (true) {
    Matcher tagMatcher = TAG_PATTERN.matcher(content);
    if (!tagMatcher.find()) {
      appendText(document, content);
      break;
    }

    String tagStart = tagMatcher.group();
    appendText(document, content.substring(0, tagMatcher.start()));
    Matcher aMatcher = A_PATTERN.matcher(tagStart);
    if (aMatcher.matches()) {
      final String href = aMatcher.group(2);
      int linkEnd = content.indexOf(A_CLOSING, tagMatcher.end());
      if (linkEnd > 0) {
        String linkText = content.substring(tagMatcher.end(), linkEnd).replaceAll(TAG_PATTERN.pattern(), "");
        int linkStart = document.getTextLength();
        appendText(document, linkText);
        links.put(document.createRangeMarker(new TextRange(linkStart, document.getTextLength())), new NotificationHyperlinkInfo(notification, href));
        content = content.substring(linkEnd + A_CLOSING.length());
        continue;
      }
    }

    if (isTag(HTML_TAGS, tagStart)) {
      hasHtml = true;
      if (NEW_LINES.contains(tagStart)) {
        if (initialLen != document.getTextLength()) {
          lineSeparators.add(document.createRangeMarker(TextRange.from(document.getTextLength(), 0)));
        }
      }
      else if (!isTag(SKIP_TAGS, tagStart)) {
        showMore.set(true);
      }
    }
    else {
      appendText(document, content.substring(tagMatcher.start(), tagMatcher.end()));
    }
    content = content.substring(tagMatcher.end());
  }
  for (Iterator<RangeMarker> iterator = lineSeparators.iterator(); iterator.hasNext(); ) {
    RangeMarker next = iterator.next();
    if (next.getEndOffset() == document.getTextLength()) {
      iterator.remove();
    }
  }
  return hasHtml;
}
 
Example 17
Source File: FindUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static TextRange doReplace(final Project project,
                                  final Document document,
                                  final FindModel model,
                                  FindResult result,
                                  @Nonnull String stringToReplace,
                                  boolean reallyReplace,
                                  List<Pair<TextRange, String>> rangesToChange) {
  final int startOffset = result.getStartOffset();
  final int endOffset = result.getEndOffset();

  int newOffset;
  if (reallyReplace) {
    newOffset = doReplace(project, document, startOffset, endOffset, stringToReplace);
  }
  else {
    final String converted = StringUtil.convertLineSeparators(stringToReplace);
    TextRange textRange = new TextRange(startOffset, endOffset);
    rangesToChange.add(Pair.create(textRange, converted));

    newOffset = endOffset;
  }

  int start = startOffset;
  int end = newOffset;
  if (model.isRegularExpressions()) {
    String toFind = model.getStringToFind();
    if (model.isForward()) {
      if (StringUtil.endsWithChar(toFind, '$')) {
        int i = 0;
        int length = toFind.length();
        while (i + 2 <= length && toFind.charAt(length - i - 2) == '\\') i++;
        if (i % 2 == 0) end++; //This $ is a special symbol in regexp syntax
      }
      else if (StringUtil.startsWithChar(toFind, '^')) {
        while (end < document.getTextLength() && document.getCharsSequence().charAt(end) != '\n') end++;
      }
    }
    else {
      if (StringUtil.startsWithChar(toFind, '^')) {
        start--;
      }
      else if (StringUtil.endsWithChar(toFind, '$')) {
        while (start >= 0 && document.getCharsSequence().charAt(start) != '\n') start--;
      }
    }
  }
  return new TextRange(start, end);
}
 
Example 18
Source File: ExportToFileUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void actionPerformed(ActionEvent e) {
  String s = StringUtil.convertLineSeparators(getText());
  CopyPasteManager.getInstance().setContents(new StringSelection(s));
}
 
Example 19
Source File: OneFileAtProjectTestCase.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static String doLoadFile(String myFullDataPath, String name) throws IOException {
  String text = FileUtil.loadFile(new File(myFullDataPath, name), CharsetToolkit.UTF8).trim();
  text = StringUtil.convertLineSeparators(text);
  return text;
}
 
Example 20
Source File: ConsoleLog.java    From aem-ide-tooling-4-intellij with Apache License 2.0 4 votes vote down vote up
private static boolean parseHtmlContent(String text, Notification notification,
                                        Document document,
                                        AtomicBoolean showMore,
                                        Map<RangeMarker, HyperlinkInfo> links, List<RangeMarker> lineSeparators) {
    String content = StringUtil.convertLineSeparators(text);

    int initialLen = document.getTextLength();
    boolean hasHtml = false;
    while(true) {
        Matcher tagMatcher = TAG_PATTERN.matcher(content);
        if(!tagMatcher.find()) {
            appendText(document, content);
            break;
        }

        String tagStart = tagMatcher.group();
        appendText(document, content.substring(0, tagMatcher.start()));
        Matcher aMatcher = A_PATTERN.matcher(tagStart);
        if(aMatcher.matches()) {
            final String href = aMatcher.group(2);
            int linkEnd = content.indexOf(A_CLOSING, tagMatcher.end());
            if(linkEnd > 0) {
                String linkText = content.substring(tagMatcher.end(), linkEnd).replaceAll(TAG_PATTERN.pattern(), "");
                int linkStart = document.getTextLength();
                appendText(document, linkText);
                links.put(document.createRangeMarker(new TextRange(linkStart, document.getTextLength())),
                    new NotificationHyperlinkInfo(notification, href));
                content = content.substring(linkEnd + A_CLOSING.length());
                continue;
            }
        }

        hasHtml = true;
        if(NEW_LINES.contains(tagStart)) {
            if(initialLen != document.getTextLength()) {
                lineSeparators.add(document.createRangeMarker(TextRange.from(document.getTextLength(), 0)));
            }
        } else if(!"<html>".equals(tagStart) && !"</html>".equals(tagStart) && !"<body>".equals(tagStart) && !"</body>".equals(tagStart)) {
            showMore.set(true);
        }
        content = content.substring(tagMatcher.end());
    }
    for(Iterator<RangeMarker> iterator = lineSeparators.iterator(); iterator.hasNext(); ) {
        RangeMarker next = iterator.next();
        if(next.getEndOffset() == document.getTextLength()) {
            iterator.remove();
        }
    }
    return hasHtml;
}