Java Code Examples for org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext#getCompilationUnit()

The following examples show how to use org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext#getCompilationUnit() . 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: SWTTemplateCompletionProposalComputer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected TemplateEngine computeCompletionEngine(JavaContentAssistInvocationContext context) {
	ICompilationUnit unit= context.getCompilationUnit();
	if (unit == null)
		return null;

	IJavaProject javaProject= unit.getJavaProject();
	if (javaProject == null)
		return null;

	if (isSWTOnClasspath(javaProject)) {
		CompletionContext coreContext= context.getCoreContext();
		if (coreContext != null) {
			int tokenLocation= coreContext.getTokenLocation();
			if ((tokenLocation & CompletionContext.TL_MEMBER_START) != 0) {
				return fSWTMembersTemplateEngine;
			}
			if ((tokenLocation & CompletionContext.TL_STATEMENT_START) != 0) {
				return fSWTStatementsTemplateEngine;
			}
		}
		return fSWTTemplateEngine;
	}

	return null;
}
 
Example 2
Source File: JavaCompletionProposalComputer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates the collector used to get proposals from core.
 *
 * @param context the context
 * @return the collector
 */
protected CompletionProposalCollector createCollector(JavaContentAssistInvocationContext context) {
	if (PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.CODEASSIST_FILL_ARGUMENT_NAMES))
		return new FillArgumentNamesCompletionProposalCollector(context);
	else
		return new CompletionProposalCollector(context.getCompilationUnit(), true);
}
 
Example 3
Source File: FillArgumentNamesCompletionProposalCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public FillArgumentNamesCompletionProposalCollector(JavaContentAssistInvocationContext context) {
	super(context.getCompilationUnit(), true);
	setInvocationContext(context);
	IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore();
	fIsGuessArguments= preferenceStore.getBoolean(PreferenceConstants.CODEASSIST_GUESS_METHOD_ARGUMENTS);
	if (preferenceStore.getBoolean(PreferenceConstants.CODEASSIST_FILL_ARGUMENT_NAMES)) {
		setRequireExtendedContext(true);
	}
}
 
Example 4
Source File: LegacyJavadocCompletionProposalComputer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public List<IContextInformation> computeContextInformation(ContentAssistInvocationContext context, IProgressMonitor monitor) {
	if (context instanceof JavaContentAssistInvocationContext) {
		JavaContentAssistInvocationContext javaContext= (JavaContentAssistInvocationContext) context;

		ICompilationUnit cu= javaContext.getCompilationUnit();
		int offset= javaContext.getInvocationOffset();

		ArrayList<IContextInformation> result= new ArrayList<IContextInformation>();

		IJavadocCompletionProcessor[] processors= getContributedProcessors();
		String error= null;
		for (int i= 0; i < processors.length; i++) {
			IJavadocCompletionProcessor curr= processors[i];
			IContextInformation[] contextInfos= curr.computeContextInformation(cu, offset);
			if (contextInfos != null) {
				for (int k= 0; k < contextInfos.length; k++) {
					result.add(contextInfos[k]);
				}
			} else if (error == null) {
				error= curr.getErrorMessage();
			}
		}
		fErrorMessage= error;
		return result;
	}
	return Collections.emptyList();
}
 
Example 5
Source File: JFaceCompletionProposalComputer.java    From saneclipse with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected TemplateEngine computeCompletionEngine(JavaContentAssistInvocationContext context) {
	final ICompilationUnit unit = context.getCompilationUnit();
	if (unit == null) {
		return null;
	}

	final IJavaProject javaProject = unit.getJavaProject();
	if (javaProject == null) {
		return null;
	}

	if (isJFaceOnClasspath(javaProject)) {
		final CompletionContext coreContext = context.getCoreContext();
		if (coreContext != null) {
			final int tokenLocation = coreContext.getTokenLocation();
			if ((tokenLocation & CompletionContext.TL_MEMBER_START) != 0) {
				return jFaceMembersTemplateEngine;
			}
			if ((tokenLocation & CompletionContext.TL_STATEMENT_START) != 0) {
				return jFaceStatementsTemplateEngine;
			}
		}
		return jFaceTemplateEngine;
	}

	return null;
}
 
Example 6
Source File: AbstractTemplateCompletionProposalComputer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) {
	if (!(context instanceof JavaContentAssistInvocationContext))
		return Collections.emptyList();

	JavaContentAssistInvocationContext javaContext= (JavaContentAssistInvocationContext) context;
	ICompilationUnit unit= javaContext.getCompilationUnit();
	if (unit == null)
		return Collections.emptyList();

	fEngine= computeCompletionEngine(javaContext);
	if (fEngine == null)
		return Collections.emptyList();

	fEngine.reset();
	fEngine.complete(javaContext.getViewer(), javaContext.getInvocationOffset(), unit);

	TemplateProposal[] templateProposals= fEngine.getResults();
	List<ICompletionProposal> result= new ArrayList<ICompletionProposal>(Arrays.asList(templateProposals));

	IJavaCompletionProposal[] keyWordResults= javaContext.getKeywordProposals();
	if (keyWordResults.length == 0)
		return result;

	/* Update relevance of template proposals that match with a keyword
	 * give those templates slightly more relevance than the keyword to
	 * sort them first.
	 */
	for (int k= 0; k < templateProposals.length; k++) {
		TemplateProposal curr= templateProposals[k];
		String name= curr.getTemplate().getPattern();
		for (int i= 0; i < keyWordResults.length; i++) {
			String keyword= keyWordResults[i].getDisplayString();
			if (name.startsWith(keyword)) {
				String content= curr.getTemplate().getPattern();
				if (content.startsWith(keyword)) {
					curr.setRelevance(keyWordResults[i].getRelevance() + 1);
					break;
				}
			}
		}
	}
	return result;
}
 
