Java Code Examples for org.eclipse.xtext.ui.editor.model.edit.IModificationContext#getXtextDocument()

The following examples show how to use org.eclipse.xtext.ui.editor.model.edit.IModificationContext#getXtextDocument() . 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: 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 2
Source File: CheckQuickfixProvider.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void apply(final IModificationContext context) throws BadLocationException {
  final IXtextDocument xtextDocument = context.getXtextDocument();
  xtextDocument.readOnly(new IUnitOfWork.Void<XtextResource>() {
    @Override
    public void process(final XtextResource state) throws Exception { // NOPMD
      final EObject target = EcoreUtil2.getContainerOfType(state.getEObject(issue.getUriToProblem().fragment()), type);
      if (type.isInstance(target)) {
        int offset = NodeModelUtils.findActualNodeFor(target).getOffset();
        int lineOfOffset = xtextDocument.getLineOfOffset(offset);
        int lineOffset = xtextDocument.getLineOffset(lineOfOffset);
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < (offset - lineOffset); i++) {
          buffer.append(' ');
        }
        xtextDocument.replace(offset, 0, NLS.bind(autodocumentation, buffer.toString()));
      }
    }
  });
}
 
Example 3
Source File: SARLQuickfixProvider.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Override
protected void internalDoAddAbstractKeyword(EObject element, IModificationContext context)
		throws BadLocationException {
	EObject container = element;
	if (element instanceof SarlAction) {
		container = element.eContainer();
	}
	XtendTypeDeclaration declaration = null;
	String keyword = null;
	if (container instanceof SarlAgent) {
		declaration = (XtendTypeDeclaration) container;
		keyword = getGrammarAccess().getAgentKeyword();
	} else if (container instanceof SarlBehavior) {
		declaration = (XtendTypeDeclaration) container;
		keyword = getGrammarAccess().getBehaviorKeyword();
	} else if (container instanceof SarlSkill) {
		declaration = (XtendTypeDeclaration) container;
		keyword = getGrammarAccess().getSkillKeyword();
	}
	if (declaration != null && keyword != null) {
		final IXtextDocument document = context.getXtextDocument();
		addAbstractKeyword(declaration, document, keyword);
	} else {
		super.internalDoAddAbstractKeyword(container, context);
	}
}
 
Example 4
Source File: SuperTypeRemoveModification.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 Issue issue = getIssue();
	final SARLQuickfixProvider tools = getTools();
	final IXtextDocument document = context.getXtextDocument();
	final String sep = tools.getGrammarAccess().getCommaKeyword();
	if (!tools.removeToPreviousSeparator(issue, document, sep)) {
		if (!tools.removeToNextSeparator(issue, document, sep)) {
			tools.removeToPreviousKeyword(issue, document,
					tools.getGrammarAccess()
					.getImplementsKeyword(),
					tools.getGrammarAccess()
					.getExtendsKeyword());
		}
	}
}
 
Example 5
Source File: ExtendedTypeRemoveModification.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 IXtextDocument document = context.getXtextDocument();
	final SARLQuickfixProvider tools = getTools();
	final Issue issue = getIssue();
	final String sep = tools.getGrammarAccess().getCommaKeyword();
	switch (this.type) {
	case PRE:
		tools.removeToPreviousSeparator(issue, document, sep);
		break;
	case POST:
		tools.removeToNextSeparator(issue, document, sep);
		break;
	case OTHER:
	default:
		if (!tools.removeToPreviousSeparator(issue, document, sep)) {
			if (!tools.removeToNextSeparator(issue, document, sep)) {
				tools.removeToPreviousKeyword(issue, document,
						tools.getGrammarAccess().getExtendsKeyword());
			}
		}
	}
}
 
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: CapacityReferenceRemoveModification.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 Issue issue = getIssue();
	final SARLQuickfixProvider tools = getTools();
	final IXtextDocument document = context.getXtextDocument();
	final String sep = tools.getGrammarAccess().getCommaKeyword();
	if (!tools.removeToPreviousSeparator(issue, document, sep)) {
		if (!tools.removeToNextSeparator(issue, document, sep)) {
			tools.removeToPreviousKeyword(issue, document,
					tools.getGrammarAccess()
					.getRequiresKeyword(),
					tools.getGrammarAccess()
					.getUsesKeyword());
		}
	}
}
 
