use of org.eclipse.jdt.core.CompletionContext in project flux by eclipse.
the class ContentAssistService method computeContentAssist.
protected JSONArray computeContentAssist(String username, String resourcePath, int offset, String prefix) throws JSONException {
final List<CompletionProposal> proposals = new ArrayList<CompletionProposal>();
final CompletionContext[] completionContextParam = new CompletionContext[] { null };
ICompilationUnit liveEditUnit = liveEditUnits.getLiveEditUnit(username, resourcePath);
try {
if (liveEditUnit != null) {
CompletionRequestor collector = new CompletionRequestor() {
@Override
public void accept(CompletionProposal proposal) {
proposals.add(proposal);
}
@Override
public void acceptContext(CompletionContext context) {
super.acceptContext(context);
completionContextParam[0] = context;
}
};
// Allow completions for unresolved types - since 3.3
collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.TYPE_REF, true);
collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.TYPE_IMPORT, true);
collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.FIELD_IMPORT, true);
collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.TYPE_REF, true);
collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.TYPE_IMPORT, true);
collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.METHOD_IMPORT, true);
collector.setAllowsRequiredProposals(CompletionProposal.CONSTRUCTOR_INVOCATION, CompletionProposal.TYPE_REF, true);
collector.setAllowsRequiredProposals(CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION, CompletionProposal.TYPE_REF, true);
collector.setAllowsRequiredProposals(CompletionProposal.ANONYMOUS_CLASS_DECLARATION, CompletionProposal.TYPE_REF, true);
collector.setAllowsRequiredProposals(CompletionProposal.TYPE_REF, CompletionProposal.TYPE_REF, true);
liveEditUnit.codeComplete(offset, collector, new NullProgressMonitor());
}
} catch (JavaModelException e) {
e.printStackTrace();
}
List<JSONObject> jsonProposals = new ArrayList<JSONObject>(proposals.size());
CompletionContext completionContext = completionContextParam[0];
for (CompletionProposal proposal : proposals) {
JSONObject jsonDescription = getDescription(proposal, completionContext);
ProposalReplcamentInfo replacementInfo = new CompletionProposalReplacementProvider(liveEditUnit, proposal, completionContext, offset, prefix).createReplacement();
JSONObject jsonProposal = new JSONObject();
jsonProposal.put("description", jsonDescription);
jsonProposal.put("proposal", replacementInfo.replacement);
int initOffset = offset - prefix.length();
if (replacementInfo.extraChanges != null) {
jsonProposal.put("additionalEdits", Utils.editsToJsonArray(replacementInfo.extraChanges));
initOffset += Utils.getOffsetAdjustment(replacementInfo.extraChanges, initOffset);
}
if (replacementInfo.positions != null && !replacementInfo.positions.isEmpty()) {
jsonProposal.put("positions", getPositions(replacementInfo.positions, initOffset));
}
jsonProposal.put("escapePosition", initOffset + replacementInfo.replacement.length());
jsonProposal.put("style", "attributedString");
jsonProposal.put("replace", true);
jsonProposal.put("relevance", proposal.getRelevance());
jsonProposals.add(jsonProposal);
}
Collections.sort(jsonProposals, new Comparator<JSONObject>() {
@Override
public int compare(JSONObject o1, JSONObject o2) {
try {
int diff = o2.getInt("relevance") - o1.getInt("relevance");
if (diff == 0) {
JSONArray nameDescription1 = o1.getJSONObject("description").getJSONArray("segments");
JSONArray nameDescription2 = o2.getJSONObject("description").getJSONArray("segments");
StringBuilder nameBuffer1 = new StringBuilder();
for (int i = 0; i < nameDescription1.length(); i++) {
nameBuffer1.append(nameDescription1.getJSONObject(i).getString("value"));
}
StringBuilder nameBuffer2 = new StringBuilder();
for (int i = 0; i < nameDescription2.length(); i++) {
nameBuffer2.append(nameDescription2.getJSONObject(i).getString("value"));
}
return nameBuffer1.toString().compareTo(nameBuffer2.toString());
} else {
return diff;
}
} catch (JSONException e) {
return -1;
}
}
});
return new JSONArray(jsonProposals);
}
use of org.eclipse.jdt.core.CompletionContext in project jbosstools-hibernate by jbosstools.
the class JavaCompletionProcessor method computeCompletionProposals.
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int position) {
try {
setErrorMessage(null);
if (editor.getConsoleConfiguration() == null) {
setErrorMessage(HibernateConsoleMessages.JavaCompletionProcessor_no_console_configuration_found);
return new ICompletionProposal[0];
}
// has to do this because of https://bugs.eclipse.org/bugs/show_bug.cgi?id=141518
String prefix = HibernateConsoleMessages.JavaCompletionProcessor_session_session;
IJavaCompletionProposal[] results = new IJavaCompletionProposal[0];
IJavaProject[] projects = null;
// try {
projects = ProjectUtils.findJavaProjects(editor.getConsoleConfiguration());
/*} catch (RuntimeException e){
String mess = NLS.bind(HibernateConsoleMessages.JavaCompletionProcessor_error_find_project,
editor.getConsoleConfiguration().getName());
HibernateConsolePlugin.getDefault().logErrorMessage(mess, e);
}*/
for (int i = 0; projects != null && i < projects.length && results.length <= 0; i++) {
IJavaProject javaProject = projects[i];
collector = new CompletionProposalCollector(javaProject);
collector.acceptContext(new CompletionContext());
try {
editor.codeComplete(prefix, collector, position, javaProject);
} catch (JavaModelException x) {
Shell shell = viewer.getTextWidget().getShell();
ErrorDialog.openError(shell, HibernateConsoleMessages.JavaCompletionProcessor_error, HibernateConsoleMessages.JavaCompletionProcessor_error_while_performing_code_completion, x.getStatus());
HibernateConsolePlugin.getDefault().log(x);
}
results = collector.getJavaCompletionProposals();
}
CompletionHelper.transpose(null, -prefix.length(), results);
return results;
} finally {
if (collector != null) {
setErrorMessage(collector.getErrorMessage());
collector = null;
}
}
}
use of org.eclipse.jdt.core.CompletionContext in project jbosstools-hibernate by jbosstools.
the class CompletionHelper method completeOnJavaTypes.
public static ICompletionProposal[] completeOnJavaTypes(IJavaProject javaProject, Settings settings, String packageName, String start, int offset) {
if (javaProject != null) {
IEvaluationContext context = javaProject.newEvaluationContext();
if (packageName != null) {
context.setPackageName(packageName);
}
HibernateResultCollector rc = new HibernateResultCollector(javaProject);
rc.acceptContext(new CompletionContext());
// rc.reset(offset, javaProject, null);
rc.setAccepts(settings);
try {
// cannot send in my own document as it won't compile as
// java - so we just send in
// the smallest snippet possible
context.codeComplete(start, start.length(), rc);
} catch (JavaModelException jme) {
HibernateConsolePlugin.getDefault().logErrorMessage(HibernateConsoleMessages.CompletionHelper_could_not_complete_java_types, jme);
}
IJavaCompletionProposal[] results = rc.getJavaCompletionProposals();
transpose(start, offset, results);
return results;
}
return new ICompletionProposal[0];
}
use of org.eclipse.jdt.core.CompletionContext in project eclipse.jdt.ls by eclipse.
the class SnippetCompletionProposal method getGenericSnippets.
private static List<CompletionItem> getGenericSnippets(SnippetCompletionContext scc) throws JavaModelException {
CompletionResponse response = new CompletionResponse();
response.setContext(scc.getCompletionContext());
response.setOffset(scc.getCompletionContext().getOffset());
List<CompletionItem> res = new ArrayList<>();
CompletionContext completionContext = scc.getCompletionContext();
char[] completionToken = completionContext.getToken();
if (completionToken == null) {
return Collections.emptyList();
}
int tokenLocation = completionContext.getTokenLocation();
JavaContextType contextType = (JavaContextType) JavaLanguageServerPlugin.getInstance().getTemplateContextRegistry().getContextType(JavaContextType.ID_STATEMENTS);
if (contextType == null) {
return Collections.emptyList();
}
ICompilationUnit cu = scc.getCompilationUnit();
IDocument document = new Document(cu.getSource());
DocumentTemplateContext javaContext = contextType.createContext(document, completionContext.getOffset(), completionToken.length, cu);
Template[] templates = null;
if ((tokenLocation & CompletionContext.TL_STATEMENT_START) != 0) {
templates = JavaLanguageServerPlugin.getInstance().getTemplateStore().getTemplates(JavaContextType.ID_STATEMENTS);
} else {
// We only support statement templates for now.
}
if (templates == null || templates.length == 0) {
return Collections.emptyList();
}
String uri = JDTUtils.toURI(cu);
Template[] availableTemplates = Arrays.stream(templates).filter(t -> javaContext.canEvaluate(t)).toArray(Template[]::new);
List<CompletionProposal> proposals = new ArrayList<>();
for (int i = 0; i < availableTemplates.length; i++) {
Template template = availableTemplates[i];
final CompletionItem item = new CompletionItem();
item.setLabel(template.getName());
item.setKind(CompletionItemKind.Snippet);
item.setInsertTextFormat(InsertTextFormat.Snippet);
item.setInsertText(SnippetUtils.templateToSnippet(template.getPattern()));
item.setDetail(template.getDescription());
Map<String, String> data = new HashMap<>(3);
data.put(CompletionResolveHandler.DATA_FIELD_URI, uri);
data.put(CompletionResolveHandler.DATA_FIELD_REQUEST_ID, String.valueOf(response.getId()));
data.put(CompletionResolveHandler.DATA_FIELD_PROPOSAL_ID, String.valueOf(i));
item.setData(data);
proposals.add(i, new SnippetCompletionProposal(template));
res.add(item);
}
response.setProposals(proposals);
CompletionResponses.store(response);
return res;
}
use of org.eclipse.jdt.core.CompletionContext in project eclipse.jdt.ui by eclipse-jdt.
the class JavaTypeCompletionProposalComputer method computeCompletionProposals.
/*
* @see org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposalComputer#computeCompletionProposals(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
*/
@Override
public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) {
List<ICompletionProposal> types = super.computeCompletionProposals(context, monitor);
if (!(context instanceof JavaContentAssistInvocationContext))
return types;
JavaContentAssistInvocationContext javaContext = (JavaContentAssistInvocationContext) context;
CompletionContext coreContext = javaContext.getCoreContext();
if (coreContext != null && coreContext.getTokenLocation() != CompletionContext.TL_CONSTRUCTOR_START)
return types;
try {
if (types.size() > 0 && context.computeIdentifierPrefix().length() == 0) {
IType expectedType = javaContext.getExpectedType();
if (expectedType != null) {
// empty prefix completion - insert LRU types if known, but prune if they already occur in the core list
// compute minmimum relevance and already proposed list
int relevance = Integer.MAX_VALUE;
Set<String> proposed = new HashSet<>();
for (ICompletionProposal iCompletionProposal : types) {
AbstractJavaCompletionProposal p = (AbstractJavaCompletionProposal) iCompletionProposal;
IJavaElement element = p.getJavaElement();
if (element instanceof IType)
proposed.add(((IType) element).getFullyQualifiedName());
relevance = Math.min(relevance, p.getRelevance());
}
// insert history types
List<String> history = JavaPlugin.getDefault().getContentAssistHistory().getHistory(expectedType.getFullyQualifiedName()).getTypes();
relevance -= history.size() + 1;
for (String type : history) {
if (proposed.contains(type))
continue;
IJavaCompletionProposal proposal = createTypeProposal(relevance, type, javaContext);
if (proposal != null)
types.add(proposal);
relevance++;
}
}
}
} catch (BadLocationException | JavaModelException x) {
// log & ignore
JavaPlugin.log(x);
}
return types;
}
Aggregations