コード例 #1
0
  public MethodDeclarationCompletionProposal(
      Type type, String methodName, String returnTypeSig, int start, int length, int relevance) {
    super(
        "",
        type.getCompilationUnit(),
        start,
        length,
        null,
        getDisplayName(methodName, returnTypeSig),
        relevance); //$NON-NLS-1$
    Assert.isNotNull(type);
    Assert.isNotNull(methodName);

    fType = type;
    fMethodName = methodName;
    fReturnTypeSig = returnTypeSig;

    if (returnTypeSig == null) {
      setProposalInfo(new ProposalInfo(type));

      ImageDescriptor desc =
          new DartElementImageDescriptor(
              DartPluginImages.DESC_MISC_PUBLIC,
              DartElementImageDescriptor.CONSTRUCTOR,
              DartElementImageProvider.SMALL_SIZE);
      setImage(DartToolsPlugin.getImageDescriptorRegistry().get(desc));
    } else {
      setImage(DartPluginImages.get(DartPluginImages.IMG_MISC_PRIVATE));
    }
  }
コード例 #2
0
  @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;
  }