use of org.eclipse.jface.text.IRewriteTarget in project eclipse.platform.text by eclipse.
the class CompletionProposalPopup2 method insertProposal.
/**
* Applies the given proposal at the given offset. The given character is the
* one that triggered the insertion of this proposal.
*
* @param p the completion proposal
* @param trigger the trigger character
* @param stateMask the state mask of the keyboard event triggering the insertion
* @param offset the offset
* @since 2.1
*/
private void insertProposal(ICompletionProposal p, char trigger, int stateMask, final int offset) {
fInserting = true;
IRewriteTarget target = null;
IEditingSupportRegistry registry = null;
try {
IDocument document = fViewer.getDocument();
if (fViewer instanceof ITextViewerExtension) {
ITextViewerExtension extension = (ITextViewerExtension) fViewer;
target = extension.getRewriteTarget();
}
if (target != null)
target.beginCompoundChange();
if (fViewer instanceof IEditingSupportRegistry) {
registry = (IEditingSupportRegistry) fViewer;
registry.register(fModificationEditingSupport);
}
if (p instanceof ICompletionProposalExtension2) {
ICompletionProposalExtension2 e = (ICompletionProposalExtension2) p;
e.apply(fViewer, trigger, stateMask, offset);
} else if (p instanceof ICompletionProposalExtension) {
ICompletionProposalExtension e = (ICompletionProposalExtension) p;
e.apply(document, trigger, offset);
} else {
p.apply(document);
}
Point selection = p.getSelection(document);
if (selection != null) {
fViewer.setSelectedRange(selection.x, selection.y);
fViewer.revealRange(selection.x, selection.y);
}
IContextInformation info = p.getContextInformation();
if (info != null) {
int position;
if (p instanceof ICompletionProposalExtension) {
ICompletionProposalExtension e = (ICompletionProposalExtension) p;
position = e.getContextInformationPosition();
} else {
if (selection == null)
selection = fViewer.getSelectedRange();
position = selection.x + selection.y;
}
fContentAssistant.showContextInformation(info, position);
}
fContentAssistant.fireProposalChosen(p);
} finally {
if (target != null)
target.endCompoundChange();
if (registry != null)
registry.unregister(fModificationEditingSupport);
fInserting = false;
}
}
use of org.eclipse.jface.text.IRewriteTarget in project eclipse.platform.text by eclipse.
the class LinkedModeUI method endCompoundChangeIfNeeded.
private void endCompoundChangeIfNeeded() {
if (fHasOpenCompoundChange) {
ITextViewerExtension extension = (ITextViewerExtension) fCurrentTarget.getViewer();
IRewriteTarget target = extension.getRewriteTarget();
target.endCompoundChange();
fHasOpenCompoundChange = false;
}
}
use of org.eclipse.jface.text.IRewriteTarget in project linuxtools by eclipse.
the class IndentHandler method execute.
@Override
public Object execute(ExecutionEvent event) {
// the framework
if (!isEnabled())
return null;
ITextEditor editor = (ITextEditor) HandlerUtil.getActiveEditor(event);
if (editor == null || !editor.isEditable()) {
return null;
}
ITextSelection selection = getSelection(editor);
final IDocument document = getDocument(editor);
if (document != null) {
final int offset = selection.getOffset();
final int length = selection.getLength();
final Position end = new Position(offset + length);
final int firstLine, nLines;
fCaretOffset = -1;
try {
firstLine = document.getLineOfOffset(offset);
// check for marginal (zero-length) lines
int minusOne = length == 0 ? 0 : 1;
nLines = document.getLineOfOffset(offset + length - minusOne) - firstLine + 1;
document.addPosition(end);
} catch (BadLocationException e) {
// will only happen on concurrent modification
// $NON-NLS-1$
IDEPlugin.log(new Status(IStatus.ERROR, IDEPlugin.PLUGIN_ID, IStatus.OK, "", e));
return null;
}
Runnable runnable = () -> {
IRewriteTarget target = editor.getAdapter(IRewriteTarget.class);
if (target != null) {
target.beginCompoundChange();
}
try {
STPHeuristicScanner scanner = new STPHeuristicScanner(document);
STPIndenter indenter = new STPIndenter(document, scanner, getProject(editor));
final boolean multiLine = nLines > 1;
boolean hasChanged = false;
for (int i = 0; i < nLines; i++) {
hasChanged |= indentLine(document, firstLine + i, offset, indenter, scanner, multiLine);
}
// update caret position: move to new position when
// indenting just one line
// keep selection when indenting multiple
int newOffset, newLength;
if (!fIsTabAction && multiLine) {
newOffset = offset;
newLength = end.getOffset() - offset;
} else {
newOffset = fCaretOffset;
newLength = 0;
}
// but not when we had a single line non-tab invocation
if (newOffset != -1 && (hasChanged || newOffset != offset || newLength != length))
selectAndReveal(editor, newOffset, newLength);
} catch (BadLocationException e) {
// will only happen on concurrent modification
IDEPlugin.log(new Status(IStatus.ERROR, IDEPlugin.PLUGIN_ID, IStatus.OK, "ConcurrentModification in IndentAction", // $NON-NLS-1$
e));
} finally {
document.removePosition(end);
if (target != null) {
target.endCompoundChange();
}
}
};
if (nLines > 50) {
Display display = editor.getEditorSite().getWorkbenchWindow().getShell().getDisplay();
BusyIndicator.showWhile(display, runnable);
} else {
runnable.run();
}
}
return null;
}
Aggregations