com.intellij.lang.java.JavaLanguage Java Examples

The following examples show how to use com.intellij.lang.java.JavaLanguage. 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: GenerateAction.java    From RIBs with Apache License 2.0 5 votes vote down vote up
/**
 * Writes a source file generated by a {@link Generator} to disk.
 *
 * @param project   to write file in.
 * @param generator to generate file with.
 * @param directory to write file to.
 */
private static void createSourceFile(
    Project project, Generator generator, PsiDirectory directory) {
  PsiFile file =
      PsiFileFactory.getInstance(project)
          .createFileFromText(
              String.format("%s" + generator.getFileExtension(), generator.getClassName()),
              JavaLanguage.INSTANCE,
              generator.generate());
  directory.add(file);
}
 
Example #2
Source File: GutterTestUtil.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
static List<GotoRelatedItem> getGutterNavigationDestinationElements(LineMarkerInfo.LineMarkerGutterIconRenderer gutter) {
    LineMarkerProvider lineMarkerProvider1 = LineMarkersPass.getMarkerProviders(JavaLanguage.INSTANCE, gutter
        .getLineMarkerInfo()
        .getElement().getProject())
        .stream()
        .filter(lineMarkerProvider -> lineMarkerProvider instanceof CamelRouteLineMarkerProvider).findAny()
        .get();
    List<RelatedItemLineMarkerInfo> result = new ArrayList<>();
    ((CamelRouteLineMarkerProvider) lineMarkerProvider1).collectNavigationMarkers(gutter.getLineMarkerInfo().getElement(), result);
    return (List<GotoRelatedItem>) result.get(0).createGotoRelatedItems();
}
 
Example #3
Source File: SqliteMagicLightMethodBuilder.java    From sqlitemagic with Apache License 2.0 5 votes vote down vote up
public SqliteMagicLightMethodBuilder(@NotNull PsiManager manager, @NotNull String name) {
  super(manager, JavaLanguage.INSTANCE, name,
      new SqliteMagicLightParameterListBuilder(manager, JavaLanguage.INSTANCE),
      new SqliteMagicLightModifierList(manager, JavaLanguage.INSTANCE));
  myThrowsList = new SqliteMagicLightReferenceListBuilder(manager, JavaLanguage.INSTANCE, PsiReferenceList.Role.THROWS_LIST);
  setBaseIcon(SqliteMagicIcons.METHOD_ICON);
}
 
Example #4
Source File: WitherFieldProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
public PsiMethod createWitherMethod(@NotNull PsiField psiField, @NotNull String methodModifier, @NotNull AccessorsInfo accessorsInfo) {
  LombokLightMethodBuilder methodBuilder = null;
  final PsiClass psiFieldContainingClass = psiField.getContainingClass();
  if (psiFieldContainingClass != null) {
    final PsiType returnType = PsiClassUtil.getTypeWithGenerics(psiFieldContainingClass);
    final String psiFieldName = psiField.getName();
    final PsiType psiFieldType = psiField.getType();

    methodBuilder = new LombokLightMethodBuilder(psiField.getManager(), getWitherName(accessorsInfo, psiFieldName, psiFieldType))
      .withMethodReturnType(returnType)
      .withContainingClass(psiFieldContainingClass)
      .withNavigationElement(psiField)
      .withModifier(methodModifier);

    PsiAnnotation witherAnnotation = PsiAnnotationSearchUtil.findAnnotation(psiField, Wither.class, With.class);
    addOnXAnnotations(witherAnnotation, methodBuilder.getModifierList(), "onMethod");

    final LombokLightParameter methodParameter = new LombokLightParameter(psiFieldName, psiFieldType, methodBuilder, JavaLanguage.INSTANCE);
    PsiModifierList methodParameterModifierList = methodParameter.getModifierList();
    copyAnnotations(psiField, methodParameterModifierList,
      LombokUtils.NON_NULL_PATTERN, LombokUtils.NULLABLE_PATTERN, LombokUtils.DEPRECATED_PATTERN);
    addOnXAnnotations(witherAnnotation, methodParameterModifierList, "onParam");
    methodBuilder.withParameter(methodParameter);

    if (psiFieldContainingClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
      methodBuilder.withModifier(PsiModifier.ABSTRACT);
    } else {
      final String paramString = getConstructorCall(psiField, psiFieldContainingClass);
      final String blockText = String.format("return this.%s == %s ? this : new %s(%s);", psiFieldName, psiFieldName, returnType.getCanonicalText(), paramString);
      methodBuilder.withBody(PsiMethodUtil.createCodeBlockFromText(blockText, methodBuilder));
    }
  }
  return methodBuilder;
}
 
