Search in sources :

Example 46 with LeafPsiElement

use of com.intellij.psi.impl.source.tree.LeafPsiElement in project Perl5-IDEA by Camelcade.

the class CompoundToStatementIntention method computeStatementText.

/**
 * Computes statement text for foreach compound block. Replacing iterator variable with $_ if necessary
 *
 * @param forCompound   for compound statement
 * @param statementExpr statement expression
 * @return adjusted text or null if something went wrong
 */
@NotNull
private static String computeStatementText(@NotNull PerlForCompound forCompound, @NotNull PsiPerlExpr statementExpr) {
    String statementText = statementExpr.getText();
    PsiPerlForeachIterator foreachIterator = forCompound.getForeachIterator();
    if (foreachIterator == null) {
        return statementText;
    }
    PsiPerlExpr variableExpression = foreachIterator.getExpr();
    if (variableExpression instanceof PerlVariableDeclarationExpr) {
        List<PerlVariable> variables = ((PerlVariableDeclarationExpr) variableExpression).getVariables();
        if (variables.size() == 1) {
            variableExpression = variables.get(0);
        }
    }
    // replacing variables instatement to $_
    if (variableExpression instanceof PerlVariable) {
        TextRange statementExprTextRange = statementExpr.getTextRange();
        PerlVariable finalVariable = (PerlVariable) variableExpression;
        List<PerlVariable> varsToReplace = new ArrayList<>();
        statementExpr.accept(new PerlRecursiveVisitor() {

            @Override
            public void visitPerlVariable(@NotNull PerlVariable o) {
                if (PerlVariableUtil.equal(o, finalVariable)) {
                    varsToReplace.add(o);
                }
                super.visitPerlVariable(o);
            }
        });
        for (int i = varsToReplace.size() - 1; i >= 0; i--) {
            PerlVariable variable = varsToReplace.get(i);
            PsiElement nameElement = variable.getVariableNameElement();
            if (nameElement instanceof LeafPsiElement) {
                // replacing variable name with _ => $var => $_
                statementText = nameElement.getTextRange().shiftRight(-statementExprTextRange.getStartOffset()).replace(statementText, "_");
            }
        }
    }
    return statementText;
}
Also used : ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiElement(com.intellij.psi.PsiElement)

Example 47 with LeafPsiElement

use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij by bazelbuild.

the class BlazeScalaRunLineMarkerContributorTest method testGetMainInfo.

@Test
public void testGetMainInfo() {
    BlazeScalaRunLineMarkerContributor markerContributor = new BlazeScalaRunLineMarkerContributor();
    ScalaRunLineMarkerContributor replacedContributor = new ScalaRunLineMarkerContributor();
    PsiFile scalaFile = createAndIndexFile(WorkspacePath.createIfValid("com/google/binary/MainClass.scala"), "package com.google.binary {", "  object MainClass {", "    def main(args: Array[String]) {}", "    def foo() {}", "  }", "}", "package scala { final class Array[T] {} }", "package java.lang { public final class String {} }");
    List<LeafPsiElement> elements = PsiUtils.findAllChildrenOfClassRecursive(scalaFile, LeafPsiElement.class);
    LeafPsiElement objectIdentifier = elements.stream().filter(e -> Objects.equal(e.getText(), "MainClass")).findFirst().orElse(null);
    LeafPsiElement methodIdentifier = elements.stream().filter(e -> Objects.equal(e.getText(), "main")).findFirst().orElse(null);
    assertThat(objectIdentifier).isNotNull();
    assertThat(methodIdentifier).isNotNull();
    // Have main object info.
    Info objectInfo = markerContributor.getInfo(objectIdentifier);
    assertThat(objectInfo).isNotNull();
    assertThat(objectInfo.icon).isEqualTo(AllIcons.RunConfigurations.TestState.Run);
    assertThat(objectInfo.actions).hasLength(2);
    assertThat(objectInfo.actions[0].getTemplatePresentation().getText()).startsWith("Run ");
    assertThat(objectInfo.actions[1].getTemplatePresentation().getText()).startsWith("Debug ");
    // Main object info replaces the one from the scala plugin
    Info replacedObjectInfo = replacedContributor.getInfo(objectIdentifier);
    assertThat(replacedObjectInfo).isNotNull();
    assertThat(objectInfo.shouldReplace(replacedObjectInfo)).isTrue();
    // Hae main method info
    Info methodInfo = markerContributor.getInfo(methodIdentifier);
    assertThat(methodInfo).isNotNull();
    assertThat(methodInfo.icon).isEqualTo(AllIcons.RunConfigurations.TestState.Run);
    assertThat(methodInfo.actions).hasLength(2);
    assertThat(methodInfo.actions[0].getTemplatePresentation().getText()).startsWith("Run ");
    assertThat(methodInfo.actions[1].getTemplatePresentation().getText()).startsWith("Debug ");
    // Main method info replaces the one from the scala plugin
    Info replacedMethodInfo = replacedContributor.getInfo(methodIdentifier);
    assertThat(replacedMethodInfo).isNotNull();
    assertThat(methodInfo.shouldReplace(replacedMethodInfo)).isTrue();
    // No other element should get an info
    elements.stream().filter(e -> !Objects.equal(e, objectIdentifier) && !Objects.equal(e, methodIdentifier)).forEach(e -> assertThat(markerContributor.getInfo(e)).isNull());
}
Also used : AllIcons(com.intellij.icons.AllIcons) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) RunWith(org.junit.runner.RunWith) PsiUtils(com.google.idea.blaze.base.lang.buildfile.psi.util.PsiUtils) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) List(java.util.List) ScalaRunLineMarkerContributor(org.jetbrains.plugins.scala.runner.ScalaRunLineMarkerContributor) PsiFile(com.intellij.psi.PsiFile) Info(com.intellij.execution.lineMarker.RunLineMarkerContributor.Info) WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) Objects(com.google.common.base.Objects) BlazeRunConfigurationProducerTestCase(com.google.idea.blaze.base.run.producer.BlazeRunConfigurationProducerTestCase) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiFile(com.intellij.psi.PsiFile) ScalaRunLineMarkerContributor(org.jetbrains.plugins.scala.runner.ScalaRunLineMarkerContributor) Info(com.intellij.execution.lineMarker.RunLineMarkerContributor.Info) Test(org.junit.Test)

