Java Code Examples for org.eclipse.jdt.core.CompletionProposal#getReplaceEnd()

The following examples show how to use org.eclipse.jdt.core.CompletionProposal#getReplaceEnd() . 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: CompletionProposalRequestor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private void acceptPotentialMethodDeclaration(CompletionProposal proposal) {
	try {
		IJavaElement enclosingElement = null;
		if (response.getContext().isExtended()) {
			enclosingElement = response.getContext().getEnclosingElement();
		} else if (unit != null) {
			// kept for backward compatibility: CU is not reconciled at this moment, information is missing (bug 70005)
			enclosingElement = unit.getElementAt(proposal.getCompletionLocation() + 1);
		}
		if (enclosingElement == null) {
			return;
		}
		IType type = (IType) enclosingElement.getAncestor(IJavaElement.TYPE);
		if (type != null) {
			String prefix = String.valueOf(proposal.getName());
			int completionStart = proposal.getReplaceStart();
			int completionEnd = proposal.getReplaceEnd();
			int relevance = proposal.getRelevance() + 6;

			GetterSetterCompletionProposal.evaluateProposals(type, prefix, completionStart, completionEnd - completionStart, relevance, proposals);
		}
	} catch (CoreException e) {
		JavaLanguageServerPlugin.logException("Accept potential method declaration failed for completion ", e);
	}
}
 
