| /*=============================================================================# |
| # 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.r.ui.correction; |
| |
| import org.eclipse.jface.text.BadLocationException; |
| import org.eclipse.jface.text.IDocument; |
| import org.eclipse.jface.text.Position; |
| import org.eclipse.jface.text.link.LinkedPositionGroup; |
| |
| import org.eclipse.statet.jcommons.collections.ImCollections; |
| import org.eclipse.statet.jcommons.collections.ImIdentityList; |
| import org.eclipse.statet.jcommons.text.core.TextRegion; |
| |
| import org.eclipse.statet.internal.r.ui.RUIMessages; |
| import org.eclipse.statet.ltk.ui.sourceediting.assist.AssistInvocationContext; |
| import org.eclipse.statet.ltk.ui.sourceediting.assist.LinkedNamesAssistProposal; |
| import org.eclipse.statet.r.core.model.RElementAccess; |
| import org.eclipse.statet.r.core.rsource.ast.RAst; |
| |
| |
| public class RLinkedNamesAssistProposal extends LinkedNamesAssistProposal { |
| |
| |
| public static final int IN_FILE = 1; |
| public static final int IN_FILE_PRECEDING = 2; |
| public static final int IN_FILE_FOLLOWING = 3; |
| public static final int IN_CHUNK = 4; |
| |
| |
| private final RElementAccess fAccess; |
| private final int fMode; |
| private final TextRegion fRegion; |
| |
| |
| public RLinkedNamesAssistProposal(final int mode, |
| final AssistInvocationContext invocationContext, final RElementAccess access) { |
| super(invocationContext); |
| fMode = mode; |
| fRegion = null; |
| switch (mode) { |
| case IN_FILE: |
| init(RUIMessages.Proposal_RenameInFile_label, |
| RUIMessages.Proposal_RenameInFile_description, |
| 90 ); |
| break; |
| case IN_FILE_PRECEDING: |
| init(RUIMessages.Proposal_RenameInFilePrecending_label, |
| RUIMessages.Proposal_RenameInFilePrecending_description, |
| 85 ); |
| break; |
| case IN_FILE_FOLLOWING: |
| init(RUIMessages.Proposal_RenameInFileFollowing_label, |
| RUIMessages.Proposal_RenameInFileFollowing_description, |
| 84 ); |
| break; |
| default: |
| throw new IllegalArgumentException(); |
| } |
| fAccess = access; |
| } |
| |
| public RLinkedNamesAssistProposal(final int mode, |
| final AssistInvocationContext invocationContext, final RElementAccess access, |
| final TextRegion region) { |
| super(invocationContext); |
| fMode = mode; |
| fRegion = region; |
| switch (mode) { |
| case IN_CHUNK: |
| init(RUIMessages.Proposal_RenameInChunk_label, |
| RUIMessages.Proposal_RenameInChunk_description, |
| 89 ); |
| break; |
| default: |
| throw new IllegalArgumentException(); |
| } |
| fAccess = access; |
| } |
| |
| |
| @Override |
| protected void collectPositions(final IDocument document, final LinkedPositionGroup group) |
| throws BadLocationException { |
| final ImIdentityList<? extends RElementAccess> allAccess= ImCollections.toIdentityList( |
| fAccess.getAllInUnit(false) ); |
| final int current= allAccess.indexOf(fAccess); |
| if (current < 0) { |
| return; |
| } |
| int idx= 0; |
| idx= addPosition(group, document, getPosition(allAccess.get(current)), idx); |
| if (fMode == IN_FILE || fMode == IN_FILE_FOLLOWING) { |
| for (int i= current + 1; i < allAccess.size(); i++) { |
| idx= addPosition(group, document, getPosition(allAccess.get(i)), idx); |
| } |
| } |
| else if (fMode == IN_CHUNK) { |
| final int regionOffset= fRegion.getEndOffset(); |
| for (int i= current + 1; i < allAccess.size(); i++) { |
| if (regionOffset > allAccess.get(i).getNameNode().getStartOffset()) { |
| idx= addPosition(group, document, getPosition(allAccess.get(i)), idx); |
| } |
| else { |
| break; |
| } |
| } |
| } |
| if (fMode == IN_FILE || fMode == IN_FILE_PRECEDING) { |
| for (int i= 0; i < current; i++) { |
| idx= addPosition(group, document, getPosition(allAccess.get(i)), idx); |
| } |
| } |
| else if (fMode == IN_CHUNK) { |
| final int regionOffset= fRegion.getStartOffset(); |
| for (int i= 0; i < current; i++) { |
| if (regionOffset <= allAccess.get(i).getNameNode().getStartOffset()) { |
| idx= addPosition(group, document, getPosition(allAccess.get(i)), idx); |
| } |
| } |
| } |
| } |
| |
| private Position getPosition(final RElementAccess access) { |
| return RAst.getElementNamePosition(access.getNameNode()); |
| } |
| |
| } |