/*
- * Copyright 2000-2013 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
protected void processIntention(Editor editor, @NotNull PsiElement element) {
final SelectionModel selectionModel = editor.getSelectionModel();
if (selectionModel.hasSelection()) {
- // does not check if octal escape is inside char or string literal (garbage in, garbage out)
+ // does not check if Unicode escape is inside char or string literal (garbage in, garbage out)
final Document document = editor.getDocument();
final int start = selectionModel.getSelectionStart();
final int end = selectionModel.getSelectionEnd();
final int column = caretModel.getLogicalPosition().column;
final int index1 = indexOfUnicodeEscape(line, column);
final int index2 = indexOfUnicodeEscape(line, column + 1);
- final int escapeStart = index2 == column ? index2 : index1; // if caret is between two unicode escape, replace the right one
+ // if the caret is between two unicode escapes, replace the one to the right
+ final int escapeStart = index2 == column ? index2 : index1;
int hexStart = escapeStart + 1;
while (line.charAt(hexStart) == 'u') {
hexStart++;
}
- final int c = Integer.parseInt(line.substring(hexStart, hexStart + 4), 16);
- document.replaceString(lineStartOffset + escapeStart, lineStartOffset + hexStart + 4, String.valueOf((char) c));
+ final char c = (char)Integer.parseInt(line.substring(hexStart, hexStart + 4), 16);
+ if (Character.isHighSurrogate(c)) {
+ hexStart += 4;
+ if (line.charAt(hexStart++) == '\\' && line.charAt(hexStart++) == 'u') {
+ while (line.charAt(hexStart) == 'u') hexStart++;
+ final char d = (char)Integer.parseInt(line.substring(hexStart, hexStart + 4), 16);
+ document.replaceString(lineStartOffset + escapeStart, lineStartOffset + hexStart + 4, String.valueOf(new char[] {c, d}));
+ return;
+ }
+ }
+ else if (Character.isLowSurrogate(c)) {
+ if (escapeStart >= 6 &&
+ StringUtil.isHexDigit(line.charAt(escapeStart - 1)) && StringUtil.isHexDigit(line.charAt(escapeStart - 2)) &&
+ StringUtil.isHexDigit(line.charAt(escapeStart - 3)) && StringUtil.isHexDigit(line.charAt(escapeStart - 4))) {
+ int i = escapeStart - 5;
+ while (i > 0 && line.charAt(i) == 'u') i--;
+ if (line.charAt(i) == '\\' && (i == 0 || line.charAt(i - 1) != '\\')) {
+ final char b = (char)Integer.parseInt(line.substring(escapeStart - 4, escapeStart), 16);
+ document.replaceString(lineStartOffset + i, lineStartOffset + hexStart + 4, String.valueOf(new char[] {b, c}));
+ return;
+ }
+ }
+ }
+ document.replaceString(lineStartOffset + escapeStart, lineStartOffset + hexStart + 4, String.valueOf(c));
}
}
+/*
+ * Copyright 2000-2016 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.siyeh.ipp.unicode;
import com.siyeh.IntentionPowerPackBundle;
public void testSimple() { doTest(); }
public void testSelection() { doTest(); }
+ public void testSurrogatePairs1() { doTest(); }
+ public void testSurrogatePairs2() { doTest(); }
public void testNoException() { assertIntentionNotAvailable(); }
public void testU() { assertIntentionNotAvailable(); }