예제 #1
0
 /** Checks if the parameter names are valid. */
 public RefactoringStatus checkParameterNames() {
   RefactoringStatus result = new RefactoringStatus();
   for (ParameterInfo parameter : parameters) {
     result.merge(Checks.checkParameter(parameter.getNewName()));
     for (ParameterInfo other : parameters) {
       if (parameter != other && StringUtils.equals(other.getNewName(), parameter.getNewName())) {
         result.addError(
             Messages.format(
                 RefactoringCoreMessages.ExtractMethodRefactoring_error_sameParameter,
                 other.getNewName()));
         return result;
       }
     }
     if (parameter.isRenamed() && usedNames.contains(parameter.getNewName())) {
       result.addError(
           Messages.format(
               RefactoringCoreMessages.ExtractMethodRefactoring_error_nameInUse,
               parameter.getNewName()));
       return result;
     }
   }
   return result;
 }
  @Override
  public void createControl(Composite parent) {
    fRefactoring = (ExtractMethodRefactoring) getRefactoring();
    loadSettings();

    Composite result = new Composite(parent, SWT.NONE);
    setControl(result);
    GridLayout layout = new GridLayout();
    layout.numColumns = 2;
    result.setLayout(layout);
    RowLayouter layouter = new RowLayouter(2);
    GridData gd = null;

    initializeDialogUnits(result);

    Label label = new Label(result, SWT.NONE);
    label.setText(getLabelText());

    fTextField = createTextInputField(result, SWT.BORDER);
    fTextField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    layouter.perform(label, fTextField, 1);

    if (!fRefactoring.getParameters().isEmpty()) {
      ChangeParametersControl cp =
          new ChangeParametersControl(
              result,
              SWT.NONE,
              RefactoringMessages.ExtractMethodInputPage_parameters,
              new IParameterListChangeListener.Empty() {
                @Override
                public void parameterChanged(ParameterInfo parameter) {
                  parameterModified();
                }

                @Override
                public void parameterListChanged() {
                  parameterModified();
                }
              },
              ChangeParametersControl.Mode.EXTRACT_METHOD);
      gd = new GridData(GridData.FILL_BOTH);
      gd.horizontalSpan = 2;
      cp.setLayoutData(gd);
      cp.setInput(fRefactoring.getParameters());
    }

    //    checkBox = new Button(result, SWT.CHECK);
    //    checkBox.setText(RefactoringMessages.ExtractMethodInputPage_generateJavadocComment);
    //    boolean generate = computeGenerateJavadoc();
    //    setGenerateJavadoc(generate);
    //    checkBox.setSelection(generate);
    //    checkBox.addSelectionListener(new SelectionAdapter() {
    //      @Override
    //      public void widgetSelected(SelectionEvent e) {
    //        setGenerateJavadoc(((Button) e.widget).getSelection());
    //      }
    //    });
    //    layouter.perform(checkBox);

    int duplicates = fRefactoring.getNumberOfDuplicates();
    Button checkBox = new Button(result, SWT.CHECK);
    if (duplicates == 0) {
      checkBox.setText(RefactoringMessages.ExtractMethodInputPage_duplicates_none);
    } else if (duplicates == 1) {
      checkBox.setText(RefactoringMessages.ExtractMethodInputPage_duplicates_single);
    } else {
      checkBox.setText(
          Messages.format(
              RefactoringMessages.ExtractMethodInputPage_duplicates_multi,
              new Integer(duplicates)));
    }
    checkBox.setSelection(fRefactoring.getReplaceAllOccurrences());
    checkBox.setEnabled(duplicates > 0);
    checkBox.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            fRefactoring.setReplaceAllOccurrences(((Button) e.widget).getSelection());
          }
        });
    layouter.perform(checkBox);

    label = new Label(result, SWT.SEPARATOR | SWT.HORIZONTAL);
    label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    layouter.perform(label);

    createSignaturePreview(result, layouter);

    Dialog.applyDialogFont(result);
    PlatformUI.getWorkbench()
        .getHelpSystem()
        .setHelp(getControl(), DartHelpContextIds.EXTRACT_METHOD_WIZARD_PAGE);
  }
예제 #3
0
 /**
  * Fills {@link #parameters} with information about used variables, which should be turned into
  * parameters.
  */
 private RefactoringStatus initializeParameters() {
   RefactoringStatus result = new RefactoringStatus();
   final List<VariableElement> assignedUsedVariables = Lists.newArrayList();
   unitNode.accept(
       new ASTVisitor<Void>() {
         @Override
         public Void visitIdentifier(DartIdentifier node) {
           SourceRange nodeRange = SourceRangeFactory.create(node);
           if (SourceRangeUtils.covers(selectionRange, nodeRange)) {
             // analyze local variable
             VariableElement variableElement = ASTNodes.getVariableOrParameterElement(node);
             if (variableElement != null) {
               // if declared outside, add parameter
               if (!isDeclaredInSelection(variableElement)) {
                 String variableName = variableElement.getName();
                 // add parameter
                 if (!selectionParametersToRanges.containsKey(variableName)) {
                   parameters.add(new ParameterInfo(variableElement));
                 }
                 // add reference to parameter
                 {
                   List<SourceRange> ranges = selectionParametersToRanges.get(variableName);
                   if (ranges == null) {
                     ranges = Lists.newArrayList();
                     selectionParametersToRanges.put(variableName, ranges);
                   }
                   ranges.add(nodeRange);
                 }
               }
               // remember, if assigned and used after seleciton
               if (isLeftHandOfAssignment(node) && isUsedAfterSelection(variableElement)) {
                 if (!assignedUsedVariables.contains(variableElement)) {
                   assignedUsedVariables.add(variableElement);
                 }
               }
             }
             // remember declaration names
             if (ASTNodes.isNameOfDeclaration(node)) {
               usedNames.add(node.getName());
             }
           }
           return null;
         }
       });
   // may be single variable to return
   if (assignedUsedVariables.size() == 1) {
     returnVariable = assignedUsedVariables.get(0);
   }
   // fatal, if multiple variables assigned and used after selection
   if (assignedUsedVariables.size() > 1) {
     StringBuilder sb = new StringBuilder();
     for (VariableElement variable : assignedUsedVariables) {
       sb.append(variable.getName());
       sb.append("\n");
     }
     result.addFatalError(
         Messages.format(
             RefactoringCoreMessages.ExtractMethodAnalyzer_assignments_to_local,
             sb.toString().trim()));
   }
   // done
   return result;
 }
