org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender.Factory.OptionalParameters Java Examples

The following examples show how to use org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender.Factory.OptionalParameters. 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: DocumentRewriter.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public Section newSection(int offset, int length, boolean ensureEmptyLinesAround) {
	OptionalParameters parameters = createOptionalParameters();
	parameters.ensureEmptyLinesAround = ensureEmptyLinesAround;
	Section section = factory.sectionFactory.create(document, resource, offset, length, parameters);
	addSection(section);
	return section;
}
 
Example #2
Source File: DocumentRewriter.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public Section newSection(int offset, int length, int baseIndentationLevel, boolean ensureEmptyLinesAround) {
	OptionalParameters parameters = createOptionalParameters();
	parameters.ensureEmptyLinesAround = ensureEmptyLinesAround;
	parameters.baseIndentationLevel = baseIndentationLevel;
	Section section = factory.sectionFactory.create(document, resource, offset, length, parameters);
	addSection(section);
	return section;
}
 
Example #3
Source File: XtendQuickfixProvider.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Fix(IssueCodes.MISSING_CONSTRUCTOR)
public void addConstuctorFromSuper(final Issue issue, IssueResolutionAcceptor acceptor) {
	if (issue.getData() != null) {
		for(int i=0; i<issue.getData().length; i+=2) {
			final URI constructorURI = URI.createURI(issue.getData()[i]);
			String javaSignature = issue.getData()[i+1];
			String xtendSignature = "new" + javaSignature.substring(javaSignature.indexOf('('));
			acceptor.accept(issue, "Add constructor " + xtendSignature, "Add constructor " + xtendSignature, "fix_indent.gif",
				new ISemanticModification() {
					@Override
					public void apply(EObject element, IModificationContext context) throws Exception {
						XtendClass clazz = (XtendClass) element;
						JvmGenericType inferredType = associations.getInferredType(clazz);
						ResolvedFeatures features = overrideHelper.getResolvedFeatures(inferredType);
						ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) clazz.eResource(),
								insertionOffsets.getNewConstructorInsertOffset(null, clazz), 0, new OptionalParameters() {{ 
									ensureEmptyLinesAround = true;
									baseIndentationLevel = 1;	
								}});
						EObject constructor = clazz.eResource().getResourceSet().getEObject(constructorURI, true);
						if (constructor instanceof JvmConstructor) {
							superMemberImplementor.appendConstructorFromSuper(
									clazz,
									new ResolvedConstructor((JvmConstructor) constructor, features.getType()),
									appendable);
						}
						appendable.commitChanges();
					}
				});
		}
	}
}
 
Example #4
Source File: XtendQuickfixProvider.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void doOverrideMethods(final Issue issue, IssueResolutionAcceptor acceptor, String label, final String[] operationUris) {
	acceptor.accept(issue, label, label, "fix_indent.gif",
			new ISemanticModification() {
				@Override
				public void apply(EObject element, IModificationContext context) throws Exception {
					XtendTypeDeclaration clazz = (XtendTypeDeclaration) element;
					JvmGenericType inferredType = (JvmGenericType) associations.getInferredType(clazz);
					ResolvedFeatures resolvedOperations = overrideHelper.getResolvedFeatures(inferredType);
					IXtextDocument document = context.getXtextDocument();
					final int offset = insertionOffsets.getNewMethodInsertOffset(null, clazz);
					int currentIndentation = appendableFactory.getIndentationLevelAtOffset(offset, document, (XtextResource) clazz.eResource());
					final int indentationToUse = clazz.getMembers().isEmpty() ? currentIndentation + 1 : currentIndentation;
					ReplacingAppendable appendable = appendableFactory.create(document, (XtextResource) clazz.eResource(),
							offset, 0, new OptionalParameters() {{ 
								ensureEmptyLinesAround = true;
								baseIndentationLevel = indentationToUse;	
							}});
					boolean isFirst = true;
					for (String operationUriAsString : operationUris) {
						URI operationURI = URI.createURI(operationUriAsString);
						EObject overridden = clazz.eResource().getResourceSet().getEObject(operationURI, true);
						if (overridden instanceof JvmOperation) {
							if(!isFirst) 
								appendable.newLine().newLine();
							isFirst = false;
							
							superMemberImplementor.appendOverrideFunction(clazz, resolvedOperations.getResolvedOperation((JvmOperation) overridden),
									appendable);
						}
					}
					appendable.commitChanges();
				}
			});
}
 
