Templates: add whitespace marker in non-empty line (RUBY-16672)

This commit is contained in:
Alexander Zolotov
2015-05-26 11:43:13 +02:00
parent 6a32f42e8f
commit ee165b9f83
2 changed files with 26 additions and 18 deletions
@@ -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) {
@@ -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$).
* <p/>
* Current approach is to do the following:
* <pre>
@@ -489,7 +490,7 @@ public class CodeStyleManagerImpl extends CodeStyleManager {
* </pre>
* <p/>
* This method inserts that dummy comment (fallback to identifier <code>xxx</code>, see {@link CodeStyleManagerImpl#createDummy(PsiFile)})
* if necessary (if target line contains white space symbols only).
* if necessary.
* <p/>
* <b>Note:</b> 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;
}
}