2.09 python doc strings | Python 文档字符串 fixed #41

This commit is contained in:
林万程
2023-08-27 14:24:04 +08:00
parent c86901c4be
commit 54f102fdfb
4 changed files with 58 additions and 5 deletions

View File

@@ -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<StructuredDocString> {
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 <T extends SettingsInfo> 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 <T extends SettingsInfo> 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 <T extends SettingsInfo> 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);
}
}
}
}

View File

@@ -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();
}
}