Example 8
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 9
Source File: ImplementedTypeRemoveModification.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 IXtextDocument document = context.getXtextDocument();
	final SARLQuickfixProvider tools = getTools();
	final Issue issue = getIssue();
	final String sep = tools.getGrammarAccess().getCommaKeyword();
	switch (this.type) {
	case PRE:
		tools.removeToPreviousSeparator(issue, document, sep);
		break;
	case POST:
		tools.removeToNextSeparator(issue, document, sep);
		break;
	case OTHER:
	default:
		if (!tools.removeToPreviousSeparator(issue, document, sep)) {
			if (!tools.removeToNextSeparator(issue, document, sep)) {
				tools.removeToPreviousKeyword(issue, document,
						tools.getGrammarAccess().getImplementsKeyword());
			}
		}
	}
}
 
Example 10
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 11
Source File: XbaseQuickfixProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Filter quickfixes for types and constructors.
 */
@Override
public void createLinkingIssueResolutions(final Issue issue, final IssueResolutionAcceptor issueResolutionAcceptor) {
	final IModificationContext modificationContext = getModificationContextFactory().createModificationContext(
			issue);
	final IXtextDocument xtextDocument = modificationContext.getXtextDocument();
	if (xtextDocument != null) {
		xtextDocument.tryReadOnly(new CancelableUnitOfWork<Void, XtextResource>() {
			@Override
			public java.lang.Void exec(XtextResource state, CancelIndicator cancelIndicator) throws Exception {
				try {
					EObject target = state.getEObject(issue.getUriToProblem().fragment());
					EReference reference = getUnresolvedEReference(issue, target);
					if (reference != null && reference.getEReferenceType() != null) {
						createLinkingIssueQuickfixes(issue,
								getCancelableAcceptor(issueResolutionAcceptor, cancelIndicator), xtextDocument,
								state, target, reference);
					}
				} catch (WrappedException e) {
					// issue information seems to be out of sync, e.g. there is no
					// EObject with the given fragment
				}
				return null;
			}
		});
	}
}
 
Example 12
Source File: XbaseQuickfixProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void moveUp(ICompositeNode node, ICompositeNode previousNode, IModificationContext context)
		throws BadLocationException {
	IXtextDocument document = context.getXtextDocument();
	String text = node.getText() + previousNode.getText();
	text = text.trim();
	remove(document, node);
	document.replace(previousNode.getOffset(), previousNode.getLength(), text);
}
 
Example 13
Source File: FiredEventRemoveModification.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 Issue issue = getIssue();
	final SARLQuickfixProvider tools = getTools();
	final IXtextDocument document = context.getXtextDocument();
	final String sep = tools.getGrammarAccess().getCommaKeyword();
	if (!tools.removeToPreviousSeparator(issue, document, sep)) {
		if (!tools.removeToNextSeparator(issue, document, sep)) {
			tools.removeToPreviousKeyword(issue, document,
					tools.getGrammarAccess().getFiresKeyword());
		}
	}
}
 
Example 14
Source File: ProtectKeywordModification.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 IXtextDocument document = context.getXtextDocument();
	final Issue issue = getIssue();
	final int keywordOffset = getTools().getOffsetForPattern(document, issue.getOffset(),
			".?" + Pattern.quote(this.oldKeyword)); //$NON-NLS-1$
	if (keywordOffset >= issue.getOffset()) {
		context.getXtextDocument().replace(keywordOffset - this.oldKeyword.length(),
				this.oldKeyword.length(), this.newKeyword);
	}
}
 
Example 15
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();
}
 
Example 16
Source File: BehaviorUnitGuardRemoveModification.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 IXtextDocument document = context.getXtextDocument();
	getTools().removeBetweenSeparators(getIssue(), document, "[", "]"); //$NON-NLS-1$//$NON-NLS-2$
}
 
Example 17
Source File: ReturnTypeReplaceModification.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 IXtextDocument document = context.getXtextDocument();
	document.replace(getIssue().getOffset(), getIssue().getLength(), this.expectedType);
}
 
Example 18
Source File: GamlQuickfixProvider.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void apply(final IModificationContext context) throws BadLocationException {
	final IXtextDocument xtextDocument = context.getXtextDocument();
	final String tmp = text + xtextDocument.get(offset, length) + suffix;
	xtextDocument.replace(offset, length, tmp);
}
 
Example 19
Source File: GamlQuickfixProvider.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void apply(final IModificationContext context) throws BadLocationException {
	final IXtextDocument xtextDocument = context.getXtextDocument();
	xtextDocument.replace(offset, length, text);
}