6
6
import * as fs from 'fs' ;
7
7
import * as path from 'path' ;
8
8
import * as assert from 'assert' ;
9
- import { DisposableStore } from 'vs/base/common/lifecycle' ;
9
+ import { DisposableStore , IDisposable } from 'vs/base/common/lifecycle' ;
10
10
import { ensureNoDisposablesAreLeakedInTestSuite } from 'vs/base/test/common/utils' ;
11
11
import { ILanguageConfigurationService } from 'vs/editor/common/languages/languageConfigurationRegistry' ;
12
12
import { getReindentEditOperations } from 'vs/editor/contrib/indentation/common/indentation' ;
@@ -15,6 +15,9 @@ import { TestInstantiationService } from 'vs/platform/instantiation/test/common/
15
15
import { ILanguageConfiguration , LanguageConfigurationFileHandler } from 'vs/workbench/contrib/codeEditor/common/languageConfigurationExtensionPoint' ;
16
16
import { parse } from 'vs/base/common/json' ;
17
17
import { IRange } from 'vs/editor/common/core/range' ;
18
+ import { ISingleEditOperation } from 'vs/editor/common/core/editOperation' ;
19
+ import { trimTrailingWhitespace } from 'vs/editor/common/commands/trimTrailingWhitespaceCommand' ;
20
+ import { execSync } from 'child_process' ;
18
21
19
22
function getIRange ( range : IRange ) : IRange {
20
23
return {
@@ -25,9 +28,28 @@ function getIRange(range: IRange): IRange {
25
28
} ;
26
29
}
27
30
31
+ const enum LanguageId {
32
+ TypeScript = 'ts-test'
33
+ }
34
+
35
+ function registerLanguage ( languageConfigurationService : ILanguageConfigurationService , languageId : LanguageId ) : IDisposable {
36
+ let configPath : string ;
37
+ switch ( languageId ) {
38
+ case LanguageId . TypeScript :
39
+ configPath = path . join ( 'extensions' , 'typescript-basics' , 'language-configuration.json' ) ;
40
+ break ;
41
+ default :
42
+ throw new Error ( 'Unknown languageId' ) ;
43
+ }
44
+ const configContent = fs . readFileSync ( configPath , { encoding : 'utf-8' } ) ;
45
+ const parsedConfig = < ILanguageConfiguration > parse ( configContent , [ ] ) ;
46
+ const languageConfig = LanguageConfigurationFileHandler . extractValidConfig ( languageId , parsedConfig ) ;
47
+ return languageConfigurationService . register ( languageId , languageConfig ) ;
48
+ }
49
+
28
50
suite ( 'Auto-Reindentation - TypeScript/JavaScript' , ( ) => {
29
51
30
- const languageId = 'ts-test' ;
52
+ const languageId = LanguageId . TypeScript ;
31
53
const options : IRelaxedTextModelCreationOptions = { } ;
32
54
let disposables : DisposableStore ;
33
55
let instantiationService : TestInstantiationService ;
@@ -37,11 +59,7 @@ suite('Auto-Reindentation - TypeScript/JavaScript', () => {
37
59
disposables = new DisposableStore ( ) ;
38
60
instantiationService = createModelServices ( disposables ) ;
39
61
languageConfigurationService = instantiationService . get ( ILanguageConfigurationService ) ;
40
- const configPath = path . join ( 'extensions' , 'typescript-basics' , 'language-configuration.json' ) ;
41
- const configString = fs . readFileSync ( configPath ) . toString ( ) ;
42
- const config = < ILanguageConfiguration > parse ( configString , [ ] ) ;
43
- const configParsed = LanguageConfigurationFileHandler . extractValidConfig ( languageId , config ) ;
44
- disposables . add ( languageConfigurationService . register ( languageId , configParsed ) ) ;
62
+ disposables . add ( registerLanguage ( languageConfigurationService , languageId ) ) ;
45
63
} ) ;
46
64
47
65
teardown ( ( ) => {
@@ -51,20 +69,64 @@ suite('Auto-Reindentation - TypeScript/JavaScript', () => {
51
69
ensureNoDisposablesAreLeakedInTestSuite ( ) ;
52
70
53
71
// Test which can be ran to find cases of incorrect indentation...
54
- test . skip ( 'Find Cases of Incorrect Indentation' , ( ) => {
55
-
56
- const filePath = path . join ( '..' , 'TypeScript' , 'src' , 'server' , 'utilities.ts' ) ;
57
- const fileContents = fs . readFileSync ( filePath ) . toString ( ) ;
58
-
59
- const model = disposables . add ( instantiateTextModel ( instantiationService , fileContents , languageId , options ) ) ;
60
- const editOperations = getReindentEditOperations ( model , languageConfigurationService , 1 , model . getLineCount ( ) ) ;
61
- model . applyEdits ( editOperations ) ;
62
-
63
- // save the files to disk
64
- const initialFile = path . join ( '..' , 'autoindent' , 'initial.ts' ) ;
65
- const finalFile = path . join ( '..' , 'autoindent' , 'final.ts' ) ;
66
- fs . writeFileSync ( initialFile , fileContents ) ;
67
- fs . writeFileSync ( finalFile , model . getValue ( ) ) ;
72
+ test . skip ( 'Find Cases of Incorrect Indentation with the Reindent Lines Command' , ( ) => {
73
+
74
+ // ./scripts/test.sh --inspect --grep='Find Cases of Incorrect Indentation with the Reindent Lines Command' --timeout=15000
75
+
76
+ function walkDirectoryAndReindent ( directory : string , languageId : string ) {
77
+ const files = fs . readdirSync ( directory , { withFileTypes : true } ) ;
78
+ const directoriesToRecurseOn : string [ ] = [ ] ;
79
+ for ( const file of files ) {
80
+ if ( file . isDirectory ( ) ) {
81
+ directoriesToRecurseOn . push ( path . join ( directory , file . name ) ) ;
82
+ } else {
83
+ const filePathName = path . join ( directory , file . name ) ;
84
+ const fileExtension = path . extname ( filePathName ) ;
85
+ if ( fileExtension !== '.ts' ) {
86
+ continue ;
87
+ }
88
+ const fileContents = fs . readFileSync ( filePathName , { encoding : 'utf-8' } ) ;
89
+ const modelOptions : IRelaxedTextModelCreationOptions = {
90
+ tabSize : 4 ,
91
+ insertSpaces : false
92
+ } ;
93
+ const model = disposables . add ( instantiateTextModel ( instantiationService , fileContents , languageId , modelOptions ) ) ;
94
+ const lineCount = model . getLineCount ( ) ;
95
+ const editOperations : ISingleEditOperation [ ] = [ ] ;
96
+ for ( let line = 1 ; line <= lineCount - 1 ; line ++ ) {
97
+ /*
98
+ NOTE: Uncomment in order to ignore incorrect JS DOC indentation
99
+ const lineContent = model.getLineContent(line);
100
+ const trimmedLineContent = lineContent.trim();
101
+ if (trimmedLineContent.length === 0 || trimmedLineContent.startsWith('*') || trimmedLineContent.startsWith('/*')) {
102
+ continue;
103
+ }
104
+ */
105
+ const lineContent = model . getLineContent ( line ) ;
106
+ const trimmedLineContent = lineContent . trim ( ) ;
107
+ if ( trimmedLineContent . length === 0 ) {
108
+ continue ;
109
+ }
110
+ const editOperation = getReindentEditOperations ( model , languageConfigurationService , line , line + 1 ) ;
111
+ /*
112
+ NOTE: Uncomment in order to see actual incorrect indentation diff
113
+ model.applyEdits(editOperation);
114
+ */
115
+ editOperations . push ( ...editOperation ) ;
116
+ }
117
+ model . applyEdits ( editOperations ) ;
118
+ model . applyEdits ( trimTrailingWhitespace ( model , [ ] , true ) ) ;
119
+ fs . writeFileSync ( filePathName , model . getValue ( ) ) ;
120
+ }
121
+ }
122
+ for ( const directory of directoriesToRecurseOn ) {
123
+ walkDirectoryAndReindent ( directory , languageId ) ;
124
+ }
125
+ }
126
+
127
+ walkDirectoryAndReindent ( '/Users/aiday/Desktop/Test/vscode-test' , 'ts-test' ) ;
128
+ const output = execSync ( 'cd /Users/aiday/Desktop/Test/vscode-test && git diff --shortstat' , { encoding : 'utf-8' } ) ;
129
+ console . log ( '\ngit diff --shortstat:\n' , output ) ;
68
130
} ) ;
69
131
70
132
// Unit tests for increase and decrease indent patterns...
0 commit comments