From ee165b9f834f167a2f85635008a8e6f83cf6aa0c Mon Sep 17 00:00:00 2001 From: Alexander Zolotov Date: Tue, 26 May 2015 11:39:52 +0200 Subject: [PATCH] Templates: add whitespace marker in non-empty line (RUBY-16672) --- .../template/impl/TemplateState.java | 15 ++++++++-- .../codeStyle/CodeStyleManagerImpl.java | 29 +++++++++---------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateState.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateState.java index c80c812668b8..fe432b5a672c 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateState.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateState.java @@ -743,10 +743,21 @@ public class TemplateState implements Disposable { ExpressionContext context = createExpressionContext(start); Result result = isQuick ? expressionNode.calculateQuickResult(context) : expressionNode.calculateResult(context); - if (isQuick && result == null && !oldValue.isEmpty()) { + if (isQuick && result == null) { + if (!oldValue.isEmpty()) { + return; + } + } + + final boolean resultIsNullOrEmpty = result == null || result.equalsToText("", element); + + // do not update default value of neighbour segment + if (resultIsNullOrEmpty && myCurrentSegmentNumber >= 0 && + (mySegments.getSegmentStart(segmentNumber) == mySegments.getSegmentEnd(myCurrentSegmentNumber) || + mySegments.getSegmentEnd(segmentNumber) == mySegments.getSegmentStart(myCurrentSegmentNumber))) { return; } - if (defaultValue != null && (result == null || result.equalsToText("", element))) { + if (defaultValue != null && resultIsNullOrEmpty) { result = defaultValue.calculateResult(context); } if (element != null) { diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java index 529585953a88..e678edb57f63 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -477,7 +477,8 @@ public class CodeStyleManagerImpl extends CodeStyleManager { /** * Formatter trims line that contains white spaces symbols only, however, there is a possible case that we want - * to preserve them for particular line (e.g. for live template that defines blank line that contains $END$ marker). + * to preserve them for particular line + * (e.g. for live template that defines line with whitespaces that contains $END$ marker: templateText $END$). *

* Current approach is to do the following: *

@@ -489,7 +490,7 @@ public class CodeStyleManagerImpl extends CodeStyleManager {
    * 
*

* This method inserts that dummy comment (fallback to identifier xxx, see {@link CodeStyleManagerImpl#createDummy(PsiFile)}) - * if necessary (if target line contains white space symbols only). + * if necessary. *

* Note: it's expected that the whole white space region that contains given offset is processed in a way that all @@ -505,26 +506,22 @@ public class CodeStyleManagerImpl extends CodeStyleManager { */ @Nullable public static TextRange insertNewLineIndentMarker(@NotNull PsiFile file, @NotNull Document document, int offset) { - CharSequence text = document.getCharsSequence(); - if (offset < 0 || offset >= text.length() || !isWhiteSpaceSymbol(text.charAt(offset))) { + CharSequence text = document.getImmutableCharSequence(); + if (offset <= 0 || offset >= text.length() || !isWhiteSpaceSymbol(text.charAt(offset))) { return null; } - - for (int i = offset - 1; i >= 0; i--) { - char c = text.charAt(i); - // We don't want to insert a marker if target line is not blank (doesn't consist from white space symbols only). - if (c == '\n') { - break; - } - if (!isWhiteSpaceSymbol(c)) { - return null; - } + + if (!isWhiteSpaceSymbol(text.charAt(offset - 1))) { + return null; // no whitespaces before offset } int end = offset; for (; end < text.length(); end++) { + if (text.charAt(end) == '\n') { + break; // line is empty till the end + } if (!isWhiteSpaceSymbol(text.charAt(end))) { - break; + return null; } }