Search in sources :

Example 1 with AbstractExpectation

use of org.eclipse.xpect.expectation.impl.AbstractExpectation 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")
}
Also used : ChangeInfo(org.eclipse.n4js.xpect.ui.common.QuickFixTestHelper.ChangeInfo) AbstractExpectation(org.eclipse.xpect.expectation.impl.AbstractExpectation) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) Point(org.eclipse.swt.graphics.Point) IExpectationRegion(org.eclipse.xpect.expectation.IExpectationRegion) Point(org.eclipse.swt.graphics.Point) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) ConsumedIssues(org.eclipse.xpect.xtext.lib.tests.ValidationTestModuleSetup.ConsumedIssues) Xpect(org.eclipse.xpect.runner.Xpect) ParameterParser(org.eclipse.xpect.parameter.ParameterParser)

Aggregations

ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)1 ChangeInfo (org.eclipse.n4js.xpect.ui.common.QuickFixTestHelper.ChangeInfo)1 Point (org.eclipse.swt.graphics.Point)1 IExpectationRegion (org.eclipse.xpect.expectation.IExpectationRegion)1 AbstractExpectation (org.eclipse.xpect.expectation.impl.AbstractExpectation)1 ParameterParser (org.eclipse.xpect.parameter.ParameterParser)1 Xpect (org.eclipse.xpect.runner.Xpect)1 ConsumedIssues (org.eclipse.xpect.xtext.lib.tests.ValidationTestModuleSetup.ConsumedIssues)1 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)1