<commit_before>// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.diagnostic;

import com.intellij.openapi.diagnostic.Attachment;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.PathUtilRt;
import org.jetbrains.annotations.NotNull;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 * @author yole
 */
public class AttachmentFactory {
  private static final Logger LOG = Logger.getInstance(AttachmentFactory.class);

  private static final long BIG_FILE_THRESHOLD_BYTES = 50 * 1024;

  @NotNull
  public static Attachment createContext(@NotNull Object start, Object... more) {
    StringBuilder builder = new StringBuilder(String.valueOf(start));
    for (Object o : more) builder.append(",").append(o);
    return new Attachment("current-context.txt", builder.length() > 0 ? builder.toString() : "(unknown)");
  }

  @NotNull
  public static Attachment createAttachment(@NotNull Document document) {
    VirtualFile file = FileDocumentManager.getInstance().getFile(document);
    return new Attachment(file != null ? file.getPath() : "unknown.txt", document.getText());
  }

  @NotNull
  public static Attachment createAttachment(@NotNull VirtualFile file) {
    try (InputStream inputStream = file.getInputStream()) {
      return createAttachment(file.getPresentableUrl(), inputStream, file.getLength(), file.getFileType().isBinary());
    }
    catch (IOException e) {
      return handleException(e, file.getPath());
    }
  }

  @NotNull
  public static Attachment createAttachment(@NotNull File file, boolean isBinary) {
    return createAttachment(file.toPath(), isBinary);
  }

  @NotNull
  public static Attachment createAttachment(@NotNull Path file, boolean isBinary) {
    try (InputStream inputStream = Files.newInputStream(file)) {
      return createAttachment(file.toString(), inputStream, Files.size(file), isBinary);
    }
    catch (IOException e) {
      return handleException(e, file.toString());
    }<commit_msg>AttachmentFactory: add overload to set custom path to file.<commit_after>// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.diagnostic;

import com.intellij.openapi.diagnostic.Attachment;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.PathUtilRt;
import org.jetbrains.annotations.NotNull;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 * @author yole
 */
public class AttachmentFactory {
  private static final Logger LOG = Logger.getInstance(AttachmentFactory.class);

  private static final long BIG_FILE_THRESHOLD_BYTES = 50 * 1024;

  @NotNull
  public static Attachment createContext(@NotNull Object start, Object... more) {
    StringBuilder builder = new StringBuilder(String.valueOf(start));
    for (Object o : more) builder.append(",").append(o);
    return new Attachment("current-context.txt", builder.length() > 0 ? builder.toString() : "(unknown)");
  }

  @NotNull
  public static Attachment createAttachment(@NotNull Document document) {
    VirtualFile file = FileDocumentManager.getInstance().getFile(document);
    return new Attachment(file != null ? file.getPath() : "unknown.txt", document.getText());
  }

  @NotNull
  public static Attachment createAttachment(@NotNull VirtualFile file) {
    return createAttachment(file.getPresentableUrl(), file);
  }

  @NotNull
  public static Attachment createAttachment(@NotNull String path, @NotNull VirtualFile file) {
    try (InputStream inputStream = file.getInputStream()) {
      return createAttachment(path, inputStream, file.getLength(), file.getFileType().isBinary());
    }
    catch (IOException e) {
      return handleException(e, file.getPath());
    }
  }

  @NotNull
  public static Attachment createAttachment(@NotNull File file, boolean isBinary) {
    return createAttachment(file.toPath(), isBinary);
  }

  @NotNull
  public static Attachment createAttachment(@NotNull Path file, boolean isBinary) {
    try (InputStream inputStream = Files.newInputStream(file)) {
      return createAttachment(file.toString(), inputStream, Files.size(file), isBinary);
    }
    catch (IOException e) {
      return handleException(e, file.toString());
    }
{'fraction_non_alphanumeric': 0.1103363412633306, 'fraction_numerical': 0.00697292863002461, 'mean_word_length': 4.724178403755869, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5108938', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1363, 'n_words': 359}