Java Code Examples for org.eclipse.xtext.ui.editor.model.IXtextDocument#modify()

The following examples show how to use org.eclipse.xtext.ui.editor.model.IXtextDocument#modify() . 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: ContentAssistXpectMethod.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private String applyProposal(ICompletionProposal proposal, IXtextDocument document) {
	return document.modify(
			state -> {
				state.setValidationDisabled(false);
				if (!(proposal instanceof TemplateProposal)) {
					proposal.apply(document);
				}
				return document.get();
			});
}
 
Example 2
Source File: ProposalXpectMethod.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private String applyProposal(ICompletionProposal proposal, IXtextDocument document) {
	return document.modify(
			new IUnitOfWork<String, XtextResource>() {
				@Override
				public String exec(XtextResource state) throws Exception {
					state.setValidationDisabled(false);
					if (!(proposal instanceof TemplateProposal)) {
						proposal.apply(document);
					}
					return document.get();
				}
			});
}
 
Example 3
Source File: DocumentImportsOrganizer.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Modifies provided document with provided changes. If list of changes is not meaningful will not touch the
 * document.
 */
private void applyChangesToDocument(final IXtextDocument document, List<IChange> result) {
	if (result != null && !result.isEmpty()) {
		// do the changes really modify anything?
		ChangeAnalysis changeAnalysis = condense(result);
		if (changeAnalysis.noRealChanges) {
			// verify again:
			String del = document.get().substring(changeAnalysis.deletion.getOffset(),
					changeAnalysis.deletion.getOffset() + changeAnalysis.deletion.getLength());
			if (changeAnalysis.newText.getText().equals(del)) {
				return;
			}
		}
		document.modify(
				new IUnitOfWork.Void<XtextResource>() {
					@Override
					public void process(XtextResource state) throws Exception {
						try {
							EcoreUtil.resolveAll(state);
							changeManager.applyAllInSameDocument(changeAnalysis.changes, document);
						} catch (BadLocationException e) {
							LOGGER.error(e);
						}
					}
				});
	}
}
 
Example 4
Source File: XtextDocumentModifyTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testTextualModification() throws Exception {
	final String grammar = "grammar foo.Foo\n" 
			+ "generate foo \"http://foo.net/foo\"\n"
			+ "Foo: 'foo';"; 
	final IXtextDocument document = createDocument(grammar);
	document.modify(new IUnitOfWork.Void<XtextResource>() {
		@Override
		public void process(XtextResource state) throws Exception {
			document.replace(grammar.indexOf("Foo"), 3, "Bar");
		}
	});
	assertEquals(grammar.replace("foo.Foo", "foo.Bar"), document.get());
}
 
Example 5
Source File: LambdaTermModifier.java    From xsemantics with Eclipse Public License 1.0 5 votes vote down vote up
public void modifyAbstractionWithInferredType(
		IXtextDocument xtextDocument) {
	xtextDocument.modify(new IUnitOfWork.Void<XtextResource>() {
		public void process(XtextResource resource) {
			Program program = (Program) resource.getContents().get(0);
			Abstraction abstraction = (Abstraction) (program.getTerm());
			lambdaTypeModifier.modifyAbstractionParamType(abstraction);
		}
	});
}
 
Example 6
Source File: LambdaTermModifier.java    From xsemantics with Eclipse Public License 1.0 5 votes vote down vote up
public void modifyTermWithInferredType(IXtextDocument xtextDocument) {
	xtextDocument.modify(new IUnitOfWork.Void<XtextResource>() {
		public void process(XtextResource resource) {
			Program program = (Program) resource.getContents().get(0);
			lambdaTypeModifier.setAllTypes(program.getTerm());
		}
	});
}