From 54f102fdfbf926e7f88b0473b637eb7541e8181d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=9E=97=E4=B8=87=E7=A8=8B?= <1498425439@qq.com>
Date: Sun, 27 Aug 2023 14:24:04 +0800
Subject: [PATCH] =?UTF-8?q?2.09=20python=20doc=20strings=20|=20Python=20?=
=?UTF-8?q?=E6=96=87=E6=A1=A3=E5=AD=97=E7=AC=A6=E4=B8=B2=20fixed=20#41?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +
build.gradle | 4 +-
.../plugin/show/lang/PythonLangDoc.java | 48 ++++++++++++++++++-
.../plugin/show/lang/base/DocFilter.java | 9 +++-
4 files changed, 58 insertions(+), 5 deletions(-)
diff --git a/README.md b/README.md
index 41a92a9..aad0ecd 100644
--- a/README.md
+++ b/README.md
@@ -113,6 +113,7 @@ Show doc comment at the Project view Tree, line End, json, other
English Change Notes:
+- 2.09 Add line-end-comment support Python doc strings
- 2.08 Add i18n and chinese
- 2.07 Add global-setting reset default
- 2.06 Add project-view-tree xx-abc.xxx from Abc.java doc
@@ -150,6 +151,7 @@ Show doc comment at the Project view Tree, line End, json, other
中文更新说明:
+- 2.09 增加 行末注释 支持 Python 文档字符串
- 2.08 增加 多语言与中文支持
- 2.07 增加 全局设置 复位默认值
- 2.06 增加 文件树注释 xx-abc.xxx 来自 Abc.java 的文档注释
diff --git a/build.gradle b/build.gradle
index 86f1b5d..44def5a 100644
--- a/build.gradle
+++ b/build.gradle
@@ -4,7 +4,7 @@ plugins {
}
group 'io.github.linwancen'
-version '2.08.0.' + (new Date().format('yyyy.MM.dd_HH.mm'))
+version '2.09.0.' + (new Date().format('yyyy.MM.dd_HH.mm'))
repositories {
mavenCentral()
@@ -87,6 +87,7 @@ patchPluginXml {
changeNotes = """
English Change Notes:
+- 2.09 Add line-end-comment support Python doc strings
- 2.08 Add i18n and chinese
- 2.07 Add global-setting reset default
- 2.06 Add project-view-tree xx-abc.xxx from Abc.java doc
@@ -124,6 +125,7 @@ patchPluginXml {
中文更新说明:
+- 2.09 增加 行末注释 支持 Python 文档字符串
- 2.08 增加 多语言与中文支持
- 2.07 增加 全局设置 复位默认值
- 2.06 增加 文件树注释 xx-abc.xxx 来自 Abc.java 的文档注释
diff --git a/src/main/java/io/github/linwancen/plugin/show/lang/PythonLangDoc.java b/src/main/java/io/github/linwancen/plugin/show/lang/PythonLangDoc.java
index 12c860b..b45437d 100644
--- a/src/main/java/io/github/linwancen/plugin/show/lang/PythonLangDoc.java
+++ b/src/main/java/io/github/linwancen/plugin/show/lang/PythonLangDoc.java
@@ -2,12 +2,20 @@ package io.github.linwancen.plugin.show.lang;
import com.intellij.psi.PsiElement;
import com.jetbrains.python.PythonLanguage;
+import com.jetbrains.python.documentation.docstrings.TagBasedDocString;
+import com.jetbrains.python.psi.PyDocStringOwner;
import com.jetbrains.python.psi.PyReferenceExpression;
+import com.jetbrains.python.psi.StructuredDocString;
+import com.jetbrains.python.toolbox.Substring;
import io.github.linwancen.plugin.show.bean.LineInfo;
-import io.github.linwancen.plugin.show.lang.base.BaseLangDoc;
+import io.github.linwancen.plugin.show.bean.SettingsInfo;
+import io.github.linwancen.plugin.show.lang.base.BaseTagLangDoc;
+import io.github.linwancen.plugin.show.lang.base.DocFilter;
+import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
-public class PythonLangDoc extends BaseLangDoc {
+public class PythonLangDoc extends BaseTagLangDoc {
static {
LANG_DOC_MAP.put(PythonLanguage.INSTANCE.getID(), new PythonLangDoc());
@@ -22,4 +30,40 @@ public class PythonLangDoc extends BaseLangDoc {
public boolean show(@NotNull LineInfo lineInfo) {
return lineInfo.appSettings.showLineEndCommentPy;
}
+
+ @Nullable
+ @Override
+ protected StructuredDocString toDocElement(@NotNull T settingsInfo,
+ @NotNull PsiElement resolve) {
+ if (resolve instanceof PyDocStringOwner) {
+ @NotNull PyDocStringOwner pyDocStringOwner = (PyDocStringOwner) resolve;
+ return pyDocStringOwner.getStructuredDocString();
+ }
+ return null;
+ }
+
+ @NotNull
+ @Override
+ protected String descDoc(@NotNull T lineInfo,
+ @NotNull StructuredDocString structuredDocString) {
+ String summary = structuredDocString.getSummary();
+ if (StringUtils.isNotEmpty(summary)) {
+ return summary;
+ }
+ @NotNull String description = structuredDocString.getDescription();
+ return DocFilter.cutDoc(DocFilter.html2Text(description), lineInfo.appSettings, false);
+ }
+
+ @Override
+ protected void appendTag(@NotNull T lineInfo, @NotNull StringBuilder tagStrBuilder,
+ @NotNull StructuredDocString structuredDocString,
+ @NotNull String name) {
+ if (structuredDocString instanceof TagBasedDocString) {
+ @Nullable Substring tagValue = ((TagBasedDocString) structuredDocString).getTagValue(name);
+ if (tagValue != null) {
+ @NotNull String cutDoc = DocFilter.cutDoc(tagValue.getValue(), lineInfo.appSettings, false);
+ tagStrBuilder.append(cutDoc);
+ }
+ }
+ }
}
diff --git a/src/main/java/io/github/linwancen/plugin/show/lang/base/DocFilter.java b/src/main/java/io/github/linwancen/plugin/show/lang/base/DocFilter.java
index d92d07c..fc23f1f 100644
--- a/src/main/java/io/github/linwancen/plugin/show/lang/base/DocFilter.java
+++ b/src/main/java/io/github/linwancen/plugin/show/lang/base/DocFilter.java
@@ -92,9 +92,14 @@ public class DocFilter {
* trim end with space
*/
public static void addHtml(@NotNull StringBuilder sb, @NotNull String s) {
- @NotNull String deleteHtml = HTML_PATTERN.matcher(s).replaceAll("").trim();
+ @NotNull String deleteHtml = html2Text(s);
if (deleteHtml.length() > 0) {
- sb.append(deleteHtml).append(" ");
+ sb.append(deleteHtml);
}
}
+
+ @NotNull
+ public static String html2Text(@NotNull String s) {
+ return HTML_PATTERN.matcher(s).replaceAll(" ").trim();
+ }
}