IDEA-67301 Line wrap: expand the API

This commit is contained in:
Denis Zhdanov
2011-03-31 13:08:58 +04:00
parent 962b5e35af
commit f705add832
7 changed files with 37 additions and 11 deletions
@@ -150,7 +150,11 @@ public class AutoHardWrapHandler {
new VisualPosition(caretModel.getVisualPosition().line, margin - FormatConstants.RESERVED_LINE_WRAP_WIDTH_IN_COLUMNS)
));
int wrapOffset = strategy.calculateWrapPosition(document.getCharsSequence(), startOffset, endOffset, maxPreferredOffset, true);
int wrapOffset = strategy.calculateWrapPosition(document, startOffset, endOffset, maxPreferredOffset, true);
if (wrapOffset < 0) {
return;
}
WhiteSpaceFormattingStrategy formattingStrategy = WhiteSpaceFormattingStrategyFactory.getStrategy(editor);
if (wrapOffset <= startOffset || wrapOffset > maxPreferredOffset
|| formattingStrategy.check(document.getCharsSequence(), startOffset, wrapOffset) >= wrapOffset)
@@ -370,8 +370,11 @@ public class CodeFormatterFacade {
// We know that current line exceeds right margin if control flow reaches this place, so, wrap it.
int wrapOffset = strategy.calculateWrapPosition(
text, Math.max(startLineOffset, startOffsetToUse), Math.min(endLineOffset, endOffsetToUse), preferredWrapPosition, false
document, Math.max(startLineOffset, startOffsetToUse), Math.min(endLineOffset, endOffsetToUse), preferredWrapPosition, false
);
if (wrapOffset < 0) {
continue;
}
editor.getCaretModel().moveToOffset(wrapOffset);
final DataContext dataContext = DataManager.getInstance().getDataContext(editor.getComponent());
+1
View File
@@ -28,6 +28,7 @@
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" scope="TEST" name="Mocks" level="project" />
</component>
</module>
@@ -41,7 +41,7 @@ public class GenericLineWrapPositionStrategy implements LineWrapPositionStrategy
private final Storage myOffset2weight = new Storage();
@Override
public int calculateWrapPosition(@NotNull CharSequence text,
public int calculateWrapPosition(@NotNull Document document,
int startOffset,
int endOffset,
int maxPreferredOffset,
@@ -53,6 +53,7 @@ public class GenericLineWrapPositionStrategy implements LineWrapPositionStrategy
myOffset2weight.clear();
myOffset2weight.anchor = startOffset;
CharSequence text = document.getCharsSequence();
// Normalization.
int maxPreferredOffsetToUse = maxPreferredOffset >= endOffset ? endOffset - 1 : maxPreferredOffset;
@@ -32,7 +32,7 @@ public interface LineWrapPositionStrategy {
/**
* Allows to calculate the most appropriate position to wrap target line.
*
* @param text target text holder
* @param document target document which text is being processed
* @param startOffset start offset to use with the given text holder (inclusive)
* @param endOffset end offset to use with the given text holder (exclusive)
* @param maxPreferredOffset this method is expected to do its best to return offset that belongs to
@@ -43,9 +43,10 @@ public interface LineWrapPositionStrategy {
* <code>(maxPreferredOffset; endOffset]</code> interval in case of inability to
* find appropriate offset from <code>(startOffset; maxPreferredOffset]</code> interval
* @return offset from <code>(startOffset; endOffset]</code> interval where
* target line should be wrapped
* target line should be wrapped OR <code>-1</code> if no wrapping should be performed
*/
int calculateWrapPosition(
@NotNull CharSequence text, int startOffset, int endOffset, int maxPreferredOffset, boolean allowToBeyondMaxPreferredOffset
@NotNull Document document, int startOffset, int endOffset, int maxPreferredOffset,
boolean allowToBeyondMaxPreferredOffset
);
}
@@ -15,6 +15,11 @@
*/
package com.intellij.openapi.editor;
import org.jetbrains.annotations.NotNull;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Before;
import org.junit.Test;
@@ -29,11 +34,16 @@ public class DefaultLineWrapPositionStrategyTest {
private static final String EDGE_MARKER = "<EDGE>";
private static final String WRAP_MARKER = "<WRAP>";
private Mockery myMockery;
private DefaultLineWrapPositionStrategy myStrategy;
@Before
public void setUp() {
myStrategy = new DefaultLineWrapPositionStrategy();
myMockery = new JUnit4Mockery() {{
setImposteriser(ClassImposteriser.INSTANCE);
}};
}
@Test
@@ -65,11 +75,19 @@ public class DefaultLineWrapPositionStrategyTest {
final Context context = new Context(document);
context.init();
int actual = myStrategy.calculateWrapPosition(
context.document, 0, context.document.length(), context.edgeIndex, allowToBeyondMaxPreferredOffset
createMockDocument(context.document), 0, context.document.length(), context.edgeIndex, allowToBeyondMaxPreferredOffset
);
assertSame(context.wrapIndex, actual);
}
private Document createMockDocument(@NotNull final String text) {
final Document result = myMockery.mock(Document.class);
myMockery.checking(new Expectations() {{
allowing(result).getCharsSequence(); will(returnValue(text));
}});
return result;
}
/**
* Utility class for parsing and initialising test data.
* <p/>
@@ -517,12 +517,10 @@ public class SoftWrapApplianceManager implements FoldingListener, DocumentListen
myLineWrapPositionStrategy = LanguageLineWrapPositionStrategy.INSTANCE.forEditor(myEditor);
}
softWrapOffset = myLineWrapPositionStrategy.calculateWrapPosition(
document.getCharsSequence(), minOffset, maxOffset, preferredOffset, true
);
softWrapOffset = myLineWrapPositionStrategy.calculateWrapPosition(document, minOffset, maxOffset, preferredOffset, true);
}
if (softWrapOffset >= lineData.endLineOffset) {
if (softWrapOffset >= lineData.endLineOffset || softWrapOffset < 0) {
return null;
}