下面列出了org.eclipse.jface.text.source.ISourceViewer#getDocument ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected ContentAssistProcessorTestBuilder appendAndApplyProposal(ICompletionProposal proposal, ISourceViewer sourceViewer, String model, int position)
throws Exception {
IDocument document = sourceViewer.getDocument();
int offset = position;
if (model != null) {
document.set(getModel() + model);
offset += model.length();
}
if (proposal instanceof ICompletionProposalExtension2) {
ICompletionProposalExtension2 proposalExtension2 = (ICompletionProposalExtension2) proposal;
proposalExtension2.apply(sourceViewer, (char) 0, SWT.NONE, offset);
} else if (proposal instanceof ICompletionProposalExtension) {
ICompletionProposalExtension proposalExtension = (ICompletionProposalExtension) proposal;
proposalExtension.apply(document, (char) 0, offset);
} else {
proposal.apply(document);
}
return reset().append(document.get());
}
/**
* Finds the previous position before the given position.
*
* @param position the current position
* @return the previous position
*/
protected int findPreviousPosition(int position) {
ISourceViewer viewer= getSourceViewer();
int widget= -1;
int previous= position;
while (previous != BreakIterator.DONE && widget == -1) { // XXX: optimize
previous= fIterator.preceding(previous);
if (previous != BreakIterator.DONE)
widget= modelOffset2WidgetOffset(viewer, previous);
}
IDocument document= viewer.getDocument();
LinkedModeModel model= LinkedModeModel.getModel(document, position);
if (model != null && previous != BreakIterator.DONE) {
LinkedPosition linkedPosition= model.findPosition(new LinkedPosition(document, position, 0));
if (linkedPosition != null) {
int linkedPositionOffset= linkedPosition.getOffset();
if (position != linkedPositionOffset && previous < linkedPositionOffset)
previous= linkedPositionOffset;
} else {
LinkedPosition previousLinkedPosition= model.findPosition(new LinkedPosition(document, previous, 0));
if (previousLinkedPosition != null) {
int previousLinkedPositionEnd= previousLinkedPosition.getOffset() + previousLinkedPosition.getLength();
if (position != previousLinkedPositionEnd && previous < previousLinkedPositionEnd)
previous= previousLinkedPositionEnd;
}
}
}
return previous;
}
@Override
public void setSourceViewer(ISourceViewer sourceViewer) {
this.sourceViewer = sourceViewer;
IDocument document = sourceViewer.getDocument();
xtextDocument = xtextDocumentUtil.getXtextDocument(document);
Assert.isNotNull(xtextDocument);
configureTextInputListener();
}
public boolean start(IRenameElementContext renameElementContext, Provider<LinkedPositionGroup> provider, IProgressMonitor monitor) {
if (renameElementContext == null)
throw new IllegalArgumentException("RenameElementContext is null");
this.linkedPositionGroup = provider.get();
if (linkedPositionGroup == null || linkedPositionGroup.isEmpty())
return false;
this.editor = (XtextEditor) renameElementContext.getTriggeringEditor();
this.focusEditingSupport = new FocusEditingSupport();
ISourceViewer viewer = editor.getInternalSourceViewer();
IDocument document = viewer.getDocument();
originalSelection = viewer.getSelectedRange();
currentPosition = linkedPositionGroup.getPositions()[0];
originalName = getCurrentName();
try {
linkedModeModel = new LinkedModeModel();
linkedModeModel.addGroup(linkedPositionGroup);
linkedModeModel.forceInstall();
linkedModeModel.addLinkingListener(new EditorSynchronizer());
LinkedModeUI ui = new EditorLinkedModeUI(linkedModeModel, viewer);
ui.setExitPolicy(new ExitPolicy(document));
if (currentPosition.includes(originalSelection.x))
ui.setExitPosition(viewer, originalSelection.x, 0, Integer.MAX_VALUE);
ui.enter();
if (currentPosition.includes(originalSelection.x)
&& currentPosition.includes(originalSelection.x + originalSelection.y))
viewer.setSelectedRange(originalSelection.x, originalSelection.y);
if (viewer instanceof IEditingSupportRegistry) {
IEditingSupportRegistry registry = (IEditingSupportRegistry) viewer;
registry.register(focusEditingSupport);
}
openPopup();
return true;
} catch (BadLocationException e) {
throw new WrappedException(e);
}
}
/**
* Checks if <code>selection</code> is contained by the visible region of <code>viewer</code>.
* As a special case, a selection is considered contained even if it extends over the visible
* region, but the extension stays on a partially contained line and contains only white space.
*
* @param selection the selection to be checked
* @param viewer the viewer displaying a visible region of <code>selection</code>'s document.
* @return <code>true</code>, if <code>selection</code> is contained, <code>false</code> otherwise.
*/
private boolean containedByVisibleRegion(ITextSelection selection, ISourceViewer viewer) {
int min= selection.getOffset();
int max= min + selection.getLength();
IDocument document= viewer.getDocument();
IRegion visible;
if (viewer instanceof ITextViewerExtension5)
visible= ((ITextViewerExtension5) viewer).getModelCoverage();
else
visible= viewer.getVisibleRegion();
int visOffset= visible.getOffset();
try {
if (visOffset > min) {
if (document.getLineOfOffset(visOffset) != selection.getStartLine())
return false;
if (!isWhitespace(document.get(min, visOffset - min))) {
showStatus();
return false;
}
}
int visEnd= visOffset + visible.getLength();
if (visEnd < max) {
if (document.getLineOfOffset(visEnd) != selection.getEndLine())
return false;
if (!isWhitespace(document.get(visEnd, max - visEnd))) {
showStatus();
return false;
}
}
return true;
} catch (BadLocationException e) {
}
return false;
}
/**
* Find a problem marker from the given line and return its error message.
*
* @param sourceViewer the source viewer
* @param lineNumber line number in the file, starting from zero
* @return the message of the marker of null, if no marker at the specified line
*/
public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
IDocument document= sourceViewer.getDocument();
IAnnotationModel model= sourceViewer.getAnnotationModel();
if (model == null)
return null;
List<String> lineMarkers = null;
Iterator<Annotation> e= model.getAnnotationIterator();
while (e.hasNext()) {
Annotation o= e.next();
if (o instanceof MarkerAnnotation) {
MarkerAnnotation a= (MarkerAnnotation) o;
if (isRulerLine(model.getPosition(a), document, lineNumber)) {
if (lineMarkers == null)
lineMarkers = new LinkedList<String>();
lineMarkers.add(a.getMarker().getAttribute(IMarker.MESSAGE, null));
}
}
}
if (lineMarkers != null)
return getMessage(lineMarkers);
return null;
}
private String[] getMessagesForLine(final ISourceViewer viewer, final int line) {
final IAnnotationModel model = viewer.getAnnotationModel();
if (model == null) {
return new String[0];
}
final Iterator<Annotation> it = model.getAnnotationIterator();
final IDocument document = viewer.getDocument();
final ArrayList<String> messages = new ArrayList<>();
final HashMap<Position, Set<String>> placeMessagesMap = new HashMap<>();
while (it.hasNext()) {
final Annotation annotation = it.next();
if (annotation instanceof MarkerAnnotation) {
final MarkerAnnotation ma = (MarkerAnnotation) annotation;
final Position p = model.getPosition(ma);
if (compareRulerLine(p, document, line)) {
final IMarker marker = ma.getMarker();
final String message = marker.getAttribute(IMarker.MESSAGE, null);
if ((message != null) && (message.trim().length() > 0)) {
Set<String> priorMessages = placeMessagesMap.get(p);
if (priorMessages == null) {
priorMessages = new HashSet<>();
placeMessagesMap.put(p, priorMessages);
}
if (!priorMessages.contains(message)) {
messages.add(message);
priorMessages.add(message);
}
}
}
}
}
return (String[]) messages.toArray(new String[messages.size()]);
}
public String getOldName(int offset, ITypeScriptFile tsFile) {
try {
Location startLoc = null;
Location endLoc = null;
if (true) {
QuickInfo info = tsFile.quickInfo(offset).get(1000, TimeUnit.MILLISECONDS);
if (info == null) {
return null;
}
startLoc = info.getStart();
endLoc = info.getEnd();
} else {
List<OccurrencesResponseItem> occurrences = tsFile.occurrences(offset).get(1000, TimeUnit.MILLISECONDS);
if (occurrences.isEmpty()) {
return null;
}
OccurrencesResponseItem occurrence = occurrences.get(0);
startLoc = occurrence.getStart();
endLoc = occurrence.getEnd();
}
ISourceViewer viewer = fEditor.getViewer();
IDocument document = viewer.getDocument();
int start = DocumentUtils.getPosition(document, startLoc);
int end = DocumentUtils.getPosition(document, endLoc);
int length = end - start;
return document.get(start, length);
} catch (
Exception e) {
return null;
}
}
@Override
public void run() {
// get involved objects
if (pyEdit == null) {
return;
}
if (!validateEditorInputState()) {
return;
}
ISourceViewer viewer = pyEdit.getEditorSourceViewer();
if (viewer == null) {
return;
}
IDocument document = viewer.getDocument();
if (document == null) {
return;
}
StyledText widget = viewer.getTextWidget();
if (widget == null) {
return;
}
// get selection
ITextSelection sel = (ITextSelection) viewer.getSelectionProvider().getSelection();
move(pyEdit, viewer, document, new CoreTextSelection(document, sel.getOffset(), sel.getLength()));
}
@Override
public void run() {
// Check whether we are in a java code partition and the preference is enabled
final IPreferenceStore store= getPreferenceStore();
if (!store.getBoolean(PreferenceConstants.EDITOR_SUB_WORD_NAVIGATION)) {
super.run();
return;
}
final ISourceViewer viewer= getSourceViewer();
final IDocument document= viewer.getDocument();
try {
fIterator.setText((CharacterIterator)new DocumentCharacterIterator(document));
int position= widgetOffset2ModelOffset(viewer, viewer.getTextWidget().getCaretOffset());
if (position == -1)
return;
int next= findNextPosition(position);
if (isBlockSelectionModeEnabled() && document.getLineOfOffset(next) != document.getLineOfOffset(position)) {
super.run(); // may navigate into virtual white space
} else if (next != BreakIterator.DONE) {
setCaretPosition(next);
getTextWidget().showSelection();
fireSelectionChanged();
}
} catch (BadLocationException x) {
// ignore
}
}
/**
* If length of line at 'lineNum' is 'len', then validate all proposals for invocation
* index varying from '(len - numCharsCompleted)' to 'len'.
*/
private static void validateExpectedProposals(IJavaProject javaProject,
String fullyQualifiedClassName, String source, int lineNum, int numCharsCompleted,
String... expectedProposals) throws CoreException, BadLocationException {
IProgressMonitor monitor = new NullProgressMonitor();
ICompilationUnit iCompilationUnit = JavaProjectUtilities.createCompilationUnit(
javaProject, fullyQualifiedClassName, source);
CompilationUnitEditor cuEditor = (CompilationUnitEditor) JavaUI.openInEditor(iCompilationUnit);
ISourceViewer viewer = cuEditor.getViewer();
IDocument document = viewer.getDocument();
IRegion lineInformation = document.getLineInformation(lineNum);
JsniMethodBodyCompletionProposalComputer jcpc = new JsniMethodBodyCompletionProposalComputer();
for (int numCharsToOverwrite = 0; numCharsToOverwrite <= numCharsCompleted;
numCharsToOverwrite++){
int invocationOffset = lineInformation.getOffset()
+ lineInformation.getLength() - numCharsToOverwrite;
JavaContentAssistInvocationContext context = new JavaContentAssistInvocationContext(
viewer, invocationOffset, cuEditor);
List<ICompletionProposal> completions = jcpc.computeCompletionProposals(
context, monitor);
int indentationUnits = JsniMethodBodyCompletionProposalComputer.measureIndentationUnits(
document, lineNum, lineInformation.getOffset(), javaProject);
List<String> expected = createJsniBlocks(javaProject, indentationUnits,
expectedProposals);
for (int i = 0; i < expected.size(); i++){
String expectedBlock = expected.get(i).substring(numCharsCompleted - numCharsToOverwrite);
expected.set(i, expectedBlock);
}
assertExpectedProposals(expected, completions, numCharsToOverwrite);
}
}
protected List<IMarker> getMarkersFor(ISourceViewer sourceViewer, int offset) throws BadLocationException {
final IDocument document = sourceViewer.getDocument();
int line = document.getLineOfOffset(offset);
int lineOffset = document.getLineOffset(line);
String delim = document.getLineDelimiter(line);
int delimLength = delim != null ? delim.length() : 0;
int lineLength = document.getLineLength(line) - delimLength;
return getMarkersFor(sourceViewer, lineOffset, lineLength);
}
/**
* getDocument
*
* @return
*/
protected IDocument getDocument() {
ISourceViewer sourceViewer = getSourceViewer();
IDocument result = null;
if (sourceViewer != null) {
result = sourceViewer.getDocument();
}
return result;
}
@Override
protected Object getHoverInfoForLine(final ISourceViewer viewer, final int line) {
final boolean showTemporaryProblems= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_CORRECTION_INDICATION);
IAnnotationModel model= viewer.getAnnotationModel();
IDocument document= viewer.getDocument();
if (model == null)
return null;
List<Annotation> exact= new ArrayList<Annotation>();
HashMap<Position, Object> messagesAtPosition= new HashMap<Position, Object>();
Iterator<Annotation> e= model.getAnnotationIterator();
while (e.hasNext()) {
Annotation annotation= e.next();
if (fAnnotationAccess instanceof IAnnotationAccessExtension)
if (!((IAnnotationAccessExtension)fAnnotationAccess).isPaintable(annotation))
continue;
if (annotation instanceof IJavaAnnotation && !isIncluded((IJavaAnnotation)annotation, showTemporaryProblems))
continue;
AnnotationPreference pref= fLookup.getAnnotationPreference(annotation);
if (pref != null) {
String key= pref.getVerticalRulerPreferenceKey();
if (key != null && !fStore.getBoolean(key))
continue;
}
Position position= model.getPosition(annotation);
if (position == null)
continue;
if (compareRulerLine(position, document, line) == 1) {
if (isDuplicateMessage(messagesAtPosition, position, annotation.getText()))
continue;
exact.add(annotation);
}
}
sort(exact, model);
if (exact.size() > 0)
setLastRulerMouseLocation(viewer, line);
if (exact.size() > 0) {
Annotation first= exact.get(0);
if (!isBreakpointAnnotation(first))
exact.add(0, new NoBreakpointAnnotation());
}
if (exact.size() <= 1)
return null;
AnnotationHoverInput input= new AnnotationHoverInput();
input.fAnnotations= exact.toArray(new Annotation[0]);
input.fViewer= viewer;
input.fRulerInfo= fCompositeRuler;
input.fAnnotationListener= fgListener;
input.fDoubleClickListener= fDblClickListener;
input.redoAction= new AnnotationExpansionControl.ICallback() {
public void run(IInformationControlExtension2 control) {
control.setInput(getHoverInfoForLine(viewer, line));
}
};
input.model= model;
return input;
}
private static boolean getEscapeUnescapeBackslashProposals(IQuickAssistInvocationContext invocationContext, ArrayList<ICompletionProposal> resultingCollections) throws BadLocationException,
BadPartitioningException {
ISourceViewer sourceViewer= invocationContext.getSourceViewer();
IDocument document= sourceViewer.getDocument();
Point selectedRange= sourceViewer.getSelectedRange();
int selectionOffset= selectedRange.x;
int selectionLength= selectedRange.y;
int proposalOffset;
int proposalLength;
String text;
if (selectionLength == 0) {
if (selectionOffset != document.getLength()) {
char ch= document.getChar(selectionOffset);
if (ch == '=' || ch == ':') { //see PropertiesFilePartitionScanner()
return false;
}
}
ITypedRegion partition= null;
if (document instanceof IDocumentExtension3)
partition= ((IDocumentExtension3)document).getPartition(IPropertiesFilePartitions.PROPERTIES_FILE_PARTITIONING, invocationContext.getOffset(), false);
if (partition == null)
return false;
String type= partition.getType();
if (!(type.equals(IPropertiesFilePartitions.PROPERTY_VALUE) || type.equals(IDocument.DEFAULT_CONTENT_TYPE))) {
return false;
}
proposalOffset= partition.getOffset();
proposalLength= partition.getLength();
text= document.get(proposalOffset, proposalLength);
if (type.equals(IPropertiesFilePartitions.PROPERTY_VALUE)) {
text= text.substring(1); //see PropertiesFilePartitionScanner()
proposalOffset++;
proposalLength--;
}
} else {
proposalOffset= selectionOffset;
proposalLength= selectionLength;
text= document.get(proposalOffset, proposalLength);
}
if (PropertiesFileEscapes.containsUnescapedBackslash(text)) {
if (resultingCollections == null)
return true;
resultingCollections.add(new EscapeBackslashCompletionProposal(PropertiesFileEscapes.escape(text, false, true, false), proposalOffset, proposalLength,
PropertiesFileEditorMessages.EscapeBackslashCompletionProposal_escapeBackslashes));
return true;
}
if (PropertiesFileEscapes.containsEscapedBackslashes(text)) {
if (resultingCollections == null)
return true;
resultingCollections.add(new EscapeBackslashCompletionProposal(PropertiesFileEscapes.unescapeBackslashes(text), proposalOffset, proposalLength,
PropertiesFileEditorMessages.EscapeBackslashCompletionProposal_unescapeBackslashes));
return true;
}
return false;
}
public static PySelection createPySelectionFromEditor(ISourceViewer viewer, ITextSelection textSelection) {
return new PySelection(viewer.getDocument(), new CoreTextSelection(
viewer.getDocument(), textSelection.getOffset(), textSelection.getLength()));
}
/**
* Mostly based on code from {@link org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedNamesAssistProposal}
*/
private void startEditing(ISourceViewer viewer) throws ExecutionException {
Point selOffsetAndLen = viewer.getSelectedRange();
int selStart = CoordinatesUtil.fromOffsetAndLengthToStartAndEnd(selOffsetAndLen).x;
IDocument document = viewer.getDocument();
try {
String selectedText;
if (selOffsetAndLen.y == 0) { // no characters selected
String documentText = document.get();
Point wordOffsetAndLen = TextUtil.findWordSurrounding(documentText, selStart);
if (wordOffsetAndLen != null) {
selectedText = document.get(wordOffsetAndLen.x, wordOffsetAndLen.y);
} else {
IRegion selectedLine = document.getLineInformationOfOffset(selStart);
selectedText = document.get(selectedLine.getOffset(), selectedLine.getLength());
}
} else {
selectedText = document.get(selOffsetAndLen.x, selOffsetAndLen.y);
}
LinkedPositionGroup linkedPositionGroup = new LinkedPositionGroup();
FindReplaceDocumentAdapter findReplaceAdaptor = new FindReplaceDocumentAdapter(document);
IRegion matchingRegion = findReplaceAdaptor.find(0, selectedText, true, true, false, false);
while (matchingRegion != null) {
linkedPositionGroup.addPosition(new LinkedPosition(document, matchingRegion.getOffset(), matchingRegion
.getLength()));
matchingRegion = findReplaceAdaptor.find(matchingRegion.getOffset() + matchingRegion.getLength(),
selectedText, true, true, false, false);
}
LinkedModeModel model = new LinkedModeModel();
model.addGroup(linkedPositionGroup);
model.forceInstall();
LinkedModeUI ui = new EditorLinkedModeUI(model, viewer);
ui.setExitPolicy(new DeleteBlockingExitPolicy(document));
ui.enter();
// by default the text being edited is selected so restore original selection
viewer.setSelectedRange(selOffsetAndLen.x, selOffsetAndLen.y);
} catch (BadLocationException e) {
throw new ExecutionException("Editing failed", e);
}
}
/**
* Checks if <code>selection</code> is contained by the visible region of <code>viewer</code>.
* As a special case, a selection is considered contained even if it extends over the visible
* region, but the extension stays on a partially contained line and contains only white space.
*
* @param selection the selection to be checked
* @param viewer the viewer displaying a visible region of <code>selection</code>'s document.
* @return <code>true</code>, if <code>selection</code> is contained, <code>false</code> otherwise.
*/
private boolean containedByVisibleRegion(ICoreTextSelection selection, ISourceViewer viewer) {
if (viewer == null) {
return true; //in tests
}
int min = selection.getOffset();
int max = min + selection.getLength();
IDocument document = viewer.getDocument();
IRegion visible;
if (viewer instanceof ITextViewerExtension5) {
visible = ((ITextViewerExtension5) viewer).getModelCoverage();
} else {
visible = viewer.getVisibleRegion();
}
int visOffset = visible.getOffset();
try {
if (visOffset > min) {
if (document.getLineOfOffset(visOffset) != selection.getStartLine()) {
return false;
}
if (!isWhitespace(document.get(min, visOffset - min))) {
showStatus();
return false;
}
}
int visEnd = visOffset + visible.getLength();
if (visEnd < max) {
if (document.getLineOfOffset(visEnd) != selection.getEndLine()) {
return false;
}
if (!isWhitespace(document.get(visEnd, max - visEnd))) {
showStatus();
return false;
}
}
return true;
} catch (BadLocationException e) {
}
return false;
}
public void start() {
if (getActiveLinkedMode() != null) {
// for safety; should already be handled in RenameJavaElementAction
fgActiveLinkedMode.startFullDialog();
return;
}
ISourceViewer viewer = fEditor.getViewer();
IDocument document = viewer.getDocument();
ITypeScriptFile tsFile = fEditor.getTypeScriptFile();
tsFile.setDisableChanged(true);
fOriginalSelection = viewer.getSelectedRange();
int offset = fOriginalSelection.x;
try {
fLinkedPositionGroup = new LinkedPositionGroup();
if (viewer instanceof ITextViewerExtension6) {
IUndoManager undoManager = ((ITextViewerExtension6) viewer).getUndoManager();
if (undoManager instanceof IUndoManagerExtension) {
IUndoManagerExtension undoManagerExtension = (IUndoManagerExtension) undoManager;
IUndoContext undoContext = undoManagerExtension.getUndoContext();
IOperationHistory operationHistory = OperationHistoryFactory.getOperationHistory();
fStartingUndoOperation = operationHistory.getUndoOperation(undoContext);
}
}
// Find occurrences
List<OccurrencesResponseItem> occurrences = tsFile.occurrences(offset).get(1000, TimeUnit.MILLISECONDS);
// Create Eclipse linked position from the occurrences list.
int start, length;
for (int i = 0; i < occurrences.size(); i++) {
OccurrencesResponseItem item = occurrences.get(i);
start = tsFile.getPosition(item.getStart());
length = tsFile.getPosition(item.getEnd()) - start;
LinkedPosition linkedPosition = new LinkedPosition(document, start, length, i);
if (i == 0) {
fOriginalName = document.get(start, length);
fNamePosition = linkedPosition;
}
fLinkedPositionGroup.addPosition(linkedPosition);
}
fLinkedModeModel = new LinkedModeModel();
fLinkedModeModel.addGroup(fLinkedPositionGroup);
fLinkedModeModel.forceInstall();
fLinkedModeModel.addLinkingListener(new EditorHighlightingSynchronizer(fEditor));
fLinkedModeModel.addLinkingListener(new EditorSynchronizer());
LinkedModeUI ui = new EditorLinkedModeUI(fLinkedModeModel, viewer);
ui.setExitPosition(viewer, offset, 0, Integer.MAX_VALUE);
ui.setExitPolicy(new ExitPolicy(document));
ui.enter();
viewer.setSelectedRange(fOriginalSelection.x, fOriginalSelection.y); // by
// default,
// full
// word
// is
// selected;
// restore
// original
// selection
if (viewer instanceof IEditingSupportRegistry) {
IEditingSupportRegistry registry = (IEditingSupportRegistry) viewer;
registry.register(fFocusEditingSupport);
}
openSecondaryPopup();
// startAnimation();
fgActiveLinkedMode = this;
} catch (Exception e) {
JSDTTypeScriptUIPlugin.log(e);
}
}
/**
* AbstractFoldingEditor
*/
public AbstractFoldingEditor()
{
// Hack because we cannot override org.eclipse.ui.texteditor.AbstractTextEditor.getSelectionChangedListener().
ISelectionChangedListener selectionChangedListener = new ISelectionChangedListener()
{
private Runnable fRunnable = new Runnable()
{
public void run()
{
ISourceViewer sourceViewer = getSourceViewer();
// check whether editor has not been disposed yet
if (sourceViewer != null && sourceViewer.getDocument() != null)
{
updateSelectionDependentActions();
}
}
};
private Display fDisplay;
public void selectionChanged(SelectionChangedEvent event)
{
Display current = Display.getCurrent();
if (current != null)
{
// Don't execute asynchronously if we're in a thread that has a display.
// Fix for: https://jira.appcelerator.org/browse/APSTUD-3061 (the rationale
// is that the actions were not being enabled because they were previously
// updated in an async call).
// Ideally, if this is really the root case of that issue, this code should
// be provided back to Eclipse.org as part of the bug:
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=368354
// but just patching getSelectionChangedListener() properly.
fRunnable.run();
}
else
{
if (fDisplay == null)
{
fDisplay = getSite().getShell().getDisplay();
}
fDisplay.asyncExec(fRunnable);
}
handleCursorPositionChanged();
}
};
try
{
// Hack to change private field fSelectionChangedListener
Field field = AbstractTextEditor.class.getDeclaredField("fSelectionChangedListener"); //$NON-NLS-1$
field.setAccessible(true);
field.set(this, selectionChangedListener);
}
catch (Exception e)
{
// Should not really happen, but let's not fail if it happens.
IdeLog.logError(CommonEditorPlugin.getDefault(), e);
}
}