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.
16 package com
.intellij
.codeInsight
.editorActions
.wordSelection
;
18 import com
.intellij
.codeInsight
.editorActions
.SelectWordUtil
;
19 import com
.intellij
.lexer
.StringLiteralLexer
;
20 import com
.intellij
.openapi
.editor
.Editor
;
21 import com
.intellij
.openapi
.util
.TextRange
;
22 import com
.intellij
.psi
.JavaTokenType
;
23 import com
.intellij
.psi
.PsiElement
;
24 import com
.intellij
.psi
.PsiLiteralExpression
;
25 import com
.intellij
.psi
.StringEscapesTokenTypes
;
27 import java
.util
.List
;
29 public class LiteralSelectioner
extends BasicSelectioner
{
30 public boolean canSelect(PsiElement e
) {
31 PsiElement parent
= e
.getParent();
33 isStringLiteral(e
) || isStringLiteral(parent
);
36 private static boolean isStringLiteral(PsiElement element
) {
37 return element
instanceof PsiLiteralExpression
&&
38 ((PsiLiteralExpression
)element
).getType().equalsToText("java.lang.String") && element
.getText().startsWith("\"") && element
.getText().endsWith("\"");
41 public List
<TextRange
> select(PsiElement e
, CharSequence editorText
, int cursorOffset
, Editor editor
) {
42 List
<TextRange
> result
= super.select(e
, editorText
, cursorOffset
, editor
);
44 TextRange range
= e
.getTextRange();
45 final StringLiteralLexer lexer
= new StringLiteralLexer('\"', JavaTokenType
.STRING_LITERAL
);
46 lexer
.start(editorText
, range
.getStartOffset(), range
.getEndOffset());
48 while (lexer
.getTokenType() != null) {
49 if (lexer
.getTokenStart() <= cursorOffset
&& cursorOffset
< lexer
.getTokenEnd()) {
50 if (StringEscapesTokenTypes
.STRING_LITERAL_ESCAPES
.contains(lexer
.getTokenType())) {
51 result
.add(new TextRange(lexer
.getTokenStart(), lexer
.getTokenEnd()));
54 TextRange word
= SelectWordUtil
.getWordSelectionRange(editorText
, cursorOffset
);
56 result
.add(new TextRange(Math
.max(word
.getStartOffset(), lexer
.getTokenStart()),
57 Math
.min(word
.getEndOffset(), lexer
.getTokenEnd())));
65 result
.add(new TextRange(range
.getStartOffset() + 1, range
.getEndOffset() - 1));