blob: 418544a97d141bd4f2cba83c0109766678d45a94 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2008, 2021 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 static org.eclipse.statet.jcommons.lang.ObjectUtils.nonNullAssert;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
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.lang.NonNullByDefault;
import org.eclipse.statet.jcommons.lang.Nullable;
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.RAstNode;
import org.eclipse.statet.r.core.rsource.ast.RAsts;
@NonNullByDefault
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 access;
private final int mode;
private final @Nullable TextRegion region;
public RLinkedNamesAssistProposal(final int mode,
final AssistInvocationContext invocationContext, final RElementAccess access) {
super(invocationContext);
this.mode= mode;
this.region= 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();
}
this.access= access;
}
public RLinkedNamesAssistProposal(final int mode,
final AssistInvocationContext invocationContext, final RElementAccess access,
final TextRegion region) {
super(invocationContext);
this.mode= mode;
this.region= nonNullAssert(region);
switch (mode) {
case IN_CHUNK:
init(RUIMessages.Proposal_RenameInChunk_label,
RUIMessages.Proposal_RenameInChunk_description,
89 );
break;
default:
throw new IllegalArgumentException();
}
this.access= access;
}
private TextRegion getChunkRegion() {
if (this.mode != IN_CHUNK) {
throw new RuntimeException();
}
return nonNullAssert(this.region);
}
@Override
protected void collectPositions(final IDocument document, final LinkedPositionGroup group)
throws BadLocationException {
final ImIdentityList<? extends RElementAccess> allAccess= ImCollections.toIdentityList(
this.access.getAllInUnit(false) );
final int current= allAccess.indexOf(this.access);
if (current < 0) {
return;
}
int idx= 0;
{ final RAstNode nameNode= nonNullAssert(this.access.getNameNode());
idx= addPosition(group, document, RAsts.getElementNameRegion(nameNode), idx);
if (idx == 0) {
return;
}
}
if (this.mode == IN_FILE || this.mode == IN_FILE_FOLLOWING) {
for (int i= current + 1; i < allAccess.size(); i++) {
final RAstNode nameNode= allAccess.get(i).getNameNode();
if (nameNode != null) {
idx= addPosition(group, document, RAsts.getElementNameRegion(nameNode), idx);
}
}
}
else if (this.mode == IN_CHUNK) {
final int regionOffset= getChunkRegion().getEndOffset();
for (int i= current + 1; i < allAccess.size(); i++) {
final RAstNode nameNode= allAccess.get(i).getNameNode();
if (nameNode != null) {
if (regionOffset > nameNode.getStartOffset()) {
idx= addPosition(group, document, RAsts.getElementNameRegion(nameNode), idx);
}
else {
break;
}
}
}
}
if (this.mode == IN_FILE || this.mode == IN_FILE_PRECEDING) {
for (int i= 0; i < current; i++) {
final RAstNode nameNode= allAccess.get(i).getNameNode();
if (nameNode != null) {
idx= addPosition(group, document, RAsts.getElementNameRegion(nameNode), idx);
}
}
}
else if (this.mode == IN_CHUNK) {
final int regionOffset= getChunkRegion().getStartOffset();
for (int i= 0; i < current; i++) {
final RAstNode nameNode= allAccess.get(i).getNameNode();
if (nameNode != null) {
if (regionOffset <= nameNode.getStartOffset()) {
idx= addPosition(group, document, RAsts.getElementNameRegion(nameNode), idx);
}
}
}
}
}
}