use of org.eclipse.jface.text.source.ISourceViewer in project dbeaver by serge-rider.
the class SQLTemplatesPage method insertTemplate.
@Override
public void insertTemplate(Template template, IDocument document) {
if (!sqlEditor.validateEditorInputState())
return;
ISourceViewer contextViewer = sqlEditor.getViewer();
ITextSelection textSelection = (ITextSelection) contextViewer.getSelectionProvider().getSelection();
if (!isValidTemplate(document, template, textSelection.getOffset(), textSelection.getLength()))
return;
beginCompoundChange(contextViewer);
/*
* The Editor checks whether a completion for a word exists before it allows for the template to be
* applied. We pickup the current text at the selection position and replace it with the first char
* of the template name for this to succeed.
* Another advantage by this method is that the template replaces the selected text provided the
* selection by itself is not used in the template pattern.
*/
String savedText;
try {
savedText = document.get(textSelection.getOffset(), textSelection.getLength());
if (savedText.length() == 0) {
String prefix = getIdentifierPart(document, template, textSelection.getOffset(), textSelection.getLength());
if (prefix.length() > 0 && !template.getName().startsWith(prefix)) {
return;
}
if (prefix.length() > 0) {
contextViewer.setSelectedRange(textSelection.getOffset() - prefix.length(), prefix.length());
textSelection = (ITextSelection) contextViewer.getSelectionProvider().getSelection();
}
}
document.replace(textSelection.getOffset(), textSelection.getLength(), template.getName().substring(0, 1));
} catch (BadLocationException e) {
endCompoundChange(contextViewer);
return;
}
//Position position = new Position(textSelection.getOffset() + 1, 0);
Region region = new Region(textSelection.getOffset(), 0);
textSelection = new TextSelection(textSelection.getOffset(), 1);
contextViewer.getSelectionProvider().setSelection(textSelection);
SQLContext context = getContext(document, template, textSelection.getOffset(), textSelection.getLength());
//$NON-NLS-1$
context.setVariable("selection", savedText);
if (context.getKey().length() == 0) {
try {
document.replace(textSelection.getOffset(), 1, savedText);
} catch (BadLocationException e) {
endCompoundChange(contextViewer);
return;
}
}
SQLTemplateCompletionProposal proposal = new SQLTemplateCompletionProposal(template, context, region, null);
sqlEditor.getSite().getPage().activate(sqlEditor);
proposal.apply(sqlEditor.getViewer(), ' ', 0, region.getOffset());
final Point selection = proposal.getSelection(document);
if (selection != null) {
sqlEditor.getViewer().setSelectedRange(selection.x, selection.y);
sqlEditor.getViewer().revealRange(selection.x, selection.y);
}
endCompoundChange(contextViewer);
}
use of org.eclipse.jface.text.source.ISourceViewer in project KaiZen-OpenAPI-Editor by RepreZen.
the class JsonEditor method createSourceViewer.
@Override
protected ISourceViewer createSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {
Composite composite = new Composite(parent, SWT.NONE);
GridLayout compositeLayout = new GridLayout(1, false);
compositeLayout.marginHeight = 0;
compositeLayout.marginWidth = 0;
compositeLayout.horizontalSpacing = 0;
compositeLayout.verticalSpacing = 0;
composite.setLayout(compositeLayout);
topPanel = new Composite(composite, SWT.NONE);
topPanel.setLayout(new StackLayout());
topPanel.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
Composite editorComposite = new Composite(composite, SWT.NONE);
editorComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
FillLayout fillLayout = new FillLayout(SWT.VERTICAL);
fillLayout.marginHeight = 0;
fillLayout.marginWidth = 0;
fillLayout.spacing = 0;
editorComposite.setLayout(fillLayout);
ISourceViewer result = doCreateSourceViewer(editorComposite, ruler, styles);
return result;
}
use of org.eclipse.jface.text.source.ISourceViewer in project KaiZen-OpenAPI-Editor by RepreZen.
the class JsonEditor method doCreateSourceViewer.
protected ISourceViewer doCreateSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {
ProjectionViewer viewer = new ProjectionViewer(parent, ruler, getOverviewRuler(), isOverviewRulerVisible(), styles) {
private IInformationPresenter outlinePresenter;
@Override
public void doOperation(int operation) {
if (operation == OpenQuickOutlineHandler.QUICK_OUTLINE && outlinePresenter != null) {
outlinePresenter.showInformation();
return;
}
super.doOperation(operation);
}
@Override
public boolean canDoOperation(int operation) {
if (operation == OpenQuickOutlineHandler.QUICK_OUTLINE && outlinePresenter != null) {
return true;
}
return super.canDoOperation(operation);
}
@Override
public void configure(SourceViewerConfiguration configuration) {
super.configure(configuration);
if (configuration instanceof JsonSourceViewerConfiguration) {
JsonSourceViewerConfiguration c = (JsonSourceViewerConfiguration) configuration;
outlinePresenter = c.getOutlinePresenter(this);
if (outlinePresenter != null) {
outlinePresenter.install(this);
}
}
}
};
IFocusService focusService = (IFocusService) PlatformUI.getWorkbench().getService(IFocusService.class);
if (focusService != null) {
focusService.addFocusTracker(viewer.getTextWidget(), "com.reprezen.swagedit.editor.sourceViewer");
}
viewer.getTextWidget().addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// detectOutlineLocationChanged();
}
});
viewer.getTextWidget().setData(ISourceViewer.class.getName(), viewer);
getSourceViewerDecorationSupport(viewer);
return viewer;
}
Aggregations