/**
  * Returns the type signature of the declaring type of a <code>CompletionProposal</code>, or
  * <code>null</code> for proposals that do not have a declaring type. The return value is
  * <em>not</em> <code>null</code> for proposals of the following kinds:
  *
  * <ul>
  *   <li>METHOD_DECLARATION
  *   <li>METHOD_NAME_REFERENCE
  *   <li>METHOD_REF
  *   <li>ANNOTATION_ATTRIBUTE_REF
  *   <li>POTENTIAL_METHOD_DECLARATION
  *   <li>ANONYMOUS_CLASS_DECLARATION
  *   <li>FIELD_REF
  *   <li>PACKAGE_REF (returns the package, but no type)
  *   <li>TYPE_REF
  * </ul>
  *
  * @param proposal the completion proposal to get the declaring type for
  * @return the type signature of the declaring type, or <code>null</code> if there is none
  */
 protected final char[] getDeclaringType(CompletionProposal proposal) {
   switch (proposal.getKind()) {
     case CompletionProposal.METHOD_DECLARATION:
     case CompletionProposal.METHOD_NAME_REFERENCE:
       //      case CompletionProposal.JAVADOC_METHOD_REF:
     case CompletionProposal.METHOD_REF:
     case CompletionProposal.ARGUMENT_LIST:
     case CompletionProposal.CONSTRUCTOR_INVOCATION:
       //      case CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION:
       //      case CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER:
       //      case CompletionProposal.ANNOTATION_ATTRIBUTE_REF:
     case CompletionProposal.POTENTIAL_METHOD_DECLARATION:
       //      case CompletionProposal.ANONYMOUS_CLASS_DECLARATION:
     case CompletionProposal.FIELD_REF:
       //      case CompletionProposal.FIELD_REF_WITH_CASTED_RECEIVER:
       //      case CompletionProposal.JAVADOC_FIELD_REF:
       //      case CompletionProposal.JAVADOC_VALUE_REF:
       char[] declaration = proposal.getDeclarationSignature();
       // special methods may not have a declaring type: methods defined on arrays etc.
       // Currently known: class literals don't have a declaring type - use Object
       //        if (declaration == null) {
       //          return "java.lang.Object".toCharArray(); //$NON-NLS-1$
       //        }
       return Signature.toCharArray(declaration);
     case CompletionProposal.LIBRARY_PREFIX:
       return proposal.getDeclarationSignature();
       //      case CompletionProposal.JAVADOC_TYPE_REF:
     case CompletionProposal.TYPE_REF:
       return Signature.toCharArray(proposal.getSignature());
     case CompletionProposal.LOCAL_VARIABLE_REF:
     case CompletionProposal.VARIABLE_DECLARATION:
     case CompletionProposal.KEYWORD:
     case CompletionProposal.LABEL_REF:
     case CompletionProposal.TYPE_IMPORT:
     case CompletionProposal.OPTIONAL_ARGUMENT:
     case CompletionProposal.NAMED_ARGUMENT:
       //      case CompletionProposal.JAVADOC_BLOCK_TAG:
       //      case CompletionProposal.JAVADOC_INLINE_TAG:
       //      case CompletionProposal.JAVADOC_PARAM_REF:
       return null;
     default:
       Assert.isTrue(false);
       return null;
   }
 }
  private IDartCompletionProposal createMethodDeclarationProposal(CompletionProposal proposal) {
    if (fCompilationUnit == null || fDartProject == null) {
      return null;
    }

    String name = String.valueOf(proposal.getName());
    String[] paramTypes = Signature.getParameterTypes(String.valueOf(proposal.getSignature()));
    for (int index = 0; index < paramTypes.length; index++) {
      paramTypes[index] = Signature.toString(paramTypes[index]);
    }
    int start = proposal.getReplaceStart();
    int length = getLength(proposal);

    StyledString label =
        new StyledString(
            fLabelProvider.createOverrideMethodProposalLabel(proposal)); // TODO(messick)

    DartCompletionProposal dartProposal =
        new OverrideCompletionProposal(
            fDartProject,
            fCompilationUnit,
            name,
            paramTypes,
            start,
            length,
            getLengthIdentifier(proposal),
            label,
            String.valueOf(proposal.getCompletion()));
    dartProposal.setImage(getImage(fLabelProvider.createMethodImageDescriptor(proposal)));
    // TODO(scheglov) implement documentation comment
    //    dartProposal.setProposalInfo(new MethodProposalInfo(fDartProject, proposal));
    dartProposal.setRelevance(computeRelevance(proposal));

    fSuggestedMethodNames.add(new String(name));
    return dartProposal;
  }
 private static StyledString getDisplayName(String methodName, String returnTypeSig) {
   StyledString buf = new StyledString();
   buf.append(methodName);
   buf.append('(');
   buf.append(')');
   if (returnTypeSig != null) {
     buf.append(" : "); // $NON-NLS-1$
     buf.append(Signature.toString(returnTypeSig));
     buf.append(" - ", StyledString.QUALIFIER_STYLER); // $NON-NLS-1$
     buf.append(
         DartTextMessages.MethodCompletionProposal_method_label, StyledString.QUALIFIER_STYLER);
   } else {
     buf.append(" - ", StyledString.QUALIFIER_STYLER); // $NON-NLS-1$
     buf.append(
         DartTextMessages.MethodCompletionProposal_constructor_label,
         StyledString.QUALIFIER_STYLER);
   }
   return buf;
 }
  @Override
  protected boolean updateReplacementString(
      IDocument document, char trigger, int offset, ImportRewrite impRewrite)
      throws CoreException, BadLocationException {

    CodeGenerationSettings settings =
        JavaPreferencesSettings.getCodeGenerationSettings(fType.getDartProject());
    boolean addComments = settings.createComments;

    String[] empty = new String[0];
    String lineDelim = TextUtilities.getDefaultLineDelimiter(document);
    @SuppressWarnings("deprecation")
    String declTypeName = fType.getTypeQualifiedName('.');
    boolean isInterface = fType.isInterface();

    StringBuffer buf = new StringBuffer();
    if (addComments) {
      String comment =
          CodeGeneration.getMethodComment(
              fType.getCompilationUnit(),
              declTypeName,
              fMethodName,
              empty,
              empty,
              fReturnTypeSig,
              lineDelim);
      if (comment != null) {
        buf.append(comment);
        buf.append(lineDelim);
      }
    }

    if (fReturnTypeSig != null) {
      buf.append(Signature.toString(fReturnTypeSig));
    }
    buf.append(' ');
    buf.append(fMethodName);
    if (isInterface) {
      buf.append("();"); // $NON-NLS-1$
      buf.append(lineDelim);
    } else {
      buf.append("() {"); // $NON-NLS-1$
      buf.append(lineDelim);

      String body =
          CodeGeneration.getMethodBodyContent(
              fType.getCompilationUnit(),
              declTypeName,
              fMethodName,
              fReturnTypeSig == null,
              "",
              lineDelim); //$NON-NLS-1$
      if (body != null) {
        buf.append(body);
        buf.append(lineDelim);
      }
      buf.append("}"); // $NON-NLS-1$
      buf.append(lineDelim);
    }
    String stub = buf.toString();

    // use the code formatter
    IRegion region = document.getLineInformationOfOffset(getReplacementOffset());
    int lineStart = region.getOffset();
    int indent =
        Strings.computeIndentUnits(
            document.get(lineStart, getReplacementOffset() - lineStart),
            settings.tabWidth,
            settings.indentWidth);

    String replacement =
        CodeFormatterUtil.format(
            CodeFormatter.K_CLASS_BODY_DECLARATIONS,
            stub,
            indent,
            null,
            lineDelim,
            fType.getDartProject());

    if (replacement.endsWith(lineDelim)) {
      replacement = replacement.substring(0, replacement.length() - lineDelim.length());
    }

    setReplacementString(Strings.trimLeadingTabsAndSpaces(replacement));
    return true;
  }