use of org.eclipse.jface.text.Position in project che by eclipse.
the class TaskMarkerProposal method getUpdatedPosition.
private Position getUpdatedPosition(IDocument document) throws BadLocationException {
IScanner scanner = ToolFactory.createScanner(true, false, false, false);
scanner.setSource(document.get().toCharArray());
int token = getSurroundingComment(scanner);
if (token == ITerminalSymbols.TokenNameEOF) {
return null;
}
int commentStart = scanner.getCurrentTokenStartPosition();
int commentEnd = scanner.getCurrentTokenEndPosition() + 1;
int contentStart = commentStart + 2;
int contentEnd = commentEnd;
if (token == ITerminalSymbols.TokenNameCOMMENT_JAVADOC) {
contentStart = commentStart + 3;
contentEnd = commentEnd - 2;
} else if (token == ITerminalSymbols.TokenNameCOMMENT_BLOCK) {
contentEnd = commentEnd - 2;
}
if (hasContent(document, contentStart, fLocation.getOffset()) || hasContent(document, contentEnd, fLocation.getOffset() + fLocation.getLength())) {
return new Position(fLocation.getOffset(), fLocation.getLength());
}
IRegion startRegion = document.getLineInformationOfOffset(commentStart);
int start = startRegion.getOffset();
boolean contentAtBegining = hasContent(document, start, commentStart);
if (contentAtBegining) {
start = commentStart;
}
int end;
if (token == ITerminalSymbols.TokenNameCOMMENT_LINE) {
if (contentAtBegining) {
// only to the end of the line
end = startRegion.getOffset() + startRegion.getLength();
} else {
// includes new line
end = commentEnd;
}
} else {
int endLine = document.getLineOfOffset(commentEnd - 1);
if (endLine + 1 == document.getNumberOfLines() || contentAtBegining) {
IRegion endRegion = document.getLineInformation(endLine);
end = endRegion.getOffset() + endRegion.getLength();
} else {
IRegion endRegion = document.getLineInformation(endLine + 1);
end = endRegion.getOffset();
}
}
if (hasContent(document, commentEnd, end)) {
end = commentEnd;
// only remove comment
start = commentStart;
}
return new Position(start, end - start);
}
use of org.eclipse.jface.text.Position in project che by eclipse.
the class InclusivePositionUpdater method update.
/*
* @see org.eclipse.jface.text.IPositionUpdater#update(org.eclipse.jface.text.DocumentEvent)
*/
public void update(DocumentEvent event) {
int eventOffset = event.getOffset();
int eventOldLength = event.getLength();
int eventNewLength = event.getText() == null ? 0 : event.getText().length();
int deltaLength = eventNewLength - eventOldLength;
try {
Position[] positions = event.getDocument().getPositions(fCategory);
for (int i = 0; i != positions.length; i++) {
Position position = positions[i];
if (position.isDeleted())
continue;
int offset = position.getOffset();
int length = position.getLength();
int end = offset + length;
if (offset > eventOffset + eventOldLength)
// position comes way
// after change - shift
position.setOffset(offset + deltaLength);
else if (end < eventOffset) {
// position comes way before change -
// leave alone
} else if (offset <= eventOffset && end >= eventOffset + eventOldLength) {
// event completely internal to the position - adjust length
position.setLength(length + deltaLength);
} else if (offset < eventOffset) {
// event extends over end of position - adjust length
int newEnd = eventOffset + eventNewLength;
position.setLength(newEnd - offset);
} else if (end > eventOffset + eventOldLength) {
// event extends from before position into it - adjust offset
// and length
// offset becomes end of event, length ajusted acordingly
// we want to recycle the overlapping part
position.setOffset(eventOffset);
int deleted = eventOffset + eventOldLength - offset;
position.setLength(length - deleted + eventNewLength);
} else {
// event consumes the position - delete it
position.delete();
}
}
} catch (BadPositionCategoryException e) {
// ignore and return
}
}
use of org.eclipse.jface.text.Position in project che by eclipse.
the class TemplateEngine method complete.
/**
* Inspects the context of the compilation unit around <code>completionPosition</code>
* and feeds the collector with proposals.
* @param viewer the text viewer
* @param completionPosition the context position in the document of the text viewer
* @param compilationUnit the compilation unit (may be <code>null</code>)
*/
public void complete(ITextViewer viewer, int completionPosition, ICompilationUnit compilationUnit) {
IDocument document = viewer.getDocument();
if (!(fContextType instanceof CompilationUnitContextType))
return;
Point selection = viewer.getSelectedRange();
Position position = new Position(completionPosition, selection.y);
// remember selected text
String selectedText = null;
if (selection.y != 0) {
try {
selectedText = document.get(selection.x, selection.y);
document.addPosition(position);
fPositions.put(document, position);
} catch (BadLocationException e) {
}
}
CompilationUnitContext context = ((CompilationUnitContextType) fContextType).createContext(document, position, compilationUnit);
//$NON-NLS-1$
context.setVariable("selection", selectedText);
int start = context.getStart();
int end = context.getEnd();
IRegion region = new Region(start, end - start);
Template[] templates = JavaPlugin.getDefault().getTemplateStore().getTemplates();
if (selection.y == 0) {
for (int i = 0; i != templates.length; i++) {
Template template = templates[i];
if (context.canEvaluate(template)) {
fProposals.add(new TemplateProposal(template, context, region, getImage()));
}
}
} else {
if (context.getKey().length() == 0)
context.setForceEvaluation(true);
boolean multipleLinesSelected = areMultipleLinesSelected(viewer);
for (int i = 0; i != templates.length; i++) {
Template template = templates[i];
if (context.canEvaluate(template) && (!multipleLinesSelected && template.getPattern().indexOf($_WORD_SELECTION) != -1 || (multipleLinesSelected && template.getPattern().indexOf($_LINE_SELECTION) != -1))) {
fProposals.add(new TemplateProposal(templates[i], context, region, getImage()));
}
}
}
}
use of org.eclipse.jface.text.Position in project che by eclipse.
the class ParameterGuessingProposal method guessParameters.
// /**
// * Returns the currently active java editor, or <code>null</code> if it
// * cannot be determined.
// *
// * @return the currently active java editor, or <code>null</code>
// */
// private JavaEditor getJavaEditor() {
// IEditorPart part= JavaPlugin.getActivePage().getActiveEditor();
// if (part instanceof JavaEditor)
// return (JavaEditor) part;
// else
// return null;
// }
private ICompletionProposal[][] guessParameters(char[][] parameterNames) throws JavaModelException {
// find matches in reverse order. Do this because people tend to declare the variable meant for the last
// parameter last. That is, local variables for the last parameter in the method completion are more
// likely to be closer to the point of code completion. As an example consider a "delegation" completion:
//
// public void myMethod(int param1, int param2, int param3) {
// someOtherObject.yourMethod(param1, param2, param3);
// }
//
// The other consideration is giving preference to variables that have not previously been used in this
// code completion (which avoids "someOtherObject.yourMethod(param1, param1, param1)";
int count = parameterNames.length;
fPositions = new Position[count];
fChoices = new ICompletionProposal[count][];
String[] parameterTypes = getParameterTypes();
ParameterGuesser guesser = new ParameterGuesser(getEnclosingElement());
IJavaElement[][] assignableElements = getAssignableElements();
for (int i = count - 1; i >= 0; i--) {
String paramName = new String(parameterNames[i]);
Position position = new Position(0, 0);
boolean isLastParameter = i == count - 1;
ICompletionProposal[] argumentProposals = guesser.parameterProposals(parameterTypes[i], paramName, position, assignableElements[i], fFillBestGuess, isLastParameter);
if (argumentProposals.length == 0) {
JavaCompletionProposal proposal = new JavaCompletionProposal(paramName, 0, paramName.length(), null, paramName, 0);
if (isLastParameter)
proposal.setTriggerCharacters(new char[] { ',' });
argumentProposals = new ICompletionProposal[] { proposal };
}
fPositions[i] = position;
fChoices[i] = argumentProposals;
}
return fChoices;
}
use of org.eclipse.jface.text.Position in project che by eclipse.
the class ParameterGuessingProposal method computeGuessingCompletion.
/**
* Creates the completion string. Offsets and Lengths are set to the offsets and lengths of the
* parameters.
*
* @return the completion string
* @throws org.eclipse.jdt.core.JavaModelException if parameter guessing failed
*/
private String computeGuessingCompletion() throws JavaModelException {
StringBuffer buffer = new StringBuffer();
appendMethodNameReplacement(buffer);
FormatterPrefs prefs = getFormatterPrefs();
setCursorPosition(buffer.length());
if (prefs.afterOpeningParen)
buffer.append(SPACE);
char[][] parameterNames = fProposal.findParameterNames(null);
fChoices = guessParameters(parameterNames);
int count = fChoices.length;
int replacementOffset = getReplacementOffset();
for (int i = 0; i < count; i++) {
if (i != 0) {
if (prefs.beforeComma)
buffer.append(SPACE);
buffer.append(COMMA);
if (prefs.afterComma)
buffer.append(SPACE);
}
ICompletionProposal proposal = fChoices[i][0];
String argument = proposal.getDisplayString();
Position position = fPositions[i];
position.setOffset(replacementOffset + buffer.length());
position.setLength(argument.length());
if (// handle the "unknown" case where we only insert a proposal.
proposal instanceof JavaCompletionProposal)
((JavaCompletionProposal) proposal).setReplacementOffset(replacementOffset + buffer.length());
buffer.append(argument);
}
if (prefs.beforeClosingParen)
buffer.append(SPACE);
buffer.append(RPAREN);
if (canAutomaticallyAppendSemicolon())
buffer.append(SEMICOLON);
return buffer.toString();
}
Aggregations