| /*=============================================================================# |
| # Copyright (c) 2008, 2019 Stephan Wahlbrink and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| # which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| # |
| # Contributors: |
| # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.internal.redocs.tex.r.ui.editors; |
| |
| import org.eclipse.jface.preference.IPreferenceStore; |
| import org.eclipse.jface.text.BadLocationException; |
| import org.eclipse.jface.text.IDocument; |
| import org.eclipse.jface.text.TextUtilities; |
| import org.eclipse.jface.text.source.ISourceViewer; |
| import org.eclipse.jface.text.templates.DocumentTemplateContext; |
| import org.eclipse.jface.text.templates.Template; |
| import org.eclipse.jface.text.templates.TemplateContextType; |
| import org.eclipse.ui.texteditor.templates.TemplatesView; |
| |
| import org.eclipse.statet.ecommons.templates.TemplateVariableProcessor; |
| |
| import org.eclipse.statet.internal.r.ui.RUIPlugin; |
| import org.eclipse.statet.ltk.ui.sourceediting.SourceEditorViewerConfigurator; |
| import org.eclipse.statet.ltk.ui.templates.config.AbstractEditorTemplatesPage; |
| import org.eclipse.statet.r.core.RCore; |
| import org.eclipse.statet.r.core.source.IRDocumentConstants; |
| import org.eclipse.statet.r.ui.editors.templates.REditorContext; |
| import org.eclipse.statet.r.ui.editors.templates.REditorTemplateContextType; |
| import org.eclipse.statet.r.ui.sourceediting.RTemplateSourceViewerConfigurator; |
| import org.eclipse.statet.redocs.tex.r.core.source.ITexRweaveDocumentConstants; |
| |
| |
| /** |
| * Page for {@link LtxRweaveEditor} / {@link TemplatesView} |
| * |
| * At moment only for R templates. |
| */ |
| public class LtxRweaveEditorTemplatesPage extends AbstractEditorTemplatesPage { |
| |
| |
| private SourceEditorViewerConfigurator rPreviewConfigurator; |
| |
| |
| public LtxRweaveEditorTemplatesPage(final LtxRweaveEditor editor, final ISourceViewer viewer) { |
| super(RUIPlugin.getInstance().getREditorTemplateStore(), editor, viewer); |
| } |
| |
| |
| @Override |
| protected String getPreferencePageId() { |
| return "org.eclipse.statet.r.preferencePages.REditorTemplates"; |
| } |
| |
| @Override |
| protected IPreferenceStore getTemplatePreferenceStore() { |
| return RUIPlugin.getInstance().getPreferenceStore(); |
| } |
| |
| @Override |
| protected String[] getContextTypeIds(final IDocument document, final int offset) { |
| try { |
| final String contentType= TextUtilities.getContentType(document, ITexRweaveDocumentConstants.LTX_R_PARTITIONING, offset, true); |
| if (IRDocumentConstants.R_ANY_CONTENT_CONSTRAINT.matches(contentType)) { |
| return new String[] { REditorTemplateContextType.RCODE_CONTEXTTYPE_ID }; |
| } |
| } |
| catch (final BadLocationException e) { |
| } |
| return new String[0]; |
| } |
| |
| @Override |
| protected DocumentTemplateContext createContext(final IDocument document, final Template template, final int offset, final int length) { |
| final TemplateContextType contextType= getContextTypeRegistry().getContextType(template.getContextTypeId()); |
| if (contextType != null) { |
| return new REditorContext(contextType, document, offset, length, getEditor(), 0); |
| } |
| return null; |
| } |
| |
| @Override |
| protected SourceEditorViewerConfigurator getTemplatePreviewConfig(final Template template, final TemplateVariableProcessor templateProcessor) { |
| if (this.rPreviewConfigurator == null) { |
| this.rPreviewConfigurator= new RTemplateSourceViewerConfigurator( |
| RCore.WORKBENCH_ACCESS, |
| templateProcessor ); |
| } |
| return this.rPreviewConfigurator; |
| } |
| |
| @Override |
| protected SourceEditorViewerConfigurator getTemplateEditConfig(final Template template, final TemplateVariableProcessor templateProcessor) { |
| return new RTemplateSourceViewerConfigurator( |
| RCore.WORKBENCH_ACCESS, |
| templateProcessor ); |
| } |
| |
| } |