bbeed25f2909ff4c1779f791e49b84e7b204fd71
[fedora-idea.git] / plugins / groovy / src / org / jetbrains / plugins / groovy / editor / selection / GroovyLiteralSelectioner.java
blobbbeed25f2909ff4c1779f791e49b84e7b204fd71
1 /*
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 static org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes.mGSTRING_CONTENT;
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 /**
31 * @author ilyas
33 public class GroovyLiteralSelectioner extends GroovyBasicSelectioner {
34 public boolean canSelect(PsiElement e) {
35 PsiElement parent = e.getParent();
36 return isStringLiteral(e) || isStringLiteral(parent);
39 private static boolean isStringLiteral(PsiElement element) {
40 if (!(element instanceof GrLiteral)) return false;
41 ASTNode node = element.getNode();
42 if (node == null) return false;
43 ASTNode[] children = node.getChildren(null);
44 return children.length == 1 &&
45 (children[0].getElementType() == GroovyTokenTypes.mSTRING_LITERAL ||
46 children[0].getElementType() == GroovyTokenTypes.mGSTRING_LITERAL);
49 public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
50 List<TextRange> result = super.select(e, editorText, cursorOffset, editor);
52 /* TextRange range = e.getTextRange();
53 if (range.getLength() <= 2) {
54 result.add(range);
56 else {
57 result.add(new TextRange(range.getStartOffset() + 1, range.getEndOffset() - 1));
58 }*/
60 int startOffset = -1;
61 int endOffset = -1;
62 final String text = e.getText();
63 final int stringOffset = e.getTextOffset();
64 if (e.getNode().getElementType() == mGSTRING_CONTENT) {
65 int cur;
66 int index = -1;
67 while (true) {
68 cur = text.indexOf('\n', index + 1);
69 if (cur < 0 || cur + stringOffset > cursorOffset) break;
70 index = cur;
72 if (index >= 0) {
73 startOffset = stringOffset + index + 1;
76 index = text.indexOf('\n', cursorOffset - stringOffset);
77 if (index >= 0) {
78 endOffset = stringOffset + index + 1;
82 if (startOffset >= 0 && endOffset >= 0) {
83 result.add(new TextRange(startOffset, endOffset));
86 final String content = GrStringUtil.removeQuotes(text);
88 final int offset = stringOffset + text.indexOf(content);
89 result.add(new TextRange(offset, offset + content.length()));
90 return result;