Search in sources :

Example 31 with OpenFileDescriptor

use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project intellij-plugins by JetBrains.

the class GrStepDefinitionCreator method createStepDefinition.

@Override
public boolean createStepDefinition(@NotNull GherkinStep step, @NotNull final PsiFile file) {
    if (!(file instanceof GroovyFile))
        return false;
    final Project project = file.getProject();
    final VirtualFile vFile = ObjectUtils.assertNotNull(file.getVirtualFile());
    final OpenFileDescriptor descriptor = new OpenFileDescriptor(project, vFile);
    FileEditorManager.getInstance(project).getAllEditors(vFile);
    FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
    final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
    if (editor != null) {
        final TemplateManager templateManager = TemplateManager.getInstance(file.getProject());
        final TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
        final Template template = templateManager.getActiveTemplate(editor);
        if (templateState != null && template != null) {
            templateState.gotoEnd();
        }
    }
    // snippet text
    final GrMethodCall element = buildStepDefinitionByStep(step);
    GrMethodCall methodCall = (GrMethodCall) ((GroovyFile) file).addStatementBefore(element, null);
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(methodCall);
    methodCall = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(methodCall);
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    if (ApplicationManager.getApplication().isUnitTestMode())
        return true;
    final TemplateBuilderImpl builder = (TemplateBuilderImpl) TemplateBuilderFactory.getInstance().createTemplateBuilder(methodCall);
    // regexp str
    GrLiteral pattern = GrCucumberUtil.getStepDefinitionPattern(methodCall);
    assert pattern != null;
    String patternText = pattern.getText();
    builder.replaceElement(pattern, new TextRange(1, patternText.length() - 1), patternText.substring(1, patternText.length() - 1));
    // block vars
    GrClosableBlock closure = methodCall.getClosureArguments()[0];
    final GrParameter[] blockVars = closure.getAllParameters();
    for (GrParameter var : blockVars) {
        PsiElement identifier = var.getNameIdentifierGroovy();
        builder.replaceElement(identifier, identifier.getText());
    }
    TemplateManager manager = TemplateManager.getInstance(project);
    final Editor editorToRunTemplate;
    if (editor == null) {
        editorToRunTemplate = IntentionUtils.positionCursor(project, file, methodCall);
    } else {
        editorToRunTemplate = editor;
    }
    Template template = builder.buildTemplate();
    TextRange range = methodCall.getTextRange();
    editorToRunTemplate.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
    editorToRunTemplate.getCaretModel().moveToOffset(range.getStartOffset());
    manager.startTemplate(editorToRunTemplate, template, new TemplateEditingAdapter() {

        @Override
        public void templateFinished(Template template, boolean brokenOff) {
            if (brokenOff)
                return;
            ApplicationManager.getApplication().runWriteAction(() -> {
                PsiDocumentManager.getInstance(project).commitDocument(editorToRunTemplate.getDocument());
                final int offset = editorToRunTemplate.getCaretModel().getOffset();
                GrMethodCall methodCall1 = PsiTreeUtil.findElementOfClassAtOffset(file, offset - 1, GrMethodCall.class, false);
                if (methodCall1 != null) {
                    GrClosableBlock[] closures = methodCall1.getClosureArguments();
                    if (closures.length == 1) {
                        GrClosableBlock closure1 = closures[0];
                        selectBody(closure1, editor);
                    }
                }
            });
        }
    });
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) TextRange(com.intellij.openapi.util.TextRange) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) Project(com.intellij.openapi.project.Project) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) PsiElement(com.intellij.psi.PsiElement)

Example 32 with OpenFileDescriptor

use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project intellij-plugins by JetBrains.

the class FlexHighlightingTest method testGenerateTestClass.

@JSTestOptions({ JSTestOption.WithFlexFacet, JSTestOption.WithFlexUnit4 })
public void testGenerateTestClass() throws Exception {
    JSTestUtils.disableFileHeadersInTemplates(getProject());
    final String testName = getTestName(false);
    configureByFiles(BASE_PATH + "/" + testName, BASE_PATH + "/" + testName + "/pack/" + testName + ".mxml");
    final FlexUnitTestCreator testCreator = new FlexUnitTestCreator();
    assertTrue(testCreator.isAvailable(myProject, myEditor, myFile));
    testCreator.createTest(myProject, myEditor, myFile);
    final VirtualFile testClassFile = ModuleRootManager.getInstance(myModule).getSourceRoots()[0].findFileByRelativePath("pack/" + testName + "Test.as");
    myEditor = FileEditorManager.getInstance(myProject).openTextEditor(new OpenFileDescriptor(myProject, testClassFile), true);
    myFile = PsiDocumentManager.getInstance(myProject).getPsiFile(myEditor.getDocument());
    checkResultByFile(BASE_PATH + "/" + testName + "/pack/" + testName + "_after.as");
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FlexUnitTestCreator(com.intellij.lang.javascript.flex.flexunit.FlexUnitTestCreator) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor)

Example 33 with OpenFileDescriptor

use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project intellij-plugins by JetBrains.

