Skip to content

Commit 1f8b9de

Browse files
committed
adding indentation tests for different languages
1 parent e763a3d commit 1f8b9de

File tree

7 files changed

+211
-68
lines changed

7 files changed

+211
-68
lines changed

src/vs/editor/contrib/indentation/test/browser/indentation.test.ts

Lines changed: 140 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ import { NullState } from 'vs/editor/common/languages/nullTokenize';
1818
import { AutoIndentOnPaste, IndentationToSpacesCommand, IndentationToTabsCommand } from 'vs/editor/contrib/indentation/browser/indentation';
1919
import { withTestCodeEditor } from 'vs/editor/test/browser/testCodeEditor';
2020
import { testCommand } from 'vs/editor/test/browser/testCommand';
21-
import { javascriptIndentationRules } from 'vs/editor/test/common/modes/supports/javascriptIndentationRules';
22-
import { javascriptOnEnterRules } from 'vs/editor/test/common/modes/supports/javascriptOnEnterRules';
21+
import { javascriptIndentationRules, phpIndentationRules, rubyIndentationRules } from 'vs/editor/test/common/modes/supports/indentationRules';
22+
import { javascriptOnEnterRules, phpOnEnterRules } from 'vs/editor/test/common/modes/supports/onEnterRules';
2323

2424
enum Language {
2525
TypeScript,
26-
Ruby
26+
Ruby,
27+
PHP
2728
}
2829

2930
function testIndentationToSpacesCommand(lines: string[], selection: Selection, tabSize: number, expectedLines: string[], expectedSelection: Selection): void {
@@ -66,10 +67,18 @@ function registerLanguageConfiguration(instantiationService: TestInstantiationSe
6667
['[', ']'],
6768
['(', ')']
6869
],
69-
indentationRules: {
70-
decreaseIndentPattern: /^\s*([}\]]([,)]?\s*(#|$)|\.[a-zA-Z_]\w*\b)|(end|rescue|ensure|else|elsif)\b|(in|when)\s)/,
71-
increaseIndentPattern: /^\s*((begin|class|(private|protected)\s+def|def|else|elsif|ensure|for|if|module|rescue|unless|until|when|in|while|case)|([^#]*\sdo\b)|([^#]*=\s*(case|if|unless)))\b([^#\{;]|(\"|'|\/).*\4)*(#.*)?$/,
72-
},
70+
indentationRules: rubyIndentationRules,
71+
}));
72+
break;
73+
case Language.PHP:
74+
disposables.add(languageConfigurationService.register(languageId, {
75+
brackets: [
76+
['{', '}'],
77+
['[', ']'],
78+
['(', ')']
79+
],
80+
indentationRules: phpIndentationRules,
81+
onEnterRules: phpOnEnterRules
7382
}));
7483
break;
7584
}
@@ -606,6 +615,66 @@ suite('`Full` Auto Indent On Type - TypeScript/JavaScript', () => {
606615
});
607616
});
608617

618+
test('issue #43244: indent when lambda arrow function is detected, outdent when end is reached', () => {
619+
620+
// https://github.com/microsoft/vscode/issues/43244
621+
622+
const model = createTextModel([
623+
'const array = [1, 2, 3, 4, 5];',
624+
'array.map(_)'
625+
].join('\n'), languageId, {});
626+
disposables.add(model);
627+
628+
withTestCodeEditor(model, { autoIndent: "full" }, (editor, viewModel, instantiationService) => {
629+
registerLanguage(instantiationService, languageId, Language.TypeScript, disposables);
630+
editor.setSelection(new Selection(2, 12, 2, 12));
631+
viewModel.type("\n", 'keyboard');
632+
assert.strictEqual(model.getValue(), [
633+
'const array = [1, 2, 3, 4, 5];',
634+
'array.map(_',
635+
' ',
636+
')'
637+
].join('\n'));
638+
});
639+
});
640+
641+
test('issue #43244: incorrect indentation after if/for/while without braces', () => {
642+
643+
// https://github.com/microsoft/vscode/issues/43244
644+
645+
const model = createTextModel([
646+
'function f() {',
647+
' if (condition)',
648+
'}'
649+
].join('\n'), languageId, {});
650+
disposables.add(model);
651+
652+
withTestCodeEditor(model, { autoIndent: "full" }, (editor, viewModel, instantiationService) => {
653+
654+
registerLanguage(instantiationService, languageId, Language.TypeScript, disposables);
655+
editor.setSelection(new Selection(2, 19, 2, 19));
656+
viewModel.type("\n", 'keyboard');
657+
assert.strictEqual(model.getValue(), [
658+
'function f() {',
659+
' if (condition)',
660+
' ',
661+
'}',
662+
].join('\n'));
663+
664+
viewModel.type("return;");
665+
viewModel.type("\n", 'keyboard');
666+
assert.strictEqual(model.getValue(), [
667+
'function f() {',
668+
' if (condition)',
669+
' return;',
670+
' ',
671+
'}',
672+
].join('\n'));
673+
});
674+
});
675+
676+
// Failing tests...
677+
609678
test.skip('issue #40115: keep indentation when added', () => {
610679

611680
// https://github.com/microsoft/vscode/issues/40115
@@ -663,41 +732,6 @@ suite('`Full` Auto Indent On Type - TypeScript/JavaScript', () => {
663732
});
664733
});
665734

