feat: 2.18 support injected language like SQL | 支持注入语言如 SQL Fixed #68
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package io.github.linwancen.plugin.show.bean;
|
||||
|
||||
import com.intellij.lang.injection.InjectedLanguageManager;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.intellij.openapi.actionSystem.CommonDataKeys;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
@@ -15,6 +16,7 @@ public class FileInfo extends SettingsInfo {
|
||||
public final @NotNull VirtualFile file;
|
||||
public final @NotNull Document document;
|
||||
public final @NotNull Project project;
|
||||
public final @NotNull InjectedLanguageManager inject;
|
||||
|
||||
protected FileInfo(@NotNull VirtualFile file, @NotNull Document document, @NotNull Project project,
|
||||
@NotNull FuncEnum funcEnum) {
|
||||
@@ -22,6 +24,7 @@ public class FileInfo extends SettingsInfo {
|
||||
this.project = project;
|
||||
this.file = file;
|
||||
this.document = document;
|
||||
this.inject = InjectedLanguageManager.getInstance(project);
|
||||
}
|
||||
|
||||
public static @Nullable FileInfo of(@NotNull VirtualFile file, @NotNull Project project) {
|
||||
@@ -37,6 +40,10 @@ public class FileInfo extends SettingsInfo {
|
||||
if (psiFile == null) {
|
||||
return null;
|
||||
}
|
||||
return of(psiFile);
|
||||
}
|
||||
|
||||
public static @Nullable FileInfo of(@NotNull PsiFile psiFile) {
|
||||
@NotNull FileViewProvider viewProvider = psiFile.getViewProvider();
|
||||
@Nullable Document document = viewProvider.getDocument();
|
||||
if (document == null) {
|
||||
|
||||
@@ -10,6 +10,7 @@ import com.intellij.psi.PsiCompiledElement;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiManager;
|
||||
import com.intellij.psi.PsiReference;
|
||||
import io.github.linwancen.plugin.show.bean.FileInfo;
|
||||
import io.github.linwancen.plugin.show.bean.LineInfo;
|
||||
import io.github.linwancen.plugin.show.bean.SettingsInfo;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -57,7 +58,19 @@ public abstract class BaseLangDoc extends EditorLinePainter {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@Nullable BaseLangDoc lineEnd = PsiElementTo.lineEnd(element);
|
||||
@Nullable PsiElement injectedElement = info.inject.findInjectedElementAt(element.getContainingFile(), info.endOffset);
|
||||
if (injectedElement != null) {
|
||||
@Nullable FileInfo fileInfo = FileInfo.of(injectedElement.getContainingFile());
|
||||
if (fileInfo != null) {
|
||||
int lineNumber = fileInfo.document.getLineNumber(injectedElement.getTextRange().getStartOffset());
|
||||
@Nullable LineInfo lineInfo = LineInfo.of(fileInfo, lineNumber);
|
||||
if (lineInfo != null) {
|
||||
element = injectedElement;
|
||||
info = lineInfo;
|
||||
}
|
||||
}
|
||||
}
|
||||
@Nullable BaseLangDoc lineEnd = PsiElementTo.findLangDoc(element);
|
||||
if (lineEnd == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -162,11 +175,11 @@ public abstract class BaseLangDoc extends EditorLinePainter {
|
||||
// ignore
|
||||
}
|
||||
// support like java <-> kotlin
|
||||
@Nullable BaseLangDoc lineEnd = PsiElementTo.lineEnd(psiElement);
|
||||
if (lineEnd == null) {
|
||||
@Nullable BaseLangDoc langDoc = PsiElementTo.findLangDoc(psiElement);
|
||||
if (langDoc == null) {
|
||||
return null;
|
||||
}
|
||||
return lineEnd.resolveDocPrint(info, psiElement);
|
||||
return langDoc.resolveDocPrint(info, psiElement);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -26,7 +26,7 @@ public class PsiElementTo {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static BaseLangDoc lineEnd(@NotNull PsiElement element) {
|
||||
public static BaseLangDoc findLangDoc(@NotNull PsiElement element) {
|
||||
@Nullable Language language = element.getLanguage();
|
||||
while (true) {
|
||||
@Nullable BaseLangDoc lineEnd = BaseLangDoc.LANG_DOC_MAP.get(language.getID());
|
||||
|
||||
Reference in New Issue
Block a user