| /*=============================================================================# |
| # Copyright (c) 2009, 2018 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.docmlet.tex.core.refactoring; |
| |
| import org.eclipse.jface.text.AbstractDocument; |
| import org.eclipse.jface.text.BadLocationException; |
| import org.eclipse.jface.text.BadPartitioningException; |
| import org.eclipse.jface.text.ITypedRegion; |
| |
| import org.eclipse.statet.ecommons.text.BasicHeuristicTokenScanner; |
| |
| import org.eclipse.statet.docmlet.tex.core.TexCore; |
| import org.eclipse.statet.docmlet.tex.core.ast.Environment; |
| import org.eclipse.statet.docmlet.tex.core.ast.TexAst; |
| import org.eclipse.statet.docmlet.tex.core.ast.TexAstNode; |
| import org.eclipse.statet.docmlet.tex.core.model.ITexSourceElement; |
| import org.eclipse.statet.docmlet.tex.core.model.TexModel; |
| import org.eclipse.statet.docmlet.tex.core.source.ITexDocumentConstants; |
| import org.eclipse.statet.docmlet.tex.core.source.LtxHeuristicTokenScanner; |
| import org.eclipse.statet.ltk.model.core.ElementSet; |
| import org.eclipse.statet.ltk.model.core.elements.IModelElement; |
| import org.eclipse.statet.ltk.model.core.elements.ISourceElement; |
| import org.eclipse.statet.ltk.model.core.elements.ISourceUnit; |
| import org.eclipse.statet.ltk.refactoring.core.RefactoringAdapter; |
| import org.eclipse.statet.ltk.refactoring.core.RefactoringDestination; |
| import org.eclipse.statet.ltk.refactoring.core.RefactoringDestination.Position; |
| |
| |
| public class LtxRefactoringAdapter extends RefactoringAdapter { |
| |
| |
| public LtxRefactoringAdapter() { |
| super(TexModel.LTX_TYPE_ID); |
| } |
| |
| |
| @Override |
| public String getPluginIdentifier() { |
| return TexCore.BUNDLE_ID; |
| } |
| |
| @Override |
| public LtxHeuristicTokenScanner getScanner(final ISourceUnit su) { |
| return LtxHeuristicTokenScanner.create(su.getDocumentContentInfo()); |
| } |
| |
| @Override |
| public boolean canInsert(final ElementSet elements, final ISourceElement to, |
| final RefactoringDestination.Position pos) { |
| if (super.canInsert(elements, to, pos)) { |
| if ((to.getElementType() & IModelElement.MASK_C2) == ITexSourceElement.C2_PREAMBLE) { |
| for (final IModelElement element : elements.getModelElements()) { |
| if ((element.getElementType() & IModelElement.MASK_C2) == ITexSourceElement.C2_SECTIONING) { |
| return false; |
| } |
| } |
| } |
| if ((to.getElementType() & IModelElement.MASK_C1) == IModelElement.C1_EMBEDDED |
| && pos == RefactoringDestination.Position.INTO ) { |
| return false; |
| } |
| return true; |
| } |
| return false; |
| } |
| |
| @Override |
| public boolean isCommentContent(final ITypedRegion partition) { |
| return (partition != null |
| && (partition.getType() == ITexDocumentConstants.LTX_COMMENT_CONTENT_TYPE |
| || partition.getType() == ITexDocumentConstants.LTX_MATHCOMMENT_CONTENT_TYPE )); |
| } |
| |
| @Override |
| protected int getInsertionOffset(final AbstractDocument document, |
| final ISourceElement element, final Position pos, |
| final BasicHeuristicTokenScanner scanner) |
| throws BadLocationException, BadPartitioningException { |
| if ((element.getElementType() & IModelElement.MASK_C2) == ITexSourceElement.C2_SOURCE_FILE |
| && pos == Position.INTO) { |
| final TexAstNode astNode= element.getAdapter(TexAstNode.class); |
| if (astNode != null) { |
| final Environment environment= TexAst.getDocumentNode(astNode); |
| if (environment.getEndNode() != null) { |
| return environment.getEndNode().getStartOffset(); |
| } |
| } |
| } |
| return super.getInsertionOffset(document, element, pos, scanner); |
| } |
| |
| } |