blob: 3804d8dbd9d245acd3378b841a5c56811c8df065 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2005, 2017 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.r.ui.editors.templates;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.templates.TemplateContextType;
import org.eclipse.text.edits.DeleteEdit;
import org.eclipse.text.edits.ReplaceEdit;
import org.eclipse.text.edits.TextEdit;
import org.eclipse.statet.ecommons.text.IndentUtil;
import org.eclipse.statet.ecommons.text.IndentUtil.IndentEditAction;
import org.eclipse.statet.internal.r.ui.RUIPlugin;
import org.eclipse.statet.ltk.ui.sourceediting.ISourceEditor;
import org.eclipse.statet.ltk.ui.templates.SourceEditorTemplateContext;
import org.eclipse.statet.r.core.IRCoreAccess;
import org.eclipse.statet.r.core.RCore;
import org.eclipse.statet.r.ui.editors.IRSourceEditor;
public class REditorContext extends SourceEditorTemplateContext {
public REditorContext(final TemplateContextType type, final IDocument document, final int offset, final int length,
final ISourceEditor editor) {
super(type, document, offset, length, editor);
}
protected IRCoreAccess getRCoreAccess() {
final ISourceEditor editor= getEditor();
return (editor instanceof IRSourceEditor) ?
((IRSourceEditor) editor).getRCoreAccess() :
RCore.WORKBENCH_ACCESS;
}
@Override
public void setVariable(final String name, String value) {
if ("selection".equals(name) && value != null && value.length() > 0) { //$NON-NLS-1$
try {
final IDocument valueDoc = new Document(value);
final IndentUtil util = new IndentUtil(valueDoc, getRCoreAccess().getRCodeStyle());
final int column = util.getMultilineIndentColumn(0, valueDoc.getNumberOfLines()-1);
if (column > 0) {
final IndentEditAction action = new IndentEditAction(column) {
@Override
public void doEdit(final int line, final int offset, final int length, final StringBuilder text)
throws BadLocationException {
TextEdit edit;
if (text != null) {
final int position = util.getIndentedOffsetAt(text, column);
edit = new ReplaceEdit(offset, length, text.substring(position, text.length()));
}
else {
final int end = util.getIndentedOffsetAt(line, column);
edit = new DeleteEdit(offset, end-offset);
}
edit.apply(valueDoc, 0);
}
};
util.editInIndent(0, valueDoc.getNumberOfLines()-1, action);
setVariable("indentation", util.createIndentString(column)); //$NON-NLS-1$
value = valueDoc.get();
}
}
catch (final BadLocationException e) {
RUIPlugin.logError(RUIPlugin.INTERNAL_ERROR, "An error occurred while computing indentation variable for R editor templates.", e); //$NON-NLS-1$
}
}
super.setVariable(name, value);
}
}