666-
test('issue #43244: incorrect indentation after if/for/while without braces', () => {
667-
668-
// https://github.com/microsoft/vscode/issues/43244
669-
670-
const model = createTextModel([
671-
'function f() {',
672-
' if (condition)',
673-
'}'
674-
].join('\n'), languageId, {});
675-
disposables.add(model);
676-
677-
withTestCodeEditor(model, { autoIndent: "full" }, (editor, viewModel, instantiationService) => {
678-
679-
registerLanguage(instantiationService, languageId, Language.TypeScript, disposables);
680-
editor.setSelection(new Selection(2, 19, 2, 19));
681-
viewModel.type("\n", 'keyboard');
682-
assert.strictEqual(model.getValue(), [
683-
'function f() {',
684-
' if (condition)',
685-
' ',
686-
'}',
687-
].join('\n'));
688-
689-
viewModel.type("return;");
690-
viewModel.type("\n", 'keyboard');
691-
assert.strictEqual(model.getValue(), [
692-
'function f() {',
693-
' if (condition)',
694-
' return;',
695-
' ',
696-
'}',
697-
].join('\n'));
698-
});
699-
});
700-
701735
test.skip('issue #208232: incorrect indentation inside of comments', () => {
702736

703737
// https://github.com/microsoft/vscode/issues/208232
@@ -871,28 +905,6 @@ suite('`Full` Auto Indent On Type - TypeScript/JavaScript', () => {
871905
});
872906
});
873907

874-
test('issue #43244: indent when lambda arrow function is detected, outdent when end is reached', () => {
875-
876-
// https://github.com/microsoft/vscode/issues/43244
877-
878-
const model = createTextModel([
879-
'const array = [1, 2, 3, 4, 5];',
880-
'array.map(_)'
881-
].join('\n'), languageId, {});
882-
disposables.add(model);
883-
884-
withTestCodeEditor(model, { autoIndent: "full" }, (editor, viewModel, instantiationService) => {
885-
registerLanguage(instantiationService, languageId, Language.TypeScript, disposables);
886-
editor.setSelection(new Selection(2, 12, 2, 12));
887-
viewModel.type("\n", 'keyboard');
888-
assert.strictEqual(model.getValue(), [
889-
'const array = [1, 2, 3, 4, 5];',
890-
'array.map(_',
891-
' ',
892-
')'
893-
].join('\n'));
894-
});
895-
});
896908

