Search in sources :

Example 6 with StepValue

use of com.thoughtworks.gauge.StepValue in project Intellij-Plugin by getgauge.

the class StepCompletionProviderTest method testStepAutoCompletion.

public void testStepAutoCompletion() throws Exception {
    myFixture.configureByFiles("SimpleSpec.spec", "SimpleSpec.txt");
    Gauge.addModule(myFixture.getModule(), new GaugeService(mockProcess, mockGaugeConnection));
    StepValue stepValue1 = new StepValue("This is a step", "This is a step");
    StepValue stepValue2 = new StepValue("Hello", "Hello");
    StepValue stepValue3 = new StepValue("The step", "The step");
    when(mockGaugeConnection.fetchAllSteps()).thenReturn(asList(stepValue1, stepValue2, stepValue3));
    myFixture.getEditor().getCaretModel().moveToOffset(49);
    myFixture.complete(CompletionType.BASIC, 1);
    List<String> strings = myFixture.getLookupElementStrings();
    assertEquals(2, strings.size());
    assertTrue(strings.containsAll(asList("This is a step", "The step")));
}
Also used : StepValue(com.thoughtworks.gauge.StepValue) GaugeService(com.thoughtworks.gauge.core.GaugeService)

Example 7 with StepValue

use of com.thoughtworks.gauge.StepValue in project Intellij-Plugin by getgauge.

the class StepCompletionProviderTest method testStepAutoCompleteShouldReplaceBracesOfOnlyParams.

public void testStepAutoCompleteShouldReplaceBracesOfOnlyParams() throws Exception {
    myFixture.configureByFiles("SimpleSpec.spec", "SimpleSpec.txt");
    Gauge.addModule(myFixture.getModule(), new GaugeService(mockProcess, mockGaugeConnection));
    StepValue stepValue1 = new StepValue("This is a step with {} and >", "This is a step with <param> and >", asList("param"));
    when(mockGaugeConnection.fetchAllSteps()).thenReturn(asList(stepValue1));
    myFixture.getEditor().getCaretModel().moveToOffset(49);
    myFixture.complete(CompletionType.BASIC, 1);
    String expected = "*This is a step with \"param\" and >";
    String actual = myFixture.getEditor().getDocument().getText(new TextRange(47, 81));
    assertEquals(expected, actual);
}
Also used : StepValue(com.thoughtworks.gauge.StepValue) GaugeService(com.thoughtworks.gauge.core.GaugeService) TextRange(com.intellij.openapi.util.TextRange)

Example 8 with StepValue

use of com.thoughtworks.gauge.StepValue in project Intellij-Plugin by getgauge.

the class CreateStepImplFix method addImpl.

private void addImpl(final Project project, final VirtualFile file) {
    ApplicationManager.getApplication().invokeLater(new Runnable() {

        @Override
        public void run() {
            new WriteCommandAction.Simple(project) {

                @Override
                protected void run() throws Throwable {
                    PsiFile psifile = PsiManager.getInstance(project).findFile(file);
                    if (!FileModificationService.getInstance().prepareFileForWrite(psifile)) {
                        return;
                    }
                    PsiMethod addedStepImpl = addStepImplMethod(psifile);
                    final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(addedStepImpl);
                    templateMethodName(addedStepImpl, builder);
                    templateParams(addedStepImpl, builder);
                    templateBody(addedStepImpl, builder);
                    userTemplateModify(builder);
                }
            }.execute();
        }

        private PsiMethod addStepImplMethod(PsiFile psifile) {
            final PsiClass psiClass = PsiTreeUtil.getChildOfType(psifile, PsiClass.class);
            PsiDocumentManager.getInstance(project).commitAllDocuments();
            StepValue stepValue = step.getStepValue();
            final StringBuilder text = new StringBuilder(String.format("@" + Step.class.getName() + "(\"%s\")\n", stepValue.getStepAnnotationText()));
            text.append(String.format("public void %s(%s){\n\n", getMethodName(psiClass), getParamList(stepValue.getParameters())));
            text.append("}\n");
            final PsiMethod stepMethod = JavaPsiFacade.getElementFactory(project).createMethodFromText(text.toString(), psiClass);
            PsiMethod addedElement = (PsiMethod) psiClass.add(stepMethod);
            JavaCodeStyleManager.getInstance(project).shortenClassReferences(addedElement);
            CodeStyleManager.getInstance(project).reformat(psiClass);
            addedElement = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(addedElement);
            return addedElement;
        }

        private String getParamList(List<String> params) {
            StringBuilder paramlistBuilder = new StringBuilder();
            for (int i = 0; i < params.size(); i++) {
                paramlistBuilder.append("Object arg").append(i);
                if (i != params.size() - 1) {
                    paramlistBuilder.append(", ");
                }
            }
            return paramlistBuilder.toString();
        }

        private void templateMethodName(PsiMethod addedStepImpl, TemplateBuilder builder) {
            PsiIdentifier methodName = addedStepImpl.getNameIdentifier();
            builder.replaceElement(methodName, methodName.getText());
        }

        private void templateParams(PsiMethod addedElement, TemplateBuilder builder) {
            PsiParameterList paramsList = addedElement.getParameterList();
            PsiParameter[] parameters = paramsList.getParameters();
            for (PsiParameter parameter : parameters) {
                PsiElement nameIdentifier = parameter.getNameIdentifier();
                PsiTypeElement typeElement = parameter.getTypeElement();
                if (nameIdentifier != null) {
                    builder.replaceElement(typeElement, typeElement.getText());
                    builder.replaceElement(nameIdentifier, nameIdentifier.getText());
                }
            }
        }

        private void templateBody(PsiMethod addedElement, TemplateBuilder builder) {
            final PsiCodeBlock body = addedElement.getBody();
            if (body != null) {
                builder.replaceElement(body, new TextRange(2, 2), "");
            }
        }

        private void userTemplateModify(TemplateBuilder builder) {
            Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, file, 0), true);
            final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
            if (editor != null) {
                documentManager.doPostponedOperationsAndUnblockDocument(editor.getDocument());
                builder.run(editor, false);
            }
        }
    });
}
Also used : TemplateBuilder(com.intellij.codeInsight.template.TemplateBuilder) TextRange(com.intellij.openapi.util.TextRange) BaseListPopupStep(com.intellij.openapi.ui.popup.util.BaseListPopupStep) Step(com.thoughtworks.gauge.Step) SpecStep(com.thoughtworks.gauge.language.psi.SpecStep) PopupStep(com.intellij.openapi.ui.popup.PopupStep) StepValue(com.thoughtworks.gauge.StepValue) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor)

Aggregations

StepValue (com.thoughtworks.gauge.StepValue)8 GaugeService (com.thoughtworks.gauge.core.GaugeService)4 TextRange (com.intellij.openapi.util.TextRange)3 ReferencesSearch (com.intellij.psi.search.searches.ReferencesSearch)2 Test (org.junit.Test)2 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)1 Editor (com.intellij.openapi.editor.Editor)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 PopupStep (com.intellij.openapi.ui.popup.PopupStep)1 BaseListPopupStep (com.intellij.openapi.ui.popup.util.BaseListPopupStep)1 Step (com.thoughtworks.gauge.Step)1 GaugeConnection (com.thoughtworks.gauge.connection.GaugeConnection)1 SpecStep (com.thoughtworks.gauge.language.psi.SpecStep)1 ConceptStepImpl (com.thoughtworks.gauge.language.psi.impl.ConceptStepImpl)1 SpecStepImpl (com.thoughtworks.gauge.language.psi.impl.SpecStepImpl)1