Java Code Examples for org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable#commitChanges()

The following examples show how to use org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable#commitChanges() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XbaseQuickfixProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
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();
}
 
Example 2
Source File: XtendQuickfixProvider.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void internalDoAddAbstractKeyword(EObject element, IModificationContext context)
		throws BadLocationException {
	if (element instanceof XtendFunction) {
		element = element.eContainer();
	}
	if (element instanceof XtendClass) {
		XtendClass clazz = (XtendClass) element;
		IXtextDocument document = context.getXtextDocument();
		ICompositeNode clazzNode = NodeModelUtils.findActualNodeFor(clazz);
		if (clazzNode == null)
			throw new IllegalStateException("Cannot determine node for clazz " + clazz.getName());
		int offset = -1;
		for (ILeafNode leafNode : clazzNode.getLeafNodes()) {
			if (leafNode.getText().equals("class")) {
				offset = leafNode.getOffset();
				break;
			}
		}
		ReplacingAppendable appendable = appendableFactory.create(document, (XtextResource) clazz.eResource(),
				offset, 0);
		appendable.append("abstract ");
		appendable.commitChanges();
	}
}
 
Example 3
Source File: SARLQuickfixProvider.java    From sarl with Apache License 2.0 6 votes vote down vote up
private void addAbstractKeyword(XtendTypeDeclaration typeDeclaration, IXtextDocument document,
		String declarationKeyword) throws BadLocationException {
	final ICompositeNode clazzNode = NodeModelUtils.findActualNodeFor(typeDeclaration);
	if (clazzNode == null) {
		throw new IllegalStateException("Cannot determine node for the type declaration" //$NON-NLS-1$
				+ typeDeclaration.getName());
	}
	int offset = -1;
	final Iterator<ILeafNode> nodes = clazzNode.getLeafNodes().iterator();
	while (offset == -1 && nodes.hasNext()) {
		final ILeafNode leafNode  = nodes.next();
		if (leafNode.getText().equals(declarationKeyword)) {
			offset = leafNode.getOffset();
		}
	}
	final ReplacingAppendable appendable = this.appendableFactory.create(document,
			(XtextResource) typeDeclaration.eResource(),
			offset, 0);
	appendable.append(getGrammarAccess()
			.getAbstractKeyword())
			.append(" "); //$NON-NLS-1$
	appendable.commitChanges();
}
 
Example 4
Source File: ActionAddModification.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
	final XtendTypeDeclaration container = EcoreUtil2.getContainerOfType(element, XtendTypeDeclaration.class);
	if (container != null) {
		final int insertOffset = getTools().getInsertOffset(container);
		final IXtextDocument document = context.getXtextDocument();
		final int length = getTools().getSpaceSize(document, insertOffset);
		final ReplacingAppendable appendable = getTools().getAppendableFactory().create(document,
				(XtextResource) element.eResource(), insertOffset, length);
		final boolean changeIndentation = container.getMembers().isEmpty();
		if (changeIndentation) {
			appendable.increaseIndentation();
		}
		appendable.newLine();
		appendable.append(
				getTools().getGrammarAccess().getDefKeyword());
		appendable.append(" "); //$NON-NLS-1$
		appendable.append(this.actionName);
		if (changeIndentation) {
			appendable.decreaseIndentation();
		}
		appendable.newLine();
		appendable.commitChanges();
	}
}
 
Example 5
Source File: SuppressWarningsAddModification.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Add SuppressWarnings annotation.
 *
 * @param element the element to receive the annotation.
 * @param context the modification context.
 * @param suppressWarningsAnnotation the type for the suppress warning annotation.
 * @throws Exception if the document cannot be changed.
 */
protected void addAnnotation(EObject element, IModificationContext context,
		JvmType suppressWarningsAnnotation) throws Exception {
	final ICompositeNode node = NodeModelUtils.findActualNodeFor(element);
	if (node != null) {
		final int insertOffset = node.getOffset();
		final IXtextDocument document = context.getXtextDocument();
		final int length = getTools().getSpaceSize(document, insertOffset);
		final ReplacingAppendable appendable = getTools().getAppendableFactory().create(document,
				(XtextResource) element.eResource(), insertOffset, length);
		appendable.append(getTools().getGrammarAccess().getCommercialAtKeyword());
		appendable.append(suppressWarningsAnnotation);
		appendable.append("(\""); //$NON-NLS-1$
		appendable.append(extractId(this.code));
		appendable.append("\")"); //$NON-NLS-1$
		appendable.newLine();
		appendable.commitChanges();
	}
}
 
Example 6
Source File: SuppressWarningsAddModification.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Add SuppressWarnings annotation.
 *
 * @param element the element to receive the annotation.
 * @param annotation the suppress-warning annotation.
 * @param context the modification context.
 * @throws Exception if the document cannot be changed.
 */
protected void addAnnotation(EObject element, XAnnotation annotation,
		IModificationContext context) throws Exception {
	final ICompositeNode node = NodeModelUtils.findActualNodeFor(annotation);
	if (node != null) {
		final IXtextDocument document = context.getXtextDocument();
		final int startOffset = node.getOffset();
		final int parameterOffset = getTools().getOffsetForPattern(document, startOffset,
				"@\\s*" + toPattern(annotation.getAnnotationType().getQualifiedName()) //$NON-NLS-1$
				+ "\\s*\\(\\s*"); //$NON-NLS-1$
		final int insertOffset;
		if (document.getChar(parameterOffset) == '{') {
			insertOffset = parameterOffset + getTools().getSpaceSize(document, parameterOffset + 1) + 1;
		} else {
			insertOffset = parameterOffset;
		}
		final ReplacingAppendable appendable = getTools().getAppendableFactory().create(document,
				(XtextResource) element.eResource(), insertOffset, 0);
		appendable.append("\""); //$NON-NLS-1$
		appendable.append(extractId(this.code));
		appendable.append("\", "); //$NON-NLS-1$
		appendable.commitChanges();
	}
}
 
