From ccab6a3a3ddb1a196fcc5565d1793da6fa0e042d Mon Sep 17 00:00:00 2001 From: Daniil Ovchinnikov Date: Tue, 6 Mar 2018 20:45:27 +0300 Subject: [PATCH] [groovy] fix string content range computing (IDEA-187605) --- .../GroovyLiteralCopyPasteProcessor.java | 31 +++-- .../plugins/groovy/GroovyCopyPasteTest.groovy | 107 +++++++++++++++++- 2 files changed, 121 insertions(+), 17 deletions(-) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/editor/GroovyLiteralCopyPasteProcessor.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/editor/GroovyLiteralCopyPasteProcessor.java index 147dd205ebf4..3fef799b94c0 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/editor/GroovyLiteralCopyPasteProcessor.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/editor/GroovyLiteralCopyPasteProcessor.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 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 org.jetbrains.plugins.groovy.editor; import com.intellij.codeInsight.editorActions.StringLiteralCopyPasteProcessor; @@ -36,6 +22,7 @@ import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes; import org.jetbrains.plugins.groovy.lang.lexer.TokenSets; import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes; import org.jetbrains.plugins.groovy.lang.psi.util.GrStringUtil; +import org.jetbrains.plugins.groovy.lang.resolve.GroovyStringLiteralManipulator; /** * @author peter @@ -61,7 +48,19 @@ public class GroovyLiteralCopyPasteProcessor extends StringLiteralCopyPasteProce @Nullable @Override protected TextRange getEscapedRange(@NotNull PsiElement token) { - return isStringLiteral(token) ? token.getTextRange() : null; // TODO: calculate correct ranges for different types of literals + final ASTNode node = token.getNode(); + if (node == null) return null; + + final IElementType tokenType = node.getElementType(); + if (tokenType == GroovyTokenTypes.mSTRING_LITERAL || tokenType == GroovyTokenTypes.mGSTRING_LITERAL) { + final String text = token.getText(); + if (text == null) return null; + return GroovyStringLiteralManipulator.getLiteralRange(text); + } + if (tokenType == GroovyTokenTypes.mREGEX_CONTENT) { + return token.getTextRange(); + } + return null; } @Override diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/GroovyCopyPasteTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/GroovyCopyPasteTest.groovy index c3e1fd039a76..1fe448c61d38 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/GroovyCopyPasteTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/GroovyCopyPasteTest.groovy @@ -1,4 +1,4 @@ -// 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. +// 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 org.jetbrains.plugins.groovy import com.intellij.codeInsight.CodeInsightSettings @@ -165,4 +165,109 @@ print CONST " //text\n" + "}") } + + + void 'test single-quoted string'() { + doTest($/'\\'/$, '', $/'\\'/$) + } + + void 'test single-quoted string partial'() { + doTest($/'\\'/$, '', $/'\\/$) + } + + void 'test single-quoted string content'() { + doTest($/'\\'/$, '', $/\/$) + } + + + void 'test double-quoted string'() { + doTest($/"\\"/$, '', $/"\\"/$) + } + + void 'test double-quoted string partial'() { + doTest($/"\\"/$, '', $/"\\/$) + } + + void 'test double-quoted string content'() { + doTest($/"\\"/$, '', $/\/$) + } + + + void 'test triple-single-quoted string'() { + doTest($/'''\\'''/$, '', $/'''\\'''/$) + } + + void 'test triple-single-quoted string partial start quote 1'() { + doTest($/'''\\'''/$, '', $/''\\'''/$) + } + + void 'test triple-single-quoted string partial start quote 2'() { + doTest($/'''\\'''/$, '', $/'\\'''/$) + } + + void 'test triple-single-quoted string partial start quote 3'() { + doTest($/'''\\'''/$, '', $/\\'''/$) + } + + void 'test triple-single-quoted string partial end quote 1'() { + doTest($/'''\\'''/$, '', $/'''\\/$) + } + + void 'test triple-single-quoted string partial end quote 2'() { + doTest($/'''\\'''/$, '', $/'''\\'/$) + } + + void 'test triple-single-quoted string partial end quote 3'() { + doTest($/'''\\'''/$, '', $/'''\\''/$) + } + + void 'test triple-single-quoted string content'() { + doTest($/'''\\'''/$, '', $/\/$) + } + + + void 'test triple-double-quoted string'() { + doTest($/"""\\"""/$, '', $/"""\\"""/$) + } + + void 'test triple-double-quoted string partial start quote 1'() { + doTest($/"""\\"""/$, '', $/""\\"""/$) + } + + void 'test triple-double-quoted string partial start quote 2'() { + doTest($/"""\\"""/$, '', $/"\\"""/$) + } + + void 'test triple-double-quoted string partial start quote 3'() { + doTest($/"""\\"""/$, '', $/\\"""/$) + } + + void 'test triple-double-quoted string partial end quote 1'() { + doTest($/"""\\"""/$, '', $/"""\\/$) + } + + void 'test triple-double-quoted string partial end quote 2'() { + doTest($/"""\\"""/$, '', $/"""\\"/$) + } + + void 'test triple-double-quoted string partial end quote 3'() { + doTest($/"""\\"""/$, '', $/"""\\""/$) + } + + void 'test triple-double-quoted string content'() { + doTest($/"""\\"""/$, '', $/\/$) + } + + + void 'test slashy string'() { + doTest($//\///$, '', $//\///$) + } + + void 'test slashy string partial'() { + doTest($//\///$, '', $//\//$) + } + + void 'test slashy string content'() { + doTest($//\///$, '', $///$) + } }