mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-06 03:21:12 +07:00
surround with: adjust range if no selection is present so multi-line statement can be wrapped (IDEA-156463)
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
package com.intellij.codeInsight.generation.surroundWith;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightUtil;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiStatement;
|
||||
import com.intellij.psi.PsiWhiteSpace;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class JavaSurroundWithStatementRangeAdjuster implements SurroundWithRangeAdjuster {
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public TextRange adjustSurroundWithRange(PsiFile file, TextRange selectedRange) {
|
||||
return selectedRange;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public TextRange adjustSurroundWithRange(PsiFile file, TextRange selectedRange, boolean hasSelection) {
|
||||
if (!hasSelection) {
|
||||
int startOffset = selectedRange.getStartOffset();
|
||||
int endOffset = selectedRange.getEndOffset();
|
||||
if (CodeInsightUtil.findStatementsInRange(file, startOffset, endOffset).length == 0) {
|
||||
PsiElement elementAtLineStart = PsiTreeUtil.skipSiblingsForward(file.findElementAt(startOffset), PsiWhiteSpace.class);
|
||||
if (elementAtLineStart instanceof PsiStatement) {
|
||||
return elementAtLineStart.getTextRange();
|
||||
}
|
||||
}
|
||||
}
|
||||
return selectedRange;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class Test {
|
||||
{
|
||||
<caret> for(int i = 0; i< 9; i++) {
|
||||
System.out.println("42");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class Test {
|
||||
{
|
||||
if () {
|
||||
for(int i = 0; i< 9; i++) {
|
||||
System.out.println("42");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -96,6 +96,10 @@ public class JavaSurroundWithTest extends LightCodeInsightTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testSurroundWithStatementWithoutSelection() throws Exception {
|
||||
doTest(new JavaWithIfSurrounder());
|
||||
}
|
||||
|
||||
public void testSurroundNonExpressionWithParenthesis() throws Exception {
|
||||
doTest(new JavaWithParenthesesSurrounder());
|
||||
}
|
||||
|
||||
@@ -28,4 +28,7 @@ public interface SurroundWithRangeAdjuster {
|
||||
ExtensionPointName<SurroundWithRangeAdjuster> EP_NAME = ExtensionPointName.create("com.intellij.codeInsight.surroundWithRangeAdjuster");
|
||||
|
||||
@Nullable TextRange adjustSurroundWithRange(PsiFile file, TextRange selectedRange);
|
||||
@Nullable default TextRange adjustSurroundWithRange(PsiFile file, TextRange selectedRange, boolean hasSelection) {
|
||||
return adjustSurroundWithRange(file, selectedRange);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,7 +94,8 @@ public class SurroundWithHandler implements CodeInsightActionHandler {
|
||||
@Nullable
|
||||
public static List<AnAction> buildSurroundActions(final Project project, final Editor editor, PsiFile file, @Nullable Surrounder surrounder){
|
||||
SelectionModel selectionModel = editor.getSelectionModel();
|
||||
if (!selectionModel.hasSelection()) {
|
||||
boolean hasSelection = selectionModel.hasSelection();
|
||||
if (!hasSelection) {
|
||||
selectionModel.selectLineAtCaret();
|
||||
}
|
||||
int startOffset = selectionModel.getSelectionStart();
|
||||
@@ -109,7 +110,7 @@ public class SurroundWithHandler implements CodeInsightActionHandler {
|
||||
|
||||
TextRange textRange = new TextRange(startOffset, endOffset);
|
||||
for(SurroundWithRangeAdjuster adjuster: Extensions.getExtensions(SurroundWithRangeAdjuster.EP_NAME)) {
|
||||
textRange = adjuster.adjustSurroundWithRange(file, textRange);
|
||||
textRange = adjuster.adjustSurroundWithRange(file, textRange, hasSelection);
|
||||
if (textRange == null) return null;
|
||||
}
|
||||
startOffset = textRange.getStartOffset();
|
||||
|
||||
@@ -1083,6 +1083,8 @@
|
||||
implementationClass="com.intellij.codeInsight.generation.surroundWith.JavaStatementsSurroundDescriptor"/>
|
||||
<lang.surroundDescriptor language="JAVA"
|
||||
implementationClass="com.intellij.codeInsight.intention.impl.SurroundAutoCloseableAction$Template"/>
|
||||
<codeInsight.surroundWithRangeAdjuster
|
||||
implementation="com.intellij.codeInsight.generation.surroundWith.JavaSurroundWithStatementRangeAdjuster"/>
|
||||
|
||||
<lang.unwrapDescriptor language="JAVA" implementationClass="com.intellij.codeInsight.unwrap.JavaUnwrapDescriptor"/>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user