897909
// Add tests for:
898910
// https://github.com/microsoft/vscode/issues/88638
@@ -918,6 +930,8 @@ suite('Auto Indent On Type - Ruby', () => {
918930

919931
test('issue #198350: in or when incorrectly match non keywords for Ruby', () => {
920932

933+
// https://github.com/microsoft/vscode/issues/198350
934+
921935
const model = createTextModel("", languageId, {});
922936
disposables.add(model);
923937

@@ -938,4 +952,66 @@ suite('Auto Indent On Type - Ruby', () => {
938952
assert.strictEqual(model.getValue(), " # in ");
939953
});
940954
});
955+
956+
// Failing tests...
957+
958+
test.skip('issue #199846: in or when incorrectly match non keywords for Ruby', () => {
959+
960+
// https://github.com/microsoft/vscode/issues/199846
961+
// explanation: happening because the # is detected probably as a comment
962+
963+
const model = createTextModel("", languageId, {});
964+
disposables.add(model);
965+
966+
withTestCodeEditor(model, { autoIndent: "full" }, (editor, viewModel, instantiationService) => {
967+
968+
registerLanguage(instantiationService, languageId, Language.Ruby, disposables);
969+
970+
viewModel.type("method('#foo') do");
971+
viewModel.type("\n", 'keyboard');
972+
assert.strictEqual(model.getValue(), [
973+
"method('#foo') do",
974+
" "
975+
].join('\n'));
976+
});
977+
});
978+
});
979+
980+
suite('Auto Indent On Type - PHP', () => {
981+
982+
const languageId = "php-test";
983+
let disposables: DisposableStore;
984+
985+
setup(() => {
986+
disposables = new DisposableStore();
987+
});
988+
989+
teardown(() => {
990+
disposables.dispose();
991+
});
992+
993+
ensureNoDisposablesAreLeakedInTestSuite();
994+
995+
test('temp issue because there should be at least one passing test in a suite', () => {
996+
assert.ok(true);
997+
});
998+
999+
test.skip('issue #199050: should not indent after { detected in a string', () => {
1000+
1001+
// https://github.com/microsoft/vscode/issues/199050
1002+
1003+
const model = createTextModel("$phrase = preg_replace('#(\{1|%s).*#su', '', $phrase);", languageId, {});
1004+
disposables.add(model);
1005+
1006+
withTestCodeEditor(model, { autoIndent: "full" }, (editor, viewModel, instantiationService) => {
1007+
1008+
registerLanguage(instantiationService, languageId, Language.PHP, disposables);
1009+
editor.setSelection(new Selection(1, 54, 1, 54));
1010+
viewModel.type("\n", 'keyboard');
1011+
assert.strictEqual(model.getValue(), [
1012+
"$phrase = preg_replace('#(\{1|%s).*#su', '', $phrase);",
1013+
""
1014+
].join('\n'));
1015+
});
1016+
});
9411017
});

src/vs/editor/contrib/smartSelect/test/browser/smartSelect.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { BracketSelectionRangeProvider } from 'vs/editor/contrib/smartSelect/bro
1616
import { provideSelectionRanges } from 'vs/editor/contrib/smartSelect/browser/smartSelect';
1717
import { WordSelectionRangeProvider } from 'vs/editor/contrib/smartSelect/browser/wordSelections';
1818
import { createModelServices } from 'vs/editor/test/common/testTextModel';
19-
import { javascriptOnEnterRules } from 'vs/editor/test/common/modes/supports/javascriptOnEnterRules';
19+
import { javascriptOnEnterRules } from 'vs/editor/test/common/modes/supports/onEnterRules';
2020
import { LanguageFeatureRegistry } from 'vs/editor/common/languageFeatureRegistry';
2121
import { ILanguageSelection, ILanguageService } from 'vs/editor/common/languages/language';
2222
import { ensureNoDisposablesAreLeakedInTestSuite } from 'vs/base/test/common/utils';

src/vs/editor/test/browser/commands/shiftCommand.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { Selection } from 'vs/editor/common/core/selection';
1414
import { ILanguageService } from 'vs/editor/common/languages/language';
1515
import { ILanguageConfigurationService } from 'vs/editor/common/languages/languageConfigurationRegistry';
1616
import { getEditOperation, testCommand } from 'vs/editor/test/browser/testCommand';
17-
import { javascriptOnEnterRules } from 'vs/editor/test/common/modes/supports/javascriptOnEnterRules';
17+
import { javascriptOnEnterRules } from 'vs/editor/test/common/modes/supports/onEnterRules';
1818
import { TestLanguageConfigurationService } from 'vs/editor/test/common/modes/testLanguageConfigurationService';
1919
import { withEditorModel } from 'vs/editor/test/common/testTextModel';
2020
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';

src/vs/editor/test/browser/controller/cursor.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { TextModel } from 'vs/editor/common/model/textModel';
2626
import { ViewModel } from 'vs/editor/common/viewModel/viewModelImpl';
2727
import { OutgoingViewModelEventKind } from 'vs/editor/common/viewModelEventDispatcher';
2828
import { ITestCodeEditor, TestCodeEditorInstantiationOptions, createCodeEditorServices, instantiateTestCodeEditor, withTestCodeEditor } from 'vs/editor/test/browser/testCodeEditor';
29-
import { javascriptOnEnterRules } from 'vs/editor/test/common/modes/supports/javascriptOnEnterRules';
29+
import { javascriptOnEnterRules } from 'vs/editor/test/common/modes/supports/onEnterRules';
3030
import { IRelaxedTextModelCreationOptions, createTextModel, instantiateTextModel } from 'vs/editor/test/common/testTextModel';
3131
import { TestInstantiationService } from 'vs/platform/instantiation/test/common/instantiationServiceMock';
3232

