feat: 2.25 use xml/html/vue 1st or 2nd line comment for tree doc | 用 xml/html/vue 第 1 或第 2 行注释当文件注释
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package io.github.linwancen.plugin.show.lang;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import io.github.linwancen.plugin.show.ext.listener.FileLoader;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* for html, in here because {@code <!--} is based XML
|
||||
*/
|
||||
public class XmlCache extends FileLoader {
|
||||
|
||||
public static void indexDocToDirDoc(@NotNull VirtualFile virtualFile, @Nullable String doc) {
|
||||
if (doc != null && "index".equals(virtualFile.getNameWithoutExtension())) {
|
||||
VirtualFile parent = virtualFile.getParent();
|
||||
if (parent != null) {
|
||||
@Nullable XmlCache extension = FileLoader.EPN.findExtension(XmlCache.class);
|
||||
if (extension != null) {
|
||||
extension.fileDoc.put(parent, doc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void loadAllImpl(Project project) {}
|
||||
|
||||
@Override
|
||||
protected void loadFileImpl(@NotNull VirtualFile file, @Nullable Project project) {}
|
||||
}
|
||||
@@ -1,12 +1,19 @@
|
||||
package io.github.linwancen.plugin.show.lang;
|
||||
|
||||
import com.intellij.ide.projectView.ProjectViewNode;
|
||||
import com.intellij.lang.xml.XMLLanguage;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.extensions.ExtensionPointName;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.FileViewProvider;
|
||||
import com.intellij.psi.PsiComment;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiManager;
|
||||
import com.intellij.psi.xml.XmlAttribute;
|
||||
import com.intellij.psi.xml.XmlTag;
|
||||
import io.github.linwancen.plugin.show.bean.LineInfo;
|
||||
import io.github.linwancen.plugin.show.bean.SettingsInfo;
|
||||
import io.github.linwancen.plugin.show.lang.base.BaseLangDoc;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -29,6 +36,53 @@ public class XmlLangDoc extends BaseLangDoc {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable <T extends SettingsInfo> String treeDoc(@NotNull T info, @NotNull ProjectViewNode<?> node,
|
||||
@NotNull Project project) {
|
||||
@Nullable VirtualFile virtualFile = node.getVirtualFile();
|
||||
if (virtualFile == null) {
|
||||
return null;
|
||||
}
|
||||
@Nullable FileViewProvider viewProvider = PsiManager.getInstance(project).findViewProvider(virtualFile);
|
||||
if (viewProvider == null) {
|
||||
return null;
|
||||
}
|
||||
PsiElement psiElement = viewProvider.findElementAt(0);
|
||||
if (psiElement == null || !"<!--".equals(psiElement.getText())) {
|
||||
Document document = viewProvider.getDocument();
|
||||
if (document == null) {
|
||||
return null;
|
||||
}
|
||||
int lineCount = document.getLineCount();
|
||||
// lineNumber start 0, as 1 <= 1 should return
|
||||
if (lineCount <= 1) {
|
||||
return null;
|
||||
}
|
||||
// because in html 1st line must <!DOCTYPE html>
|
||||
int i = document.getLineStartOffset(1);
|
||||
psiElement = viewProvider.findElementAt(i);
|
||||
}
|
||||
if (psiElement == null || !"<!--".equals(psiElement.getText())) {
|
||||
return null;
|
||||
}
|
||||
PsiElement parent = psiElement.getParent();
|
||||
if (!(parent instanceof PsiComment)) {
|
||||
return null;
|
||||
}
|
||||
PsiElement[] children = parent.getChildren();
|
||||
if (children.length < 2) {
|
||||
return null;
|
||||
}
|
||||
String doc = children[1].getText();
|
||||
// Copyright or copyright
|
||||
//noinspection SpellCheckingInspection
|
||||
if (doc == null || doc.contains("opyright")) {
|
||||
return null;
|
||||
}
|
||||
XmlCache.indexDocToDirDoc(virtualFile, doc);
|
||||
return doc;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String findRefDoc(@NotNull LineInfo info, @NotNull FileViewProvider viewProvider, @NotNull PsiElement element) {
|
||||
|
||||
@@ -169,6 +169,7 @@ Show doc comment in the Project view Tree, line End, json, other
|
||||
</extensionPoints>
|
||||
<extensions defaultExtensionNs="io.github.linwancen.show-comment">
|
||||
<fileLoader implementation="io.github.linwancen.plugin.show.ext.conf.ConfCache"/>
|
||||
<fileLoader implementation="io.github.linwancen.plugin.show.lang.XmlCache"/>
|
||||
<fileLoader implementation="io.github.linwancen.plugin.show.ext.ollama.OllamaModelsCache"/>
|
||||
</extensions>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user