WEB-35067 fix handling with newlines before and after

This commit is contained in:
Anton Lobov
2018-10-01 11:42:51 +02:00
parent 63399624bd
commit 7f6972776b
2 changed files with 15 additions and 3 deletions
@@ -49,14 +49,14 @@ public class JsonTypedHandler extends TypedHandlerDelegate {
if (c != ':' || !JsonEditorOptions.getInstance().AUTO_WHITESPACE_AFTER_COLON) return;
int offset = editor.getCaretModel().getOffset();
PsiDocumentManager.getInstance(file.getProject()).commitDocument(editor.getDocument());
PsiElement element = PsiTreeUtil.skipWhitespacesBackward(file.findElementAt(offset));
if (!(element instanceof JsonProperty)) return;
PsiElement element = PsiTreeUtil.getParentOfType(PsiTreeUtil.skipWhitespacesBackward(file.findElementAt(offset)), JsonProperty.class, false);
if (element == null) return;
final ASTNode[] children = element.getNode().getChildren(TokenSet.create(JsonElementTypes.COLON));
if (children.length == 0) return;
final ASTNode colon = children[0];
final ASTNode next = colon.getTreeNext();
final String text = next.getText();
if (text.length() == 0 || !StringUtil.isEmptyOrSpaces(text)) {
if (text.length() == 0 || !StringUtil.isEmptyOrSpaces(text) || StringUtil.isLineBreak(text.charAt(0))) {
final int insOffset = colon.getStartOffset() + 1;
editor.getDocument().insertString(insOffset, " ");
editor.getCaretModel().moveToOffset(insOffset + 1);
@@ -77,4 +77,16 @@ public class JsonTypingHandlingTest extends JsonTestCase {
public void testAutoQuotesForPropNameFalse2() {
doTestColon( "{ \"x<caret>\"}", "{ \"x:<caret>\"}");
}
public void testAutoQuotesAndWhitespaceFollowingNewline() {
doTestColon("{\n" +
" \"a\": 5,\n" +
" x<caret>\n" +
" \"q\": 8\n" +
"}",
"{\n" +
" \"a\": 5,\n" +
" \"x\": <caret>\n" +
" \"q\": 8\n" +
"}");
}
}