提交 1b34b1d2 编写于 作者: R rebornix

Fix #29828, Fix #29843, Fix #29876. Enter in the middle of content should not...

Fix #29828, Fix #29843, Fix #29876. Enter in the middle of content should not adjust cursor position.
上级 01cbc6dd
......@@ -312,11 +312,8 @@ export class TypeOperations {
let indentation = strings.getLeadingWhitespace(lineText).substring(0, range.startColumn - 1);
if (ir) {
let isSelectionEmpty = range.isEmpty();
let oldEndColumn = CursorColumns.visibleColumnFromColumn2(config, model, range.getEndPosition());
if (!config.insertSpaces) {
oldEndColumn = Math.ceil(oldEndColumn / config.tabSize) + 1;
}
let oldEndViewColumn = CursorColumns.visibleColumnFromColumn2(config, model, range.getEndPosition());
let oldEndColumn = range.endColumn;
let beforeText = '\n';
if (indentation !== config.normalizeIndentation(ir.beforeEnter)) {
......@@ -335,7 +332,13 @@ export class TypeOperations {
if (keepPosition) {
return new ReplaceCommandWithoutChangingPosition(range, beforeText + config.normalizeIndentation(ir.afterEnter), true);
} else {
let offset = isSelectionEmpty ? oldEndColumn - config.normalizeIndentation(ir.afterEnter).length - 1 : 0;
let offset = 0;
if (oldEndColumn <= firstNonWhitespace + 1) {
if (!config.insertSpaces) {
oldEndViewColumn = Math.ceil(oldEndViewColumn / config.tabSize);
}
offset = Math.min(oldEndViewColumn + 1 - config.normalizeIndentation(ir.afterEnter).length - 1, 0);
}
return new ReplaceCommandWithOffsetCursorState(range, beforeText + config.normalizeIndentation(ir.afterEnter), 0, offset, true);
}
......
......@@ -2498,27 +2498,218 @@ suite('Editor Controller - Indentation Rules', () => {
});
});
// test('Enter supports intentional indentation', () => {
// usingCursor({
// text: [
// '\tif (true) {',
// '\t\tswitch(true) {',
// '\t\t\tcase true:',
// '\t\t\t\tbreak;',
// '\t\t}',
// '\t}'
// ],
// languageIdentifier: mode.getLanguageIdentifier(),
// modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
// }, (model, cursor) => {
// moveTo(cursor, 5, 4, false);
// assertCursor(cursor, new Selection(5, 4, 5, 4));
// cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
// assert.equal(model.getLineContent(5), '\t\t}');
// assertCursor(cursor, new Selection(6, 3, 6, 3));
// });
// });
test('Enter supports intentional indentation', () => {
usingCursor({
text: [
'\tif (true) {',
'\t\tswitch(true) {',
'\t\t\tcase true:',
'\t\t\t\tbreak;',
'\t\t}',
'\t}'
],
languageIdentifier: mode.getLanguageIdentifier(),
modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
}, (model, cursor) => {
moveTo(cursor, 5, 4, false);
assertCursor(cursor, new Selection(5, 4, 5, 4));
cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
assert.equal(model.getLineContent(5), '\t\t}');
assertCursor(cursor, new Selection(6, 3, 6, 3));
});
});
test('Enter should not adjust cursor position when press enter in the middle of a line 1', () => {
usingCursor({
text: [
'if (true) {',
'\tif (true) {',
'\t\treturn true;',
'\t}a}'
],
languageIdentifier: mode.getLanguageIdentifier(),
modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
}, (model, cursor) => {
moveTo(cursor, 3, 9, false);
assertCursor(cursor, new Selection(3, 9, 3, 9));
cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
assertCursor(cursor, new Selection(4, 3, 4, 3));
assert.equal(model.getLineContent(4), '\t\t true;', '001');
});
});
test('Enter should not adjust cursor position when press enter in the middle of a line 2', () => {
usingCursor({
text: [
'if (true) {',
'\tif (true) {',
'\t\treturn true;',
'\t}a}'
],
languageIdentifier: mode.getLanguageIdentifier(),
modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
}, (model, cursor) => {
moveTo(cursor, 3, 3, false);
assertCursor(cursor, new Selection(3, 3, 3, 3));
cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
assertCursor(cursor, new Selection(4, 3, 4, 3));
assert.equal(model.getLineContent(4), '\t\treturn true;', '001');
});
});
test('Enter should not adjust cursor position when press enter in the middle of a line 3', () => {
usingCursor({
text: [
'if (true) {',
' if (true) {',
' return true;',
' }a}'
],
languageIdentifier: mode.getLanguageIdentifier(),
modelOpts: { insertSpaces: true, tabSize: 2, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
}, (model, cursor) => {
moveTo(cursor, 3, 11, false);
assertCursor(cursor, new Selection(3, 11, 3, 11));
cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
assertCursor(cursor, new Selection(4, 5, 4, 5));
assert.equal(model.getLineContent(4), ' true;', '001');
});
});
test('Enter should adjust cursor position when press enter in the middle of leading whitespaces 1', () => {
usingCursor({
text: [
'if (true) {',
'\tif (true) {',
'\t\treturn true;',
'\t}a}'
],
languageIdentifier: mode.getLanguageIdentifier(),
modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
}, (model, cursor) => {
moveTo(cursor, 3, 2, false);
assertCursor(cursor, new Selection(3, 2, 3, 2));
cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
assertCursor(cursor, new Selection(4, 2, 4, 2));
assert.equal(model.getLineContent(4), '\t\treturn true;', '001');
moveTo(cursor, 4, 1, false);
assertCursor(cursor, new Selection(4, 1, 4, 1));
cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
assertCursor(cursor, new Selection(5, 1, 5, 1));
assert.equal(model.getLineContent(5), '\t\treturn true;', '002');
});
});
test('Enter should adjust cursor position when press enter in the middle of leading whitespaces 2', () => {
usingCursor({
text: [
'\tif (true) {',
'\t\tif (true) {',
'\t \treturn true;',
'\t\t}a}'
],
languageIdentifier: mode.getLanguageIdentifier(),
modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
}, (model, cursor) => {
moveTo(cursor, 3, 4, false);
assertCursor(cursor, new Selection(3, 4, 3, 4));
cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
assertCursor(cursor, new Selection(4, 3, 4, 3));
assert.equal(model.getLineContent(4), '\t\t\treturn true;', '001');
moveTo(cursor, 4, 1, false);
assertCursor(cursor, new Selection(4, 1, 4, 1));
cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
assertCursor(cursor, new Selection(5, 1, 5, 1));
assert.equal(model.getLineContent(5), '\t\t\treturn true;', '002');
});
});
test('Enter should adjust cursor position when press enter in the middle of leading whitespaces 3', () => {
usingCursor({
text: [
'if (true) {',
' if (true) {',
' return true;',
'}a}'
],
languageIdentifier: mode.getLanguageIdentifier(),
modelOpts: { insertSpaces: true, tabSize: 2, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
}, (model, cursor) => {
moveTo(cursor, 3, 2, false);
assertCursor(cursor, new Selection(3, 2, 3, 2));
cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
assertCursor(cursor, new Selection(4, 2, 4, 2));
assert.equal(model.getLineContent(4), ' return true;', '001');
moveTo(cursor, 4, 3, false);
cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
assertCursor(cursor, new Selection(5, 3, 5, 3));
assert.equal(model.getLineContent(5), ' return true;', '002');
});
});
test('Enter should adjust cursor position when press enter in the middle of leading whitespaces 4', () => {
usingCursor({
text: [
'if (true) {',
' if (true) {',
'\t return true;',
'}a}',
'',
'if (true) {',
' if (true) {',
'\t return true;',
'}a}'
],
languageIdentifier: mode.getLanguageIdentifier(),
modelOpts: { insertSpaces: true, tabSize: 2, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
}, (model, cursor) => {
moveTo(cursor, 3, 3, false);
assertCursor(cursor, new Selection(3, 3, 3, 3));
cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
assertCursor(cursor, new Selection(4, 4, 4, 4));
assert.equal(model.getLineContent(4), ' return true;', '001');
moveTo(cursor, 9, 4, false);
cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
assertCursor(cursor, new Selection(10, 5, 10, 5));
assert.equal(model.getLineContent(10), ' return true;', '001');
});
});
test('Enter should adjust cursor position when press enter in the middle of leading whitespaces 5', () => {
usingCursor({
text: [
'if (true) {',
' if (true) {',
' return true;',
' return true;',
''
],
languageIdentifier: mode.getLanguageIdentifier(),
modelOpts: { insertSpaces: true, tabSize: 2, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
}, (model, cursor) => {
moveTo(cursor, 3, 5, false);
moveTo(cursor, 4, 3, true);
assertCursor(cursor, new Selection(3, 5, 4, 3));
cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
assertCursor(cursor, new Selection(4, 3, 4, 3));
assert.equal(model.getLineContent(4), ' return true;', '001');
});
});
test('issue Microsoft/monaco-editor#108 part 1/2: Auto indentation on Enter with selection is half broken', () => {
usingCursor({
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册