org.jetbrains.plugins.scala.lang.psi.api.ScalaFile Java Examples

The following examples show how to use org.jetbrains.plugins.scala.lang.psi.api.ScalaFile. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClassEntry.java    From CodeMaker with Apache License 2.0 6 votes vote down vote up
public static ClassEntry create(PsiClass psiClass) {
    PsiFile psiFile = psiClass.getContainingFile();
    ClassEntry classEntry = new ClassEntry();
    classEntry.setClassName(psiClass.getName());
    classEntry.setPackageName(((PsiClassOwner)psiFile).getPackageName());
    if(psiFile instanceof PsiJavaFile)
    {
        classEntry.setFields(CodeMakerUtil.getFields(psiClass));
        classEntry.setImportList(CodeMakerUtil.getImportList((PsiJavaFile) psiFile));
        classEntry.setAllFields(CodeMakerUtil.getAllFields(psiClass));
    }
    else if(psiClass instanceof ScClass) {
        ScClass scalaClass = (ScClass) psiClass;
        classEntry.setFields(CodeMakerUtil.getScalaClassFields(scalaClass));
        classEntry.setImportList(CodeMakerUtil.getScalaImportList((ScalaFile)psiFile));
    }


    classEntry.setMethods(CodeMakerUtil.getMethods(psiClass));
    classEntry.setAllMethods(CodeMakerUtil.getAllMethods(psiClass));
    classEntry.setTypeParams(CodeMakerUtil.getClassTypeParameters(psiClass));
    return classEntry;
}
 
Example #2
Source File: ScalaBinaryContextProvider.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Nullable
static ScObject getMainObject(ConfigurationContext context) {
  Location location = context.getLocation();
  if (location == null) {
    return null;
  }
  location = JavaExecutionUtil.stepIntoSingleClass(context.getLocation());
  if (location == null) {
    return null;
  }
  PsiElement element = location.getPsiElement();
  if (!(element.getContainingFile() instanceof ScalaFile)) {
    return null;
  }
  if (!element.isPhysical()) {
    return null;
  }
  return getMainObjectFromElement(element);
}
 
Example #3
Source File: ScalaBinaryContextProvider.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Nullable
private static ScObject getMainObjectFromFile(ScalaFile file) {
  for (PsiClass aClass : file.getClasses()) {
    if (!(aClass instanceof ScObject)) {
      continue;
    }
    ScObject obj = (ScObject) aClass;
    if (ScalaSdkCompat.hasMainMethod(obj)) {
      // Potentially multiple matches, we'll pick the first one.
      // TODO: prefer class with same name as file?
      // TODO: skip if not main_class of a rule.
      return obj;
    }
  }
  return null;
}
 
Example #4
Source File: ScalaBinaryContextProvider.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
private static ScObject getMainObjectFromElement(PsiElement element) {
  for (; element != null; element = element.getParent()) {
    if (element instanceof ScObject) {
      ScObject obj = (ScObject) element;
      if (ScalaSdkCompat.hasMainMethod(obj)) {
        return obj;
      }
    } else if (element instanceof ScalaFile) {
      return getMainObjectFromFile((ScalaFile) element);
    }
  }
  return null;
}
 
Example #5
Source File: CodeMakerUtil.java    From CodeMaker with Apache License 2.0 4 votes vote down vote up
public static List<String> getScalaImportList(ScalaFile scalaFile) {
    List<ScImportStmt> scImportStmts = seqAsJavaList(scalaFile.getImportStatements());
    return scImportStmts.stream()
        .flatMap(stmt -> seqAsJavaList(stmt.importExprs()).stream().map(PsiElement::getText))
        .collect(Collectors.toList());
}
 
Example #6
Source File: BlazeScalaTestClassConfigurationProducerTest.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Test
public void testJunitTestProducedFromPsiClass() {
  PsiFile file =
      createAndIndexFile(
          new WorkspacePath("scala/com/google/test/TestClass.scala"),
          "package com.google.test {",
          "  class TestClass {",
          "    @org.junit.Test",
          "    def testMethod() {}",
          "  }",
          "}",
          "package org.junit { trait Test }");
  assertThat(file).isInstanceOf(ScalaFile.class);
  ScalaFile scalaFile = (ScalaFile) file;
  PsiClass[] classes = scalaFile.getClasses();
  assertThat(classes).isNotEmpty();
  PsiClass testClass = classes[0];

  MockBlazeProjectDataBuilder builder = MockBlazeProjectDataBuilder.builder(workspaceRoot);
  builder.setTargetMap(
      TargetMapBuilder.builder()
          .addTarget(
              TargetIdeInfo.builder()
                  .setKind("scala_junit_test")
                  .setLabel("//scala/com/google/test:TestClass")
                  .addSource(sourceRoot("scala/com/google/test/TestClass.scala"))
                  .build())
          .build());
  registerProjectService(
      BlazeProjectDataManager.class, new MockBlazeProjectDataManager(builder.build()));

  ConfigurationContext context = createContextFromPsi(testClass);
  List<ConfigurationFromContext> configurations = context.getConfigurationsFromContext();
  assertThat(configurations).isNotNull();
  assertThat(configurations).hasSize(1);
  ConfigurationFromContext fromContext = configurations.get(0);
  assertThat(fromContext.isProducedBy(TestContextRunConfigurationProducer.class)).isTrue();
  assertThat(fromContext.getConfiguration()).isInstanceOf(BlazeCommandRunConfiguration.class);

  BlazeCommandRunConfiguration config =
      (BlazeCommandRunConfiguration) fromContext.getConfiguration();
  assertThat(config.getTargets())
      .containsExactly(TargetExpression.fromStringSafe("//scala/com/google/test:TestClass"));
  assertThat(getTestFilterContents(config)).isEqualTo("--test_filter=com.google.test.TestClass#");
  assertThat(config.getName()).isEqualTo("Blaze test TestClass");
  assertThat(getCommandType(config)).isEqualTo(BlazeCommandName.TEST);
}
 
