use of org.eclipse.jface.text.ITextViewer in project egit by eclipse.
the class SpellcheckableMessageArea method configureContextMenu.
private void configureContextMenu() {
final boolean editable = isEditable(sourceViewer);
TextViewerAction cutAction;
TextViewerAction undoAction;
TextViewerAction redoAction;
TextViewerAction pasteAction;
IAction quickFixAction;
if (editable) {
cutAction = createFromActionFactory(ActionFactory.CUT, ITextOperationTarget.CUT);
undoAction = createFromActionFactory(ActionFactory.UNDO, ITextOperationTarget.UNDO);
redoAction = createFromActionFactory(ActionFactory.REDO, ITextOperationTarget.REDO);
pasteAction = createFromActionFactory(ActionFactory.PASTE, ITextOperationTarget.PASTE);
quickFixAction = new QuickfixAction(sourceViewer);
} else {
cutAction = null;
undoAction = null;
redoAction = null;
pasteAction = null;
quickFixAction = null;
}
TextViewerAction copyAction = createFromActionFactory(ActionFactory.COPY, ITextOperationTarget.COPY);
TextViewerAction selectAllAction = createFromActionFactory(ActionFactory.SELECT_ALL, ITextOperationTarget.SELECT_ALL);
final TextEditorPropertyAction showWhitespaceAction = new TextEditorPropertyAction(UIText.SpellcheckableMessageArea_showWhitespace, sourceViewer, AbstractTextEditor.PREFERENCE_SHOW_WHITESPACE_CHARACTERS) {
private IPainter whitespaceCharPainter;
@Override
public void propertyChange(PropertyChangeEvent event) {
String property = event.getProperty();
if (property.equals(getPreferenceKey()) || AbstractTextEditor.PREFERENCE_SHOW_LEADING_SPACES.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_SPACES.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_TRAILING_SPACES.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_LEADING_IDEOGRAPHIC_SPACES.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_IDEOGRAPHIC_SPACES.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_TRAILING_IDEOGRAPHIC_SPACES.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_LEADING_TABS.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_TABS.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_TRAILING_TABS.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_CARRIAGE_RETURN.equals(property) || AbstractTextEditor.PREFERENCE_SHOW_LINE_FEED.equals(property) || AbstractTextEditor.PREFERENCE_WHITESPACE_CHARACTER_ALPHA_VALUE.equals(property)) {
synchronizeWithPreference();
}
}
@Override
protected void toggleState(boolean checked) {
if (checked)
installPainter();
else
uninstallPainter();
}
/**
* Installs the painter on the viewer.
*/
private void installPainter() {
Assert.isTrue(whitespaceCharPainter == null);
ITextViewer v = getTextViewer();
if (v instanceof ITextViewerExtension2) {
IPreferenceStore store = getStore();
whitespaceCharPainter = new WhitespaceCharacterPainter(v, store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_LEADING_SPACES), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_SPACES), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_TRAILING_SPACES), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_LEADING_IDEOGRAPHIC_SPACES), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_IDEOGRAPHIC_SPACES), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_TRAILING_IDEOGRAPHIC_SPACES), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_LEADING_TABS), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_TABS), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_TRAILING_TABS), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_CARRIAGE_RETURN), store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_LINE_FEED), store.getInt(AbstractTextEditor.PREFERENCE_WHITESPACE_CHARACTER_ALPHA_VALUE));
((ITextViewerExtension2) v).addPainter(whitespaceCharPainter);
}
}
/**
* Remove the painter from the viewer.
*/
private void uninstallPainter() {
if (whitespaceCharPainter == null)
return;
ITextViewer v = getTextViewer();
if (v instanceof ITextViewerExtension2)
((ITextViewerExtension2) v).removePainter(whitespaceCharPainter);
whitespaceCharPainter.deactivate(true);
whitespaceCharPainter = null;
}
};
MenuManager contextMenu = new MenuManager();
if (cutAction != null) {
contextMenu.add(cutAction);
}
contextMenu.add(copyAction);
if (pasteAction != null) {
contextMenu.add(pasteAction);
}
contextMenu.add(selectAllAction);
if (undoAction != null) {
contextMenu.add(undoAction);
}
if (redoAction != null) {
contextMenu.add(redoAction);
}
contextMenu.add(new Separator());
contextMenu.add(showWhitespaceAction);
contextMenu.add(new Separator());
if (editable) {
final SubMenuManager quickFixMenu = new SubMenuManager(contextMenu);
quickFixMenu.setVisible(true);
quickFixMenu.addMenuListener(new IMenuListener() {
@Override
public void menuAboutToShow(IMenuManager manager) {
quickFixMenu.removeAll();
addProposals(quickFixMenu);
}
});
}
final StyledText textWidget = getTextWidget();
List<IAction> globalActions = new ArrayList<>();
if (editable) {
globalActions.add(cutAction);
globalActions.add(pasteAction);
globalActions.add(undoAction);
globalActions.add(redoAction);
globalActions.add(quickFixAction);
}
globalActions.add(copyAction);
globalActions.add(selectAllAction);
if (contentAssistAction != null) {
globalActions.add(contentAssistAction);
}
ActionUtils.setGlobalActions(textWidget, globalActions, getHandlerService());
textWidget.setMenu(contextMenu.createContextMenu(textWidget));
sourceViewer.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
if (cutAction != null)
cutAction.update();
copyAction.update();
}
});
if (editable) {
sourceViewer.addTextListener(new ITextListener() {
@Override
public void textChanged(TextEvent event) {
if (undoAction != null)
undoAction.update();
if (redoAction != null)
redoAction.update();
}
});
}
textWidget.addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent disposeEvent) {
showWhitespaceAction.dispose();
}
});
}
use of org.eclipse.jface.text.ITextViewer in project egit by eclipse.
the class LogicalLineNumberRulerColumn method getLogicalLineNumberProvider.
/**
* Retrieves the {@link ILogicalLineNumberProvider} to use. This
* implementation returns a {@link LogicalLineNumberProvider}.
*
* @return the {@link ILogicalLineNumberProvider}
*/
protected ILogicalLineNumberProvider getLogicalLineNumberProvider() {
if (logicalLineNumberProvider == null) {
ITextViewer viewer = getParentRuler().getTextViewer();
Assert.isNotNull(viewer);
logicalLineNumberProvider = new LogicalLineNumberProvider(getSide(), viewer);
}
return logicalLineNumberProvider;
}
use of org.eclipse.jface.text.ITextViewer in project webtools.sourceediting by eclipse.
the class AbstractJSONCompletionProposalComputer method computeCompletionProposals.
@Override
public List computeCompletionProposals(CompletionProposalInvocationContext context, IProgressMonitor monitor) {
ITextViewer textViewer = context.getViewer();
int documentPosition = context.getInvocationOffset();
setErrorMessage(null);
fTextViewer = textViewer;
IndexedRegion treeNode = ContentAssistUtils.getNodeAt(textViewer, documentPosition <= 0 ? 0 : documentPosition - 1);
IJSONNode node = (IJSONNode) treeNode;
while ((node != null) && (node.getNodeType() == Node.TEXT_NODE) && (node.getParentNode() != null)) {
node = node.getParentNode();
}
ContentAssistRequest contentAssistRequest = null;
IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition);
ITextRegion completionRegion = getCompletionRegion(documentPosition, node);
// Fix completion region in case of JSON_OBJECT_CLOSE
if (completionRegion != null && completionRegion.getType() == JSONRegionContexts.JSON_OBJECT_CLOSE && documentPosition > 0) {
completionRegion = getCompletionRegion(documentPosition, node);
}
String matchString = EMPTY;
if (completionRegion != null) {
if (isPairValue(context, node)) {
try {
String nodeText = getNodeText(node);
int colonIndex = nodeText.indexOf(COLON);
int offset = documentPosition - node.getStartOffset();
if (colonIndex >= 0 && offset >= 0) {
String str = nodeText.substring(colonIndex + 1, offset);
// $NON-NLS-1$
str = str.replaceAll(",", BLANK);
matchString = str;
}
} catch (BadLocationException e) {
// ignore
}
} else {
matchString = getMatchString(sdRegion, completionRegion, documentPosition);
}
}
// compute normal proposals
contentAssistRequest = computeCompletionProposals(matchString, completionRegion, (IJSONNode) treeNode, node != null ? node.getParentNode() : null, context);
if (contentAssistRequest == null) {
contentAssistRequest = new ContentAssistRequest((IJSONNode) treeNode, node != null ? node.getParentNode() : null, sdRegion, completionRegion, documentPosition, 0, EMPTY);
setErrorMessage(JSONUIMessages.Content_Assist_not_availab_UI_);
}
/*
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=123892 Only set this
* error message if nothing else was already set
*/
if (contentAssistRequest.getProposals().size() == 0 && getErrorMessage() == null) {
setErrorMessage(JSONUIMessages.Content_Assist_not_availab_UI_);
}
ICompletionProposal[] props = contentAssistRequest.getCompletionProposals();
return (props != null) ? Arrays.asList(props) : new ArrayList(0);
}
use of org.eclipse.jface.text.ITextViewer in project webtools.sourceediting by eclipse.
the class DocumentRegionProcessor method getFoldingStrategy.
/**
* <p>Get the folding strategy for this processor. Retrieved from the
* extended configuration builder. The processor chosen is set by the plugin.</p>
*
* <p>EX:<br />
* <code><extension point="org.eclipse.wst.sse.ui.editorConfiguration"><br />
* <provisionalConfiguration<br />
* type="foldingstrategy"<br />
* class="org.eclipse.wst.xml.ui.internal.projection.XMLFoldingStrategy"<br />
* target="org.eclipse.core.runtime.xml, org.eclipse.wst.xml.core.xmlsource" /><br />
* </extension></code></p>
*
* <p>The type must be equal to <code>AbstractFoldingStrategy.ID</code> (AKA: foldingstrategy)
* and the class must extend <code>org.eclipse.wst.sse.ui.internal.projection.AbstractFoldingStrategy</code>
* and the target must be a structured editor content type ID</p>
*
* @return the requested folding strategy or null if none can be found
*/
protected IReconcilingStrategy getFoldingStrategy() {
if (fFoldingStrategy == null && getDocument() != null) {
String contentTypeId = getContentType(getDocument());
if (contentTypeId == null) {
contentTypeId = IContentTypeManager.CT_TEXT;
}
ITextViewer viewer = getTextViewer();
if (viewer instanceof ProjectionViewer) {
ExtendedConfigurationBuilder builder = ExtendedConfigurationBuilder.getInstance();
IContentType type = Platform.getContentTypeManager().getContentType(contentTypeId);
while (fFoldingStrategy == null && type != null) {
fFoldingStrategy = (AbstractStructuredFoldingStrategy) builder.getConfiguration(AbstractStructuredFoldingStrategy.ID, type.getId());
type = type.getBaseType();
}
if (fFoldingStrategy != null) {
fFoldingStrategy.setViewer((ProjectionViewer) viewer);
fFoldingStrategy.setDocument(getDocument());
}
}
}
return fFoldingStrategy;
}
use of org.eclipse.jface.text.ITextViewer in project webtools.sourceediting by eclipse.
the class JSPELCompletionProposalComputer method computeCompletionProposals.
/**
* @see org.eclipse.jst.jsp.ui.internal.contentassist.JSPJavaCompletionProposalComputer#computeCompletionProposals(org.eclipse.wst.sse.ui.contentassist.CompletionProposalInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
*/
public List computeCompletionProposals(CompletionProposalInvocationContext context, IProgressMonitor monitor) {
ITextViewer viewer = context.getViewer();
int documentPosition = context.getInvocationOffset();
// get results from JSP completion processor
// 3 for the "get" at the beginning of the java proposal
List results = new ArrayList(computeJavaCompletionProposals(viewer, documentPosition, 3));
// get the function proposals for syntax like: ${ fn:| }
IStructuredDocumentRegion flat = ContentAssistUtils.getStructuredDocumentRegion(viewer, documentPosition);
if (flat != null) {
ITextRegion cursorRegion = flat.getRegionAtCharacterOffset(documentPosition);
String elText;
int startOffset;
// else can use flat region
if (cursorRegion instanceof ITextRegionContainer) {
ITextRegionContainer container = (ITextRegionContainer) cursorRegion;
cursorRegion = container.getRegionAtCharacterOffset(documentPosition);
elText = container.getText(cursorRegion);
startOffset = container.getStartOffset(cursorRegion);
} else {
elText = flat.getText(cursorRegion);
startOffset = flat.getStartOffset(cursorRegion);
}
// sanity check that we are actually in EL region
if (cursorRegion.getType() == DOMJSPRegionContexts.JSP_EL_CONTENT) {
String prefix = getPrefix(documentPosition - startOffset, elText);
if (null != prefix) {
List proposals = getFunctionProposals(prefix, viewer, documentPosition);
results.addAll(proposals);
}
}
}
return results;
}
Aggregations