Search in sources :

Example 6 with ISourceViewer

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);
}
Also used : Point(org.eclipse.swt.graphics.Point) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer)

Example 7 with ISourceViewer

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;
}
Also used : GridLayout(org.eclipse.swt.layout.GridLayout) Composite(org.eclipse.swt.widgets.Composite) StackLayout(org.eclipse.swt.custom.StackLayout) GridData(org.eclipse.swt.layout.GridData) FillLayout(org.eclipse.swt.layout.FillLayout) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer)

Example 8 with ISourceViewer

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;
}
Also used : YEditSourceViewerConfiguration(org.dadacoalition.yedit.editor.YEditSourceViewerConfiguration) SourceViewerConfiguration(org.eclipse.jface.text.source.SourceViewerConfiguration) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) SelectionEvent(org.eclipse.swt.events.SelectionEvent) IInformationPresenter(org.eclipse.jface.text.information.IInformationPresenter) ProjectionViewer(org.eclipse.jface.text.source.projection.ProjectionViewer) IFocusService(org.eclipse.ui.swt.IFocusService) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer)

Aggregations

ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)8 IDocument (org.eclipse.jface.text.IDocument)2 StyledText (org.eclipse.swt.custom.StyledText)2 FillLayout (org.eclipse.swt.layout.FillLayout)2 JsonEditor (com.reprezen.swagedit.core.editor.JsonEditor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Hashtable (java.util.Hashtable)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 BuildErrorDetailsHandler (org.bndtools.build.api.BuildErrorDetailsHandler)1 YEditSourceViewerConfiguration (org.dadacoalition.yedit.editor.YEditSourceViewerConfiguration)1 ResourceChangeEvent (org.eclipse.core.internal.events.ResourceChangeEvent)1 IMarker (org.eclipse.core.resources.IMarker)1 IResourceChangeEvent (org.eclipse.core.resources.IResourceChangeEvent)1 IWorkspaceRunnable (org.eclipse.core.resources.IWorkspaceRunnable)1 CoreException (org.eclipse.core.runtime.CoreException)1 JavaSourceViewer (org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer)1 SimpleJavaSourceViewerConfiguration (org.eclipse.jdt.internal.ui.text.SimpleJavaSourceViewerConfiguration)1 JavaTextTools (org.eclipse.jdt.ui.text.JavaTextTools)1 Action (org.eclipse.jface.action.Action)1