Example #5
Source File: CreateMemberQuickfixes.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
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();
							}
						}
					}
				}
			}));
}
 
Example #6
Source File: DocumentRewriter.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Section newSection(int offset, int length) {
	OptionalParameters parameters = createOptionalParameters();
	Section section = factory.sectionFactory.create(document, resource, offset, length, parameters);
	addSection(section);
	return section;
}
 
Example #7
Source File: DocumentRewriter.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected OptionalParameters createOptionalParameters() {
	OptionalParameters parameters = new OptionalParameters();
	parameters.importSection = importSection;
	return parameters;
}
 
Example #8
Source File: ImplementMemberFromSuperAssist.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
protected ICompletionProposal createOverrideMethodProposal(XtendTypeDeclaration model, IResolvedExecutable overrideable,
		final ContentAssistContext context, IProposalConflictHelper conflictHelper) {
	IXtextDocument document = context.getDocument();
	XtextResource resource = (XtextResource) model.eResource();
	int offset = context.getReplaceRegion().getOffset();
	int currentIndentation = appendableFactory.getIndentationLevelAtOffset(offset, document, resource);
	final int indentationLevel = currentIndentation == 0 ? 1 : currentIndentation;
	ReplacingAppendable appendable = appendableFactory.create(document, resource, offset, context.getReplaceRegion().getLength(), new OptionalParameters() {{ 
				ensureEmptyLinesAround = true;
				baseIndentationLevel = indentationLevel;	
			}});
	final String simpleName;
	JvmExecutable declaration = overrideable.getDeclaration();
	if (overrideable instanceof IResolvedOperation) {
		implementor.appendOverrideFunction(model, (IResolvedOperation) overrideable, appendable);
		simpleName = overrideable.getDeclaration().getSimpleName();
	} else if (model instanceof XtendClass) {
		implementor.appendConstructorFromSuper((XtendClass) model, (IResolvedConstructor) overrideable, appendable);
		simpleName = "new";
	} else {
		return null;
	}
	String code = appendable.getCode();
	if (!isValidProposal(code.trim(), context, conflictHelper) && !isValidProposal(simpleName, context, conflictHelper))
		return null;
	ImageDescriptor imageDescriptor = images.forOperation(declaration.getVisibility(), adornments.getOverrideAdornment(declaration));
	ImportOrganizingProposal completionProposal = createCompletionProposal(appendable, context.getReplaceRegion(),
			getLabel(overrideable), imageHelper.getImage(imageDescriptor));
	Matcher matcher = bodyExpressionPattern.matcher(code);
	if (matcher.find()) {
		int bodyExpressionLength = matcher.end(1) - matcher.start(1);
		int bodyExpressionStart = matcher.start(1) + appendable.getTotalOffset() - completionProposal.getReplacementOffset();
		if (bodyExpressionLength == 0) {
			completionProposal.setCursorPosition(bodyExpressionStart);
		} else {
			completionProposal.setSelectionStart(completionProposal.getReplacementOffset() + bodyExpressionStart);
			completionProposal.setSelectionLength(bodyExpressionLength);
			completionProposal.setAutoInsertable(false);
			completionProposal.setCursorPosition(bodyExpressionStart + bodyExpressionLength);
			completionProposal.setSimpleLinkedMode(context.getViewer(), '\t');
		}
	}
	completionProposal.setPriority(getPriority(model, declaration, context));
	completionProposal.setMatcher(new PrefixMatcher() {

		@Override
		public boolean isCandidateMatchingPrefix(String name, String prefix) {
			PrefixMatcher delegate = context.getMatcher();
			boolean result = delegate.isCandidateMatchingPrefix(simpleName, prefix);
			return result;
		}
		
	});
	return completionProposal;
}
 
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();
}