@@ -12,47 +12,64 @@ const unformattedFile = path.join(formatFilesPath, 'fileToFormatOnEnter.py');
1212
1313suite ( 'Formatting - OnEnter provider' , ( ) => {
1414 let document : vscode . TextDocument ;
15+ let editor : vscode . TextEditor ;
1516
1617 suiteSetup ( initialize ) ;
1718 setup ( async ( ) => {
1819 document = await vscode . workspace . openTextDocument ( unformattedFile ) ;
19- await vscode . window . showTextDocument ( document ) ;
20+ editor = await vscode . window . showTextDocument ( document ) ;
2021 } ) ;
2122 suiteTeardown ( closeActiveWindows ) ;
2223 teardown ( closeActiveWindows ) ;
2324
24- test ( 'Regular string ' , async ( ) => {
25- const edits = await formatAtPosition ( 1 , 0 ) ;
26- assert . notEqual ( edits ! . length , 0 , 'Line was not formatted' ) ;
25+ test ( 'Simple statement ' , async ( ) => {
26+ const text = await formatAtPosition ( 1 , 0 ) ;
27+ assert . equal ( text , 'x = 1' , 'Line was not formatted' ) ;
2728 } ) ;
2829
2930 test ( 'No formatting inside strings' , async ( ) => {
30- const edits = await formatAtPosition ( 2 , 0 ) ;
31- assert . equal ( edits ! . length , 0 , 'Text inside string was formatted' ) ;
31+ let text = await formatAtPosition ( 2 , 0 ) ;
32+ assert . equal ( text , '"""x=1' , 'Text inside string was formatted' ) ;
33+ text = await formatAtPosition ( 3 , 0 ) ;
34+ assert . equal ( text , '"""' , 'Text inside string was formatted' ) ;
3235 } ) ;
3336
3437 test ( 'Whitespace before comment' , async ( ) => {
35- const edits = await formatAtPosition ( 4 , 0 ) ;
36- assert . equal ( edits ! . length , 0 , 'Whitespace before comment was formatted ' ) ;
38+ const text = await formatAtPosition ( 4 , 0 ) ;
39+ assert . equal ( text , ' # comment' , 'Whitespace before comment was not preserved ' ) ;
3740 } ) ;
3841
3942 test ( 'No formatting of comment' , async ( ) => {
40- const edits = await formatAtPosition ( 5 , 0 ) ;
41- assert . equal ( edits ! . length , 0 , 'Text inside comment was formatted' ) ;
43+ const text = await formatAtPosition ( 5 , 0 ) ;
44+ assert . equal ( text , '# x=1' , 'Text inside comment was formatted' ) ;
4245 } ) ;
4346
4447 test ( 'Formatting line ending in comment' , async ( ) => {
45- const edits = await formatAtPosition ( 6 , 0 ) ;
46- assert . notEqual ( edits ! . length , 0 , 'Line ending in comment was not formatted' ) ;
48+ const text = await formatAtPosition ( 6 , 0 ) ;
49+ assert . equal ( text , 'x + 1 # ' , 'Line ending in comment was not formatted' ) ;
50+ } ) ;
51+
52+ test ( 'Formatting line with @' , async ( ) => {
53+ const text = await formatAtPosition ( 7 , 0 ) ;
54+ assert . equal ( text , '@x' , 'Line with @ was reformatted' ) ;
55+ } ) ;
56+
57+ test ( 'Formatting line with @' , async ( ) => {
58+ const text = await formatAtPosition ( 8 , 0 ) ;
59+ assert . equal ( text , 'x.y' , 'Line ending with period was reformatted' ) ;
4760 } ) ;
4861
4962 test ( 'Formatting line ending in string' , async ( ) => {
50- const edits = await formatAtPosition ( 7 , 0 ) ;
51- assert . notEqual ( edits ! . length , 0 , 'Line ending in multilint string was not formatted' ) ;
63+ const text = await formatAtPosition ( 9 , 0 ) ;
64+ assert . equal ( text , 'x + """' , 'Line ending in multiline string was not formatted' ) ;
5265 } ) ;
5366
54- async function formatAtPosition ( line : number , character : number ) : Promise < vscode . TextEdit [ ] | undefined > {
55- return await vscode . commands . executeCommand < vscode . TextEdit [ ] > ( 'vscode.executeFormatOnTypeProvider' ,
67+ async function formatAtPosition ( line : number , character : number ) : Promise < string > {
68+ const edits = await vscode . commands . executeCommand < vscode . TextEdit [ ] > ( 'vscode.executeFormatOnTypeProvider' ,
5669 document . uri , new vscode . Position ( line , character ) , '\n' , { insertSpaces : true , tabSize : 2 } ) ;
70+ if ( edits ) {
71+ await editor . edit ( builder => edits . forEach ( e => builder . replace ( e . range , e . newText ) ) ) ;
72+ }
73+ return document . lineAt ( line - 1 ) . text ;
5774 }
5875} ) ;
0 commit comments