Example 48 with LeafPsiElement

use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij by bazelbuild.

the class BlazeScalaTestRunLineMarkerContributorTest method testIgnoreNonTest.

@Test
public void testIgnoreNonTest() {
    PsiFile scalaFile = createAndIndexFile(WorkspacePath.createIfValid("src/main/scala/com/google/library/Library.scala"), "package com.google.library", "class Library {", "  def method() {}", "}");
    List<LeafPsiElement> elements = PsiUtils.findAllChildrenOfClassRecursive(scalaFile, LeafPsiElement.class);
    elements.forEach(e -> assertThat(markerContributor.getInfo(e)).isNull());
}
Also used : LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiFile(com.intellij.psi.PsiFile) Test(org.junit.Test)

Example 49 with LeafPsiElement

use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij by bazelbuild.

the class BlazeScalaTestRunLineMarkerContributorTest method testGetJunitTestInfo.

@Test
public void testGetJunitTestInfo() {
    PsiFile junitTestFile = createAndIndexFile(WorkspacePath.createIfValid("src/test/scala/com/google/test/JunitTest.scala"), "package com.google.test {", "  class JunitTest {", "    @org.junit.Test", "    def testMethod() {}", "  }", "}", "package org.junit { trait Test }");
    List<LeafPsiElement> elements = PsiUtils.findAllChildrenOfClassRecursive(junitTestFile, LeafPsiElement.class);
    LeafPsiElement classIdentifier = elements.stream().filter(e -> Objects.equal(e.getText(), "JunitTest")).findFirst().orElse(null);
    LeafPsiElement methodIdentifier = elements.stream().filter(e -> Objects.equal(e.getText(), "testMethod")).findFirst().orElse(null);
    assertThat(classIdentifier).isNotNull();
    assertThat(methodIdentifier).isNotNull();
    Info classInfo = markerContributor.getInfo(classIdentifier);
    assertThat(classInfo).isNotNull();
    assertThat(classInfo.icon).isEqualTo(AllIcons.RunConfigurations.TestState.Run_run);
    assertThat(classInfo.actions).hasLength(2);
    assertThat(classInfo.actions[0].getTemplatePresentation().getText()).startsWith("Run ");
    assertThat(classInfo.actions[1].getTemplatePresentation().getText()).startsWith("Debug ");
    Info methodInfo = markerContributor.getInfo(methodIdentifier);
    assertThat(methodInfo).isNotNull();
    assertThat(methodInfo.icon).isEqualTo(AllIcons.RunConfigurations.TestState.Run);
    assertThat(methodInfo.actions).hasLength(2);
    assertThat(methodInfo.actions[0].getTemplatePresentation().getText()).startsWith("Run ");
    assertThat(methodInfo.actions[1].getTemplatePresentation().getText()).startsWith("Debug ");
    elements.stream().filter(e -> !Objects.equal(e, classIdentifier) && !Objects.equal(e, methodIdentifier)).forEach(e -> assertThat(markerContributor.getInfo(e)).isNull());
}
Also used : AllIcons(com.intellij.icons.AllIcons) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) RunWith(org.junit.runner.RunWith) PsiUtils(com.google.idea.blaze.base.lang.buildfile.psi.util.PsiUtils) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) List(java.util.List) PsiFile(com.intellij.psi.PsiFile) Info(com.intellij.execution.lineMarker.RunLineMarkerContributor.Info) WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) Objects(com.google.common.base.Objects) BlazeRunConfigurationProducerTestCase(com.google.idea.blaze.base.run.producer.BlazeRunConfigurationProducerTestCase) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiFile(com.intellij.psi.PsiFile) Info(com.intellij.execution.lineMarker.RunLineMarkerContributor.Info) Test(org.junit.Test)

