mirror of
https://github.com/sigmasternchen/xtext-core
synced 2025-03-15 08:18:55 +00:00
[rename] applied feedback
This commit is contained in:
parent
afe2878506
commit
95849c89c4
2 changed files with 43 additions and 23 deletions
|
@ -35,6 +35,7 @@ import org.eclipse.xtext.ide.refactoring.RenameContext
|
|||
import org.eclipse.xtext.ide.serializer.IChangeSerializer
|
||||
import org.eclipse.xtext.ide.server.Document
|
||||
import org.eclipse.xtext.ide.server.ILanguageServerAccess
|
||||
import org.eclipse.xtext.linking.impl.LinkingHelper
|
||||
import org.eclipse.xtext.nodemodel.ILeafNode
|
||||
import org.eclipse.xtext.nodemodel.util.NodeModelUtils
|
||||
import org.eclipse.xtext.parsetree.reconstr.impl.TokenUtil
|
||||
|
@ -68,6 +69,8 @@ class RenameService2 implements IRenameService2 {
|
|||
|
||||
@Inject IValueConverterService valueConverterService
|
||||
|
||||
@Inject LinkingHelper linkingHelper
|
||||
|
||||
Function<EObject, String> attributeResolver = SimpleAttributeResolver.newResolver(String, 'name')
|
||||
|
||||
override rename(Options options) {
|
||||
|
@ -200,9 +203,9 @@ class RenameService2 implements IRenameService2 {
|
|||
if (element !== null && !element.eIsProxy) {
|
||||
val leaf = NodeModelUtils.findLeafNodeAtOffset(rootNode, candidateOffset)
|
||||
if (leaf !== null && leaf.isIdentifier) {
|
||||
val leafText = getConvertedValue(leaf.grammarElement, leaf)
|
||||
val convertedNameValue = getConvertedValue(leaf.grammarElement, leaf)
|
||||
val elementName = element.elementName
|
||||
if (!leafText.nullOrEmpty && !elementName.nullOrEmpty && leafText == elementName) {
|
||||
if (!convertedNameValue.nullOrEmpty && !elementName.nullOrEmpty && convertedNameValue == elementName) {
|
||||
val start = document.getPosition(leaf.offset)
|
||||
val end = document.getPosition(leaf.endOffset)
|
||||
return Either.forLeft(new Range(start, end))
|
||||
|
@ -223,11 +226,16 @@ class RenameService2 implements IRenameService2 {
|
|||
}
|
||||
|
||||
protected def String getConvertedValue(EObject grammarElement, ILeafNode leaf) {
|
||||
try {
|
||||
switch (grammarElement) {
|
||||
RuleCall: valueConverterService.toValue(leaf.text, grammarElement.rule.name, leaf).toString()
|
||||
CrossReference: getConvertedValue(grammarElement.terminal, leaf)
|
||||
default: leaf.text
|
||||
RuleCall: return valueConverterService.toValue(leaf.text, grammarElement.rule.name, leaf).toString()
|
||||
CrossReference: return linkingHelper.getCrossRefNodeAsString(leaf, true)
|
||||
}
|
||||
} catch (Exception exc) {
|
||||
// The current name text cannot be converted to a value.
|
||||
// Rename could be used to fix this.
|
||||
}
|
||||
return leaf.text
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -50,6 +50,7 @@ import org.eclipse.xtext.ide.server.ILanguageServerAccess;
|
|||
import org.eclipse.xtext.ide.server.rename.ChangeConverter2;
|
||||
import org.eclipse.xtext.ide.server.rename.IRenameService2;
|
||||
import org.eclipse.xtext.ide.server.rename.ServerRefactoringIssueAcceptor;
|
||||
import org.eclipse.xtext.linking.impl.LinkingHelper;
|
||||
import org.eclipse.xtext.nodemodel.ICompositeNode;
|
||||
import org.eclipse.xtext.nodemodel.ILeafNode;
|
||||
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
|
||||
|
@ -92,6 +93,9 @@ public class RenameService2 implements IRenameService2 {
|
|||
@Inject
|
||||
private IValueConverterService valueConverterService;
|
||||
|
||||
@Inject
|
||||
private LinkingHelper linkingHelper;
|
||||
|
||||
private Function<EObject, String> attributeResolver = SimpleAttributeResolver.<EObject, String>newResolver(String.class, "name");
|
||||
|
||||
@Override
|
||||
|
@ -283,9 +287,9 @@ public class RenameService2 implements IRenameService2 {
|
|||
if (((element != null) && (!element.eIsProxy()))) {
|
||||
final ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(rootNode, candidateOffset);
|
||||
if (((leaf != null) && this.isIdentifier(leaf))) {
|
||||
final String leafText = this.getConvertedValue(leaf.getGrammarElement(), leaf);
|
||||
final String convertedNameValue = this.getConvertedValue(leaf.getGrammarElement(), leaf);
|
||||
final String elementName = this.getElementName(element);
|
||||
if ((((!StringExtensions.isNullOrEmpty(leafText)) && (!StringExtensions.isNullOrEmpty(elementName))) && Objects.equal(leafText, elementName))) {
|
||||
if ((((!StringExtensions.isNullOrEmpty(convertedNameValue)) && (!StringExtensions.isNullOrEmpty(elementName))) && Objects.equal(convertedNameValue, elementName))) {
|
||||
final Position start = document.getPosition(leaf.getOffset());
|
||||
final Position end = document.getPosition(leaf.getEndOffset());
|
||||
Range _range = new Range(start, end);
|
||||
|
@ -324,22 +328,25 @@ public class RenameService2 implements IRenameService2 {
|
|||
}
|
||||
|
||||
protected String getConvertedValue(final EObject grammarElement, final ILeafNode leaf) {
|
||||
String _switchResult = null;
|
||||
try {
|
||||
boolean _matched = false;
|
||||
if (grammarElement instanceof RuleCall) {
|
||||
_matched=true;
|
||||
_switchResult = this.valueConverterService.toValue(leaf.getText(), ((RuleCall)grammarElement).getRule().getName(), leaf).toString();
|
||||
return this.valueConverterService.toValue(leaf.getText(), ((RuleCall)grammarElement).getRule().getName(), leaf).toString();
|
||||
}
|
||||
if (!_matched) {
|
||||
if (grammarElement instanceof CrossReference) {
|
||||
_matched=true;
|
||||
_switchResult = this.getConvertedValue(((CrossReference)grammarElement).getTerminal(), leaf);
|
||||
return this.linkingHelper.getCrossRefNodeAsString(leaf, true);
|
||||
}
|
||||
}
|
||||
if (!_matched) {
|
||||
_switchResult = leaf.getText();
|
||||
} catch (final Throwable _t) {
|
||||
if (_t instanceof Exception) {
|
||||
} else {
|
||||
throw Exceptions.sneakyThrow(_t);
|
||||
}
|
||||
return _switchResult;
|
||||
}
|
||||
return leaf.getText();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -436,6 +443,11 @@ public class RenameService2 implements IRenameService2 {
|
|||
return this.valueConverterService;
|
||||
}
|
||||
|
||||
@Pure
|
||||
protected LinkingHelper getLinkingHelper() {
|
||||
return this.linkingHelper;
|
||||
}
|
||||
|
||||
@Pure
|
||||
protected Function<EObject, String> getAttributeResolver() {
|
||||
return this.attributeResolver;
|
||||
|
|
Loading…
Reference in a new issue