Skip to content

Instantly share code, notes, and snippets.

@pavlov200912
Last active March 25, 2021 09:57
Show Gist options
  • Save pavlov200912/4d8dccb7df3f34a1ca4324b6ce7cae98 to your computer and use it in GitHub Desktop.
Save pavlov200912/4d8dccb7df3f34a1ca4324b6ce7cae98 to your computer and use it in GitHub Desktop.

RefactorInsight

Example 0

PROB: 0.53 (Found accidentally) commit

UNCHANGED:

/**
   * Returns the Refactoring Info parent of a node.
   * This may be at position 1 or 3.
   *
   * @param node current position in the tree.
   * @return the refactoring info parent.
   */

CODE DIFFS:

public static RefactoringInfo getRefactoringInfo(DefaultMutableTreeNode node) {
+     return ((Node) node.getUserObject()).getInfo();
-     RefactoringInfo info = null;
-     if (node.getUserObjectPath()[1] instanceof RefactoringInfo) {
-       info = (RefactoringInfo) node.getUserObjectPath()[1];
-     } else {
-       if (node.getUserObjectPath().length > 3
-           && node.getUserObjectPath()[3] instanceof RefactoringInfo) {
-         info = (RefactoringInfo) node.getUserObjectPath()[3];
-       }
-     }
-     return info;
    }

Example 1

PROB: 0.951127192716583 Commit: 5f9fae52a0c1882d791063dc14e3f3cc25afb1ba COMMENT DIFFS:

UNCHANGED:
/**
   * Converter to Json.
   *
   * @param refactorings to be processed.
   * @param commit       current commit.
   * @return Json string.
   */

CODE DIFFS:

- public static String convert(List<Refactoring> refactorings, VcsCommitMetadata commit,
?               ^

+ public static RefactoringEntry convert(List<Refactoring> refactorings, VcsCommitMetadata commit,
?               ^^^^^ +    +++++

-                                Project project) {
+                                          Project project) {
? ++++++++++

  
      RefactoringEntry entry =
          new RefactoringEntry(commit.getId().asString(),
              commit.getParents().get(0).asString(), commit.getTimestamp());
  
      List<RefactoringInfo> infos =
          refactorings.stream().map(ref -> factory.create(ref, entry)).collect(
              Collectors.toList());
  
      entry.setRefactorings(infos).combineRelated();
  
      entry.refactorings.forEach(info -> Utils.check(info, project));
-     return entry.toString();
?                 -----------

+     return entry;
    }

Example 2

PROB: 0.9065423445020695 Commit: 541d2d456192a7443b946a9a5fe42423e6b9c8ef COMMENT DIFFS: UNCHANGED:

/**
   * Implement this method to provide your action handler.
   *
   * @param e Carries information on the invocation place
   */

CODE DIFFS:

  @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
      Project project = e.getProject();
      if (project == null) {
        return;
      }
      map = e.getProject().getService(MiningService.class).getMethodHistory();
      DataContext dataContext = e.getDataContext();
      UsageTarget[] usageTarget = dataContext.getData(UsageView.USAGE_TARGETS_KEY);
      if (usageTarget != null) {
        UsageTarget target = usageTarget[0];
        if (target instanceof PsiElementUsageTarget) {
          if (((PsiElementUsageTarget) target).getElement() instanceof PsiMethod) {
            PsiMethod method = (PsiMethod) ((PsiElementUsageTarget) target).getElement();
            String signature = calculateSignature(method);
-           getToolbarWindow(project).showToolbar(map.get(signature), signature);
?                                                                    ------------

+           getToolbarWindow(project).showToolbar(map.get(signature),
+               method.getName(), e.getDataContext());
          }
        }
      }
    }

Example 4

PROB: 0.937594047404403 Commit: 0d837db6035e174545506d282fd5af4d9a7c4174 COMMENT DIFFS: UNCHANGED:

/**
   * Generate Conflicttype from ThreeSidedType.
   * Conflict type is used to set highlighting colors and can disable
   * highlighting in the left or right editor.
   */

CODE DIFFS:

