From d45f5724ae4ed446f638d534daee3427c1d90644 Mon Sep 17 00:00:00 2001 From: Alexander Doroshko Date: Mon, 17 Dec 2018 23:53:53 +0300 Subject: [PATCH] Use MDN data in CSS documentation --- .../HtmlDocumentationProvider.java | 64 +++---------------- .../documentation/MdnDocumentationUtil.java | 41 ++++++++++-- 2 files changed, 46 insertions(+), 59 deletions(-) diff --git a/xml/impl/src/com/intellij/xml/util/documentation/HtmlDocumentationProvider.java b/xml/impl/src/com/intellij/xml/util/documentation/HtmlDocumentationProvider.java index 3eb4fd0b75b3..836767b29758 100644 --- a/xml/impl/src/com/intellij/xml/util/documentation/HtmlDocumentationProvider.java +++ b/xml/impl/src/com/intellij/xml/util/documentation/HtmlDocumentationProvider.java @@ -1,23 +1,8 @@ -/* - * Copyright 2000-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2018 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.xml.util.documentation; import com.intellij.lang.Language; import com.intellij.lang.LanguageDocumentation; -import com.intellij.lang.documentation.DocumentationMarkup; import com.intellij.lang.documentation.DocumentationProvider; import com.intellij.lang.documentation.ExternalDocumentationProvider; import com.intellij.openapi.application.ReadAction; @@ -101,10 +86,11 @@ public class HtmlDocumentationProvider implements DocumentationProvider, Externa if (url.contains("#attr-")) return null; } - String mdn = MdnDocumentationUtil.fetchExternalDocumentation(docUrls, () -> null); - if (mdn != null) { + String mdnDoc = MdnDocumentationUtil.fetchExternalDocumentation(docUrls, () -> null); + if (mdnDoc != null) { String name = descriptor != null ? descriptor.getName() : ReadAction.compute(() -> SymbolPresentationUtil.getSymbolPresentableText(element)); - return generateJavaDoc(descriptor, name, originalElement, mdn); + Map mdnCompatData = ReadAction.compute(() -> getCompatData(descriptor, originalElement)); + return MdnDocumentationUtil.buildDoc(name, mdnDoc, mdnCompatData); } return null; } @@ -230,7 +216,9 @@ public class HtmlDocumentationProvider implements DocumentationProvider, Externa if (descriptor != null) { String description = descriptor.getDescription(); if (!description.endsWith(".")) description += "."; - return generateJavaDoc(descriptor, descriptor.getName(), originalElement, description); + + Map mdnData = ReadAction.compute(() -> getCompatData(descriptor, originalElement)); + return MdnDocumentationUtil.buildDoc(descriptor.getName(), description, mdnData); } if (element instanceof XmlEntityDecl) { final XmlEntityDecl entityDecl = (XmlEntityDecl)element; @@ -240,40 +228,6 @@ public class HtmlDocumentationProvider implements DocumentationProvider, Externa return null; } - private static String generateJavaDoc(EntityDescriptor descriptor, - String name, - PsiElement element, - String description) { - StringBuilder buf = new StringBuilder(); - - buf.append(DocumentationMarkup.DEFINITION_START).append(name).append(DocumentationMarkup.DEFINITION_END); - buf.append(DocumentationMarkup.CONTENT_START); - buf.append(StringUtil.capitalize(description)); - buf.append(DocumentationMarkup.CONTENT_END); - - Map data = ReadAction.compute(() -> getCompatData(descriptor, element)); - String compatibilityData = MdnDocumentationUtil.getFormattedCompatibilityData(data); - - boolean deprecated = MdnDocumentationUtil.isDeprecated(data); - if (deprecated || !compatibilityData.isEmpty()) { - buf.append(DocumentationMarkup.SECTIONS_START); - } - if (deprecated) { - buf.append(DocumentationMarkup.SECTION_HEADER_START).append("Deprecated"); - buf.append(DocumentationMarkup.SECTION_END); - } - if (!compatibilityData.isEmpty()) { - buf.append(DocumentationMarkup.SECTION_HEADER_START).append("Supported by:"); - buf.append(DocumentationMarkup.SECTION_SEPARATOR).append(compatibilityData); - buf.append(DocumentationMarkup.SECTION_END); - } - if (deprecated || !compatibilityData.isEmpty()) { - buf.append(DocumentationMarkup.SECTIONS_END); - } - - return buf.toString(); - } - @Nullable private static Map getCompatData(EntityDescriptor descriptor, @Nullable PsiElement element) { XmlAttribute attribute = ReadAction.compute(() -> PsiTreeUtil.getParentOfType(element, XmlAttribute.class, true)); @@ -371,7 +325,7 @@ public class HtmlDocumentationProvider implements DocumentationProvider, Externa public static void registerScriptDocumentationProvider(final DocumentationProvider provider) { ourScriptProvider = provider; } - + @Nullable private DocumentationProvider getStyleProvider() { if (!myUseStyleProvider) return null; diff --git a/xml/impl/src/com/intellij/xml/util/documentation/MdnDocumentationUtil.java b/xml/impl/src/com/intellij/xml/util/documentation/MdnDocumentationUtil.java index b56710f06c46..15a4ff20655f 100644 --- a/xml/impl/src/com/intellij/xml/util/documentation/MdnDocumentationUtil.java +++ b/xml/impl/src/com/intellij/xml/util/documentation/MdnDocumentationUtil.java @@ -8,6 +8,7 @@ import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.platform.templates.github.DownloadUtil; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.File; @@ -21,7 +22,8 @@ public class MdnDocumentationUtil { private static final List VISIBLE_BROWSERS = Arrays.asList("chrome", "chrome_android", "edge", "firefox", "ie", "opera", "safari", "safari_ios"); - public static String getFormattedCompatibilityData(@Nullable Map data) { + @NotNull + private static String getFormattedCompatibilityData(@Nullable Map data) { Object compat = data != null ? data.get("__compat") : null; if (compat != null) { Map support = (Map)((Map)compat).get("support"); @@ -58,7 +60,7 @@ public class MdnDocumentationUtil { return ""; } - protected static boolean anyVersion(String browser, String version) { + private static boolean anyVersion(String browser, String version) { if (browser.startsWith("edge") && "12".equals(version)) return true; if (browser.equals("firefox_android") && "4".equals(version)) return true; if (browser.equals("chrome_android") && "18".equals(version)) return true; @@ -91,7 +93,7 @@ public class MdnDocumentationUtil { return null; } - public static boolean isDeprecated(Map data) { + public static boolean isDeprecated(@Nullable Map data) { Object compat = data != null ? data.get("__compat") : null; if (compat != null) { Object status = ((Map)compat).get("status"); @@ -151,7 +153,7 @@ public class MdnDocumentationUtil { return s.replaceAll("href=\"/", "href=\"https://developer.mozilla.org/"); } - public static String makeUniqueFileName(String filePath) { + private static String makeUniqueFileName(String filePath) { String path = filePath; for (String prefix : HTTP_PREFIXES) { if (filePath.contains(prefix)) { @@ -161,4 +163,35 @@ public class MdnDocumentationUtil { } return path.replace('/', '_').replace('\\', '_'); } + + @NotNull + public static String buildDoc(@NotNull String name, @NotNull String description, @Nullable Map mdnCompatData) { + StringBuilder buf = new StringBuilder(); + + buf.append(DocumentationMarkup.DEFINITION_START).append(name).append(DocumentationMarkup.DEFINITION_END); + buf.append(DocumentationMarkup.CONTENT_START); + buf.append(StringUtil.capitalize(description)); + buf.append(DocumentationMarkup.CONTENT_END); + + String compatibilityData = getFormattedCompatibilityData(mdnCompatData); + + boolean deprecated = isDeprecated(mdnCompatData); + if (deprecated || !compatibilityData.isEmpty()) { + buf.append(DocumentationMarkup.SECTIONS_START); + } + if (deprecated) { + buf.append(DocumentationMarkup.SECTION_HEADER_START).append("Deprecated"); + buf.append(DocumentationMarkup.SECTION_END); + } + if (!compatibilityData.isEmpty()) { + buf.append(DocumentationMarkup.SECTION_HEADER_START).append("Supported by:"); + buf.append(DocumentationMarkup.SECTION_SEPARATOR).append(compatibilityData); + buf.append(DocumentationMarkup.SECTION_END); + } + if (deprecated || !compatibilityData.isEmpty()) { + buf.append(DocumentationMarkup.SECTIONS_END); + } + + return buf.toString(); + } }