2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.jetbrains
.plugins
.groovy
.editor
.selection
;
19 import com
.intellij
.lang
.ASTNode
;
20 import com
.intellij
.openapi
.editor
.Editor
;
21 import com
.intellij
.openapi
.util
.TextRange
;
22 import com
.intellij
.psi
.PsiElement
;
23 import org
.jetbrains
.plugins
.groovy
.lang
.lexer
.GroovyTokenTypes
;
24 import org
.jetbrains
.plugins
.groovy
.lang
.psi
.api
.auxiliary
.GrListOrMap
;
25 import org
.jetbrains
.plugins
.groovy
.lang
.psi
.api
.statements
.expressions
.literals
.GrLiteral
;
26 import org
.jetbrains
.plugins
.groovy
.lang
.psi
.util
.GrStringUtil
;
28 import java
.util
.List
;
30 import static org
.jetbrains
.plugins
.groovy
.lang
.lexer
.GroovyTokenTypes
.mGSTRING_CONTENT
;
35 public class GroovyLiteralSelectioner
extends GroovyBasicSelectioner
{
36 public boolean canSelect(PsiElement e
) {
37 PsiElement parent
= e
.getParent();
38 return isLiteral(e
) || isLiteral(parent
);
41 private static boolean isLiteral(PsiElement element
) {
42 if (element
instanceof GrListOrMap
) {
46 if (!(element
instanceof GrLiteral
)) return false;
47 ASTNode node
= element
.getNode();
48 if (node
== null) return false;
49 ASTNode
[] children
= node
.getChildren(null);
50 return children
.length
== 1 &&
51 (children
[0].getElementType() == GroovyTokenTypes
.mSTRING_LITERAL
||
52 children
[0].getElementType() == GroovyTokenTypes
.mGSTRING_LITERAL
);
55 public List
<TextRange
> select(PsiElement e
, CharSequence editorText
, int cursorOffset
, Editor editor
) {
56 List
<TextRange
> result
= super.select(e
, editorText
, cursorOffset
, editor
);
58 if (e
instanceof GrListOrMap
) {
64 final String text
= e
.getText();
65 final int stringOffset
= e
.getTextOffset();
66 if (e
.getNode().getElementType() == mGSTRING_CONTENT
) {
70 cur
= text
.indexOf('\n', index
+ 1);
71 if (cur
< 0 || cur
+ stringOffset
> cursorOffset
) break;
75 startOffset
= stringOffset
+ index
+ 1;
78 index
= text
.indexOf('\n', cursorOffset
- stringOffset
);
80 endOffset
= stringOffset
+ index
+ 1;
84 if (startOffset
>= 0 && endOffset
>= 0) {
85 result
.add(new TextRange(startOffset
, endOffset
));
88 final String content
= GrStringUtil
.removeQuotes(text
);
90 final int offset
= stringOffset
+ text
.indexOf(content
);
91 result
.add(new TextRange(offset
, offset
+ content
.length()));