src/vs/editor/test/common/modes/supports/javascriptIndentationRules.ts renamed to src/vs/editor/test/common/modes/supports/indentationRules.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,15 @@ export const javascriptIndentationRules = {
1010
unIndentedLinePattern: /^(\t|[ ])*[ ]\*[^/]*\*\/\s*$|^(\t|[ ])*[ ]\*\/\s*$|^(\t|[ ])*[ ]\*([ ]([^\*]|\*(?!\/))*)?$/,
1111
indentNextLinePattern: /^((.*=>\s*)|((.*[^\w]+|\s*)(if|while|for)\s*\(.*\)\s*))$/,
1212
};
13+
14+
export const rubyIndentationRules = {
15+
decreaseIndentPattern: /^\s*([}\]]([,)]?\s*(#|$)|\.[a-zA-Z_]\w*\b)|(end|rescue|ensure|else|elsif)\b|(in|when)\s)/,
16+
increaseIndentPattern: /^\s*((begin|class|(private|protected)\s+def|def|else|elsif|ensure|for|if|module|rescue|unless|until|when|in|while|case)|([^#]*\sdo\b)|([^#]*=\s*(case|if|unless)))\b([^#\{;]|(\"|'|\/).*\4)*(#.*)?$/,
17+
};
18+
19+
export const phpIndentationRules = {
20+
increaseIndentPattern: /({(?!.*}).*|\(|\[|((else(\s)?)?if|else|for(each)?|while|switch|case).*:)\s*((\/[/*].*|)?$|\?>)/,
21+
decreaseIndentPattern: /^(.*\*\/)?\s*((\})|(\)+[;,])|(\]\)*[;,])|\b(else:)|\b((end(if|for(each)?|while|switch));))/,
22+
};
23+
24+

src/vs/editor/test/common/modes/supports/onEnter.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import * as assert from 'assert';
66
import { CharacterPair, IndentAction } from 'vs/editor/common/languages/languageConfiguration';
77
import { OnEnterSupport } from 'vs/editor/common/languages/supports/onEnter';
8-
import { javascriptOnEnterRules } from 'vs/editor/test/common/modes/supports/javascriptOnEnterRules';
8+
import { javascriptOnEnterRules } from 'vs/editor/test/common/modes/supports/onEnterRules';
99
import { EditorAutoIndentStrategy } from 'vs/editor/common/config/editorOptions';
1010
import { ensureNoDisposablesAreLeakedInTestSuite } from 'vs/base/test/common/utils';
1111

src/vs/editor/test/common/modes/supports/javascriptOnEnterRules.ts renamed to src/vs/editor/test/common/modes/supports/onEnterRules.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,58 @@ export const javascriptOnEnterRules = [
5959
action: { indentAction: IndentAction.IndentOutdent, appendText: '\t' }
6060
},
6161
];
62+
63+
export const phpOnEnterRules = [
64+
{
65+
beforeText: /^\s*\/\*\*(?!\/)([^\*]|\*(?!\/))*$/,
66+
afterText: /^\s*\*\/$/,
67+
action: {
68+
indentAction: IndentAction.IndentOutdent,
69+
appendText: ' * ',
70+
}
71+
},
72+
{
73+
beforeText: /^\s*\/\*\*(?!\/)([^\*]|\*(?!\/))*$/,
74+
action: {
75+
indentAction: IndentAction.None,
76+
appendText: ' * ',
77+
}
78+
},
79+
{
80+
beforeText: /^(\t|(\ \ ))*\ \*(\ ([^\*]|\*(?!\/))*)?$/,
81+
action: {
82+
indentAction: IndentAction.None,
83+
appendText: '* ',
84+
}
85+
},
86+
{
87+
beforeText: /^(\t|(\ \ ))*\ \*\/\s*$/,
88+
action: {
89+
indentAction: IndentAction.None,
90+
removeText: 1,
91+
}
92+
},
93+
{
94+
beforeText: /^(\t|(\ \ ))*\ \*[^/]*\*\/\s*$/,
95+
action: {
96+
indentAction: IndentAction.None,
97+
removeText: 1,
98+
}
99+
},
100+
{
101+
beforeText: /^\s+([^{i\s]|i(?!f\b))/,
102+
previousLineText: /^\s*(((else ?)?if|for(each)?|while)\s*\(.*\)\s*|else\s*)$/,
103+
action: {
104+
indentAction: IndentAction.Outdent
105+
}
106+
},
107+
];
108+
109+
/** Note
110+
export enum IndentAction {
111+
None = 0,
112+
Indent = 1,
113+
IndentOutdent = 2,
114+
Outdent = 3
115+
}
116+
*/

0 commit comments

Comments
 (0)