the class SwfHighlightingTest method testLineMarkersInSwf.

@JSTestOptions({ JSTestOption.WithFlexFacet, JSTestOption.WithLineMarkers })
public void testLineMarkersInSwf() throws Exception {
    final String testName = getTestName(false);
    myAfterCommitRunnable = () -> FlexTestUtils.addLibrary(myModule, "lib", getTestDataPath() + getBasePath() + "/", testName + ".swc", null, null);
    // actual test data is in library.swf; this file is here just because we need any file
    configureByFile("/" + testName + ".as");
    VirtualFile vFile = LocalFileSystem.getInstance().findFileByPath(getTestDataPath() + getBasePath() + "/" + testName + ".swc");
    vFile = JarFileSystem.getInstance().getJarRootForLocalFile(vFile).findChild("library.swf");
    myEditor = FileEditorManager.getInstance(myProject).openTextEditor(new OpenFileDescriptor(myProject, vFile, 0), false);
    PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
    myFile = myPsiManager.findFile(vFile);
    ((EditorImpl) myEditor).setCaretActive();
    vFile = LocalFileSystem.getInstance().findFileByPath(getTestDataPath() + getBasePath() + "/" + testName + ".as");
    final String verificationText = StreamUtil.convertSeparators(VfsUtilCore.loadText(vFile));
    checkHighlighting(new ExpectedHighlightingData(new DocumentImpl(verificationText), false, false, true, myFile));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ExpectedHighlightingData(com.intellij.testFramework.ExpectedHighlightingData) EditorImpl(com.intellij.openapi.editor.impl.EditorImpl) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) DocumentImpl(com.intellij.openapi.editor.impl.DocumentImpl) JSTestOptions(com.intellij.lang.javascript.JSTestOptions)

Example 34 with OpenFileDescriptor

use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project intellij-plugins by JetBrains.

the class SwfHighlightingTest method testProtectSwf.

public void testProtectSwf() throws Exception {
    configureByFiles((String) null);
    VirtualFile vFile = LocalFileSystem.getInstance().findFileByPath(getTestDataPath() + getBasePath() + "/" + getTestName(false) + ".swf");
    myEditor = FileEditorManager.getInstance(myProject).openTextEditor(new OpenFileDescriptor(myProject, vFile, 0), false);
    PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
    myFile = myPsiManager.findFile(vFile);
    ((EditorImpl) myEditor).setCaretActive();
    assertFalse(FileDocumentManager.getInstance().requestWriting(myEditor.getDocument(), myProject));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) EditorImpl(com.intellij.openapi.editor.impl.EditorImpl) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor)

Example 35 with OpenFileDescriptor

use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project android by JetBrains.

the class UnresolvedDependenciesReporterTest method testReportWithRegularJavaLibrary.

public void testReportWithRegularJavaLibrary() throws Exception {
    loadSimpleApplication();
    mySyncMessagesStub.clearReportedMessages();
    when(mySyncIssue.getData()).thenReturn("com.google.guava:guava:19.0");
    Module appModule = myModules.getAppModule();
    VirtualFile buildFile = getGradleBuildFile(appModule);
    myReporter.report(mySyncIssue, appModule, buildFile);
    SyncMessage message = mySyncMessagesStub.getFirstReportedMessage();
    assertNotNull(message);
    assertThat(message.getText()).hasLength(1);
    // @formatter:off
    assertAbout(syncMessage()).that(message).hasGroup("Unresolved dependencies").hasMessageLine("Failed to resolve: com.google.guava:guava:19.0", 0);
    // @formatter:on
    assertThat(message.getNavigatable()).isInstanceOf(OpenFileDescriptor.class);
    OpenFileDescriptor navigatable = (OpenFileDescriptor) message.getNavigatable();
    assertEquals(buildFile, navigatable.getFile());
    PositionInFile position = message.getPosition();
    assertNotNull(position);
    assertSame(buildFile, position.file);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SyncMessage(com.android.tools.idea.gradle.project.sync.messages.SyncMessage) PositionInFile(com.android.tools.idea.gradle.util.PositionInFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Module(com.intellij.openapi.module.Module)

Aggregations

OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)155 VirtualFile (com.intellij.openapi.vfs.VirtualFile)97 Project (com.intellij.openapi.project.Project)40 Editor (com.intellij.openapi.editor.Editor)30 PsiElement (com.intellij.psi.PsiElement)27 PsiFile (com.intellij.psi.PsiFile)21 Navigatable (com.intellij.pom.Navigatable)19 Nullable (org.jetbrains.annotations.Nullable)18 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)10 Document (com.intellij.openapi.editor.Document)9 FileEditor (com.intellij.openapi.fileEditor.FileEditor)9 TextRange (com.intellij.openapi.util.TextRange)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)9 File (java.io.File)9 Result (com.intellij.openapi.application.Result)8 EditorImpl (com.intellij.openapi.editor.impl.EditorImpl)8 NotNull (org.jetbrains.annotations.NotNull)6 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)5 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)5 XmlTag (com.intellij.psi.xml.XmlTag)5