Example #7
Source File: BlazeScalaTestClassConfigurationProducerTest.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Test
public void testScalaTestProducedFromPsiClass() {
  PsiFile file =
      createAndIndexFile(
          WorkspacePath.createIfValid("scala/com/google/test/TestClass.scala"),
          "package com.google.test {",
          "  class TestClass extends org.scalatest.FlatSpec {",
          "    \"this test\" should \"pass\" in {}",
          "  }",
          "}",
          "package org.scalatest {",
          "  trait FlatSpec extends Suite",
          "  trait Suite",
          "}");
  assertThat(file).isInstanceOf(ScalaFile.class);
  ScalaFile scalaFile = (ScalaFile) file;
  PsiClass[] classes = scalaFile.getClasses();
  assertThat(classes).isNotEmpty();
  PsiClass testClass = classes[0];

  MockBlazeProjectDataBuilder builder = MockBlazeProjectDataBuilder.builder(workspaceRoot);
  builder.setTargetMap(
      TargetMapBuilder.builder()
          .addTarget(
              TargetIdeInfo.builder()
                  .setKind("scala_test")
                  .setLabel("//scala/com/google/test:TestClass")
                  .addSource(sourceRoot("scala/com/google/test/TestClass.scala"))
                  .build())
          .build());
  registerProjectService(
      BlazeProjectDataManager.class, new MockBlazeProjectDataManager(builder.build()));

  ConfigurationContext context = createContextFromPsi(testClass);
  List<ConfigurationFromContext> configurations = context.getConfigurationsFromContext();
  assertThat(configurations).isNotNull();
  assertThat(configurations).hasSize(1);
  ConfigurationFromContext fromContext = configurations.get(0);
  assertThat(fromContext.isProducedBy(TestContextRunConfigurationProducer.class)).isTrue();
  assertThat(fromContext.getConfiguration()).isInstanceOf(BlazeCommandRunConfiguration.class);

  BlazeCommandRunConfiguration config =
      (BlazeCommandRunConfiguration) fromContext.getConfiguration();
  assertThat(config.getTargets())
      .containsExactly(TargetExpression.fromStringSafe("//scala/com/google/test:TestClass"));
  assertThat(getTestFilterContents(config)).isEqualTo("--test_filter=com.google.test.TestClass");
  assertThat(config.getName()).isEqualTo("Blaze test TestClass");
  assertThat(getCommandType(config)).isEqualTo(BlazeCommandName.TEST);
}
 
Example #8
Source File: ScalaSpecs2TestContextProviderTest.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Test
public void testSpecs2TestProducedFromPsiClass() {
  PsiFile file = createTestPsiFile();

  assertThat(file).isInstanceOf(ScalaFile.class);
  ScalaFile scalaFile = (ScalaFile) file;
  PsiClass[] classes = scalaFile.getClasses();
  assertThat(classes).isNotEmpty();
  PsiClass testClass = classes[0];

  MockBlazeProjectDataBuilder builder = MockBlazeProjectDataBuilder.builder(workspaceRoot);
  builder.setTargetMap(
      TargetMapBuilder.builder()
          .addTarget(
              TargetIdeInfo.builder()
                  .setKind("scala_junit_test")
                  .setLabel("//scala/com/google/test:TestClass")
                  .addSource(sourceRoot("scala/com/google/test/TestClass.scala"))
                  .build())
          .build());
  registerProjectService(
      BlazeProjectDataManager.class, new MockBlazeProjectDataManager(builder.build()));

  ConfigurationContext context = createContextFromPsi(testClass);
  List<ConfigurationFromContext> configurations = context.getConfigurationsFromContext();
  assertThat(configurations).isNotNull();
  assertThat(configurations).hasSize(1);
  ConfigurationFromContext fromContext = configurations.get(0);
  assertThat(fromContext.isProducedBy(TestContextRunConfigurationProducer.class)).isTrue();
  assertThat(fromContext.getConfiguration()).isInstanceOf(BlazeCommandRunConfiguration.class);

  BlazeCommandRunConfiguration config =
      (BlazeCommandRunConfiguration) fromContext.getConfiguration();
  assertThat(config.getTargets())
      .containsExactly(TargetExpression.fromStringSafe("//scala/com/google/test:TestClass"));
  assertThat(getTestFilterContents(config)).isEqualTo("--test_filter=com.google.test.TestClass#");
  assertThat(config.getName()).isEqualTo("Blaze test TestClass");
  assertThat(getCommandType(config)).isEqualTo(BlazeCommandName.TEST);
  // TODO: add tests for infix expression run configurations
  // TODO: also test BlazeScalaTestEventsHandler
}
 
Example #9
Source File: PantsScalaHighlightVisitor.java    From intellij-pants-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public boolean suitableForFile(@NotNull PsiFile file) {
  return PantsUtil.isPythonAvailable() && file instanceof ScalaFile;
}