mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-20 13:31:28 +07:00
add postfix template for Objects.requireNonNull
This commit is contained in:
@@ -1,18 +1,4 @@
|
||||
/*
|
||||
* Copyright 2000-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.codeInsight.template.postfix.templates;
|
||||
|
||||
import com.intellij.codeInsight.completion.CompletionInitializationContext;
|
||||
@@ -64,7 +50,8 @@ public class JavaPostfixTemplateProvider implements PostfixTemplateProvider {
|
||||
new WhileStatementPostfixTemplate(),
|
||||
new StreamPostfixTemplate(),
|
||||
new OptionalPostfixTemplate(),
|
||||
new LambdaPostfixTemplate());
|
||||
new LambdaPostfixTemplate(),
|
||||
new ObjectsRequireNonNullPostfixTemplate());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.codeInsight.template.postfix.templates;
|
||||
|
||||
import com.intellij.codeInsight.template.postfix.util.JavaPostfixTemplatesUtils;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import static com.intellij.codeInsight.template.postfix.util.JavaPostfixTemplatesUtils.IS_NOT_PRIMITIVE;
|
||||
import static com.intellij.codeInsight.template.postfix.util.JavaPostfixTemplatesUtils.selectorTopmost;
|
||||
|
||||
public class ObjectsRequireNonNullPostfixTemplate extends StringBasedPostfixTemplate {
|
||||
public ObjectsRequireNonNullPostfixTemplate() {
|
||||
super("reqnonnull", "Objects.requireNonNull(expr)", JavaPostfixTemplatesUtils.minimalLanguageLevelSelector(selectorTopmost(IS_NOT_PRIMITIVE),
|
||||
LanguageLevel.JDK_1_7));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getTemplateString(@NotNull PsiElement element) {
|
||||
return "java.util.Objects.requireNonNull($expr$)";
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,4 @@
|
||||
/*
|
||||
* Copyright 2000-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.codeInsight.template.postfix.util;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightServicesUtil;
|
||||
@@ -23,6 +9,7 @@ import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.project.DumbService;
|
||||
import com.intellij.openapi.util.Condition;
|
||||
import com.intellij.openapi.util.Conditions;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.psi.util.PsiExpressionTrimRenderer;
|
||||
@@ -45,16 +32,21 @@ public abstract class JavaPostfixTemplatesUtils {
|
||||
}
|
||||
|
||||
public static PostfixTemplateExpressionSelector atLeastJava8Selector(final PostfixTemplateExpressionSelector selector) {
|
||||
return minimalLanguageLevelSelector(selector, LanguageLevel.JDK_1_8);
|
||||
}
|
||||
|
||||
public static PostfixTemplateExpressionSelector minimalLanguageLevelSelector(@NotNull PostfixTemplateExpressionSelector selector,
|
||||
@NotNull LanguageLevel minimalLevel) {
|
||||
return new PostfixTemplateExpressionSelector() {
|
||||
@Override
|
||||
public boolean hasExpression(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) {
|
||||
return PsiUtil.isLanguageLevel8OrHigher(context) && selector.hasExpression(context, copyDocument, newOffset);
|
||||
return PsiUtil.getLanguageLevel(context).isAtLeast(minimalLevel) && selector.hasExpression(context, copyDocument, newOffset);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<PsiElement> getExpressions(@NotNull PsiElement context, @NotNull Document document, int offset) {
|
||||
return PsiUtil.isLanguageLevel8OrHigher(context)
|
||||
return PsiUtil.getLanguageLevel(context).isAtLeast(minimalLevel)
|
||||
? selector.getExpressions(context, document, offset)
|
||||
: Collections.emptyList();
|
||||
}
|
||||
@@ -66,7 +58,7 @@ public abstract class JavaPostfixTemplatesUtils {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public static PostfixTemplateExpressionSelector selectorTopmost() {
|
||||
return selectorTopmost(Conditions.alwaysTrue());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
class Foo {
|
||||
void m(int i) {
|
||||
i.reqnonnull<caret>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Foo {
|
||||
void m(int i) {
|
||||
i.reqnonnull
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Foo {
|
||||
void m(String s) {
|
||||
s.reqnonnull<caret>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Foo {
|
||||
void m(String s) {
|
||||
java.util.Objects.requireNonNull(s)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.java.codeInsight.template.postfix.templates;
|
||||
|
||||
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ObjectsRequireNonNullPostfixTemplateTest extends PostfixTemplateTestCase {
|
||||
private LanguageLevel myDefaultLanguageLevel;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
LanguageLevelProjectExtension levelProjectExtension = LanguageLevelProjectExtension.getInstance(myFixture.getProject());
|
||||
myDefaultLanguageLevel = levelProjectExtension.getLanguageLevel();
|
||||
levelProjectExtension.setLanguageLevel(LanguageLevel.JDK_1_7);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
try {
|
||||
LanguageLevelProjectExtension.getInstance(myFixture.getProject()).setLanguageLevel(myDefaultLanguageLevel);
|
||||
myDefaultLanguageLevel = null;
|
||||
}
|
||||
finally {
|
||||
//noinspection ThrowFromFinallyBlock
|
||||
super.tearDown();
|
||||
}
|
||||
}
|
||||
|
||||
public void testSimple() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testPrimitive() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getSuffix() {
|
||||
return "reqnonnull";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
public class Foo {
|
||||
void m(Object o) {
|
||||
<spot>Objects.requireNonNull(o)</spot>$key
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
public class Foo {
|
||||
void m(Object o) {
|
||||
<spot>o</spot>$key
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Wraps object with Objects.requireNonNull.
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user