use of org.eclipse.che.jface.text.contentassist.ICompletionProposal in project che by eclipse.
the class TemplateProposal method apply.
/*
* @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
*/
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
IDocument document = viewer.getDocument();
try {
fContext.setReadOnly(false);
int start;
TemplateBuffer templateBuffer;
try {
beginCompoundChange(viewer);
int oldReplaceOffset = getReplaceOffset();
try {
// this may already modify the document (e.g. add imports)
templateBuffer = fContext.evaluate(fTemplate);
} catch (TemplateException e1) {
fSelectedRegion = fRegion;
return;
}
start = getReplaceOffset();
int shift = start - oldReplaceOffset;
int end = Math.max(getReplaceEndOffset(), offset + shift);
// insert template string
if (end > document.getLength())
end = offset;
String templateString = templateBuffer.getString();
document.replace(start, end - start, templateString);
} finally {
endCompoundChange(viewer);
}
// translate positions
LinkedModeModelImpl model = new LinkedModeModelImpl();
TemplateVariable[] variables = templateBuffer.getVariables();
MultiVariableGuess guess = fContext instanceof CompilationUnitContext ? ((CompilationUnitContext) fContext).getMultiVariableGuess() : null;
boolean hasPositions = false;
for (int i = 0; i != variables.length; i++) {
TemplateVariable variable = variables[i];
if (variable.isUnambiguous())
continue;
LinkedPositionGroupImpl group = new LinkedPositionGroupImpl();
int[] offsets = variable.getOffsets();
int length = variable.getLength();
LinkedPosition first;
if (guess != null && variable instanceof MultiVariable) {
first = new VariablePosition(document, offsets[0] + start, length, guess, (MultiVariable) variable);
guess.addSlave((VariablePosition) first);
} else {
String[] values = variable.getValues();
ICompletionProposal[] proposals = new ICompletionProposal[values.length];
for (int j = 0; j < values.length; j++) {
// ensurePositionCategoryInstalled(document, model);
Position pos = new Position(offsets[0] + start, length);
// document.addPosition(getCategory(), pos);
proposals[j] = new PositionBasedCompletionProposal(values[j], pos, length);
}
if (proposals.length > 1)
first = new ProposalPosition(document, offsets[0] + start, length, proposals);
else
first = new LinkedPosition(document, offsets[0] + start, length);
}
for (int j = 0; j != offsets.length; j++) if (j == 0) {
if (first instanceof ProposalPosition) {
RegionImpl region = new RegionImpl();
region.setLength(first.getLength());
region.setOffset(first.getOffset());
LinkedDataImpl data = new LinkedDataImpl();
ICompletionProposal[] choices = ((ProposalPosition) first).getChoices();
if (choices != null) {
for (ICompletionProposal choice : choices) {
data.addValues(choice.getDisplayString());
}
group.setData(data);
}
group.addPositions(region);
} else {
RegionImpl region = new RegionImpl();
region.setLength(first.getLength());
region.setOffset(first.getOffset());
group.addPositions(region);
}
} else {
RegionImpl region = new RegionImpl();
region.setLength(length);
region.setOffset(offsets[j] + start);
group.addPositions(region);
}
model.addGroups(group);
hasPositions = true;
}
if (hasPositions) {
model.setEscapePosition(getCaretOffset(templateBuffer) + start);
this.linkedModeModel = model;
// model.forceInstall();
// JavaEditor editor= getJavaEditor();
// if (editor != null) {
// model.addLinkingListener(new EditorHighlightingSynchronizer(editor));
// }
//
// LinkedModeUI ui= new EditorLinkedModeUI(model, viewer);
// ui.setExitPosition(viewer, getCaretOffset(templateBuffer) + start, 0, Integer.MAX_VALUE);
// ui.enter();
//ui.getSelectedRegion();
fSelectedRegion = fRegion;
} else {
fSelectedRegion = new Region(getCaretOffset(templateBuffer) + start, 0);
}
} catch (BadLocationException e) {
JavaPlugin.log(e);
// openErrorDialog(viewer.getTextWidget().getShell(), e);
fSelectedRegion = fRegion;
}
}
use of org.eclipse.che.jface.text.contentassist.ICompletionProposal in project che by eclipse.
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<String>();
for (Iterator<ICompletionProposal> it = types.iterator(); it.hasNext(); ) {
AbstractJavaCompletionProposal p = (AbstractJavaCompletionProposal) it.next();
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 (Iterator<String> it = history.iterator(); it.hasNext(); ) {
String type = it.next();
if (proposed.contains(type))
continue;
IJavaCompletionProposal proposal = createTypeProposal(relevance, type, javaContext);
if (proposal != null)
types.add(proposal);
relevance++;
}
}
}
} catch (BadLocationException x) {
// log & ignore
JavaPlugin.log(x);
} catch (JavaModelException x) {
// log & ignore
JavaPlugin.log(x);
}
return types;
}
use of org.eclipse.che.jface.text.contentassist.ICompletionProposal in project che by eclipse.
the class CodeAssist method computeProposals.
public Proposals computeProposals(IJavaProject project, String fqn, int offset, final String content) throws JavaModelException {
WorkingCopyOwner copyOwner = new WorkingCopyOwner() {
@Override
public IBuffer createBuffer(ICompilationUnit workingCopy) {
return new org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter(workingCopy, workingCopy.getPath(), content);
}
};
ICompilationUnit compilationUnit;
IType type = project.findType(fqn);
if (type == null) {
return null;
}
if (type.isBinary()) {
compilationUnit = type.getClassFile().getWorkingCopy(copyOwner, null);
} else {
compilationUnit = type.getCompilationUnit().getWorkingCopy(copyOwner, null);
}
IBuffer buffer = compilationUnit.getBuffer();
IDocument document;
if (buffer instanceof org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) {
document = ((org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) buffer).getDocument();
} else {
document = new DocumentAdapter(buffer);
}
TextViewer viewer = new TextViewer(document, new Point(offset, 0));
JavaContentAssistInvocationContext context = new JavaContentAssistInvocationContext(viewer, offset, compilationUnit);
List<ICompletionProposal> proposals = new ArrayList<>();
proposals.addAll(new JavaAllCompletionProposalComputer().computeCompletionProposals(context, null));
proposals.addAll(new TemplateCompletionProposalComputer().computeCompletionProposals(context, null));
Collections.sort(proposals, new RelevanceSorter());
return convertProposals(offset, compilationUnit, viewer, proposals);
}
use of org.eclipse.che.jface.text.contentassist.ICompletionProposal in project che by eclipse.
the class MultiVariableGuess method getProposals.
public ICompletionProposal[] getProposals(final MultiVariable variable, int offset, int length) {
MultiVariable master = fBackwardDeps.get(variable);
Object[] choices;
if (master == null)
choices = variable.getChoices();
else
choices = variable.getChoices(master.getCurrentChoice());
if (choices == null)
return null;
if (fDependencies.containsKey(variable)) {
ICompletionProposal[] ret = new ICompletionProposal[choices.length];
for (int i = 0; i < ret.length; i++) {
final Object choice = choices[i];
ret[i] = new Proposal(variable.toString(choice), offset, length, offset + length) {
@Override
public void apply(IDocument document) {
super.apply(document);
Object oldChoice = variable.getCurrentChoice();
variable.setCurrentChoice(choice);
updateSlaves(variable, document, oldChoice);
}
};
}
return ret;
} else {
if (choices.length < 2)
return null;
ICompletionProposal[] ret = new ICompletionProposal[choices.length];
for (int i = 0; i < ret.length; i++) ret[i] = new Proposal(variable.toString(choices[i]), offset, length, offset + length);
return ret;
}
}
use of org.eclipse.che.jface.text.contentassist.ICompletionProposal in project che by eclipse.
the class ParameterGuesser method parameterProposals.
/**
* Returns the matches for the type and name argument, ordered by match quality.
*
* @param expectedType - the qualified type of the parameter we are trying to match
* @param paramName - the name of the parameter (used to find similarly named matches)
* @param pos the position
* @param suggestions the suggestions or <code>null</code>
* @param fillBestGuess <code>true</code> if the best guess should be filled in
* @param isLastParameter <code>true</code> iff this proposal is for the last parameter of a method
* @return returns the name of the best match, or <code>null</code> if no match found
* @throws org.eclipse.jdt.core.JavaModelException if it fails
*/
public ICompletionProposal[] parameterProposals(String expectedType, String paramName, Position pos, IJavaElement[] suggestions, boolean fillBestGuess, boolean isLastParameter) throws JavaModelException {
List<Variable> typeMatches = evaluateVisibleMatches(expectedType, suggestions);
orderMatches(typeMatches, paramName);
boolean hasVarWithParamName = false;
ICompletionProposal[] ret = new ICompletionProposal[typeMatches.size()];
int i = 0;
int replacementLength = 0;
for (Iterator<Variable> it = typeMatches.iterator(); it.hasNext(); ) {
Variable v = it.next();
if (i == 0) {
fAlreadyMatchedNames.add(v.name);
replacementLength = v.name.length();
}
String displayString = v.name;
hasVarWithParamName |= displayString.equals(paramName);
final char[] triggers;
if (isLastParameter) {
triggers = v.triggerChars;
} else {
triggers = new char[v.triggerChars.length + 1];
System.arraycopy(v.triggerChars, 0, triggers, 0, v.triggerChars.length);
triggers[triggers.length - 1] = ',';
}
ret[i++] = new PositionBasedCompletionProposal(v.name, pos, replacementLength, getImage(v.descriptor), displayString, null, null, triggers);
}
if (!fillBestGuess && !hasVarWithParamName) {
// insert a proposal with the argument name
ICompletionProposal[] extended = new ICompletionProposal[ret.length + 1];
System.arraycopy(ret, 0, extended, 1, ret.length);
extended[0] = new PositionBasedCompletionProposal(paramName, pos, replacementLength, null, paramName, null, null, isLastParameter ? null : new char[] { ',' });
return extended;
}
return ret;
}
Aggregations