Example #5
Source File: LombokLightFieldBuilder.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public LombokLightFieldBuilder(@NotNull PsiManager manager, @NotNull String name, @NotNull PsiType type) {
  super(manager, name, type);
  myName = name;
  myNameIdentifier = new LombokLightIdentifier(manager, name);
  myModifierList = new LombokLightModifierList(manager, JavaLanguage.INSTANCE, Collections.emptyList());
  setBaseIcon(LombokIcons.FIELD_ICON);
}
 
Example #6
Source File: LombokLightMethodBuilder.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public LombokLightMethodBuilder(@NotNull PsiManager manager, @NotNull String name) {
  super(manager, JavaLanguage.INSTANCE, name,
    new LombokLightParameterListBuilder(manager, JavaLanguage.INSTANCE),
    new LombokLightModifierList(manager, JavaLanguage.INSTANCE, Collections.emptySet()),
    new LombokLightReferenceListBuilder(manager, JavaLanguage.INSTANCE, PsiReferenceList.Role.THROWS_LIST),
    new LightTypeParameterListBuilder(manager, JavaLanguage.INSTANCE));
  setBaseIcon(LombokIcons.METHOD_ICON);
}
 
Example #7
Source File: JavaGetImportCandidatesHandler.java    From ijaas with Apache License 2.0 4 votes vote down vote up
@Override
protected Response handle(Request request) {
  Project project = findProject(request.file);
  if (project == null) {
    throw new RuntimeException("Cannot find the target project");
  }
  Application application = ApplicationManager.getApplication();
  Response response = new Response();
  application.runReadAction(
      () -> {
        PsiFile psiFile =
            PsiFileFactory.getInstance(project)
                .createFileFromText(JavaLanguage.INSTANCE, request.text);
        if (!(psiFile instanceof PsiJavaFile)) {
          throw new RuntimeException("Cannot parse as Java file");
        }
        PsiJavaFile psiJavaFile = (PsiJavaFile) psiFile;

        Set<String> processed = new HashSet<>();
        for (PsiClass psiClass : psiJavaFile.getClasses()) {
          psiClass.accept(
              new JavaRecursiveElementWalkingVisitor() {
                @Override
                public void visitReferenceElement(PsiJavaCodeReferenceElement reference) {
                  try {
                    if (reference.getQualifier() != null) {
                      return;
                    }
                    String name = reference.getReferenceName();
                    if (processed.contains(name)) {
                      return;
                    }
                    processed.add(name);

                    Set<String> candidates = new HashSet<>();
                    for (PsiClass t : new ImportClassFix(reference).getClassesToImport()) {
                      candidates.add(String.format("import %s;", t.getQualifiedName()));
                    }
                    if (!candidates.isEmpty()) {
                      response.choices.add(candidates.stream().sorted().collect(toList()));
                    }
                  } finally {
                    super.visitReferenceElement(reference);
                  }
                }
              });
        }
      });
  return response;
}
 
Example #8
Source File: StatePropCompletionContributor.java    From litho with Apache License 2.0 4 votes vote down vote up
private static ElementPattern<? extends PsiElement> codeReferencePattern() {
  return PlatformPatterns.psiElement(PsiIdentifier.class)
      .withParent(PsiJavaCodeReferenceElement.class)
      .withLanguage(JavaLanguage.INSTANCE);
}
 
Example #9
Source File: IdeaUtils.java    From camel-idea-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Is the element from Java language
 */
public boolean isJavaLanguage(PsiElement element) {
    return element != null && PsiUtil.getNotAnyLanguage(element.getNode()).is(JavaLanguage.INSTANCE);
}
 
Example #10
Source File: SqliteMagicLightMethodBuilder.java    From sqlitemagic with Apache License 2.0 4 votes vote down vote up
public SqliteMagicLightMethodBuilder withParameter(@NotNull String name, @NotNull PsiType type) {
  return withParameter(new SqliteMagicLightParameter(name, type, this, JavaLanguage.INSTANCE));
}
 
Example #11
Source File: LombokLightMethodBuilder.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@NotNull
private LombokLightParameter createParameter(@NotNull String name, @NotNull PsiType type) {
  return new LombokLightParameter(name, type, this, JavaLanguage.INSTANCE);
}
 
Example #12
Source File: LombokInlineMethodHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public boolean canInlineElement(PsiElement element) {
  return element instanceof LombokLightMethodBuilder && element.getLanguage() == JavaLanguage.INSTANCE;
}
 
Example #13
Source File: MarkdownDocumentationProvider.java    From markdown-doclet with GNU General Public License v3.0 4 votes vote down vote up
@Nullable
@Override
public String generateDoc(PsiElement element, @Nullable PsiElement originalElement) {
    boolean process = false;
    for ( Class supported: SUPPORTED_ELEMENT_TYPES ) {
        if ( supported.isInstance(element) ) {
            process = true;
            break;
        }
    }
    if ( !process ) {
        return null;
    }
    PsiFile file = null;
    if ( element instanceof PsiDirectory ) {
        // let's see whether we can map the directory to a package; if so, change the
        // element to the package and continue
        PsiPackage pkg = JavaDirectoryService.getInstance().getPackage((PsiDirectory)element);
        if ( pkg != null ) {
            element = pkg;
        }
        else {
            return null;
        }
    }
    if ( element instanceof PsiPackage ) {
        for ( PsiDirectory dir : ((PsiPackage)element).getDirectories() ) {
            PsiFile info = dir.findFile(PsiPackage.PACKAGE_INFO_FILE);
            if ( info != null ) {
                ASTNode node = info.getNode();
                if ( node != null ) {
                    ASTNode docCommentNode = node.findChildByType(JavaDocElementType.DOC_COMMENT);
                    if ( docCommentNode != null ) {
                        // the default implementation will now use this file
                        // we're going to take over below, if Markdown is enabled in
                        // the corresponding module
                        // see JavaDocInfoGenerator.generatePackageJavaDoc()
                        file = info;
                        break;
                    }
                }
            }
            if ( dir.findFile("package.html") != null ) {
                // leave that to the default
                return null;
            }
        }
    }
    else {
        if ( JavaLanguage.INSTANCE.equals(element.getLanguage()) ) {
            element = element.getNavigationElement();
            if ( element.getContainingFile() != null ) {
                file = element.getContainingFile();
            }
        }
    }
    if ( file != null ) {
        DocCommentProcessor processor = new DocCommentProcessor(file);
        if ( processor.isEnabled() ) {
            String docHtml;
            if ( element instanceof PsiMethod ) {
                docHtml = super.generateDoc(PsiProxy.forMethod((PsiMethod)element), originalElement);
            }
            else if ( element instanceof PsiParameter ) {
                docHtml = super.generateDoc(PsiProxy.forParameter((PsiParameter)element), originalElement);
            }
            else {
                MarkdownJavaDocInfoGenerator javaDocInfoGenerator = new MarkdownJavaDocInfoGenerator(element.getProject(), element, processor);
                List<String> docURLs = getExternalJavaDocUrl(element);
                String text = javaDocInfoGenerator.generateDocInfo(docURLs);
                Plugin.print("Intermediate HTML output", text);
                docHtml = JavaDocExternalFilter.filterInternalDocInfo(text);
            }
            docHtml = extendCss(docHtml);
            Plugin.print("Final HTML output", docHtml);
            return docHtml;
        }
        else {
            return null;
        }
    }
    else {
        return null;
    }
}