From 4e8575578e6ae2013edec96ba26cd8def93deef8 Mon Sep 17 00:00:00 2001 From: Alexey Utkin Date: Fri, 18 Jan 2019 21:43:16 +0300 Subject: [PATCH] CPP-14614,CPP-15164 Recognize presence of .clang-format configuration * move `ClangFormatSettings` back to `ClangFormat` plugin, clean it from static methods * introduce new `ExternalFormatProcessor` extension point * fix CPP-15164: Update ClangFormat plugin description * move clangd-format procedure from `OCPostFormatProcessor` to `ClangDaemonFormatProvider` --- .../codeStyle/ExternalFormatProcessor.java | 70 +++++++++++++++++++ .../src/META-INF/LangExtensionPoints.xml | 1 + 2 files changed, 71 insertions(+) create mode 100644 platform/lang-impl/src/com/intellij/psi/codeStyle/ExternalFormatProcessor.java diff --git a/platform/lang-impl/src/com/intellij/psi/codeStyle/ExternalFormatProcessor.java b/platform/lang-impl/src/com/intellij/psi/codeStyle/ExternalFormatProcessor.java new file mode 100644 index 000000000000..acadd1a10a2a --- /dev/null +++ b/platform/lang-impl/src/com/intellij/psi/codeStyle/ExternalFormatProcessor.java @@ -0,0 +1,70 @@ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.psi.codeStyle; + +import com.intellij.openapi.extensions.ExtensionPointName; +import com.intellij.openapi.util.TextRange; +import com.intellij.psi.PsiFile; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; + +import java.util.Optional; + +@ApiStatus.Experimental +public interface ExternalFormatProcessor { + ExtensionPointName EP_NAME = ExtensionPointName.create("com.intellij.externalFormatProcessor"); + + /** + * @param source the source file with code + * @return true, if external processor selected as active (enabled) for the source file + */ + boolean activeForFile(@NotNull PsiFile source); + + /** + * Formats the range in a source file. + * + * @param source the source file with code + * @param range the range for formatting + * @return the range after formatting or the initial range, if external format procedure cannot be applied to the source + */ + TextRange format(@NotNull PsiFile source, @NotNull TextRange range); + + /** + * @return the unique id for external formatter + */ + @NonNls + @NotNull + String getId(); + + /** + * @param source the source file with code + * @return true, if there is an active external (enabled) formatter for the source + */ + static boolean useExternalFormatter(@NotNull PsiFile source) { + return EP_NAME.getExtensionList().stream().anyMatch(efp -> efp.activeForFile(source)); + } + + /** + * @param externalFormatterId the unique id for external formatter + * @return the external formatter with the unique id, if any + */ + @NotNull + static Optional findExternalFormatter(@NonNls @NotNull String externalFormatterId) { + return EP_NAME.getExtensionList().stream().filter(efp -> externalFormatterId.equals(efp.getId())).findFirst(); + } + + /** + * @param source the source file with code + * @param range the range for formatting + * @return the range after formatting or empty value, if external format procedure was not found or inactive (disabled) + */ + @NotNull + static Optional formatExternally(@NotNull PsiFile source, @NotNull TextRange range) { + for (ExternalFormatProcessor efp : EP_NAME.getExtensionList()) { + if (efp.activeForFile(source)) { + return Optional.of(efp.format(source, range)); + } + } + return Optional.empty(); + } +} diff --git a/platform/platform-resources/src/META-INF/LangExtensionPoints.xml b/platform/platform-resources/src/META-INF/LangExtensionPoints.xml index 07cb2f90e566..a82d6f126672 100644 --- a/platform/platform-resources/src/META-INF/LangExtensionPoints.xml +++ b/platform/platform-resources/src/META-INF/LangExtensionPoints.xml @@ -323,6 +323,7 @@ +