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;
}
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());
}
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());
}
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());
}
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());
}
Aggregations