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

The following examples show how to use org.jetbrains.plugins.scala.lang.psi.api.ScalaFile. These examples are extracted from open source projects. 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 Project: CodeMaker   Source File: ClassEntry.java    License: 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 Project: intellij   Source File: ScalaBinaryContextProvider.java    License: 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 Project: intellij   Source File: ScalaBinaryContextProvider.java    License: 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 Project: intellij   Source File: ScalaBinaryContextProvider.java    License: 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 Project: CodeMaker   Source File: CodeMakerUtil.java    License: 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
@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
@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
@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
@Override
public boolean suitableForFile(@NotNull PsiFile file) {
  return PantsUtil.isPythonAvailable() && file instanceof ScalaFile;
}