use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.
the class XbaseQuickfixProvider method fixEqualsWithNull.
@Fix(IssueCodes.EQUALS_WITH_NULL)
public void fixEqualsWithNull(final Issue issue, IssueResolutionAcceptor acceptor) {
String[] data = issue.getData();
if (data == null || data.length == 0) {
return;
}
String operator = data[0];
String message = "Replace '" + operator + "' with '" + operator + "='";
acceptor.accept(issue, message, message, null, new IModification() {
@Override
public void apply(IModificationContext context) throws Exception {
IXtextDocument document = context.getXtextDocument();
document.replace(issue.getOffset(), issue.getLength(), operator + "=");
}
});
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project n4js by eclipse.
the class OwnResourceValidatorAwareValidatingEditorCallback method newValidationJob.
private ValidationJob newValidationJob(final XtextEditor editor) {
final IXtextDocument document = editor.getDocument();
final IAnnotationModel annotationModel = editor.getInternalSourceViewer().getAnnotationModel();
final IssueResolutionProvider issueResolutionProvider = getService(editor, IssueResolutionProvider.class);
final MarkerTypeProvider markerTypeProvider = getService(editor, MarkerTypeProvider.class);
final MarkerCreator markerCreator = getService(editor, MarkerCreator.class);
final IValidationIssueProcessor issueProcessor = new CompositeValidationIssueProcessor(new AnnotationIssueProcessor(document, annotationModel, issueResolutionProvider), new MarkerIssueProcessor(editor.getResource(), markerCreator, markerTypeProvider));
return editor.getDocument().modify(resource -> {
final IResourceServiceProvider serviceProvider = resource.getResourceServiceProvider();
final IResourceValidator resourceValidator = serviceProvider.getResourceValidator();
return new ValidationJob(resourceValidator, editor.getDocument(), issueProcessor, ALL);
});
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project n4js by eclipse.
the class DocumentImportsOrganizer method organizeFile.
/**
* Obtains {@link IXtextDocument} for the provided file and delegates to
* {@link #organizeDocument(IXtextDocument, Interaction)}
*/
void organizeFile(IFile file, final Interaction interaction, IProgressMonitor mon) throws CoreException {
SubMonitor subMon = SubMonitor.convert(mon, "Organizing " + file.getName(), IProgressMonitor.UNKNOWN);
FileEditorInput fei = new FileEditorInput(file);
// without connecting no document will be provided
docProvider.connect(fei);
IXtextDocument document = (IXtextDocument) docProvider.getDocument(fei);
docProvider.aboutToChange(fei);
organizeDocument(document, interaction);
subMon.setTaskName("Saving " + file.getName());
docProvider.saveDocument(subMon.split(0), fei, document, true);
docProvider.changed(fei);
docProvider.disconnect(fei);
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project n4js by eclipse.
the class ProposalXpectMethod method proposalChange.
// @formatter:off
/**
* Test the application of the given content assist entry. After applying the content assist there should be the
* expected diff produced when comparing the document before and after applying the content assist.
*
* The diff has the following semantics:
*
* " ": (whitespace) nothing changed
* "+ ": line was added
* "- ": line was deleted
* "| ": line was modified, displayed as [oldText|newText]
*
* If the given expected content assist entry cannot be found, an error message is inserted at the offset resulting
* in expectation mismatch and so also to a failing test.
*
* Also clf.
* https://github.com/meysholdt/Xpect/tree/master/org.eclipse.xtext.example.domainmodel.xpect.tests/src/org
* /eclipse/xtext/example/domainmodel/xpect/tests/modify for some more examples.
*
* /\* XPECT proposalChange 'methodA2' at 'methodA' ---
* (...)
* }
* }
* var A a = new A();
* |a.[methodA|methodA2methodA]
* --- *\/
*
* Please note, that the comparison always contains also some lines around the line that contains the diff.
* Xpect currently doesn't allow to configure the number of those lines.
*
* @param expectation
* the expected diff comparing the document before and after applying the content assist
* @param resource
* the resource under test
* @param arg2
* the offset of where to invoke content assist (note: it must be named arg2 as Xpect injects the
* parameter values by position)
* @param arg3
* the content assist entry to select
* @throws Exception
* some exception
*/
// @formatter:on
@ParameterParser(syntax = "(arg3=STRING 'at' arg2=STRING)?")
@Xpect
public void proposalChange(@StringDiffExpectation IStringDiffExpectation expectation, @ThisResource XtextResource resource, /* @ThisOffset int */
RegionWithCursor arg2, String arg3) throws Exception {
// val int offset = arg2;
RegionWithCursor offset = arg2;
String text = arg3;
// println("proposal change with offset="+arg2+" selection="+arg3)
N4ContentAssistProcessorTestBuilder fixture = n4ContentAssistProcessorTestBuilderHelper.createTestBuilderForResource(resource);
ICompletionProposal proposal = exactlyMatchingProposal(offset, fixture, text);
String before = resource.getParseResult().getRootNode().getText();
if (proposal != null) {
IXtextDocument document = fixture.getDocument(XtextResourceCleanUtil.cleanXtextResource(resource), before);
String after = applyProposal(proposal, document);
before = XpectCommentRemovalUtil.removeAllXpectComments(before);
after = XpectCommentRemovalUtil.removeAllXpectComments(after);
expectation.assertDiffEquals(before, after);
}
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project n4js by eclipse.
the class ContentAssistXpectMethod method contentAssist.
/*-
contentAssist at ’a.<|>methodA’ ’methodA2’ --> a.methodA2() <|>methodA
contentAssist at ’a.<|><[>methodA<]>’ ’methodA2’ override --> a.methodA2()<|>
contentAssist at ’a.methodA’ ’methodA2’ --> a.methodA2() <|>methodA
contentAssist at ’var A<|> a’ ’A - import’ ---
contentAssist at 'a.<|>methodA' 'methodA2' --> a.methodA2()<|>methodA
contentAssist at 'a.<|>methodA' 'methodA2' insert --> a.methodA2()<|>methodA
contentAssist at 'a.<|>methodA' 'methodA2' override --> a.methodA2()<|>
contentAssist kind 'smart' at '<a|>methodA' 'methodA2' ---
kind offset selected mode
arg3 arg2 arg4 arg5
*/
/**
* Application of a content assist. The right hand side describes the expected modifications.
*
* Up to now only single-line changes are supported.
*
* @param expectation
* injected Applicaiton
* @param resource
* injected xtext resource
* @param offset
* parsed arg2 - Cursorposition and Selection
* @param kind
* parsed arg3 - named kind of assistence-provider 'recommenders',...
* @param selected
* parsed arg4 - string of things to select in proposal list.
* @param mode
* parsed arg5 - mode modifier
* @throws Exception
* in junit-error case
*/
@Xpect
@ParameterParser(syntax = "( ('kind' arg3=STRING)? 'at' (arg2=STRING ('apply' arg4=STRING)? (arg5=ID)? )? )?")
@ConsumedIssues({ Severity.INFO, Severity.ERROR, Severity.WARNING })
public void contentAssist(// arg0
IStringExpectation expectation, // arg1
@ThisResource XtextResource resource, // arg2 //@ThisOffset is obsolete
RegionWithCursor offset, // arg3
String kind, // arg4
String selected, // arg5
String mode) throws Exception {
N4ContentAssistProcessorTestBuilder fixture = n4ContentAssistProcessorTestBuilderHelper.createTestBuilderForResource(resource);
ICompletionProposal proposal = exactlyMatchingProposal(offset, fixture, selected);
String before = resource.getParseResult().getRootNode().getText();
// apply:
// so working with the fixture cannot get any selection information since these are mocked away.
IXtextDocument document = fixture.getDocument(XtextResourceCleanUtil.cleanXtextResource(resource), before);
Optional<String> optionalMode = Optional.ofNullable(mode);
if (optionalMode.isPresent() && optionalMode.get().trim() == "override") {
// TODO mode override: needs to have a real selectionprovider in the fixture.
// currently there is a mockup which doesn't support selection
// see org.eclipse.xtext.ui.testing.ContentAssistProcessorTestBuilder.getSourceViewer(String,
// IXtextDocument)
// val sv = fixture.getSourceViewer(null, document)
// sv.selectionProvider.selection = new TextSelection(..)
}
String after = applyProposal(proposal, document);
// Expecting change only in single line:
ChangeInfo changedLines = extractSingleChangedLine(before, after);
if (changedLines.isMoreThanOne()) {
throw new AssertionError("more then one line changed: " + changedLines.asString());
} else if (changedLines.isEmpty()) {
throw new AssertionError("Nothing changed.");
}
String exp = changedLines.first().getAfter();
Point selection = proposal.getSelection(document);
if (selection != null) {
IExpectationRegion region = ((AbstractExpectation) expectation).getRegion();
if (CursorMarkerHelper.exists(region.getRegionText(), CursorMarkerHelper.markerCursor)) {
int newPos = selection.x - changedLines.first().getAfterOffset();
exp = new StringBuilder(exp).insert(newPos, CursorMarkerHelper.markerCursor).toString();
}
}
// Single changed line:
expectation.assertEquals(exp);
// TODO up to now this only removes the locations for the cursor but doesn't check the location.
// TODO multilines must be supported.
// // before = replaceXPECT(before);
// // after = replaceXPECT(after);
// expectation.assertEquals(after)
// expectation.assertDiffEquals("a", "b - not implemented yet")
}
Aggregations