Example 50 with LeafPsiElement

use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij by bazelbuild.

the class BlazeScalaTestRunLineMarkerContributorTest method testGetSpecs2TestInfo.

@Test
public void testGetSpecs2TestInfo() {
    createAndIndexFile(WorkspacePath.createIfValid("scala/org/junit/runner/RunWith.scala"), "package org.junit.runner", "class RunWith");
    createAndIndexFile(WorkspacePath.createIfValid("src/test/scala/org/specs2/runner/JUnitRunner.scala"), "package org.specs2.runner", "class JUnitRunner");
    createAndIndexFile(WorkspacePath.createIfValid("src/test/scala/org/specs2/mutable/SpecificationWithJUnit.scala"), "package org.specs2.mutable", "@org.junit.runner.RunWith(classOf[org.specs2.runner.JUnitRunner])", "abstract class SpecificationWithJUnit extends org.specs2.mutable.Specification");
    createAndIndexFile(WorkspacePath.createIfValid("src/test/scala/org/specs2/mutable/Specification.scala"), "package org.specs2.mutable", "abstract class Specification extends org.specs2.mutable.SpecificationLike");
    createAndIndexFile(WorkspacePath.createIfValid("src/test/scala/org/specs2/mutable/SpecificationLike.scala"), "package org.specs2.mutable", "trait SpecificationLike extends", "org.specs2.specification.core.mutable.SpecificationStructure");
    createAndIndexFile(WorkspacePath.createIfValid("src/test/scala/org/specs2/specification/core/mutable/SpecificationStructure.scala"), "package org.specs2.specification.core.mutable", "trait SpecificationStructure extends", "org.specs2.specification.core.SpecificationStructure");
    createAndIndexFile(WorkspacePath.createIfValid("src/test/scala/org/specs2/specification/core/SpecificationStructure.scala"), "package org.specs2.specification.core", "trait SpecificationStructure");
    PsiFile specs2TestFile = createAndIndexFile(WorkspacePath.createIfValid("src/test/scala/com/google/test/Specs2Test.scala"), "package com.google.test", "class Specs2Test extends org.specs2.mutable.SpecificationWithJUnit");
    List<LeafPsiElement> elements = PsiUtils.findAllChildrenOfClassRecursive(specs2TestFile, LeafPsiElement.class);
    LeafPsiElement classIdentifier = elements.stream().filter(e -> Objects.equal(e.getText(), "Specs2Test")).findFirst().orElse(null);
    assertThat(classIdentifier).isNotNull();
    Info info = markerContributor.getInfo(classIdentifier);
    assertThat(info).isNotNull();
    assertThat(info.icon).isEqualTo(AllIcons.RunConfigurations.TestState.Run_run);
    assertThat(info.actions).hasLength(2);
    assertThat(info.actions[0].getTemplatePresentation().getText()).startsWith("Run ");
    assertThat(info.actions[1].getTemplatePresentation().getText()).startsWith("Debug ");
    elements.stream().filter(e -> !Objects.equal(e, classIdentifier)).forEach(e -> assertThat(markerContributor.getInfo(e)).isNull());
}
Also used : AllIcons(com.intellij.icons.AllIcons) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) RunWith(org.junit.runner.RunWith) PsiUtils(com.google.idea.blaze.base.lang.buildfile.psi.util.PsiUtils) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) List(java.util.List) PsiFile(com.intellij.psi.PsiFile) Info(com.intellij.execution.lineMarker.RunLineMarkerContributor.Info) WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) Objects(com.google.common.base.Objects) BlazeRunConfigurationProducerTestCase(com.google.idea.blaze.base.run.producer.BlazeRunConfigurationProducerTestCase) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiFile(com.intellij.psi.PsiFile) Info(com.intellij.execution.lineMarker.RunLineMarkerContributor.Info) Test(org.junit.Test)

Aggregations

LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)50 PsiElement (com.intellij.psi.PsiElement)29 Nullable (org.jetbrains.annotations.Nullable)8 IElementType (com.intellij.psi.tree.IElementType)7 Test (org.junit.Test)7 PsiFile (com.intellij.psi.PsiFile)6 TextRange (com.intellij.openapi.util.TextRange)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 NotNull (org.jetbrains.annotations.NotNull)5 Objects (com.google.common.base.Objects)4 Truth.assertThat (com.google.common.truth.Truth.assertThat)4 PsiUtils (com.google.idea.blaze.base.lang.buildfile.psi.util.PsiUtils)4 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)4 BlazeRunConfigurationProducerTestCase (com.google.idea.blaze.base.run.producer.BlazeRunConfigurationProducerTestCase)4 Info (com.intellij.execution.lineMarker.RunLineMarkerContributor.Info)4 AllIcons (com.intellij.icons.AllIcons)4 PsiReference (com.intellij.psi.PsiReference)3 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)3 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)3