mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 21:11:28 +07:00
allow to split var declaration if it has denotable type
This commit is contained in:
@@ -1,18 +1,4 @@
|
||||
/*
|
||||
* Copyright 2000-2013 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-2018 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.intention.impl;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightBundle;
|
||||
@@ -28,9 +14,9 @@ import com.intellij.psi.impl.source.tree.Factory;
|
||||
import com.intellij.psi.impl.source.tree.TreeElement;
|
||||
import com.intellij.psi.javadoc.PsiDocComment;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiTypesUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.refactoring.util.RefactoringUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.ig.psiutils.CommentTracker;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -84,7 +70,7 @@ public class SplitDeclarationAction extends PsiElementBaseIntentionAction {
|
||||
if (declaredElements.length == 1) {
|
||||
PsiLocalVariable var = (PsiLocalVariable)declaredElements[0];
|
||||
if (var.getInitializer() == null) return false;
|
||||
if (var.getTypeElement().isInferredType()) {
|
||||
if (var.getTypeElement().isInferredType() && !PsiTypesUtil.isDenotableType(var.getType(), var)) {
|
||||
return false;
|
||||
}
|
||||
PsiElement parent = decl.getParent();
|
||||
@@ -128,7 +114,7 @@ public class SplitDeclarationAction extends PsiElementBaseIntentionAction {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
|
||||
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
|
||||
final PsiDeclarationStatement decl = PsiTreeUtil.getParentOfType(element, PsiDeclarationStatement.class);
|
||||
|
||||
final PsiManager psiManager = PsiManager.getInstance(project);
|
||||
@@ -144,23 +130,32 @@ public class SplitDeclarationAction extends PsiElementBaseIntentionAction {
|
||||
}
|
||||
|
||||
public static PsiAssignmentExpression invokeOnDeclarationStatement(PsiDeclarationStatement decl, PsiManager psiManager,
|
||||
Project project) throws IncorrectOperationException {
|
||||
Project project) {
|
||||
if (decl.getDeclaredElements().length == 1) {
|
||||
PsiLocalVariable var = (PsiLocalVariable)decl.getDeclaredElements()[0];
|
||||
var.normalizeDeclaration();
|
||||
final PsiTypeElement typeElement = var.getTypeElement();
|
||||
if (typeElement.isInferredType()) {
|
||||
PsiTypesUtil.replaceWithExplicitType(typeElement);
|
||||
}
|
||||
final String name = var.getName();
|
||||
assert name != null;
|
||||
PsiExpressionStatement statement = (PsiExpressionStatement)JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory()
|
||||
.createStatementFromText(var.getName() + "=xxx;", decl);
|
||||
.createStatementFromText(name + "=xxx;", decl);
|
||||
statement = (PsiExpressionStatement)CodeStyleManager.getInstance(project).reformat(statement);
|
||||
PsiAssignmentExpression assignment = (PsiAssignmentExpression)statement.getExpression();
|
||||
PsiExpression initializer = var.getInitializer();
|
||||
assert initializer != null;
|
||||
PsiExpression rExpression = RefactoringUtil.convertInitializerToNormalExpression(initializer, var.getType());
|
||||
|
||||
assignment.getRExpression().replace(rExpression);
|
||||
final PsiExpression expression = assignment.getRExpression();
|
||||
assert expression != null;
|
||||
expression.replace(rExpression);
|
||||
|
||||
PsiElement block = decl.getParent();
|
||||
if (block instanceof PsiForStatement) {
|
||||
final PsiDeclarationStatement varDeclStatement =
|
||||
JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createVariableDeclarationStatement(var.getName(), var.getType(), null);
|
||||
JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createVariableDeclarationStatement(name, var.getType(), null);
|
||||
|
||||
// For index can't be final, right?
|
||||
for (PsiElement varDecl : varDeclStatement.getDeclaredElements()) {
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
// "Split into declaration and assignment" "true"
|
||||
class Test {
|
||||
{
|
||||
String l;
|
||||
l = "foo";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Split into declaration and assignment" "true"
|
||||
class Test {
|
||||
{
|
||||
var l <caret>= "foo";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user