blob: 4a9757ddfe93705b334fd968f49dff36a8812ba9 [file] [log] [blame]
/*=============================================================================#
# 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.wikitext.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.wikitext.r.core.source.WikitextRweaveDocumentConstants;
import org.eclipse.statet.redocs.wikitext.r.ui.editors.WikidocRweaveDocEditor;
/**
* Page for {@link WikidocRweaveDocEditor} / {@link TemplatesView}
*
* At moment only for R templates.
*/
public class WikidocRweaveEditorTemplatesPage extends AbstractEditorTemplatesPage {
private SourceEditorViewerConfigurator rPreviewConfigurator;
public WikidocRweaveEditorTemplatesPage(final WikidocRweaveDocEditor 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, WikitextRweaveDocumentConstants.WIKIDOC_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 );
}
}