use of org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable in project xtext-eclipse by eclipse.
the class JavaTypeQuickfixes method createImportProposals.
@SuppressWarnings("restriction")
protected void createImportProposals(final JvmDeclaredType contextType, final Issue issue, String typeName, IJavaSearchScope searchScope, final IssueResolutionAcceptor acceptor) throws JavaModelException {
if (contextType != null) {
final IVisibilityHelper visibilityHelper = getVisibilityHelper(contextType);
final Pair<String, String> packageAndType = typeNameGuesser.guessPackageAndTypeName(contextType, typeName);
final String wantedPackageName = packageAndType.getFirst();
org.eclipse.jdt.internal.core.search.BasicSearchEngine searchEngine = new org.eclipse.jdt.internal.core.search.BasicSearchEngine();
final char[] wantedPackageChars = (isEmpty(wantedPackageName)) ? null : wantedPackageName.toCharArray();
final String wantedTypeName = packageAndType.getSecond();
searchEngine.searchAllTypeNames(wantedPackageChars, SearchPattern.R_EXACT_MATCH, wantedTypeName.toCharArray(), SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE, IJavaSearchConstants.TYPE, searchScope, new org.eclipse.jdt.internal.core.search.IRestrictedAccessTypeRequestor() {
@Override
public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path, org.eclipse.jdt.internal.compiler.env.AccessRestriction access) {
final String qualifiedTypeName = getQualifiedTypeName(packageName, enclosingTypeNames, simpleTypeName);
if (access == null || (access.getProblemId() != IProblem.ForbiddenReference && !access.ignoreIfBetter())) {
JvmType importType = services.getTypeReferences().findDeclaredType(qualifiedTypeName, contextType);
if (importType instanceof JvmDeclaredType && visibilityHelper.isVisible((JvmDeclaredType) importType)) {
StringBuilder label = new StringBuilder("Import '");
label.append(simpleTypeName);
label.append("' (");
label.append(packageName);
if (enclosingTypeNames.length > 0) {
for (char[] enclosingTypeName : enclosingTypeNames) {
label.append(".");
label.append(enclosingTypeName);
}
}
label.append(")");
acceptor.accept(issue, label.toString(), label.toString(), "impc_obj.gif", new ISemanticModification() {
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) element.eResource(), 0, 0);
appendable.append(services.getTypeReferences().findDeclaredType(qualifiedTypeName, element));
appendable.insertNewImports();
}
}, jdtTypeRelevance.getRelevance(qualifiedTypeName, wantedTypeName) + 100);
}
}
}
}, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, new NullProgressMonitor());
}
}
use of org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable in project xtext-eclipse by eclipse.
the class XbaseQuickfixProvider method fixIncompleteCasesOnEnum.
@Fix(IssueCodes.INCOMPLETE_CASES_ON_ENUM)
public void fixIncompleteCasesOnEnum(final Issue issue, IssueResolutionAcceptor acceptor) {
acceptor.accept(issue, "Add 'default' case", "Add 'default' case", null, new ISemanticModification() {
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
XSwitchExpression switchExpression = EcoreUtil2.getContainerOfType(element, XSwitchExpression.class);
if (switchExpression == null) {
return;
}
int insertOffset = getInsertOffset(switchExpression);
IXtextDocument document = context.getXtextDocument();
ReplacingAppendable appendable = appendableFactory.create(document, (XtextResource) element.eResource(), insertOffset, 0);
if (switchExpression.getCases().isEmpty()) {
appendable.increaseIndentation();
}
appendable.newLine();
appendable.append("default: {");
appendable.newLine().append("}");
appendable.commitChanges();
}
});
acceptor.accept(issue, "Add missing cases", "Add missing cases", null, new ISemanticModification() {
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
XSwitchExpression switchExpression = EcoreUtil2.getContainerOfType(element, XSwitchExpression.class);
if (switchExpression == null) {
return;
}
int insertOffset = getInsertOffset(switchExpression);
IXtextDocument document = context.getXtextDocument();
ReplacingAppendable appendable = appendableFactory.create(document, (XtextResource) element.eResource(), insertOffset, 0);
if (switchExpression.getCases().isEmpty()) {
appendable.increaseIndentation();
}
for (String expectedEnumerationLiteral : issue.getData()) {
appendable.newLine().append("case ").append(expectedEnumerationLiteral).append(": {");
appendable.newLine().append("}");
}
appendable.commitChanges();
}
});
}
use of org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable in project xtext-eclipse by eclipse.
the class XbaseQuickfixProvider method addTypeCastToExplicitReceiver.
private void addTypeCastToExplicitReceiver(XAbstractFeatureCall featureCall, IModificationContext context, JvmType declaringType, EReference structuralFeature) throws BadLocationException {
List<INode> nodes = NodeModelUtils.findNodesForFeature(featureCall, structuralFeature);
if (nodes.isEmpty())
return;
INode firstNode = IterableExtensions.head(nodes);
INode lastNode = IterableExtensions.last(nodes);
int offset = firstNode.getOffset();
int length = lastNode.getEndOffset() - offset;
ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) featureCall.eResource(), offset, length);
appendable.append("(");
ListIterator<INode> nodeIter = nodes.listIterator();
while (nodeIter.hasNext()) {
String text = nodeIter.next().getText();
if (nodeIter.previousIndex() == 0)
appendable.append(Strings.removeLeadingWhitespace(text));
else if (nodeIter.nextIndex() == nodes.size())
appendable.append(Strings.trimTrailingLineBreak(text));
else
appendable.append(text);
}
appendable.append(" as ");
appendable.append(declaringType);
appendable.append(")");
appendable.commitChanges();
}
use of org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable in project xtext-eclipse by eclipse.
the class XbaseQuickfixProvider method addTypeCastToImplicitReceiver.
private void addTypeCastToImplicitReceiver(XFeatureCall featureCall, IModificationContext context, JvmType declaringType) throws BadLocationException {
String receiver;
if (featureCall.getImplicitReceiver() instanceof XAbstractFeatureCall)
receiver = ((XAbstractFeatureCall) featureCall.getImplicitReceiver()).getFeature().getSimpleName();
else
return;
List<INode> nodes = NodeModelUtils.findNodesForFeature(featureCall, XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE);
if (nodes.isEmpty())
return;
INode firstNode = IterableExtensions.head(nodes);
int offset = firstNode.getOffset();
ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) featureCall.eResource(), offset, 0);
appendable.append("(");
appendable.append(receiver);
appendable.append(" as ");
appendable.append(declaringType);
appendable.append(").");
appendable.commitChanges();
}
use of org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable in project xtext-xtend by eclipse.
the class CreateMemberQuickfixes method newLocalVariableQuickfix.
protected void newLocalVariableQuickfix(final String variableName, XAbstractFeatureCall call, Issue issue, IssueResolutionAcceptor issueResolutionAcceptor) {
LightweightTypeReference variableType = getNewMemberType(call);
final StringBuilderBasedAppendable localVarDescriptionBuilder = new StringBuilderBasedAppendable();
localVarDescriptionBuilder.append("...").newLine();
final String defaultValueLiteral = getDefaultValueLiteral(variableType);
localVarDescriptionBuilder.append("val ").append(variableName).append(" = ").append(defaultValueLiteral);
localVarDescriptionBuilder.newLine().append("...");
issueResolutionAcceptor.accept(issue, "Create local variable '" + variableName + "'", localVarDescriptionBuilder.toString(), "fix_local_var.png", new SemanticModificationWrapper(issue.getUriToProblem(), new ISemanticModification() {
@Override
public void apply(/* @Nullable */
final EObject element, /* @Nullable */
final IModificationContext context) throws Exception {
if (element != null) {
XtendMember xtendMember = EcoreUtil2.getContainerOfType(element, XtendMember.class);
if (xtendMember != null) {
int offset = getFirstOffsetOfKeyword(xtendMember, "{");
IXtextDocument xtextDocument = context.getXtextDocument();
if (offset != -1 && xtextDocument != null) {
final ReplacingAppendable appendable = appendableFactory.create(xtextDocument, (XtextResource) element.eResource(), offset, 0, new OptionalParameters() {
{
baseIndentationLevel = 1;
}
});
appendable.increaseIndentation().newLine().append("val ").append(variableName).append(" = ").append(defaultValueLiteral);
appendable.commitChanges();
}
}
}
}
}));
}
Aggregations