• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Java LinkedCorrectionProposal类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal的典型用法代码示例。如果您正苦于以下问题:Java LinkedCorrectionProposal类的具体用法?Java LinkedCorrectionProposal怎么用?Java LinkedCorrectionProposal使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



LinkedCorrectionProposal类属于org.eclipse.jdt.internal.ui.text.correction.proposals包,在下文中一共展示了LinkedCorrectionProposal类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: addExceptionTypeLinkProposals

import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal; //导入依赖的package包/类
private static void addExceptionTypeLinkProposals(
    LinkedCorrectionProposal proposal, ITypeBinding exc, String key) {
  // all super classes except Object
  while (exc != null && !"java.lang.Object".equals(exc.getQualifiedName())) { // $NON-NLS-1$
    proposal.addLinkedPositionProposal(key, exc);
    exc = exc.getSuperclass();
  }
}
 
开发者ID:eclipse,项目名称:che,代码行数:9,代码来源:LocalCorrectionsSubProcessor.java


示例2: addExceptionTypeLinkProposals

import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal; //导入依赖的package包/类
private static void addExceptionTypeLinkProposals(LinkedCorrectionProposal proposal, ITypeBinding exc, String key) {
	// all super classes except Object
	while (exc != null && !"java.lang.Object".equals(exc.getQualifiedName())) { //$NON-NLS-1$
		proposal.addLinkedPositionProposal(key, exc);
		exc= exc.getSuperclass();
	}
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:8,代码来源:LocalCorrectionsSubProcessor.java


示例3: addUninitializedLocalVariableProposal

import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal; //导入依赖的package包/类
public static void addUninitializedLocalVariableProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ICompilationUnit cu= context.getCompilationUnit();

	ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
	if (!(selectedNode instanceof Name)) {
		return;
	}
	Name name= (Name) selectedNode;
	IBinding binding= name.resolveBinding();
	if (!(binding instanceof IVariableBinding)) {
		return;
	}
	IVariableBinding varBinding= (IVariableBinding) binding;

	CompilationUnit astRoot= context.getASTRoot();
	ASTNode node= astRoot.findDeclaringNode(binding);
	if (node instanceof VariableDeclarationFragment) {
		ASTRewrite rewrite= ASTRewrite.create(node.getAST());

		VariableDeclarationFragment fragment= (VariableDeclarationFragment) node;
		if (fragment.getInitializer() != null) {
			return;
		}
		Expression expression= ASTNodeFactory.newDefaultExpression(astRoot.getAST(), varBinding.getType());
		if (expression == null) {
			return;
		}
		rewrite.set(fragment, VariableDeclarationFragment.INITIALIZER_PROPERTY, expression, null);

		String label= CorrectionMessages.LocalCorrectionsSubProcessor_uninitializedvariable_description;
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);

		LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.INITIALIZE_VARIABLE, image);
		proposal.addLinkedPosition(rewrite.track(expression), false, "initializer"); //$NON-NLS-1$
		proposals.add(proposal);
	}
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:38,代码来源:LocalCorrectionsSubProcessor.java


示例4: addUninitializedLocalVariableProposal

import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal; //导入依赖的package包/类
public static void addUninitializedLocalVariableProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ICompilationUnit cu= context.getCompilationUnit();

	ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
	if (!(selectedNode instanceof Name)) {
		return;
	}
	Name name= (Name) selectedNode;
	IBinding binding= name.resolveBinding();
	if (!(binding instanceof IVariableBinding)) {
		return;
	}
	IVariableBinding varBinding= (IVariableBinding) binding;

	CompilationUnit astRoot= context.getASTRoot();
	ASTNode node= astRoot.findDeclaringNode(binding);
	if (node instanceof VariableDeclarationFragment) {
		ASTRewrite rewrite= ASTRewrite.create(node.getAST());

		VariableDeclarationFragment fragment= (VariableDeclarationFragment) node;
		if (fragment.getInitializer() != null) {
			return;
		}
		Expression expression= ASTNodeFactory.newDefaultExpression(astRoot.getAST(), varBinding.getType());
		if (expression == null) {
			return;
		}
		rewrite.set(fragment, VariableDeclarationFragment.INITIALIZER_PROPERTY, expression, null);

		String label= CorrectionMessages.LocalCorrectionsSubProcessor_uninitializedvariable_description;
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);

		LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, cu, rewrite, 6, image);
		proposal.addLinkedPosition(rewrite.track(expression), false, "initializer"); //$NON-NLS-1$
		proposals.add(proposal);
	}
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion-Juno38,代码行数:38,代码来源:LocalCorrectionsSubProcessor.java


示例5: addUninitializedLocalVariableProposal

import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal; //导入依赖的package包/类
public static void addUninitializedLocalVariableProposal(
    IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
  ICompilationUnit cu = context.getCompilationUnit();

  ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
  if (!(selectedNode instanceof Name)) {
    return;
  }
  Name name = (Name) selectedNode;
  IBinding binding = name.resolveBinding();
  if (!(binding instanceof IVariableBinding)) {
    return;
  }
  IVariableBinding varBinding = (IVariableBinding) binding;

  CompilationUnit astRoot = context.getASTRoot();
  ASTNode node = astRoot.findDeclaringNode(binding);
  if (node instanceof VariableDeclarationFragment) {
    ASTRewrite rewrite = ASTRewrite.create(node.getAST());

    VariableDeclarationFragment fragment = (VariableDeclarationFragment) node;
    if (fragment.getInitializer() != null) {
      return;
    }
    Expression expression =
        ASTNodeFactory.newDefaultExpression(astRoot.getAST(), varBinding.getType());
    if (expression == null) {
      return;
    }
    rewrite.set(fragment, VariableDeclarationFragment.INITIALIZER_PROPERTY, expression, null);

    String label =
        CorrectionMessages.LocalCorrectionsSubProcessor_uninitializedvariable_description;
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);

    LinkedCorrectionProposal proposal =
        new LinkedCorrectionProposal(
            label, cu, rewrite, IProposalRelevance.INITIALIZE_VARIABLE, image);
    proposal.addLinkedPosition(rewrite.track(expression), false, "initializer"); // $NON-NLS-1$
    proposals.add(proposal);
  }
}
 
开发者ID:eclipse,项目名称:che,代码行数:43,代码来源:LocalCorrectionsSubProcessor.java


示例6: getConvertStringConcatenationProposals

import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal; //导入依赖的package包/类
private static boolean getConvertStringConcatenationProposals(
    IInvocationContext context, Collection<ICommandAccess> resultingCollections) {
  ASTNode node = context.getCoveringNode();
  BodyDeclaration parentDecl = ASTResolving.findParentBodyDeclaration(node);
  if (!(parentDecl instanceof MethodDeclaration || parentDecl instanceof Initializer))
    return false;

  AST ast = node.getAST();
  ITypeBinding stringBinding = ast.resolveWellKnownType("java.lang.String"); // $NON-NLS-1$

  if (node instanceof Expression && !(node instanceof InfixExpression)) {
    node = node.getParent();
  }
  if (node instanceof VariableDeclarationFragment) {
    node = ((VariableDeclarationFragment) node).getInitializer();
  } else if (node instanceof Assignment) {
    node = ((Assignment) node).getRightHandSide();
  }

  InfixExpression oldInfixExpression = null;
  while (node instanceof InfixExpression) {
    InfixExpression curr = (InfixExpression) node;
    if (curr.resolveTypeBinding() == stringBinding
        && curr.getOperator() == InfixExpression.Operator.PLUS) {
      oldInfixExpression = curr; // is a infix expression we can use
    } else {
      break;
    }
    node = node.getParent();
  }
  if (oldInfixExpression == null) return false;

  if (resultingCollections == null) {
    return true;
  }

  LinkedCorrectionProposal stringBufferProposal =
      getConvertToStringBufferProposal(context, ast, oldInfixExpression);
  resultingCollections.add(stringBufferProposal);

  ASTRewriteCorrectionProposal messageFormatProposal =
      getConvertToMessageFormatProposal(context, ast, oldInfixExpression);
  if (messageFormatProposal != null) resultingCollections.add(messageFormatProposal);

  return true;
}
 
开发者ID:eclipse,项目名称:che,代码行数:47,代码来源:QuickAssistProcessor.java


示例7: getArrayInitializerToArrayCreation

import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal; //导入依赖的package包/类
private static boolean getArrayInitializerToArrayCreation(
    IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
  if (!(node instanceof ArrayInitializer)) {
    return false;
  }
  ArrayInitializer initializer = (ArrayInitializer) node;

  ASTNode parent = initializer.getParent();
  while (parent instanceof ArrayInitializer) {
    initializer = (ArrayInitializer) parent;
    parent = parent.getParent();
  }
  ITypeBinding typeBinding = initializer.resolveTypeBinding();
  if (!(parent instanceof VariableDeclaration) || typeBinding == null || !typeBinding.isArray()) {
    return false;
  }
  if (resultingCollections == null) {
    return true;
  }

  AST ast = node.getAST();
  ASTRewrite rewrite = ASTRewrite.create(ast);

  String label = CorrectionMessages.QuickAssistProcessor_typetoarrayInitializer_description;
  Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);

  LinkedCorrectionProposal proposal =
      new LinkedCorrectionProposal(
          label,
          context.getCompilationUnit(),
          rewrite,
          IProposalRelevance.ADD_TYPE_TO_ARRAY_INITIALIZER,
          image);

  ImportRewrite imports = proposal.createImportRewrite(context.getASTRoot());
  ImportRewriteContext importRewriteContext =
      new ContextSensitiveImportRewriteContext(node, imports);
  String typeName = imports.addImport(typeBinding, importRewriteContext);

  ArrayCreation creation = ast.newArrayCreation();
  creation.setInitializer((ArrayInitializer) rewrite.createMoveTarget(initializer));
  creation.setType((ArrayType) ASTNodeFactory.newType(ast, typeName));

  rewrite.replace(initializer, creation, null);

  resultingCollections.add(proposal);
  return true;
}
 
开发者ID:eclipse,项目名称:che,代码行数:49,代码来源:QuickAssistProcessor.java


示例8: getCombineStringProposals

import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal; //导入依赖的package包/类
private static boolean getCombineStringProposals(
    IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
  // we work with InfixExpressions
  InfixExpression infixExpression;
  if (node instanceof InfixExpression) {
    infixExpression = (InfixExpression) node;
  } else if (node.getParent() instanceof InfixExpression) {
    infixExpression = (InfixExpression) node.getParent();
  } else {
    return false;
  }

  // only + is valid for combining strings
  if (!(infixExpression.getOperator().equals(InfixExpression.Operator.PLUS))) {
    return false;
  }

  // all expressions must be strings
  Expression leftOperand = infixExpression.getLeftOperand();
  Expression rightOperand = infixExpression.getRightOperand();
  if (!(leftOperand instanceof StringLiteral && rightOperand instanceof StringLiteral)) {
    return false;
  }

  StringLiteral leftString = (StringLiteral) leftOperand;
  StringLiteral rightString = (StringLiteral) rightOperand;

  if (resultingCollections == null) {
    return true;
  }

  // begin building combined string
  StringBuilder stringBuilder = new StringBuilder(leftString.getLiteralValue());
  stringBuilder.append(rightString.getLiteralValue());

  // append extended string literals
  for (Object operand : infixExpression.extendedOperands()) {
    if (!(operand instanceof StringLiteral)) return false;
    StringLiteral stringLiteral = (StringLiteral) operand;
    stringBuilder.append(stringLiteral.getLiteralValue());
  }

  // prepare new string literal
  AST ast = node.getAST();
  StringLiteral combinedStringLiteral = ast.newStringLiteral();
  combinedStringLiteral.setLiteralValue(stringBuilder.toString());

  ASTRewrite rewrite = ASTRewrite.create(ast);
  rewrite.replace(infixExpression, combinedStringLiteral, null);

  // add correction proposal
  String label = CorrectionMessages.AdvancedQuickAssistProcessor_combineSelectedStrings;
  Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
  LinkedCorrectionProposal proposal =
      new LinkedCorrectionProposal(
          label,
          context.getCompilationUnit(),
          rewrite,
          IProposalRelevance.COMBINE_STRINGS,
          image);
  resultingCollections.add(proposal);
  return true;
}
 
开发者ID:eclipse,项目名称:che,代码行数:64,代码来源:AdvancedQuickAssistProcessor.java


示例9: addMissingReturnTypeProposals

import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal; //导入依赖的package包/类
public static void addMissingReturnTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ICompilationUnit cu= context.getCompilationUnit();

	CompilationUnit astRoot= context.getASTRoot();
	ASTNode selectedNode= problem.getCoveringNode(astRoot);
	if (selectedNode == null) {
		return;
	}
	BodyDeclaration decl= ASTResolving.findParentBodyDeclaration(selectedNode);
	if (decl instanceof MethodDeclaration) {
		MethodDeclaration methodDeclaration= (MethodDeclaration) decl;

		ReturnStatementCollector eval= new ReturnStatementCollector();
		decl.accept(eval);

		AST ast= astRoot.getAST();

		ITypeBinding typeBinding= eval.getTypeBinding(decl.getAST());
		typeBinding= Bindings.normalizeTypeBinding(typeBinding);
		if (typeBinding == null) {
			typeBinding= ast.resolveWellKnownType("void"); //$NON-NLS-1$
		}
		if (typeBinding.isWildcardType()) {
			typeBinding= ASTResolving.normalizeWildcardType(typeBinding, true, ast);
		}

		ASTRewrite rewrite= ASTRewrite.create(ast);

		String label= Messages.format(CorrectionMessages.ReturnTypeSubProcessor_missingreturntype_description, BindingLabelProvider.getBindingLabel(typeBinding, BindingLabelProvider.DEFAULT_TEXTFLAGS));
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
		LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.MISSING_RETURN_TYPE, image);

		ImportRewrite imports= proposal.createImportRewrite(astRoot);
		ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(decl, imports);
		Type type= imports.addImport(typeBinding, ast, importRewriteContext);

		rewrite.set(methodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
		rewrite.set(methodDeclaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null);

		Javadoc javadoc= methodDeclaration.getJavadoc();
		if (javadoc != null && typeBinding != null) {
			TagElement newTag= ast.newTagElement();
			newTag.setTagName(TagElement.TAG_RETURN);
			TextElement commentStart= ast.newTextElement();
			newTag.fragments().add(commentStart);

			JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
			proposal.addLinkedPosition(rewrite.track(commentStart), false, "comment_start"); //$NON-NLS-1$
		}

		String key= "return_type"; //$NON-NLS-1$
		proposal.addLinkedPosition(rewrite.track(type), true, key);
		if (typeBinding != null) {
			ITypeBinding[] bindings= ASTResolving.getRelaxingTypes(ast, typeBinding);
			for (int i= 0; i < bindings.length; i++) {
				proposal.addLinkedPositionProposal(key, bindings[i]);
			}
		}

		proposals.add(proposal);

		// change to constructor
		ASTNode parentType= ASTResolving.findParentType(decl);
		if (parentType instanceof AbstractTypeDeclaration) {
			boolean isInterface= parentType instanceof TypeDeclaration && ((TypeDeclaration) parentType).isInterface();
			if (!isInterface) {
				String constructorName= ((AbstractTypeDeclaration) parentType).getName().getIdentifier();
				ASTNode nameNode= methodDeclaration.getName();
				label= Messages.format(CorrectionMessages.ReturnTypeSubProcessor_wrongconstructorname_description, BasicElementLabels.getJavaElementName(constructorName));
				proposals.add(new ReplaceCorrectionProposal(label, cu, nameNode.getStartPosition(), nameNode.getLength(), constructorName, IProposalRelevance.CHANGE_TO_CONSTRUCTOR));
			}
		}
	}
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:75,代码来源:ReturnTypeSubProcessor.java


示例10: getConvertStringConcatenationProposals

import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal; //导入依赖的package包/类
private static boolean getConvertStringConcatenationProposals(IInvocationContext context, Collection<ICommandAccess> resultingCollections) {
	ASTNode node= context.getCoveringNode();
	BodyDeclaration parentDecl= ASTResolving.findParentBodyDeclaration(node);
	if (!(parentDecl instanceof MethodDeclaration || parentDecl instanceof Initializer))
		return false;

	AST ast= node.getAST();
	ITypeBinding stringBinding= ast.resolveWellKnownType("java.lang.String"); //$NON-NLS-1$

	if (node instanceof Expression && !(node instanceof InfixExpression)) {
		node= node.getParent();
	}
	if (node instanceof VariableDeclarationFragment) {
		node= ((VariableDeclarationFragment) node).getInitializer();
	} else if (node instanceof Assignment) {
		node= ((Assignment) node).getRightHandSide();
	}

	InfixExpression oldInfixExpression= null;
	while (node instanceof InfixExpression) {
		InfixExpression curr= (InfixExpression) node;
		if (curr.resolveTypeBinding() == stringBinding && curr.getOperator() == InfixExpression.Operator.PLUS) {
			oldInfixExpression= curr; // is a infix expression we can use
		} else {
			break;
		}
		node= node.getParent();
	}
	if (oldInfixExpression == null)
		return false;

	if (resultingCollections == null) {
		return true;
	}

	LinkedCorrectionProposal stringBufferProposal= getConvertToStringBufferProposal(context, ast, oldInfixExpression);
	resultingCollections.add(stringBufferProposal);

	ASTRewriteCorrectionProposal messageFormatProposal= getConvertToMessageFormatProposal(context, ast, oldInfixExpression);
	if (messageFormatProposal != null)
		resultingCollections.add(messageFormatProposal);

	return true;
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:45,代码来源:QuickAssistProcessor.java


示例11: getPickOutStringProposals

import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal; //导入依赖的package包/类
private static boolean getPickOutStringProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
	// we work with String's
	if (!(node instanceof StringLiteral)) {
		return false;
	}
	// user should select part of String
	int selectionPos= context.getSelectionOffset();
	int selectionLen= context.getSelectionLength();
	if (selectionLen == 0) {
		return false;
	}
	int valueStart= node.getStartPosition() + 1;
	int valueEnd= node.getStartPosition() + node.getLength() - 1;

	// selection must be inside node and the quotes and not contain the full value
	if (selectionPos < valueStart || selectionPos + selectionLen > valueEnd || valueEnd - valueStart == selectionLen) {
		return false;
	}

	// prepare string parts positions
	StringLiteral stringLiteral= (StringLiteral) node;
	String stringValue= stringLiteral.getEscapedValue();

	int firstPos= selectionPos - node.getStartPosition();
	int secondPos= firstPos + selectionLen;


	// prepare new string literals

	AST ast= node.getAST();
	StringLiteral leftLiteral= ast.newStringLiteral();
	StringLiteral centerLiteral= ast.newStringLiteral();
	StringLiteral rightLiteral= ast.newStringLiteral();
	try {
		leftLiteral.setEscapedValue('"' + stringValue.substring(1, firstPos) + '"');
		centerLiteral.setEscapedValue('"' + stringValue.substring(firstPos, secondPos) + '"');
		rightLiteral.setEscapedValue('"' + stringValue.substring(secondPos, stringValue.length() - 1) + '"');
	} catch (IllegalArgumentException e) {
		return false;
	}
	if (resultingCollections == null) {
		return true;
	}

	ASTRewrite rewrite= ASTRewrite.create(ast);

	// prepare new expression instead of StringLiteral
	InfixExpression expression= ast.newInfixExpression();
	expression.setOperator(InfixExpression.Operator.PLUS);
	if (firstPos != 1) {
		expression.setLeftOperand(leftLiteral);
	}


	if (firstPos == 1) {
		expression.setLeftOperand(centerLiteral);
	} else {
		expression.setRightOperand(centerLiteral);
	}

	if (secondPos < stringValue.length() - 1) {
		if (firstPos == 1) {
			expression.setRightOperand(rightLiteral);
		} else {
			expression.extendedOperands().add(rightLiteral);
		}
	}
	// use new expression instead of old StirngLiteral
	rewrite.replace(stringLiteral, expression, null);
	// add correction proposal
	String label= CorrectionMessages.AdvancedQuickAssistProcessor_pickSelectedString;
	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
	LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.PICK_SELECTED_STRING, image);
	proposal.addLinkedPosition(rewrite.track(centerLiteral), true, "CENTER_STRING"); //$NON-NLS-1$
	resultingCollections.add(proposal);
	return true;
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:78,代码来源:AdvancedQuickAssistProcessor.java


示例12: addMissingReturnTypeProposals

import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal; //导入依赖的package包/类
public static void addMissingReturnTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ICompilationUnit cu= context.getCompilationUnit();

	CompilationUnit astRoot= context.getASTRoot();
	ASTNode selectedNode= problem.getCoveringNode(astRoot);
	if (selectedNode == null) {
		return;
	}
	BodyDeclaration decl= ASTResolving.findParentBodyDeclaration(selectedNode);
	if (decl instanceof MethodDeclaration) {
		MethodDeclaration methodDeclaration= (MethodDeclaration) decl;

		ReturnStatementCollector eval= new ReturnStatementCollector();
		decl.accept(eval);

		AST ast= astRoot.getAST();

		ITypeBinding typeBinding= eval.getTypeBinding(decl.getAST());
		typeBinding= Bindings.normalizeTypeBinding(typeBinding);
		if (typeBinding == null) {
			typeBinding= ast.resolveWellKnownType("void"); //$NON-NLS-1$
		}
		if (typeBinding.isWildcardType()) {
			typeBinding= ASTResolving.normalizeWildcardType(typeBinding, true, ast);
		}

		ASTRewrite rewrite= ASTRewrite.create(ast);

		String label= Messages.format(CorrectionMessages.ReturnTypeSubProcessor_missingreturntype_description, BindingLabelProvider.getBindingLabel(typeBinding, BindingLabelProvider.DEFAULT_TEXTFLAGS));
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
		LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, cu, rewrite, 6, image);

		ImportRewrite imports= proposal.createImportRewrite(astRoot);
		ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(decl, imports);
		Type type= imports.addImport(typeBinding, ast, importRewriteContext);

		rewrite.set(methodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
		rewrite.set(methodDeclaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null);

		Javadoc javadoc= methodDeclaration.getJavadoc();
		if (javadoc != null && typeBinding != null) {
			TagElement newTag= ast.newTagElement();
			newTag.setTagName(TagElement.TAG_RETURN);
			TextElement commentStart= ast.newTextElement();
			newTag.fragments().add(commentStart);

			JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
			proposal.addLinkedPosition(rewrite.track(commentStart), false, "comment_start"); //$NON-NLS-1$
		}

		String key= "return_type"; //$NON-NLS-1$
		proposal.addLinkedPosition(rewrite.track(type), true, key);
		if (typeBinding != null) {
			ITypeBinding[] bindings= ASTResolving.getRelaxingTypes(ast, typeBinding);
			for (int i= 0; i < bindings.length; i++) {
				proposal.addLinkedPositionProposal(key, bindings[i]);
			}
		}

		proposals.add(proposal);

		// change to constructor
		ASTNode parentType= ASTResolving.findParentType(decl);
		if (parentType instanceof AbstractTypeDeclaration) {
			boolean isInterface= parentType instanceof TypeDeclaration && ((TypeDeclaration) parentType).isInterface();
			if (!isInterface) {
				String constructorName= ((AbstractTypeDeclaration) parentType).getName().getIdentifier();
				ASTNode nameNode= methodDeclaration.getName();
				label= Messages.format(CorrectionMessages.ReturnTypeSubProcessor_wrongconstructorname_description, BasicElementLabels.getJavaElementName(constructorName));
				proposals.add(new ReplaceCorrectionProposal(label, cu, nameNode.getStartPosition(), nameNode.getLength(), constructorName, 5));
			}
		}
	}
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion-Juno38,代码行数:75,代码来源:ReturnTypeSubProcessor.java


