@@ -2,43 +2,41 @@ import { GetConfig } from './types'
2
2
import { patchMethod } from './utils'
3
3
4
4
export default ( proxy : ts . LanguageService , languageService : ts . LanguageService , c : GetConfig ) => {
5
- // const oldGetAllRules = tsFull.formatting.getAllRules;
6
- // tsFull.formatting.getAllRules = () => {
7
- // }
5
+ // todo: add our formatting rules!
6
+ // tsFull.formatting.getAllRules
8
7
8
+ const isExpectedDirective = ( line : string | undefined , expected : string ) => {
9
+ if ( ! line ) return false
10
+ line = line . trim ( )
11
+ if ( ! line . startsWith ( '//' ) ) return false
12
+ line = line . slice ( 2 ) . trim ( )
13
+ if ( line . startsWith ( expected ) ) return true
14
+ return false
15
+ }
9
16
const isFormattingLineIgnored = ( sourceFile : ts . SourceFile , position : number ) => {
10
- // const sourceFile = languageService.getProgram()!.getSourceFile(fileName)!
11
17
const fullText = sourceFile . getFullText ( )
12
18
// check that lines before line are not ignored
13
19
const linesBefore = fullText . slice ( 0 , position ) . split ( '\n' )
14
- if ( linesBefore [ linesBefore . length - 2 ] ?. trim ( ) === '// @ts-format-ignore-line') {
20
+ if ( isExpectedDirective ( linesBefore [ linesBefore . length - 2 ] , ' @ts-format-ignore-line') ) {
15
21
return true
16
22
}
17
23
18
24
let isInsideIgnoredRegion = false
19
25
for ( const line of linesBefore ) {
20
- if ( line . trim ( ) === '// @ts-format-ignore-region') {
26
+ if ( isExpectedDirective ( line , ' @ts-format-ignore-region') ) {
21
27
isInsideIgnoredRegion = true
22
28
}
23
- if ( line . trim ( ) === '// @ts-format-ignore-endregion') {
29
+ if ( isExpectedDirective ( line , ' @ts-format-ignore-endregion') ) {
24
30
isInsideIgnoredRegion = false
25
31
}
26
32
}
27
33
return isInsideIgnoredRegion
28
34
}
29
- // proxy.getFormattingEditsAfterKeystroke = (fileName, position, key, options) => {
30
- // // if (isFormattingLineIgnored(fileName, position)) {
31
- // // return []
32
- // // }
33
- // return languageService.getFormattingEditsAfterKeystroke(fileName, position, key, options)
34
- // }
35
- // proxy.getFormattingEditsForDocument = (fileName, options) => {
36
- // return []
37
- // }
38
35
const toPatchFormatMethods = [ 'formatSelection' , 'formatOnOpeningCurly' , 'formatOnClosingCurly' , 'formatOnSemicolon' , 'formatOnEnter' ]
39
36
for ( const toPatchFormatMethod of toPatchFormatMethods ) {
40
37
patchMethod ( tsFull . formatting , toPatchFormatMethod as any , oldFn => ( ...args ) => {
41
38
const result = oldFn ( ...args )
39
+ // arg position depends on the method, so we need to find it
42
40
const sourceFile = args . find ( arg => ts . isSourceFile ( arg as any ) )
43
41
return result . filter ( ( { span } ) => {
44
42
if ( isFormattingLineIgnored ( sourceFile as ts . SourceFile , span . start ) ) {
@@ -48,12 +46,6 @@ export default (proxy: ts.LanguageService, languageService: ts.LanguageService,
48
46
} )
49
47
} )
50
48
}
51
- // proxy.getFormattingEditsForRange = (fileName, start, end, options) => {
52
- // return languageService.getFormattingEditsForRange(fileName, start, end, options).filter(({ span }) => {
53
- // if (isFormattingLineIgnored(fileName, span.start)) {
54
- // return false
55
- // }
56
- // return true
57
- // })
58
- // }
49
+ // we could easily patch languageService methods getFormattingEditsForDocument, getFormattingEditsAfterKeystroke and getFormattingEditsForRange
50
+ // but since formatting happens in syntax server, we don't have access to actual sourceFile, so we can't provide implementation
59
51
}
0 commit comments