<commit_before>package com.jetbrains.python.inspections.quickfix;

import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.documentation.PyDocstringGenerator;
import com.jetbrains.python.documentation.PyDocumentationSettings;
import com.jetbrains.python.documentation.PythonDocumentationProvider;
import com.jetbrains.python.editor.PythonDocCommentUtil;
import com.jetbrains.python.psi.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * User : catherine
 */
public class DocstringQuickFix implements LocalQuickFix {

  PyParameter myMissing;
  String myMissingText = "";
  String myUnexpected;
  String myPrefix;

  public DocstringQuickFix(PyParameter missing, String unexpected) {
    myMissing = missing;
    if (myMissing != null) {
      if (myMissing.getText().startsWith("*")) {
        myMissingText = myMissing.getText();
      }
      else {
        myMissingText = myMissing.getName();
      }
    }
    myUnexpected = unexpected;
  }

  @NotNull
  public String getName() {
    if (myMissing != null) {
      return PyBundle.message("QFIX.docstring.add.$0", myMissingText);
    }
    else if (myUnexpected != null){
      return PyBundle.message("QFIX.docstring.remove.$0", myUnexpected);
    }
    else  {
      return PyBundle.message("QFIX.docstring.insert.stub");
    }
  }

  @NotNull
  public String getFamilyName() {
    return "Fix docstring";
  }

  @Nullable
  private static Editor getEditor(Project project, PsiFile file) {
    Document document = PsiDocumentManager.getInstance(project).getDocument(file);
    if (document != null) {
      final EditorFactory instance = EditorFactory.getInstance();
      if (instance == null) return null;
      Editor[] editors = instance.getEditors(document);
      if (editors.length > 0)
        return editors[0];
    }
    return null;
  }

  public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PyDocStringOwner docStringOwner = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PyDocStringOwner.class);
    if (docStringOwner == null) return;
    PyStringLiteralExpression docStringExpression = docStringOwner.getDocStringExpression();
    if (docStringExpression == null && myMissing == null && myUnexpected == null) {
      if (docStringOwner instanceof PyFunction) {
        PythonDocumentationProvider.insertDocStub((PyFunction)docStringOwner, project,
                                                  getEditor(project, docStringOwner.getContainingFile()));
      }
      if (docStringOwner instanceof PyClass) {
        PyFunction init = ((PyClass)docStringOwner).findInitOrNew(false);
        if (init == null) return;
        PythonDocumentationProvider.insertDocStub(init, ((PyClass)docStringOwner).getStatementList(),
                                                  project, getEditor(project, docStringOwner.getContainingFile()));
      }
      return;
    }
    if (docStringExpression == null) return;
    PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
    PyDocumentationSettings documentationSettings = PyDocumentationSettings.getInstance(project);
    if (documentationSettings.isEpydocFormat(docStringExpression.getContainingFile())) {
      myPrefix = "@";
    }
    else {
      myPrefix = ":";
    }

    String replacement = docStringExpression.getText();
    if (myMissing != null) {
      replacement = createMissingReplacement(docStringOwner);
    }
    if (myUnexpected != null) {
      replacement = PythonDocCommentUtil.removeParamFromDocstring(replacement, myPrefix, myUnexpected);
    }
    if (!replacement.equals(docStringExpression.getText())) {
      PyExpression str = elementGenerator.createDocstring(replacement).getExpression();
      docStringExpression.replace(str);
    }
  }

  private String createMissingReplacement(PyDocStringOwner docStringOwner) {
    return new PyDocstringGenerator(docStringOwner).withParam("param", myMissingText).docStringAsText();
  }
}<commit_msg>fixed EA-42254 - CCE: PyElementGeneratorImpl.createDocstring<commit_after>package com.jetbrains.python.inspections.quickfix;

import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.documentation.PyDocstringGenerator;
import com.jetbrains.python.documentation.PyDocumentationSettings;
import com.jetbrains.python.documentation.PythonDocumentationProvider;
import com.jetbrains.python.editor.PythonDocCommentUtil;
import com.jetbrains.python.psi.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * User : catherine
 */
public class DocstringQuickFix implements LocalQuickFix {

  PyParameter myMissing;
  String myMissingText = "";
  String myUnexpected;
  String myPrefix;

  public DocstringQuickFix(PyParameter missing, String unexpected) {
    myMissing = missing;
    if (myMissing != null) {
      if (myMissing.getText().startsWith("*")) {
        myMissingText = myMissing.getText();
      }
      else {
        myMissingText = myMissing.getName();
      }
    }
    myUnexpected = unexpected;
  }

  @NotNull
  public String getName() {
    if (myMissing != null) {
      return PyBundle.message("QFIX.docstring.add.$0", myMissingText);
    }
    else if (myUnexpected != null){
      return PyBundle.message("QFIX.docstring.remove.$0", myUnexpected);
    }
    else  {
      return PyBundle.message("QFIX.docstring.insert.stub");
    }
  }

  @NotNull
  public String getFamilyName() {
    return "Fix docstring";
  }

  @Nullable
  private static Editor getEditor(Project project, PsiFile file) {
    Document document = PsiDocumentManager.getInstance(project).getDocument(file);
    if (document != null) {
      final EditorFactory instance = EditorFactory.getInstance();
      if (instance == null) return null;
      Editor[] editors = instance.getEditors(document);
      if (editors.length > 0)
        return editors[0];
    }
    return null;
  }

  public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PyDocStringOwner docStringOwner = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PyDocStringOwner.class);
    if (docStringOwner == null) return;
    PyStringLiteralExpression docStringExpression = docStringOwner.getDocStringExpression();
    if (docStringExpression == null && myMissing == null && myUnexpected == null) {
      if (docStringOwner instanceof PyFunction) {
        PythonDocumentationProvider.insertDocStub((PyFunction)docStringOwner, project,
                                                  getEditor(project, docStringOwner.getContainingFile()));
      }
      if (docStringOwner instanceof PyClass) {
        PyFunction init = ((PyClass)docStringOwner).findInitOrNew(false);
        if (init == null) return;
        PythonDocumentationProvider.insertDocStub(init, ((PyClass)docStringOwner).getStatementList(),
                                                  project, getEditor(project, docStringOwner.getContainingFile()));
      }
      return;
    }
    if (docStringExpression == null) return;
    PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
    PyDocumentationSettings documentationSettings = PyDocumentationSettings.getInstance(project);
    if (documentationSettings.isEpydocFormat(docStringExpression.getContainingFile())) {
      myPrefix = "@";
    }
    else {
      myPrefix = ":";
    }

    String replacement = docStringExpression.getText();
    if (myMissing != null) {
      replacement = createMissingReplacement(docStringOwner);
    }
    if (myUnexpected != null) {
      replacement = PythonDocCommentUtil.removeParamFromDocstring(replacement, myPrefix, myUnexpected);
    }
    if (!replacement.equals(docStringExpression.getText()) && !StringUtil.isEmptyOrSpaces(replacement)) {
      PyExpression str = elementGenerator.createDocstring(replacement).getExpression();
      docStringExpression.replace(str);
    }
  }

  private String createMissingReplacement(PyDocStringOwner docStringOwner) {
    return new PyDocstringGenerator(docStringOwner).withParam("param", myMissingText).docStringAsText();
  }
}
{'fraction_non_alphanumeric': 0.10143476810143477, 'fraction_numerical': 0.0014458903347792237, 'mean_word_length': 3.951541850220264, 'pattern_counts': {'":': 2, '<': 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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '378148', 'n_tokens_mistral': 2462, 'n_tokens_neox': 2412, 'n_words': 456}