<commit_before>// Copyright 2000-2020 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.codeInsight.template;

import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * Describes context in which live template supposed to be used.
 */
public final class TemplateActionContext {
  @NotNull
  private final PsiFile myFile;
  @Nullable
  private final Editor myEditor;

  private final int myStartOffset;
  private final int myEndOffset;
  private final boolean myIsSurrounding;

  private TemplateActionContext(@NotNull PsiFile file,
                                @Nullable Editor editor,
                                int startOffset,
                                int endOffset,
                                boolean isSurrounding) {
    myFile = file;
    myStartOffset = startOffset;
    myEndOffset = endOffset;
    myIsSurrounding = isSurrounding;
    myEditor = editor;
  }

  public @NotNull PsiFile getFile() {
    return myFile;
  }

  /**
   * @return editor if file is currently opened in one. Sometimes context may be used with fake files, without any editors
   */
  public @Nullable Editor getEditor() {
    return myEditor;
  }

  /**
   * @return true iff {@code surround with} action is performed
   */
  public boolean isSurrounding() {
    return myIsSurrounding;
  }

  /**
   * @return a copy of current context with specific {@code file}
   */
  public @NotNull TemplateActionContext withFile(@NotNull PsiFile file) {
    return new TemplateActionContext(file, myEditor, myStartOffset, myEndOffset, myIsSurrounding);
  }

  /**
   * @return for surround context returns selection start or caret position if there is no selection or it is expanding context
   */
  public int getStartOffset() {
    return myStartOffset;
  }

  /**
   * @return for surround context returns selection end or caret position if there is no selection or it is expanding context
   */
  public int getEndOffset() {
    return myEndOffset;
  }

  public static @NotNull TemplateActionContext expanding(@NotNull PsiFile psiFile, @NotNull Editor editor) {
    int editorOffset = editor.getCaretModel().getOffset();
    return create(psiFile, editor, editorOffset, editorOffset, false);
  }

  public static @NotNull TemplateActionContext expanding(@NotNull PsiFile psiFile, int offset) {
    return create(psiFile, null, offset, offset, false);
  }

  public static @NotNull TemplateActionContext surrounding(@NotNull PsiFile psiFile, @NotNull Editor editor) {
    SelectionModel selectionModel = editor.getSelectionModel();
    return create(psiFile, editor, selectionModel.getSelectionStart(), selectionModel.getSelectionEnd(), false);
  }

  public static @NotNull TemplateActionContext create(@NotNull PsiFile psiFile,
                                                      @Nullable Editor editor,
                                                      int startOffset,
                                                      int endOffset,<commit_msg>IDEA-CR-62882 Fixed factory method for surrounding context<commit_after>// Copyright 2000-2020 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.codeInsight.template;

import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * Describes context in which live template supposed to be used.
 */
public final class TemplateActionContext {
  @NotNull
  private final PsiFile myFile;
  @Nullable
  private final Editor myEditor;

  private final int myStartOffset;
  private final int myEndOffset;
  private final boolean myIsSurrounding;

  private TemplateActionContext(@NotNull PsiFile file,
                                @Nullable Editor editor,
                                int startOffset,
                                int endOffset,
                                boolean isSurrounding) {
    myFile = file;
    myStartOffset = startOffset;
    myEndOffset = endOffset;
    myIsSurrounding = isSurrounding;
    myEditor = editor;
  }

  public @NotNull PsiFile getFile() {
    return myFile;
  }

  /**
   * @return editor if file is currently opened in one. Sometimes context may be used with fake files, without any editors
   */
  public @Nullable Editor getEditor() {
    return myEditor;
  }

  /**
   * @return true iff {@code surround with} action is performed
   */
  public boolean isSurrounding() {
    return myIsSurrounding;
  }

  /**
   * @return a copy of current context with specific {@code file}
   */
  public @NotNull TemplateActionContext withFile(@NotNull PsiFile file) {
    return new TemplateActionContext(file, myEditor, myStartOffset, myEndOffset, myIsSurrounding);
  }

  /**
   * @return for surround context returns selection start or caret position if there is no selection or it is expanding context
   */
  public int getStartOffset() {
    return myStartOffset;
  }

  /**
   * @return for surround context returns selection end or caret position if there is no selection or it is expanding context
   */
  public int getEndOffset() {
    return myEndOffset;
  }

  public static @NotNull TemplateActionContext expanding(@NotNull PsiFile psiFile, @NotNull Editor editor) {
    int editorOffset = editor.getCaretModel().getOffset();
    return create(psiFile, editor, editorOffset, editorOffset, false);
  }

  public static @NotNull TemplateActionContext expanding(@NotNull PsiFile psiFile, int offset) {
    return create(psiFile, null, offset, offset, false);
  }

  public static @NotNull TemplateActionContext surrounding(@NotNull PsiFile psiFile, @NotNull Editor editor) {
    SelectionModel selectionModel = editor.getSelectionModel();
    return create(psiFile, editor, selectionModel.getSelectionStart(), selectionModel.getSelectionEnd(), true);
  }

  public static @NotNull TemplateActionContext create(@NotNull PsiFile psiFile,
                                                      @Nullable Editor editor,
                                                      int startOffset,
                                                      int endOffset,
{'fraction_non_alphanumeric': 0.07027863777089784, 'fraction_numerical': 0.003869969040247678, 'mean_word_length': 2.8253404381290705, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3319849', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1594, 'n_words': 569}