提交 03a0c93a 编写于 作者: A Alex Dima

Improve breaking behaviour when using WrappingIndent

上级 6cf0724b
......@@ -118,8 +118,9 @@ export class CharacterHardWrappingLineMapperFactory implements ILineMapperFactor
let wrappedTextIndent = '';
const TAB_CHAR_CODE = '\t'.charCodeAt(0);
let firstNonWhitespaceIndex = -1;
if (hardWrappingIndent !== WrappingIndent.None) {
let firstNonWhitespaceIndex = strings.firstNonWhitespaceIndex(lineText);
firstNonWhitespaceIndex = strings.firstNonWhitespaceIndex(lineText);
if (firstNonWhitespaceIndex !== -1) {
wrappedTextIndent = lineText.substring(0, firstNonWhitespaceIndex);
for (let i = 0; i < firstNonWhitespaceIndex; i++) {
......@@ -194,19 +195,19 @@ export class CharacterHardWrappingLineMapperFactory implements ILineMapperFactor
// We will break before `niceBreakLastOffset`
breakBeforeOffset = niceBreakOffset;
restoreVisibleColumnFrom = niceBreakVisibleColumn + wrappedTextIndentVisibleColumn;
restoreVisibleColumnFrom = niceBreakVisibleColumn;
} else if (obtrusiveBreakOffset !== -1) {
// We will break before `obtrusiveBreakLastOffset`
breakBeforeOffset = obtrusiveBreakOffset;
restoreVisibleColumnFrom = obtrusiveBreakVisibleColumn + wrappedTextIndentVisibleColumn;
restoreVisibleColumnFrom = obtrusiveBreakVisibleColumn;
} else {
// We will break before `i`
breakBeforeOffset = i;
restoreVisibleColumnFrom = 0 + wrappedTextIndentVisibleColumn;
restoreVisibleColumnFrom = wrappedTextIndentVisibleColumn;
}
......@@ -235,10 +236,10 @@ export class CharacterHardWrappingLineMapperFactory implements ILineMapperFactor
obtrusiveBreakVisibleColumn = CharacterHardWrappingLineMapperFactory.nextVisibleColumn(obtrusiveBreakVisibleColumn, tabSize, charCodeIsTab, charColumnSize);
}
if (charCodeClass === CharacterClass.BREAK_AFTER) {
if (charCodeClass === CharacterClass.BREAK_AFTER && (hardWrappingIndent === WrappingIndent.None || i >= firstNonWhitespaceIndex)) {
// This is a character that indicates that a break should happen after it
niceBreakOffset = i + 1;
niceBreakVisibleColumn = 0;
niceBreakVisibleColumn = wrappedTextIndentVisibleColumn;
}
// CJK breaking : after break
......@@ -247,14 +248,14 @@ export class CharacterHardWrappingLineMapperFactory implements ILineMapperFactor
let nextClass = classifier.classify(nextCode);
if (nextClass !== CharacterClass.BREAK_AFTER) { // Kinsoku Shori: Don't break before a trailing character, like a period
niceBreakOffset = i + 1;
niceBreakVisibleColumn = 0;
niceBreakVisibleColumn = wrappedTextIndentVisibleColumn;
}
}
if (charCodeClass === CharacterClass.BREAK_OBTRUSIVE) {
// This is an obtrusive character that indicates that a break should happen after it
obtrusiveBreakOffset = i + 1;
obtrusiveBreakVisibleColumn = 0;
obtrusiveBreakVisibleColumn = wrappedTextIndentVisibleColumn;
}
}
......
......@@ -9,39 +9,7 @@ import {WrappingIndent} from 'vs/editor/common/editorCommon';
import {CharacterHardWrappingLineMapperFactory} from 'vs/editor/common/viewModel/characterHardWrappingLineMapper';
import {ILineMapperFactory, ILineMapping, OutputPosition} from 'vs/editor/common/viewModel/splitLinesCollection';
function safeGetOutputLineCount(mapper:ILineMapping): number {
if (!mapper) {
return 1;
}
return mapper.getOutputLineCount();
}
function safeGetOutputPositionOfInputOffset(mapper:ILineMapping, inputOffset:number): OutputPosition {
if (!mapper) {
return new OutputPosition(0, inputOffset);
}
return mapper.getOutputPositionOfInputOffset(inputOffset);
}
function safeGetInputOffsetOfOutputPosition(mapper:ILineMapping, outputLineIndex:number, outputOffset:number): number {
if (!mapper) {
return outputOffset;
}
return mapper.getInputOffsetOfOutputPosition(outputLineIndex, outputOffset);
}
function assertMappingIdentity(mapper:ILineMapping, offset:number, expectedLineIndex:number) {
let result = safeGetOutputPositionOfInputOffset(mapper, offset);
assert.ok(result.outputLineIndex !== -1);
assert.ok(result.outputOffset !== -1);
assert.equal(result.outputLineIndex, expectedLineIndex);
let actualOffset = safeGetInputOffsetOfOutputPosition(mapper, result.outputLineIndex, result.outputOffset);
assert.equal(actualOffset, offset);
}
function assertLineMapping(factory:ILineMapperFactory, tabSize:number, breakAfter:number, annotatedText:string) {
function assertLineMapping(factory:ILineMapperFactory, tabSize:number, breakAfter:number, annotatedText:string, wrappingIndent = WrappingIndent.None) {
let rawText = '';
let currentLineIndex = 0;
......@@ -55,12 +23,25 @@ function assertLineMapping(factory:ILineMapperFactory, tabSize:number, breakAfte
}
}
var mapper = factory.createLineMapping(rawText, tabSize, breakAfter, 2, WrappingIndent.None);
assert.equal(safeGetOutputLineCount(mapper), (lineIndices.length > 0 ? lineIndices[lineIndices.length - 1] + 1 : 1));
for (let i = 0, len = rawText.length; i < len; i++) {
assertMappingIdentity(mapper, i, lineIndices[i]);
let mapper = factory.createLineMapping(rawText, tabSize, breakAfter, 2, wrappingIndent);
let actualAnnotatedText = '';
if (mapper) {
let previousLineIndex = 0;
for (let i = 0, len = rawText.length; i < len; i++) {
let r = mapper.getOutputPositionOfInputOffset(i);
if (previousLineIndex !== r.outputLineIndex) {
previousLineIndex = r.outputLineIndex;
actualAnnotatedText += '|';
}
actualAnnotatedText += rawText.charAt(i);
}
} else {
// No wrapping
actualAnnotatedText = rawText;
}
assert.equal(actualAnnotatedText, annotatedText);
}
suite('Editor ViewModel - CharacterHardWrappingLineMapper', () => {
......@@ -105,8 +86,9 @@ suite('Editor ViewModel - CharacterHardWrappingLineMapper', () => {
assertLineMapping(factory, 4, 5, 'aa.(|()|.aaa');
assertLineMapping(factory, 4, 5, 'aa.|(.)|.aaa');
});
test('CharacterHardWrappingLineMapper - CJK and Kinsoku Shori', () => {
var factory = new CharacterHardWrappingLineMapperFactory('(', ')', '.');
let factory = new CharacterHardWrappingLineMapperFactory('(', ')', '.');
assertLineMapping(factory, 4, 5, 'aa \u5b89|\u5b89');
assertLineMapping(factory, 4, 5, '\u3042 \u5b89|\u5b89');
assertLineMapping(factory, 4, 5, '\u3042\u3042|\u5b89\u5b89');
......@@ -114,4 +96,9 @@ suite('Editor ViewModel - CharacterHardWrappingLineMapper', () => {
assertLineMapping(factory, 4, 5, 'aa \u3042|\u5b89\u3042)|\u5b89');
assertLineMapping(factory, 4, 5, 'aa |(\u5b89aa|\u5b89');
});
test('CharacterHardWrappingLineMapper - WrappingIndent.Same', () => {
let factory = new CharacterHardWrappingLineMapperFactory('', ' ', '');
assertLineMapping(factory, 4, 38, ' *123456789012345678901234567890123456|7890', WrappingIndent.Same);
});
});
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册