예제 #4
0
 @Override
 public Change createChange(IProgressMonitor pm) throws CoreException {
   pm.beginTask("", 1 + occurrences.size()); // $NON-NLS-1$
   try {
     // configure Change
     {
       change = new CompilationUnitChange(unit.getElementName(), unit);
       change.setEdit(new MultiTextEdit());
       change.setKeepPreviewEdits(true);
     }
     // replace occurrences with method invocation
     for (Occurrence occurence : occurrences) {
       pm.worked(1);
       SourceRange range = occurence.range;
       // may be replacement of duplicates disabled
       if (!replaceAllOccurrences && !occurence.isSelection) {
         continue;
       }
       // prepare invocation source
       String invocationSource;
       {
         StringBuilder sb = new StringBuilder();
         // may be returns value
         if (returnVariable != null) {
           String varTypeName = ExtractUtils.getTypeSource(returnVariable.getType());
           String originalName = returnVariable.getName();
           String occurrenceName = occurence.parameterOldToOccurrenceName.get(originalName);
           if (varTypeName.equals("dynamic")) {
             sb.append("var ");
           } else {
             sb.append(varTypeName);
             sb.append(" ");
           }
           sb.append(occurrenceName);
           sb.append(" = ");
         }
         // invocation itself
         sb.append(methodName);
         sb.append("(");
         boolean firstParameter = true;
         for (ParameterInfo parameter : parameters) {
           // may be comma
           if (firstParameter) {
             firstParameter = false;
           } else {
             sb.append(", ");
           }
           // argument name
           {
             String parameterOldName = parameter.getOldName();
             String argumentName = occurence.parameterOldToOccurrenceName.get(parameterOldName);
             sb.append(argumentName);
           }
         }
         sb.append(")");
         invocationSource = sb.toString();
         // statements as extracted with their ";", so add new one after invocation
         if (selectionStatements != null) {
           invocationSource += ";";
         }
       }
       // add replace edit
       TextEdit edit = new ReplaceEdit(range.getOffset(), range.getLength(), invocationSource);
       change.addEdit(edit);
       String msg =
           Messages.format(
               occurence.isSelection
                   ? RefactoringCoreMessages.ExtractMethodRefactoring_substitute_with_call
                   : RefactoringCoreMessages.ExtractMethodRefactoring_duplicates_single,
               methodName);
       change.addTextEditGroup(new TextEditGroup(msg, edit));
     }
     // add method declaration
     {
       // prepare environment
       String prefix = utils.getNodePrefix(parentMember);
       String eol = utils.getEndOfLine();
       // prepare annotations
       String annotations = "";
       {
         // may be "static"
         if (staticContext) {
           annotations = "static ";
         }
       }
       // prepare declaration source
       String declarationSource = null;
       {
         String returnExpressionSource = getMethodBodySource();
         // expression
         if (selectionExpression != null) {
           // add return type
           String returnTypeName = ExtractUtils.getTypeSource(selectionExpression);
           if (returnTypeName != null && !returnTypeName.equals("dynamic")) {
             annotations += returnTypeName + " ";
           }
           // just return expression
           declarationSource =
               annotations + getSignature() + " => " + returnExpressionSource + ";";
         }
         // statements
         if (selectionStatements != null) {
           if (returnVariable != null) {
             String returnTypeName = ExtractUtils.getTypeSource(returnVariable.getType());
             if (returnTypeName != null && !returnTypeName.equals("dynamic")) {
               annotations += returnTypeName + " ";
             }
           } else {
             annotations += "void ";
           }
           declarationSource = annotations + getSignature() + " {" + eol;
           declarationSource += returnExpressionSource;
           if (returnVariable != null) {
             declarationSource += prefix + "  return " + returnVariable.getName() + ";" + eol;
           }
           declarationSource += prefix + "}";
         }
       }
       // insert declaration
       if (declarationSource != null) {
         int offset = parentMember.getSourceInfo().getEnd();
         TextEdit edit = new ReplaceEdit(offset, 0, eol + eol + prefix + declarationSource);
         change.addEdit(edit);
         change.addTextEditGroup(
             new TextEditGroup(
                 Messages.format(
                     selectionExpression != null
                         ? RefactoringCoreMessages.ExtractMethodRefactoring_add_method_expression
                         : RefactoringCoreMessages.ExtractMethodRefactoring_add_method,
                     methodName),
                 edit));
       }
     }
     pm.worked(1);
     // done
     return change;
   } finally {
     pm.done();
   }
 }