Example 7
Source File: XbaseQuickfixProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
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();
}
 
Example 8
Source File: ReturnTypeAddModification.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
	final XtendExecutable xtendExecutable = EcoreUtil2.getContainerOfType(element, XtendExecutable.class);
	final int insertPosition;
	if (xtendExecutable.getExpression() == null) {
		final ICompositeNode functionNode = NodeModelUtils.findActualNodeFor(xtendExecutable);
		if (functionNode == null) {
			throw new IllegalStateException("functionNode may not be null"); //$NON-NLS-1$
		}
		insertPosition = functionNode.getEndOffset();
	} else {
		final ICompositeNode expressionNode = NodeModelUtils.findActualNodeFor(xtendExecutable.getExpression());
		if (expressionNode == null) {
			throw new IllegalStateException("expressionNode may not be null"); //$NON-NLS-1$
		}
		insertPosition = expressionNode.getOffset();
	}
	final ReplacingAppendable appendable = getTools().getAppendableFactory().create(context.getXtextDocument(),
			(XtextResource) xtendExecutable.eResource(), insertPosition, 0);
	if (xtendExecutable.getExpression() == null) {
		appendable.append(" "); //$NON-NLS-1$
	}
	appendable.append(getTools().getGrammarAccess().getColonKeyword());
	appendable.append(" "); //$NON-NLS-1$
	appendable.append(this.expectedType);
	if (xtendExecutable.getExpression() != null) {
		appendable.append(" "); //$NON-NLS-1$
	}
	appendable.commitChanges();
}
 
Example 9
Source File: MissedMethodAddModification.java    From sarl with Apache License 2.0 4 votes vote down vote up
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
	final XtendTypeDeclaration container = (XtendTypeDeclaration) element;
	final IXtextDocument document = context.getXtextDocument();
	final SARLQuickfixProvider tools = getTools();

	final JvmDeclaredType declaringType = (JvmDeclaredType) this.associations.getPrimaryJvmElement(container);

	final int insertOffset = tools.getInsertOffset(container);
	final int length = tools.getSpaceSize(document, insertOffset);
	final OptionalParameters options = new OptionalParameters();
	options.isJava = false;
	options.ensureEmptyLinesAround = false;
	options.baseIndentationLevel = 1;
	final ReplacingAppendable appendable = tools.getAppendableFactory().create(document,
			(XtextResource) container.eResource(), insertOffset, length, options);
	// Compute the type parameters' mapping
	final Map<String, JvmTypeReference> typeParameterMap = buildTypeParameterMapping(container);

	for (final JvmOperation operation : tools.getJvmOperationsFromURIs(container, this.operationUris)) {
		if (this.annotationUtils.findAnnotation(operation, SyntheticMember.class.getName()) == null
				&& !isGeneratedOperation(operation)) {

			appendable.newLine().newLine();

			final XtendMethodBuilder builder = this.methodBuilder.get();
			final SarlMethodBuilder sarlBuilder = (builder instanceof SarlMethodBuilder) ? (SarlMethodBuilder) builder : null;

			builder.setContext(declaringType);
			builder.setOwner(declaringType);
			builder.setMethodName(operation.getSimpleName());

			builder.setStaticFlag(false);
			builder.setOverrideFlag(!getTools().isIgnorable(IssueCodes.MISSING_OVERRIDE));
			builder.setAbstractFlag(false);

			builder.setBodyGenerator(null);

			builder.setVisibility(operation.getVisibility());
			builder.setTypeParameters(cloneTypeParameters(operation, declaringType));

			final QualifiedActionName qualifiedActionName = this.actionPrototypeProvider.createQualifiedActionName(
					declaringType,
					operation.getSimpleName());
			final InferredPrototype prototype = this.actionPrototypeProvider.createPrototypeFromJvmModel(
					// TODO More general context?
					this.actionPrototypeProvider.createContext(),
					qualifiedActionName,
					operation.isVarArgs(),
					operation.getParameters());
			final FormalParameterProvider formalParameters = prototype.getFormalParameters();

			int i = 0;
			for (final JvmFormalParameter parameter : operation.getParameters()) {
				final SarlParameterBuilder paramBuilder = (SarlParameterBuilder) builder.newParameterBuilder();
				paramBuilder.setName(parameter.getSimpleName());
				paramBuilder.setType(cloneTypeReference(parameter.getParameterType(), typeParameterMap));
				if (formalParameters.hasFormalParameterDefaultValue(i)) {
					final String defaultValue = formalParameters.getFormalParameterDefaultValueString(i);
					if (defaultValue != null) {
						paramBuilder.setDefaultValue(defaultValue);
					}
				}
				++i;
			}
			builder.setVarArgsFlag(operation.isVarArgs());

			builder.setReturnType(cloneTypeReference(operation.getReturnType(), typeParameterMap));

			builder.setExceptions(cloneTypeReferences(operation.getExceptions(), typeParameterMap));

			if (sarlBuilder != null) {
				final JvmAnnotationReference firedEvents = this.annotationUtils.findAnnotation(operation, FiredEvent.class.getName());
				if (firedEvents != null) {
					final List<JvmTypeReference> events = this.annotationUtils.findTypeValues(firedEvents);
					if (events != null) {
						sarlBuilder.setFires(cloneTypeReferences(events, typeParameterMap));
					}
				}
			}

			builder.build(appendable);
		}
	}
	appendable.newLine().decreaseIndentation().newLine();

	appendable.commitChanges();
}