- public static MergeConflictType getConflictType(ThreeSidedType type) {
?                                                 ^^^^^^ ^^^

+ public static MergeConflictType getConflictType(VisualisationType type) {
?                                                 ^ ^^^^^^^^^^^

      switch (type) {
        case LEFT:
          return new MergeConflictType(TextDiffType.MODIFIED, true, false);
        case RIGHT:
          return new MergeConflictType(TextDiffType.INSERTED, false, true);
        default:
          return new MergeConflictType(TextDiffType.MODIFIED, true, true);
      }
    }

Example 5

PROB: 0.9354366842925568 Commit: 377f2b83b26010e3791f0b95a41a2e49bea27d8e COMMENT DIFFS: UNCHANGED:

/**
   * Data Holder for three sided code ranges.
   *
   * @param leftStart  int
   * @param leftEnd    int
   * @param midStart   int
   * @param midEnd     int
   * @param rightStart int
   * @param rightEnd   int
   */

CODE DIFFS:

  public RefactoringLine(int leftStart, int leftEnd, int midStart, int midEnd, int rightStart,
-                          int rightEnd, ThreeSidedType type) {
?                                        ^^^^^^ ^^^

+                          int rightEnd, VisualisationType type) {
?                                        ^ ^^^^^^^^^^^

-     this.leftStart = leftStart;
-     this.leftEnd = leftEnd;
-     this.midStart = midStart;
-     this.midEnd = midEnd;
-     this.rightStart = rightStart;
-     this.rightEnd = rightEnd;
+     lines[LEFT_START] = leftStart;
+     lines[LEFT_END] = leftEnd;
+     lines[MID_START] = midStart;
+     lines[MID_END] = midEnd;
+     lines[RIGHT_START] = rightStart;
+     lines[RIGHT_END] = rightEnd;
      this.type = type;
    }

Example 6

PROB: 0.9233065030946785 Commit: 514c4259917b3034d948d412e17729660f87ff21 COMMENT DIFFS: UNCHANGED:

/**
   * IntelliJ Diff Extension.
   * This is needed to obtain the viewer object.
   * Sets a listener which is activated once classical diff is calculated and
   * code ranges can be replaced with refactoring specific ranges.
   */

CODE DIFFS:

  @Override
    public void onViewerCreated(@NotNull FrameDiffTool.DiffViewer viewer,
                                @NotNull DiffContext context, @NotNull DiffRequest request) {
+     List<ThreeSidedRange> ranges = request.getUserData(REFACTORING_RANGES);
-     //Check diff viewer type for refactoring
-     Boolean isRefactoring = request.getUserData(REFACTORING);
-     if (isRefactoring == null) {
?         ^^^^^ -----

+     if (ranges == null) {
?         ^   ++

        return;
      }
+     SimpleThreesideDiffViewer myViewer = (SimpleThreesideDiffViewer) viewer;
+     myViewer.addListener(new MyDiffViewerListener(myViewer, ranges));
  
+     Boolean isRefactoring = request.getUserData(REFACTORING);
+     if (isRefactoring != null) {
-     //Check if diff viewer is three sided
-     List<ThreeSidedRange> threeSidedRanges = request.getUserData(THREESIDED_RANGES);
-     if (threeSidedRanges != null) {
-       SimpleThreesideDiffViewer myViewer = (SimpleThreesideDiffViewer) viewer;
        myViewer.getTextSettings().setExpandByDefault(false);
-       myViewer.addListener(new MyDiffViewerListener(myViewer, threeSidedRanges));
-       return;
      }
- 
-     //Check if diff viewer is more sided
-     List<MoreSidedDiffRequestGenerator.Data> moreSidedRanges =
-         request.getUserData(MORESIDED_RANGES);
-     SimpleDiffViewer myViewer = (SimpleDiffViewer) viewer;
-     if (moreSidedRanges != null) {
-       //Highlight right part
-       EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
-       TextAttributes textColors = scheme.getAttributes(TextAttributesKey.find("DIFF_MODIFIED"));
-       MoreSidedDiffRequestGenerator.Data rightData = moreSidedRanges.get(0);
-       for (int i = rightData.startLine; i < rightData.endLine; i++) {
-         myViewer.getEditor2().getMarkupModel().addLineHighlighter(i, 2, textColors);
-       }
-       List<Pair<MoreSidedDiffRequestGenerator.Data, Project>> pairs =
-           moreSidedRanges.subList(1, moreSidedRanges.size()).stream()
-           .map(data -> new Pair<>(data, myViewer.getProject()))
-           .collect(Collectors.toList());
-       //Generate Left UI
-       JBList<Pair<MoreSidedDiffRequestGenerator.Data, Project>> editorList = new JBList<>(JBList.createDefaultListModel(pairs));
-       JPanel leftPanel = (JPanel) myViewer.getEditor1().getComponent();
-       leftPanel.remove(0);
-       leftPanel.add(editorList);
-       return;
-     }
- 
-     //Asume diff viewer is two sided
-     myViewer.getTextSettings().setExpandByDefault(false);
- 
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment