Java Code Examples for org.eclipse.lsp4j.CodeActionKind#QuickFix

The following examples show how to use org.eclipse.lsp4j.CodeActionKind#QuickFix . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnresolvedElementsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static void getAmbiguousTypeReferenceProposals(IInvocationContext context, IProblemLocationCore problem,
		Collection<ChangeCorrectionProposal> proposals) throws CoreException {
	final ICompilationUnit cu= context.getCompilationUnit();
	int offset= problem.getOffset();
	int len= problem.getLength();

	IJavaElement[] elements= cu.codeSelect(offset, len);
	for (int i= 0; i < elements.length; i++) {
		IJavaElement curr= elements[i];
		if (curr instanceof IType && !TypeFilter.isFiltered((IType) curr)) {
			String qualifiedTypeName= ((IType) curr).getFullyQualifiedName('.');

			CompilationUnit root= context.getASTRoot();

			String label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_importexplicit_description, BasicElementLabels.getJavaElementName(qualifiedTypeName));
			ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, cu, ASTRewrite.create(root.getAST()), IProposalRelevance.IMPORT_EXPLICIT);

			ImportRewrite imports= proposal.createImportRewrite(root);
			imports.addImport(qualifiedTypeName);

			proposals.add(proposal);
		}
	}
}
 
Example 2
Source File: JavadocTagsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static void getInvalidQualificationProposals(IInvocationContext context, IProblemLocationCore problem,
		Collection<ChangeCorrectionProposal> proposals) {
	ASTNode node= problem.getCoveringNode(context.getASTRoot());
	if (!(node instanceof Name)) {
		return;
	}
	Name name= (Name) node;
	IBinding binding= name.resolveBinding();
	if (!(binding instanceof ITypeBinding)) {
		return;
	}
	ITypeBinding typeBinding= (ITypeBinding)binding;

	AST ast= node.getAST();
	ASTRewrite rewrite= ASTRewrite.create(ast);
	rewrite.replace(name, ast.newName(typeBinding.getQualifiedName()), null);

	String label= CorrectionMessages.JavadocTagsSubProcessor_qualifylinktoinner_description;
	ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(),
			rewrite, IProposalRelevance.QUALIFY_INNER_TYPE_NAME);

	proposals.add(proposal);
}
 
Example 3
Source File: ChangeMethodSignatureProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public ChangeMethodSignatureProposal(String label, ICompilationUnit targetCU, ASTNode invocationNode,
		IMethodBinding binding, ChangeDescription[] paramChanges, ChangeDescription[] exceptionChanges,
		int relevance) {
	super(label, CodeActionKind.QuickFix, targetCU, null, relevance);

	Assert.isTrue(binding != null && Bindings.isDeclarationBinding(binding));

	fInvocationNode= invocationNode;
	fSenderBinding= binding;
	fParameterChanges= paramChanges;
	fExceptionChanges= exceptionChanges;
}
 
Example 4
Source File: UnresolvedElementsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
static CUCorrectionProposal createTypeRefChangeFullProposal(ICompilationUnit cu, ITypeBinding binding, ASTNode node, int relevance) {
	ASTRewrite rewrite= ASTRewrite.create(node.getAST());
	String label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_change_full_type_description, BindingLabelProviderCore.getBindingLabel(binding, JavaElementLabels.ALL_DEFAULT));

	ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, cu, rewrite, relevance);

	ImportRewrite imports= proposal.createImportRewrite((CompilationUnit) node.getRoot());
	Type type= imports.addImport(binding, node.getAST());

	rewrite.replace(node, type, null);
	return proposal;
}
 
Example 5
Source File: JDTLanguageServer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private CodeActionOptions getCodeActionOptions() {
	String[] kinds = { CodeActionKind.QuickFix, CodeActionKind.Refactor, CodeActionKind.RefactorExtract, CodeActionKind.RefactorInline, CodeActionKind.RefactorRewrite, CodeActionKind.Source, CodeActionKind.SourceOrganizeImports };
	List<String> codeActionKinds = new ArrayList<>();
	for (String kind : kinds) {
		if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(kind)) {
			codeActionKinds.add(kind);
		}
	}
	CodeActionOptions options = new CodeActionOptions(codeActionKinds);
	return options;
}
 
Example 6
Source File: AbstractMethodCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public AbstractMethodCorrectionProposal(String label, ICompilationUnit targetCU, ASTNode invocationNode,
		ITypeBinding binding, int relevance) {
	super(label, CodeActionKind.QuickFix, targetCU, null, relevance);

	Assert.isTrue(binding != null && Bindings.isDeclarationBinding(binding));

	fNode= invocationNode;
	fSenderBinding= binding;
}
 
Example 7
Source File: ModifierChangeCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public ModifierChangeCorrectionProposal(String label, ICompilationUnit targetCU, IBinding binding, ASTNode node, int includedModifiers, int excludedModifiers, int relevance) {
	super(label, CodeActionKind.QuickFix, targetCU, null, relevance);
	fBinding = binding;
	fNode = node;
	fIncludedModifiers = includedModifiers;
	fExcludedModifiers = excludedModifiers;
}
 
Example 8
Source File: CorrectMainTypeNameProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Constructor for CorrectTypeNameProposal.
 * @param cu the compilation unit
 * @param context the invocation context
 * @param oldTypeName the old type name
 * @param newTypeName the new type name
 * @param relevance the relevance
 */
public CorrectMainTypeNameProposal(ICompilationUnit cu, IInvocationContext context, String oldTypeName, String newTypeName, int relevance) {
	super("", CodeActionKind.QuickFix, cu, null, relevance); //$NON-NLS-1$
	fContext= context;

	setDisplayName(Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_renametype_description, BasicElementLabels.getJavaElementName(newTypeName)));

	fOldName= oldTypeName;
	fNewName= newTypeName;
}
 
Example 9
Source File: UnresolvedElementsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static boolean useExistingParentCastProposal(ICompilationUnit cu, CastExpression expression,
		Expression accessExpression, SimpleName accessSelector, ITypeBinding[] paramTypes,
		Collection<ChangeCorrectionProposal> proposals) {
	ITypeBinding castType= expression.getType().resolveBinding();
	if (castType == null) {
		return false;
	}
	if (paramTypes != null) {
		if (Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes) == null) {
			return false;
		}
	} else if (Bindings.findFieldInHierarchy(castType, accessSelector.getIdentifier()) == null) {
		return false;
	}
	ITypeBinding bindingToCast= accessExpression.resolveTypeBinding();
	if (bindingToCast != null && !bindingToCast.isCastCompatible(castType)) {
		return false;
	}

	IMethodBinding res= Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes);
	if (res != null) {
		AST ast= expression.getAST();
		ASTRewrite rewrite= ASTRewrite.create(ast);
		CastExpression newCast= ast.newCastExpression();
		newCast.setType((Type) ASTNode.copySubtree(ast, expression.getType()));
		newCast.setExpression((Expression) rewrite.createCopyTarget(accessExpression));
		ParenthesizedExpression parents= ast.newParenthesizedExpression();
		parents.setExpression(newCast);

		ASTNode node= rewrite.createCopyTarget(expression.getExpression());
		rewrite.replace(expression, node, null);
		rewrite.replace(accessExpression, parents, null);

		String label= CorrectionMessages.UnresolvedElementsSubProcessor_missingcastbrackets_description;
		ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, cu, rewrite,
				IProposalRelevance.ADD_PARENTHESES_AROUND_CAST);
		proposals.add(proposal);
		return true;
	}
	return false;
}
 
Example 10
Source File: JavadocTagsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public AddAllMissingJavadocTagsProposal(String label, ICompilationUnit cu, BodyDeclaration bodyDecl, int relevance) {
	super(label, CodeActionKind.QuickFix, cu, null, relevance);
	fBodyDecl= bodyDecl;
}
 
Example 11
Source File: RenameNodeCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public RenameNodeCorrectionProposal(String name, ICompilationUnit cu, int offset, int length, String newName, int relevance) {
	super(name, CodeActionKind.QuickFix, cu, null, relevance);
	fOffset= offset;
	fLength= length;
	fNewName= newName;
}
 
Example 12
Source File: ReturnTypeSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static void addMissingReturnTypeProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> 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, BindingLabelProviderCore.getBindingLabel(typeBinding, BindingLabelProviderCore.DEFAULT_TEXTFLAGS));
		LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, CodeActionKind.QuickFix, cu, rewrite, IProposalRelevance.MISSING_RETURN_TYPE);

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

		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));
			}
		}
	}
}
 
Example 13
Source File: UnresolvedElementsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static void addQualifierToOuterProposal(IInvocationContext context, MethodInvocation invocationNode,
		IMethodBinding binding, Collection<ChangeCorrectionProposal> proposals) {
	ITypeBinding declaringType= binding.getDeclaringClass();
	ITypeBinding parentType= Bindings.getBindingOfParentType(invocationNode);
	ITypeBinding currType= parentType;

	boolean isInstanceMethod= !Modifier.isStatic(binding.getModifiers());

	while (currType != null && !Bindings.isSuperType(declaringType, currType)) {
		if (isInstanceMethod && Modifier.isStatic(currType.getModifiers())) {
			return;
		}
		currType= currType.getDeclaringClass();
	}
	if (currType == null || currType == parentType) {
		return;
	}

	ASTRewrite rewrite= ASTRewrite.create(invocationNode.getAST());

	String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changetoouter_description,
			org.eclipse.jdt.ls.core.internal.corrections.ASTResolving.getTypeSignature(currType));
	ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(),
			rewrite, IProposalRelevance.QUALIFY_WITH_ENCLOSING_TYPE);

	ImportRewrite imports= proposal.createImportRewrite(context.getASTRoot());
	ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(invocationNode, imports);
	AST ast= invocationNode.getAST();

	String qualifier= imports.addImport(currType, importRewriteContext);
	Name name= ASTNodeFactory.newName(ast, qualifier);

	Expression newExpression;
	if (isInstanceMethod) {
		ThisExpression expr= ast.newThisExpression();
		expr.setQualifier(name);
		newExpression= expr;
	} else {
		newExpression= name;
	}

	rewrite.set(invocationNode, MethodInvocation.EXPRESSION_PROPERTY, newExpression, null);

	proposals.add(proposal);
}
 
Example 14
Source File: NewAnnotationMemberProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public NewAnnotationMemberProposal(String label, ICompilationUnit targetCU, ASTNode invocationNode,
		ITypeBinding binding, int relevance) {
	super(label, CodeActionKind.QuickFix, targetCU, null, relevance);
	fInvocationNode= invocationNode;
	fSenderBinding= binding;
}
 
Example 15
Source File: JavadocTagsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public AddMissingJavadocTagProposal(String label, ICompilationUnit cu, BodyDeclaration bodyDecl, ASTNode missingNode, int relevance) {
	super(label, CodeActionKind.QuickFix, cu, null, relevance);
	fBodyDecl= bodyDecl;
	fMissingNode= missingNode;
}
 
Example 16
Source File: LocalCorrectionsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static void addRemoveProposal(IInvocationContext context, ASTRewrite rewrite, String label, Collection<ChangeCorrectionProposal> proposals) {
	ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), rewrite, 10);
	proposals.add(proposal);
}
 
Example 17
Source File: ReplaceCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public ReplaceCorrectionProposal(String name, ICompilationUnit cu, int offset, int length, String replacementString, int relevance) {
	super(name, CodeActionKind.QuickFix, cu, null, relevance);
	fReplacementString= replacementString;
	fOffset= offset;
	fLength= length;
}
 
Example 18
Source File: FixCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public FixCorrectionProposal(IProposableFix fix, ICleanUpCore cleanUp, int relevance, IInvocationContext context) {
	this(fix, cleanUp, relevance, context, CodeActionKind.QuickFix);
}
 
Example 19
Source File: JavadocTagsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private AddJavadocCommentProposal(String name, ICompilationUnit cu, int relevance, int insertPosition, String comment) {
	super(name, CodeActionKind.QuickFix, cu, null, relevance);
	fInsertPosition= insertPosition;
	fComment= comment;
}
 
Example 20
Source File: NewCUProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Construct a new compilation unit proposal.
 *
 * @param cu
 *            current compilation unit.
 * @param node
 *            {@link Name} corresponding to the compilation unit to be created.
 * @param typeKind
 *            possible values: { K_CLASS, K_INTERFACE, K_ENUM, K_ANNOTATION }
 * @param typeContainer
 *            enclosing {@link IJavaElement} of the target compilation unit, can
 *            be {@link IType} or {@link IPackageFragment}.
 * @param relevance
 *            the relevance of this proposal
 */
public NewCUProposal(ICompilationUnit cu, Name node, int typeKind, IJavaElement typeContainer, int relevance) {
	super("", CodeActionKind.QuickFix, null, relevance); //$NON-NLS-1$

	fCompilationUnit = cu;
	fNode = node;
	fTypeKind = typeKind;
	fTypeContainer = typeContainer;
	if (fNode != null) {
		fTypeNameWithParameters = getTypeName(typeKind, node);
	}

	setDisplayName();
}