Example 2
Source File: CompletionProposalCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the replacement length of a given completion proposal. The
 * replacement length is usually the difference between the return values of
 * <code>proposal.getReplaceEnd</code> and
 * <code>proposal.getReplaceStart</code>, but this behavior may be
 * overridden by calling {@link #setReplacementLength(int)}.
 *
 * @param proposal the completion proposal to get the replacement length for
 * @return the replacement length for <code>proposal</code>
 */
protected final int getLength(CompletionProposal proposal) {
	int start= proposal.getReplaceStart();
	int end= proposal.getReplaceEnd();
	int length;
	if (fUserReplacementLength == -1) {
		length= end - start;
	} else {
		length= fUserReplacementLength;
		// extend length to begin at start
		int behindCompletion= proposal.getCompletionLocation() + 1;
		if (start < behindCompletion) {
			length+= behindCompletion - start;
		}
	}
	return length;
}
 
Example 3
Source File: CompletionProposalCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void acceptPotentialMethodDeclaration(CompletionProposal proposal) {
	try {
		IJavaElement enclosingElement= null;
		if (getContext().isExtended()) {
			enclosingElement= getContext().getEnclosingElement();
		} else if (fCompilationUnit != null) {
			// kept for backward compatibility: CU is not reconciled at this moment, information is missing (bug 70005)
			enclosingElement= fCompilationUnit.getElementAt(proposal.getCompletionLocation() + 1);
		}
		if (enclosingElement == null)
			return;
		IType type= (IType) enclosingElement.getAncestor(IJavaElement.TYPE);
		if (type != null) {
			String prefix= String.valueOf(proposal.getName());
			int completionStart= proposal.getReplaceStart();
			int completionEnd= proposal.getReplaceEnd();
			int relevance= computeRelevance(proposal);

			GetterSetterCompletionProposal.evaluateProposals(type, prefix, completionStart, completionEnd - completionStart, relevance + 2, fSuggestedMethodNames, fJavaProposals);
			MethodDeclarationCompletionProposal.evaluateProposals(type, prefix, completionStart, completionEnd - completionStart, relevance, fSuggestedMethodNames, fJavaProposals);
		}
	} catch (CoreException e) {
		JavaPlugin.log(e);
	}
}
 
Example 4
Source File: CompletionProposalRequestor.java    From java-debug with Eclipse Public License 1.0 5 votes vote down vote up
private void acceptPotentialMethodDeclaration(CompletionProposal proposal) {
    try {
        IJavaElement enclosingElement = null;
        if (response.getContext().isExtended()) {
            enclosingElement = response.getContext().getEnclosingElement();
        } else if (typeRoot != null) {
            // kept for backward compatibility: CU is not reconciled at this moment,
            // information is missing (bug 70005)
            enclosingElement = typeRoot.getElementAt(proposal.getCompletionLocation() + 1);
        }
        if (enclosingElement == null) {
            return;
        }
        IType type = (IType) enclosingElement.getAncestor(IJavaElement.TYPE);
        if (type != null) {
            String prefix = String.valueOf(proposal.getName());
            int completionStart = proposal.getReplaceStart();
            int completionEnd = proposal.getReplaceEnd();
            int relevance = proposal.getRelevance() + 6;

            GetterSetterCompletionProposal.evaluateProposals(type, prefix, completionStart,
                    completionEnd - completionStart, relevance, proposals);
        }
    } catch (CoreException e) {
        JavaLanguageServerPlugin.logException("Accept potential method declaration failed for completion ", e);
    }
}
 
Example 5
Source File: CompletionProposalReplacementProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private void updateReplacement(CompletionProposal proposal, CompletionItem item, char trigger, boolean isResolving) {
	// reset importRewrite
	this.importRewrite = TypeProposalUtils.createImportRewrite(compilationUnit);

	List<org.eclipse.lsp4j.TextEdit> additionalTextEdits = new ArrayList<>();

	StringBuilder completionBuffer = new StringBuilder();
	Range range = null;
	if (isSupportingRequiredProposals(proposal)) {
		CompletionProposal[] requiredProposals = proposal.getRequiredProposals();
		if (requiredProposals != null) {
			for (CompletionProposal requiredProposal : requiredProposals) {
				switch (requiredProposal.getKind()) {
				case CompletionProposal.TYPE_IMPORT:
				case CompletionProposal.METHOD_IMPORT:
				case CompletionProposal.FIELD_IMPORT:
					appendImportProposal(completionBuffer, requiredProposal, proposal.getKind());
					break;
				case CompletionProposal.TYPE_REF:
					org.eclipse.lsp4j.TextEdit edit = toRequiredTypeEdit(requiredProposal, trigger, proposal.canUseDiamond(context));
					if (proposal.getKind() == CompletionProposal.CONSTRUCTOR_INVOCATION
						|| proposal.getKind() == CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION
						|| proposal.getKind() == CompletionProposal.ANONYMOUS_CLASS_DECLARATION) {
							completionBuffer.append(edit.getNewText());
							range = edit.getRange();
					} else {
						additionalTextEdits.add(edit);
					}
					break;
				default:
					/*
					 * In 3.3 we only support the above required proposals, see
					 * CompletionProposal#getRequiredProposals()
					 */
					Assert.isTrue(false);
				}
			}
		}
	}

	if (range == null) {
		boolean completionOverwrite = preferences.isCompletionOverwrite();
		if (!completionOverwrite && (proposal.getKind() == CompletionProposal.METHOD_REF || proposal.getKind() == CompletionProposal.LOCAL_VARIABLE_REF || proposal.getKind() == CompletionProposal.FIELD_REF)) {
			// See https://github.com/redhat-developer/vscode-java/issues/462
			int end = proposal.getReplaceEnd();
			if (end > offset) {
				proposal.setReplaceRange(proposal.getReplaceStart(), offset);
			}
		}
		range = toReplacementRange(proposal);
	}
	if (proposal.getKind() == CompletionProposal.METHOD_DECLARATION) {
		appendMethodOverrideReplacement(completionBuffer, proposal);
	} else if (proposal.getKind() == CompletionProposal.POTENTIAL_METHOD_DECLARATION && proposal instanceof GetterSetterCompletionProposal) {
		appendMethodPotentialReplacement(completionBuffer, (GetterSetterCompletionProposal) proposal);
	} else if (proposal.getKind() == CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION || proposal.getKind() == CompletionProposal.ANONYMOUS_CLASS_DECLARATION) {
		appendAnonymousClass(completionBuffer, proposal, range);
	} else {
		appendReplacementString(completionBuffer, proposal);
	}
	//select insertTextFormat.
	if (client.isCompletionSnippetsSupported()) {
		item.setInsertTextFormat(InsertTextFormat.Snippet);
	} else {
		item.setInsertTextFormat(InsertTextFormat.PlainText);
	}
	String text = completionBuffer.toString();
	if (range != null) {
		item.setTextEdit(new org.eclipse.lsp4j.TextEdit(range, text));
	} else {
		// fallback
		item.setInsertText(text);
	}

	if (!client.isResolveAdditionalTextEditsSupport() || isResolving) {
		addImports(additionalTextEdits);
		if(!additionalTextEdits.isEmpty()){
			item.setAdditionalTextEdits(additionalTextEdits);
		}
	}
}
 
Example 6
Source File: CompletionProposalReplacementProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private boolean shouldAppendArguments(CompletionProposal proposal,
		char trigger) {
	/*
	 * No argument list if there were any special triggers (for example a
	 * period to qualify an inner type).
	 */
	if (trigger != '\0' && trigger != '<' && trigger != LPAREN) {
		return false;
	}

	/*
	 * No argument list if the completion is empty (already within the
	 * argument list).
	 */
	char[] completion = proposal.getCompletion();
	if (completion.length == 0) {
		return false;
	}

	/*
	 * No argument list if there already is a generic signature behind the
	 * name.
	 */
	try {
		IDocument document = JsonRpcHelpers.toDocument(this.compilationUnit.getBuffer());
		IRegion region= document.getLineInformationOfOffset(proposal.getReplaceEnd());
		String line= document.get(region.getOffset(),region.getLength());

		int index= proposal.getReplaceEnd() - region.getOffset();
		while (index != line.length() && Character.isUnicodeIdentifierPart(line.charAt(index))) {
			++index;
		}

		if (index == line.length()) {
			return true;
		}

		char ch= line.charAt(index);
		return ch != '<';

	} catch (BadLocationException | JavaModelException e) {
		return true;
	}

}