use of org.eclipse.jface.text.BadLocationException in project che by eclipse.
the class JavaTypeCompletionProposalComputer method guessContextInformationPosition.
@Override
protected int guessContextInformationPosition(ContentAssistInvocationContext context) {
final int contextPosition = context.getInvocationOffset();
IDocument document = context.getDocument();
JavaHeuristicScanner scanner = new JavaHeuristicScanner(document);
int bound = Math.max(-1, contextPosition - 200);
// try the innermost scope of angle brackets that looks like a generic type argument list
try {
int pos = contextPosition - 1;
do {
int angle = scanner.findOpeningPeer(pos, bound, '<', '>');
if (angle == JavaHeuristicScanner.NOT_FOUND)
break;
int token = scanner.previousToken(angle - 1, bound);
// next token must be a method name that is a generic type
if (token == Symbols.TokenIDENT) {
int off = scanner.getPosition() + 1;
int end = angle;
String ident = document.get(off, end - off).trim();
if (JavaHeuristicScanner.isGenericStarter(ident))
return angle + 1;
}
pos = angle - 1;
} while (true);
} catch (BadLocationException x) {
}
return super.guessContextInformationPosition(context);
}
use of org.eclipse.jface.text.BadLocationException 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.jface.text.BadLocationException in project che by eclipse.
the class CodeTemplateContext method changeLineDelimiter.
private static String changeLineDelimiter(String code, String lineDelim) {
try {
ILineTracker tracker = new DefaultLineTracker();
tracker.set(code);
int nLines = tracker.getNumberOfLines();
if (nLines == 1) {
return code;
}
StringBuffer buf = new StringBuffer();
for (int i = 0; i < nLines; i++) {
if (i != 0) {
buf.append(lineDelim);
}
IRegion region = tracker.getLineInformation(i);
String line = code.substring(region.getOffset(), region.getOffset() + region.getLength());
buf.append(line);
}
return buf.toString();
} catch (BadLocationException e) {
// can not happen
return code;
}
}
use of org.eclipse.jface.text.BadLocationException in project che by eclipse.
the class JavaContext method getKey.
/*
* @see org.eclipse.jdt.internal.corext.template.DocumentTemplateContext#getKey()
*/
@Override
public String getKey() {
if (getCompletionLength() == 0)
return super.getKey();
try {
IDocument document = getDocument();
int start = getStart();
int end = getCompletionOffset();
return start <= end ? document.get(start, end - start) : //$NON-NLS-1$
"";
} catch (BadLocationException e) {
return super.getKey();
}
}
use of org.eclipse.jface.text.BadLocationException in project che by eclipse.
the class JavaContext method getIndentation.
/**
* Returns the indentation level at the position of code completion.
*
* @return the indentation level at the position of the code completion
*/
private int getIndentation() {
int start = getStart();
IDocument document = getDocument();
try {
IRegion region = document.getLineInformationOfOffset(start);
String lineContent = document.get(region.getOffset(), region.getLength());
IJavaProject project = getJavaProject();
return Strings.computeIndentUnits(lineContent, project);
} catch (BadLocationException e) {
return 0;
}
}
Aggregations