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
;
18 import com
.intellij
.openapi
.editor
.highlighter
.HighlighterIterator
;
19 import com
.intellij
.psi
.*;
20 import com
.intellij
.psi
.tree
.IElementType
;
21 import com
.intellij
.psi
.tree
.TokenSet
;
22 import org
.jetbrains
.annotations
.NotNull
;
27 public class JavaQuoteHandler
extends SimpleTokenSetQuoteHandler
implements JavaLikeQuoteHandler
{
28 private final TokenSet concatenatableStrings
;
30 public JavaQuoteHandler() {
31 super(new IElementType
[] { JavaTokenType
.STRING_LITERAL
, JavaTokenType
.CHARACTER_LITERAL
});
32 concatenatableStrings
= TokenSet
.create(JavaTokenType
.STRING_LITERAL
);
35 public boolean isOpeningQuote(HighlighterIterator iterator
, int offset
) {
36 boolean openingQuote
= super.isOpeningQuote(iterator
, offset
);
40 if (!iterator
.atEnd()) {
43 if (!iterator
.atEnd() && StringEscapesTokenTypes
.STRING_LITERAL_ESCAPES
.contains(iterator
.getTokenType())) {
52 public boolean isClosingQuote(HighlighterIterator iterator
, int offset
) {
53 boolean closingQuote
= super.isClosingQuote(iterator
, offset
);
57 if (!iterator
.atEnd()) {
60 if (!iterator
.atEnd() && StringEscapesTokenTypes
.STRING_LITERAL_ESCAPES
.contains(iterator
.getTokenType())) {
69 public TokenSet
getConcatenatableStringTokenTypes() {
70 return concatenatableStrings
;
73 public String
getStringConcatenationOperatorRepresentation() {
77 public TokenSet
getStringTokenTypes() {
78 return myLiteralTokenSet
;
81 public boolean isAppropriateElementTypeForLiteral(final @NotNull IElementType tokenType
) {
82 return isAppropriateElementTypeForLiteralStatic(tokenType
);
85 public boolean needParenthesesAroundConcatenation(final PsiElement element
) {
86 // example code: "some string".length() must become ("some" + " string").length()
87 return element
.getParent() instanceof PsiLiteralExpression
&& element
.getParent().getParent() instanceof PsiReferenceExpression
;
90 public static boolean isAppropriateElementTypeForLiteralStatic(final IElementType tokenType
) {
91 return TokenTypeEx
.WHITE_SPACE_OR_COMMENT_BIT_SET
.contains(tokenType
)
92 || tokenType
== JavaTokenType
.SEMICOLON
93 || tokenType
== JavaTokenType
.COMMA
94 || tokenType
== JavaTokenType
.RPARENTH
95 || tokenType
== JavaTokenType
.RBRACKET
96 || tokenType
== JavaTokenType
.RBRACE
97 || tokenType
== JavaTokenType
.STRING_LITERAL
98 || tokenType
== JavaTokenType
.CHARACTER_LITERAL
;