Example 7
Source File: LazyPackageCompletionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public LazyPackageCompletionProposal(CompletionProposal proposal, JavaContentAssistInvocationContext context) {
	super(proposal, context);
	fCompilationUnit= context.getCompilationUnit();
}
 
Example 8
Source File: LazyJavaTypeCompletionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public LazyJavaTypeCompletionProposal(CompletionProposal proposal, JavaContentAssistInvocationContext context) {
	super(proposal, context);
	fCompilationUnit= context.getCompilationUnit();
	fQualifiedName= null;
}
 
Example 9
Source File: ImportCompletionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public ImportCompletionProposal(CompletionProposal proposal, JavaContentAssistInvocationContext context, int parentProposalKind) {
	super(context);
	fProposal= proposal;
	fParentProposalKind= parentProposalKind;
	fCompilationUnit= context.getCompilationUnit();
}
 
Example 10
Source File: PostfixCompletionProposalComputer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected TemplateEngine computeCompletionEngine(JavaContentAssistInvocationContext context) {
	ICompilationUnit unit = context.getCompilationUnit();
	if (unit == null)
		return null;

	IJavaProject javaProject = unit.getJavaProject();
	if (javaProject == null)
		return null;

	CompletionContext coreContext = context.getCoreContext();
	if (coreContext != null) {
		int tokenLocation= coreContext.getTokenLocation();
		int tokenStart= coreContext.getTokenStart();
		int tokenKind= coreContext.getTokenKind();
		
		/*
		// XXX print out tokenlocation stuff (debugging)
		System.out.println("All Tokens: " + CompletionContext.TL_CONSTRUCTOR_START + " " + CompletionContext.TL_MEMBER_START + " " + CompletionContext.TL_STATEMENT_START);
		System.out.println("Token Start: " + coreContext.getTokenStart());
		System.out.println("Token End: " + coreContext.getTokenEnd());
		System.out.println("Token Kind: " + coreContext.getTokenKind());
		System.out.println("Token Location: " + coreContext.getTokenLocation());
		System.out.println("Enclosing Element: " + coreContext.getEnclosingElement());
		System.out.println("Offset: " + coreContext.getOffset());
		System.out.println("Token Array: " + Arrays.toString(coreContext.getToken()));
		System.out.println("Kind Tokens: " + CompletionContext.TOKEN_KIND_NAME + ", " + CompletionContext.TOKEN_KIND_STRING_LITERAL + ", " + CompletionContext.TOKEN_KIND_UNKNOWN);
		*/
		if (context.getViewer().getSelectedRange().y > 0) { // If there is an active selection we do not want to contribute to the CA
			return null;
		}
		
		if ((tokenLocation == 0 && tokenStart > -1)
				|| ((tokenLocation & CompletionContext.TL_MEMBER_START) != 0 && tokenKind == CompletionContext.TOKEN_KIND_NAME && tokenStart > -1)
				|| (tokenLocation == 0 && isAfterDot(context.getDocument(), context.getInvocationOffset()))) {
			
			analyzeCoreContext(context, coreContext);

			return postfixCompletionTemplateEngine;
		}
	}
	return null;
}
 
Example 11
Source File: ContractInputCompletionProposalComputer.java    From bonita-studio with GNU General Public License v2.0 4 votes vote down vote up
@Override
public List<ICompletionProposal> computeCompletionProposals(final ContentAssistInvocationContext context,
        final IProgressMonitor monitor) {
    if (!(context instanceof JavaContentAssistInvocationContext)
            || context instanceof JavaContentAssistInvocationContext
                    && !(((JavaContentAssistInvocationContext) context)
                            .getCompilationUnit() instanceof GroovyCompilationUnit)) {
        return Collections.emptyList();
    }
    final List<ContractInput> inputs = getContractInputs(context);
    if (inputs.isEmpty()) {
        return Collections.emptyList();
    }
    final JavaContentAssistInvocationContext javaContext = (JavaContentAssistInvocationContext) context;
    GroovyCompilationUnit compilationUnit = (GroovyCompilationUnit) javaContext.getCompilationUnit();
    if (compilationUnit.getModuleNode() == null) {
        return Collections.emptyList();
    }
    final ContentAssistContext contentAssistContext = createContentAssistContext(
            compilationUnit,
            context.getInvocationOffset(), context.getDocument());
    if (contentAssistContext == null) {
        return Collections.emptyList();
    }
    CharSequence computeIdentifierPrefix = "";
    try {
        computeIdentifierPrefix = javaContext.computeIdentifierPrefix();
    } catch (final BadLocationException e) {
        BonitaStudioLog.error("Failed to compute identifier prefix in ContractConstraint expression editor", e,
                ContractPlugin.PLUGIN_ID);
        return Collections.emptyList();
    }
    final CodeVisitorSupportContext codeVisitorSupportContext = new CodeVisitorSupportContext(
            computeIdentifierPrefix.toString(),
            (JavaContentAssistInvocationContext) context,
            contentAssistContext,
            getProjectClassloader(compilationUnit),
            new GroovyCompletionProposalComputer(),
            createMethodProposalCreator(),
            compilationUnit.getModuleNode());
    final ContractInputProposalsCodeVisitorSupport codeVistor = new ContractInputProposalsCodeVisitorSupport(inputs,
            codeVisitorSupportContext,
            monitor);
    final ASTNode completionNode = contentAssistContext.getPerceivedCompletionNode();
    if (completionNode != null) {
        completionNode.visit(codeVistor);
    }
    final List<ICompletionProposal> proposals = codeVistor.getProposals();
    if (proposals == null || proposals.isEmpty()) {
        return super.computeCompletionProposals(context, monitor);
    }
    return proposals;
}