示例13: getPickOutStringProposals

import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal; //导入依赖的package包/类
private static boolean getPickOutStringProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
	// we work with String's
	if (!(node instanceof StringLiteral)) {
		return false;
	}
	// user should select part of String
	int selectionPos= context.getSelectionOffset();
	int selectionLen= context.getSelectionLength();
	if (selectionLen == 0) {
		return false;
	}
	int valueStart= node.getStartPosition() + 1;
	int valueEnd= node.getStartPosition() + node.getLength() - 1;

	// selection must be inside node and the quotes and not contain the full value
	if (selectionPos < valueStart || selectionPos + selectionLen > valueEnd || valueEnd - valueStart == selectionLen) {
		return false;
	}

	// prepare string parts positions
	StringLiteral stringLiteral= (StringLiteral) node;
	String stringValue= stringLiteral.getEscapedValue();

	int firstPos= selectionPos - node.getStartPosition();
	int secondPos= firstPos + selectionLen;


	// prepare new string literals

	AST ast= node.getAST();
	StringLiteral leftLiteral= ast.newStringLiteral();
	StringLiteral centerLiteral= ast.newStringLiteral();
	StringLiteral rightLiteral= ast.newStringLiteral();
	try {
		leftLiteral.setEscapedValue('"' + stringValue.substring(1, firstPos) + '"');
		centerLiteral.setEscapedValue('"' + stringValue.substring(firstPos, secondPos) + '"');
		rightLiteral.setEscapedValue('"' + stringValue.substring(secondPos, stringValue.length() - 1) + '"');
	} catch (IllegalArgumentException e) {
		return false;
	}
	if (resultingCollections == null) {
		return true;
	}

	ASTRewrite rewrite= ASTRewrite.create(ast);

	// prepare new expression instead of StringLiteral
	InfixExpression expression= ast.newInfixExpression();
	expression.setOperator(InfixExpression.Operator.PLUS);
	if (firstPos != 1) {
		expression.setLeftOperand(leftLiteral);
	}


	if (firstPos == 1) {
		expression.setLeftOperand(centerLiteral);
	} else {
		expression.setRightOperand(centerLiteral);
	}

	if (secondPos < stringValue.length() - 1) {
		if (firstPos == 1) {
			expression.setRightOperand(rightLiteral);
		} else {
			expression.extendedOperands().add(rightLiteral);
		}
	}
	// use new expression instead of old StirngLiteral
	rewrite.replace(stringLiteral, expression, null);
	// add correction proposal
	String label= CorrectionMessages.AdvancedQuickAssistProcessor_pickSelectedString;
	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
	LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, 1, image);
	proposal.addLinkedPosition(rewrite.track(centerLiteral), true, "CENTER_STRING"); //$NON-NLS-1$
	resultingCollections.add(proposal);
	return true;
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion-Juno38,代码行数:78,代码来源:AdvancedQuickAssistProcessor.java



注:本文中的org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java CloseEvent类代码示例发布时间:2022-05-22
下一篇:
Java MovieDbException类代码示例发布时间:2022-05-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap