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
.psi
.impl
.source
.tree
.injected
;
18 import com
.intellij
.psi
.impl
.source
.xml
.XmlAttributeValueImpl
;
19 import com
.intellij
.psi
.xml
.XmlAttribute
;
20 import com
.intellij
.psi
.LiteralTextEscaper
;
21 import com
.intellij
.openapi
.util
.TextRange
;
22 import com
.intellij
.openapi
.util
.ProperTextRange
;
23 import org
.jetbrains
.annotations
.NotNull
;
28 public class XmlAttributeLiteralEscaper
extends LiteralTextEscaper
<XmlAttributeValueImpl
> {
29 private final XmlAttribute myXmlAttribute
;
31 public XmlAttributeLiteralEscaper(XmlAttributeValueImpl host
) {
33 myXmlAttribute
= (XmlAttribute
)host
.getParent();
36 public boolean decode(@NotNull final TextRange rangeInsideHost
, @NotNull StringBuilder outChars
) {
37 ProperTextRange
.assertProperRange(rangeInsideHost
);
38 TextRange valueTextRange
= myXmlAttribute
.getValueTextRange();
39 int startInDecoded
= myXmlAttribute
.physicalToDisplay(rangeInsideHost
.getStartOffset() - valueTextRange
.getStartOffset());
40 int endInDecoded
= myXmlAttribute
.physicalToDisplay(rangeInsideHost
.getEndOffset() - valueTextRange
.getStartOffset());
41 String displayValue
= myXmlAttribute
.getDisplayValue();
42 //todo investigate IIOB http://www.jetbrains.net/jira/browse/IDEADEV-16796
43 startInDecoded
= startInDecoded
< 0 ?
0 : startInDecoded
> displayValue
.length() ? displayValue
.length() : startInDecoded
;
44 endInDecoded
= endInDecoded
< 0 ?
0 : endInDecoded
> displayValue
.length() ? displayValue
.length() : endInDecoded
;
45 if (startInDecoded
> endInDecoded
) endInDecoded
= startInDecoded
;
46 outChars
.append(displayValue
, startInDecoded
, endInDecoded
);
50 public int getOffsetInHost(final int offsetInDecoded
, @NotNull final TextRange rangeInsideHost
) {
51 TextRange valueTextRange
= myXmlAttribute
.getValueTextRange();
52 int displayStart
= myXmlAttribute
.physicalToDisplay(rangeInsideHost
.getStartOffset());
54 int dp
= myXmlAttribute
.displayToPhysical(offsetInDecoded
+ displayStart
- valueTextRange
.getStartOffset());
55 if (dp
== -1) return -1;
56 return dp
+ valueTextRange
.getStartOffset();
59 public boolean isOneLine() {