mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-05 01:50:56 +07:00
[java] [javadoc snippet] fix injection with unclear context
use existing JShell wrappers GitOrigin-RevId: 2e0351145e1e3f750d781cd62bcda20cc49693c7
This commit is contained in:
committed by
intellij-monorepo-bot
parent
b3cd2cb11e
commit
c7328bb1a2
@@ -1,10 +1,10 @@
|
||||
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.psi.impl.source.tree.injected;
|
||||
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.lang.injection.MultiHostInjector;
|
||||
import com.intellij.lang.injection.MultiHostRegistrar;
|
||||
import com.intellij.lang.java.JavaLanguage;
|
||||
import com.intellij.lang.java.JShellLanguage;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.impl.source.javadoc.PsiSnippetDocTagImpl;
|
||||
@@ -22,8 +22,6 @@ import java.util.List;
|
||||
public class JavadocInjector implements MultiHostInjector {
|
||||
|
||||
private static final String LANG_ATTR_KEY = "lang";
|
||||
private static final String SNIPPET_INJECTION_JAVA_HEADER = "class ___JavadocSnippetPlaceholder {\n" +
|
||||
" void ___JavadocSnippetPlaceholderMethod() throws Throwable {\n";
|
||||
|
||||
@Override
|
||||
public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar,
|
||||
@@ -34,22 +32,19 @@ public class JavadocInjector implements MultiHostInjector {
|
||||
|
||||
final Language language = getLanguage(snippet);
|
||||
|
||||
final String prefix = language == JavaLanguage.INSTANCE ? SNIPPET_INJECTION_JAVA_HEADER : null;
|
||||
final String suffix = language == JavaLanguage.INSTANCE ? "\n}}" : null;
|
||||
|
||||
final List<TextRange> ranges = snippet.getContentRanges();
|
||||
if (ranges.isEmpty()) return;
|
||||
|
||||
registrar.startInjecting(language);
|
||||
if (ranges.size() == 1) {
|
||||
registrar.addPlace(prefix, suffix, snippet, ranges.get(0));
|
||||
registrar.addPlace(null, null, snippet, ranges.get(0));
|
||||
}
|
||||
else {
|
||||
registrar.addPlace(prefix, null, snippet, ranges.get(0));
|
||||
registrar.addPlace(null, null, snippet, ranges.get(0));
|
||||
for (TextRange range : ranges.subList(1, ranges.size() - 1)) {
|
||||
registrar.addPlace(null, null, snippet, range);
|
||||
}
|
||||
registrar.addPlace(null, suffix, snippet, ContainerUtil.getLastItem(ranges));
|
||||
registrar.addPlace(null, null, snippet, ContainerUtil.getLastItem(ranges));
|
||||
}
|
||||
registrar.doneInjecting();
|
||||
|
||||
@@ -57,7 +52,7 @@ public class JavadocInjector implements MultiHostInjector {
|
||||
|
||||
private static @NotNull Language getLanguage(@NotNull PsiSnippetDocTagImpl snippet) {
|
||||
PsiSnippetDocTagValue valueElement = snippet.getValueElement();
|
||||
if (valueElement == null) return JavaLanguage.INSTANCE;
|
||||
if (valueElement == null) return JShellLanguage.INSTANCE;
|
||||
final PsiSnippetAttributeList attributeList = valueElement.getAttributeList();
|
||||
|
||||
for (PsiSnippetAttribute attribute : attributeList.getAttributes()) {
|
||||
@@ -73,7 +68,7 @@ public class JavadocInjector implements MultiHostInjector {
|
||||
|
||||
return language;
|
||||
}
|
||||
return JavaLanguage.INSTANCE;
|
||||
return JShellLanguage.INSTANCE;
|
||||
}
|
||||
|
||||
private static @Nullable Language findRegisteredLanguage(@NotNull String langValueText) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/** {<warning descr="'@snippet' tag is not available at this language level">@snippet</warning> :
|
||||
* void f<error descr="';' expected">(</error><error descr="Expression expected">)</error> {}
|
||||
* void f() {}
|
||||
* }
|
||||
*/
|
||||
class A {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/** {<warning descr="'@snippet' tag is not available at this language level">@snippet</warning> :
|
||||
* void f<error descr="';' expected">(</error><error descr="Expression expected">)</error> {}
|
||||
* void f() {}
|
||||
* }
|
||||
*/
|
||||
class A {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "JAVA" "true"
|
||||
// "JShellLanguage" "true"
|
||||
|
||||
/**
|
||||
* {@snippet :<caret>}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "JAVA" "true"
|
||||
// "JShellLanguage" "true"
|
||||
|
||||
/**
|
||||
* A simple program.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "JAVA" "true"
|
||||
// "JShellLanguage" "true"
|
||||
|
||||
/**
|
||||
* {@snippet<caret> :
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "JAVA" "true"
|
||||
// "JShellLanguage" "true"
|
||||
|
||||
/**
|
||||
* {@snippet<caret> lang=_NONEXISTING_LANGUAGE_:
|
||||
|
||||
@@ -1,6 +1 @@
|
||||
// "JAVA" "true"
|
||||
|
||||
class ___JavadocSnippetPlaceholder {
|
||||
void ___JavadocSnippetPlaceholderMethod() throws Throwable {
|
||||
|
||||
}}
|
||||
@@ -1,6 +1 @@
|
||||
// "JAVA" "true"
|
||||
|
||||
class ___JavadocSnippetPlaceholder {
|
||||
void ___JavadocSnippetPlaceholderMethod() throws Throwable {
|
||||
|
||||
}}
|
||||
@@ -1,7 +1,3 @@
|
||||
// "JAVA" "true"
|
||||
|
||||
class ___JavadocSnippetPlaceholder {
|
||||
void ___JavadocSnippetPlaceholderMethod() throws Throwable {
|
||||
class Main {
|
||||
void f(Optional<Object> e) {
|
||||
if (v.isPresent()) {
|
||||
@@ -9,4 +5,3 @@ class Main {
|
||||
}
|
||||
}
|
||||
}
|
||||
}}
|
||||
@@ -1,11 +1,6 @@
|
||||
// "_ignore" "true"
|
||||
|
||||
class ___JavadocSnippetPlaceholder {
|
||||
void ___JavadocSnippetPlaceholderMethod() throws Throwable {
|
||||
class HelloWorld {
|
||||
class HelloWorld {
|
||||
void
|
||||
f(
|
||||
) {
|
||||
}
|
||||
}
|
||||
}}
|
||||
@@ -1,11 +1,6 @@
|
||||
// "_ignore" "true"
|
||||
|
||||
class ___JavadocSnippetPlaceholder {
|
||||
void ___JavadocSnippetPlaceholderMethod() throws Throwable {
|
||||
class HelloWorld {
|
||||
class HelloWorld {
|
||||
void
|
||||
f(
|
||||
) {
|
||||
}
|
||||
}
|
||||
}}
|
||||
@@ -1,4 +1,4 @@
|
||||
// "JAVA" "true"
|
||||
// "JShellLanguage" "true"
|
||||
|
||||
/**
|
||||
* {@snippet :<caret>}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "JAVA" "true"
|
||||
// "JShellLanguage" "true"
|
||||
|
||||
/**
|
||||
* A simple program.
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.java.codeInsight.javadoc;
|
||||
|
||||
import com.intellij.JavaTestUtil;
|
||||
import com.intellij.codeInsight.daemon.quickFix.ActionHint;
|
||||
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
|
||||
import com.intellij.lang.injection.InjectedLanguageManager;
|
||||
import com.intellij.openapi.application.WriteAction;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.javadoc.PsiSnippetDocTag;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtilCore;
|
||||
import com.intellij.testFramework.EqualsToFile;
|
||||
import com.intellij.testFramework.LightProjectDescriptor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase.JAVA_18;
|
||||
@@ -29,20 +31,13 @@ public class JavadocSnippetInjectionFileTest extends LightQuickFixParameterizedT
|
||||
final int offset = getEditor().getCaretModel().getPrimaryCaret().getOffset();
|
||||
final PsiElement snippet = PsiUtilCore.getElementAtOffset(getFile(), offset);
|
||||
|
||||
final PsiClass enclosingClass = getEnclosingClass(snippet);
|
||||
final PsiClass injectedClass = getInjectedClass(snippet);
|
||||
WriteAction.run(() -> enclosingClass.replace(injectedClass));
|
||||
|
||||
checkResult(testName);
|
||||
}
|
||||
|
||||
private void checkResult(@NotNull final String testName) {
|
||||
final String expectedFilePath = getBasePath() + "/after" + testName;
|
||||
checkResultByFile(expectedFilePath);
|
||||
}
|
||||
|
||||
private static @NotNull PsiClass getEnclosingClass(PsiElement element) {
|
||||
return PsiTreeUtil.getParentOfType(element, PsiClass.class);
|
||||
EqualsToFile.assertEqualsToFile(
|
||||
"Injected code",
|
||||
new File(JavaTestUtil.getJavaTestDataPath(), getBasePath() + "/after" + testName),
|
||||
injectedClass.getText()
|
||||
);
|
||||
}
|
||||
|
||||
private @NotNull PsiClass getInjectedClass(PsiElement element) {
|
||||
|
||||
Reference in New Issue
Block a user