1.13 Add Copy With Line Comment & Add Line Comment | 增加 带行末注释复制 和 添加行末注释

This commit is contained in:
林万程
2022-04-10 19:42:26 +08:00
parent 595d3730d4
commit baa50ba361
29 changed files with 385 additions and 185 deletions

View File

@@ -0,0 +1,69 @@
package io.github.linwancen.plugin.show.line;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.javadoc.PsiDocComment;
import io.github.linwancen.plugin.show.doc.PsiDocToStrDoc;
import io.github.linwancen.plugin.show.ext.LineExt;
import io.github.linwancen.plugin.show.settings.AppSettingsState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* call LineExt, ~LeftToRight, ~RightToLeft
*/
public class FileViewToDocStrUtils {
private FileViewToDocStrUtils() {}
/**
* From Ext Or PsiDoc
*/
@Nullable
public static String doc(@NotNull Document document,
@Nullable Project project,
@Nullable VirtualFile file,
@Nullable FileViewProvider viewProvider,
int startOffset, int endOffset, @NotNull String text) {
if (file != null) {
String extDoc = LineExt.extDoc(project, file.getPath(), file.getName(), file.getExtension(), text);
if (extDoc != null) {
return extDoc;
}
}
if (viewProvider == null) {
return null;
}
PsiDocComment docComment = AppSettingsState.getInstance().findElementRightToLeft
? FileViewToPsiDocRightToLeft.rightDoc(viewProvider, startOffset, endOffset)
: FileViewToPsiDocLeftToRight.leftDoc(viewProvider, document, startOffset, endOffset);
return PsiDocToStrDoc.text(docComment);
}
@NotNull
public static String textWithDoc(@NotNull AppSettingsState settings, @NotNull Document document,
int startLine, int endLine,
@Nullable Project project,
@Nullable VirtualFile file,
@Nullable FileViewProvider viewProvider) {
StringBuilder sb = new StringBuilder();
for (int i = startLine; i <= endLine; i++) {
int startOffset = document.getLineStartOffset(i);
int endOffset = document.getLineEndOffset(i);
if (startOffset != endOffset) {
String text = document.getText(new TextRange(startOffset, endOffset));
sb.append(text);
String doc = doc(document, project, file, viewProvider, startOffset, endOffset, text);
if (doc != null) {
sb.append(settings.lineEndPrefix).append(doc);
}
}
sb.append("\n");
}
sb.delete(sb.length() - 1, sb.length());
return sb.toString();
}
}

View File

@@ -8,20 +8,23 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiIdentifier;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.util.PsiTreeUtil;
import io.github.linwancen.plugin.show.doc.JsonDocUtils;
import io.github.linwancen.plugin.show.settings.AppSettingsState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class LineDocLeftToRightUtils {
/**
* call JsonToPsiDoc, NewCallRefToPsiDoc
*/
public class FileViewToPsiDocLeftToRight {
private LineDocLeftToRightUtils() {}
private FileViewToPsiDocLeftToRight() {}
static final String[] KEYS = {
"=",
};
@Nullable
public static PsiDocComment leftDoc(FileViewProvider viewProvider, Document document,
public static PsiDocComment leftDoc(@NotNull FileViewProvider viewProvider, @NotNull Document document,
int startOffset, int endOffset) {
String text = document.getText(new TextRange(startOffset, endOffset));
int offset = 0;
@@ -52,7 +55,7 @@ public class LineDocLeftToRightUtils {
}
AppSettingsState instance = AppSettingsState.getInstance();
if (instance.inJson && element.getLanguage().is(JsonLanguage.INSTANCE)) {
return JsonDocUtils.jsonDoc(element, startOffset, endOffset);
return JsonToPsiDoc.jsonDoc(element, startOffset, endOffset);
}
if (startWithSymbol) {
startOffset = 0;
@@ -61,10 +64,10 @@ public class LineDocLeftToRightUtils {
}
@Nullable
private static PsiDocComment nextDoc(PsiElement element, int startOffset, int endOffset) {
private static PsiDocComment nextDoc(@NotNull PsiElement element, int startOffset, int endOffset) {
while (element.getTextRange().getEndOffset() <= endOffset) {
if (element instanceof PsiIdentifier) {
PsiDocComment psiDocComment = LineDocUtils.elementDoc(element, element, startOffset, endOffset);
PsiDocComment psiDocComment = NewCallRefToPsiDoc.elementDoc(element, element, startOffset, endOffset);
if (psiDocComment != null) {
return psiDocComment;
}

View File

@@ -6,16 +6,19 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiIdentifier;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.util.PsiTreeUtil;
import io.github.linwancen.plugin.show.doc.JsonDocUtils;
import io.github.linwancen.plugin.show.settings.AppSettingsState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class LineDocRightToLeftUtils {
/**
* call JsonToPsiDoc, NewCallRefToPsiDoc
*/
public class FileViewToPsiDocRightToLeft {
private LineDocRightToLeftUtils() {}
private FileViewToPsiDocRightToLeft() {}
@Nullable
public static PsiDocComment rightDoc(FileViewProvider viewProvider, int startOffset, int endOffset) {
public static PsiDocComment rightDoc(@NotNull FileViewProvider viewProvider, int startOffset, int endOffset) {
// End is always white, can not -1 because @see class.name need it
PsiElement element = viewProvider.findElementAt(endOffset);
if (element == null) {
@@ -39,15 +42,15 @@ public class LineDocRightToLeftUtils {
}
}
public static PsiDocComment psiDoc(AppSettingsState setting,
private static PsiDocComment psiDoc(AppSettingsState setting,
PsiElement identifier, PsiElement element,
int startOffset, int endOffset) {
if (identifier != null && !(identifier instanceof PsiIdentifier)) {
return null;
}
if (setting.inJson && element.getLanguage().is(JsonLanguage.INSTANCE)) {
return JsonDocUtils.jsonDoc(element, startOffset, endOffset);
return JsonToPsiDoc.jsonDoc(element, startOffset, endOffset);
}
return LineDocUtils.elementDoc(element, identifier, startOffset, endOffset);
return NewCallRefToPsiDoc.elementDoc(element, identifier, startOffset, endOffset);
}
}

View File

@@ -0,0 +1,37 @@
package io.github.linwancen.plugin.show.line;
import com.intellij.json.psi.JsonProperty;
import com.intellij.psi.PsiDocCommentOwner;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.util.PsiTreeUtil;
import io.github.linwancen.plugin.show.doc.OwnerToPsiDocUtils;
import org.jetbrains.annotations.Nullable;
public class JsonToPsiDoc {
private JsonToPsiDoc() {}
/**
* depend on JsonJump
*/
@Nullable
public static PsiDocComment jsonDoc(PsiElement element, int startOffset, int endOffset) {
JsonProperty jsonProp = PsiTreeUtil.getParentOfType(element, JsonProperty.class, true, startOffset);
if (jsonProp == null || jsonProp.getNameElement().getTextRange().getEndOffset() > endOffset) {
return null;
}
for (PsiReference reference : jsonProp.getNameElement().getReferences()) {
PsiElement resolve = reference.resolve();
if (resolve instanceof PsiDocCommentOwner) {
PsiDocCommentOwner owner = (PsiDocCommentOwner) resolve;
PsiDocComment docComment = OwnerToPsiDocUtils.srcOrByteCodeDoc(owner);
if (docComment != null) {
return docComment;
}
}
}
return null;
}
}

View File

@@ -7,9 +7,12 @@ import com.intellij.psi.util.PsiTreeUtil;
import io.github.linwancen.plugin.show.settings.AppSettingsState;
import org.jetbrains.annotations.Nullable;
class LineDocUtils {
/**
* call OwnerToPsiDocSkip
*/
class NewCallRefToPsiDoc {
private LineDocUtils() {}
private NewCallRefToPsiDoc() {}
@Nullable
static PsiDocComment elementDoc(PsiElement element, PsiElement psiIdentifier,
@@ -60,7 +63,7 @@ class LineDocUtils {
return null;
}
try {
PsiDocComment methodComment = SkipDocUtils.methodDoc(call.resolveMethod());
PsiDocComment methodComment = OwnerToPsiDocSkip.methodDoc(call.resolveMethod());
if (methodComment != null) {
return methodComment;
}
@@ -77,7 +80,7 @@ class LineDocUtils {
if (newExp == null) {
return null;
}
PsiDocComment methodComment = SkipDocUtils.methodDoc(newExp.resolveMethod());
PsiDocComment methodComment = OwnerToPsiDocSkip.methodDoc(newExp.resolveMethod());
if (methodComment != null) {
return methodComment;
}
@@ -104,7 +107,7 @@ class LineDocUtils {
if (element instanceof PsiReference) {
PsiElement resolve = ((PsiReference) element).resolve();
if (resolve instanceof PsiDocCommentOwner) {
return SkipDocUtils.refDoc(((PsiDocCommentOwner) resolve));
return OwnerToPsiDocSkip.refDoc(((PsiDocCommentOwner) resolve));
}
}
// for right to left for
@@ -128,7 +131,7 @@ class LineDocUtils {
}
PsiElement resolve = ref.resolve();
if (resolve instanceof PsiDocCommentOwner) {
return SkipDocUtils.refDoc(((PsiDocCommentOwner) resolve));
return OwnerToPsiDocSkip.refDoc(((PsiDocCommentOwner) resolve));
}
return null;
}
@@ -146,10 +149,10 @@ class LineDocUtils {
}
PsiElement resolve = reference.resolve();
if (resolve instanceof PsiMethod) {
return SkipDocUtils.methodDoc(((PsiMethod) resolve));
return OwnerToPsiDocSkip.methodDoc(((PsiMethod) resolve));
}
if (resolve instanceof PsiDocCommentOwner) {
return SkipDocUtils.refDoc(((PsiDocCommentOwner) resolve));
return OwnerToPsiDocSkip.refDoc(((PsiDocCommentOwner) resolve));
}
}
return null;

View File

@@ -4,18 +4,21 @@ import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiDocCommentOwner;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.javadoc.PsiDocComment;
import io.github.linwancen.plugin.show.doc.DocUtils;
import io.github.linwancen.plugin.show.doc.OwnerToPsiDocUtils;
import org.jetbrains.annotations.Nullable;
class SkipDocUtils {
/**
* call RefToPsiDoc, PsiClassSkip
*/
class OwnerToPsiDocSkip {
private SkipDocUtils() {}
private OwnerToPsiDocSkip() {}
static PsiDocComment methodDoc(@Nullable PsiMethod psiMethod) {
if (skip(psiMethod)) {
return null;
}
return DocUtils.methodDoc(psiMethod);
return OwnerToPsiDocUtils.methodDoc(psiMethod);
}
static PsiDocComment refDoc(@Nullable PsiDocCommentOwner docOwner) {
@@ -23,18 +26,18 @@ class SkipDocUtils {
return null;
}
if (docOwner instanceof PsiMethod) {
return DocUtils.methodDoc(((PsiMethod) docOwner));
return OwnerToPsiDocUtils.methodDoc(((PsiMethod) docOwner));
}
return DocUtils.srcOrByteCodeDoc(docOwner);
return OwnerToPsiDocUtils.srcOrByteCodeDoc(docOwner);
}
static boolean skip(@Nullable PsiDocCommentOwner docOwner) {
private static boolean skip(@Nullable PsiDocCommentOwner docOwner) {
if (docOwner == null) {
return true;
}
if (docOwner instanceof PsiClass) {
return SkipUtils.skip((PsiClass) docOwner, docOwner.getProject());
return PsiClassSkip.skip((PsiClass) docOwner, docOwner.getProject());
}
return SkipUtils.skip(docOwner.getContainingClass(), docOwner.getProject());
return PsiClassSkip.skip(docOwner.getContainingClass(), docOwner.getProject());
}
}

View File

@@ -5,9 +5,9 @@ import com.intellij.psi.PsiClass;
import io.github.linwancen.plugin.show.settings.AppSettingsState;
import io.github.linwancen.plugin.show.settings.ProjectSettingsState;
class SkipUtils {
class PsiClassSkip {
private SkipUtils() {}
private PsiClassSkip() {}
static boolean skip(PsiClass psiClass, Project project) {
if (psiClass == null) {